16
16
/>
17
17
</template >
18
18
19
- <DocusEditor v- model= " content " />
19
+ <DocusEditor :model-value = " parsedContent " @update: model-value = " saveContent " />
20
20
</ProjectPage >
21
21
</template >
22
22
23
23
<script setup lang="ts">
24
24
import type { PropType , Ref } from ' vue'
25
+ import matter from ' gray-matter'
26
+ import { unflatten , flatten } from ' flat'
25
27
import type { Team , Project , File , Branch } from ' ~/types'
26
28
27
29
const props = defineProps ({
@@ -40,6 +42,8 @@ const client = useStrapiClient()
40
42
const branch: Ref <Branch > = ref (null )
41
43
const file: Ref <File > = ref (null )
42
44
const content: Ref <string > = ref (' ' )
45
+ const parsedContent: Ref <string > = ref (' ' )
46
+ const parsedMatter: Ref <string > = ref (' ' )
43
47
44
48
const { data : branches, refresh : refreshBranches } = await useAsyncData (' files' , () => client <Branch []>(` /projects/${props .project .id }/branches ` ))
45
49
@@ -49,6 +53,10 @@ const { data: files, refresh: refreshFiles } = await useAsyncData('files', () =>
49
53
}
50
54
}))
51
55
56
+ watch (content , () => {
57
+ setupEditor ()
58
+ }, { immediate: true })
59
+
52
60
// Select file when files changes
53
61
watch (files , () => {
54
62
const currentFile = file .value ?.path ? findFileFromPath (file .value .path , files .value ) : null
@@ -77,6 +85,44 @@ watch(file, async () => {
77
85
// Fetch files when branch changes
78
86
watch (branch , async () => await refreshFiles ())
79
87
88
+ function setupEditor () {
89
+ const { content, matter } = parseFrontMatter ()
90
+
91
+ parsedContent .value = content
92
+ parsedMatter .value = matter
93
+ }
94
+
95
+ function parseFrontMatter () {
96
+ const { data, content : c, ... rest } = matter (content .value )
97
+
98
+ // unflatten frontmatter data
99
+ // convert `parent.child` keys into `parent: { child: ... }`
100
+ const unflattenData = unflatten (data || {}, {})
101
+
102
+ return {
103
+ content: c .replace (/ ^ \n / , ' ' ),
104
+ matter: unflattenData ,
105
+ ... rest
106
+ }
107
+ }
108
+
109
+ function stringifyFrontMatter (content , data = {}) {
110
+ // flatten frontmatter data
111
+ // convert `parent: { child: ... }` into flat keys `parent.child`
112
+ data = flatten (data , {
113
+ // preserve arrays and their contents as is and do not waltk through arrays
114
+ // flatten array will be like `parent.0.child` and `parent.1.child` with is not readable
115
+ safe: true
116
+ })
117
+
118
+ return matter .stringify (` \n ${content }` , data )
119
+ }
120
+
121
+ function saveContent (content ) {
122
+ const updatedContent = stringifyFrontMatter (content , parsedMatter .value )
123
+ // TODO
124
+ }
125
+
80
126
function findFileFromPath (path , files ) {
81
127
for (const file of files ) {
82
128
if (file .path === path ) {
0 commit comments