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'
27
25
import type { Team , Project , File , Branch } from ' ~/types'
28
26
29
27
const props = defineProps ({
@@ -38,6 +36,7 @@ const props = defineProps({
38
36
})
39
37
40
38
const client = useStrapiClient ()
39
+ const { parseFrontMatter, stringifyFrontMatter } = useMarkdown ()
41
40
42
41
const branch: Ref <Branch > = ref (null )
43
42
const file: Ref <File > = ref (null )
@@ -46,18 +45,14 @@ const updatedContent: Ref<string> = ref('')
46
45
const parsedContent: Ref <string > = ref (' ' )
47
46
const parsedMatter: Ref <string > = ref (' ' )
48
47
49
- const { data : branches, refresh : refreshBranches } = await useAsyncData (' files ' , () => client <Branch []>(` /projects/${props .project .id }/branches ` ))
48
+ const { data : branches, refresh : refreshBranches } = await useAsyncData (' branches ' , () => client <Branch []>(` /projects/${props .project .id }/branches ` ))
50
49
51
50
const { data : files, refresh : refreshFiles } = await useAsyncData (' files' , () => client <File []>(` /projects/${props .project .id }/files ` , {
52
51
params: {
53
52
branch: branch .value ?.name
54
53
}
55
54
}))
56
55
57
- watch (content , () => {
58
- setupEditor ()
59
- }, { immediate: true })
60
-
61
56
// Select file when files changes
62
57
watch (files , () => {
63
58
const currentFile = file .value ?.path ? findFileFromPath (file .value .path , files .value ) : null
@@ -67,9 +62,7 @@ watch(files, () => {
67
62
68
63
// Select branch when branches changes
69
64
watch (branches , () => {
70
- const branchName = localStorage .getItem (` projects-${props .project .id }-branch ` )
71
-
72
- selectBranch ((branchName && branches .value .find (branch => branch .name === branchName )) || branches .value .find (branch => branch .name === props .project .repository .default_branch ) || branches .value [0 ])
65
+ selectBranch (branches .value .find (branch => branch .name === props .project .repository .default_branch ) || branches .value [0 ])
73
66
}, { immediate: true })
74
67
75
68
// Fetch content when file changes
@@ -86,39 +79,13 @@ watch(file, async () => {
86
79
// Fetch files when branch changes
87
80
watch (branch , async () => await refreshFiles ())
88
81
89
- function setupEditor () {
90
- const { content, matter } = parseFrontMatter ()
82
+ // Split markdown front-matter when content changes
83
+ watch (content , () => {
84
+ const { content : c, matter } = parseFrontMatter (content .value )
91
85
92
- parsedContent .value = content
86
+ parsedContent .value = c
93
87
parsedMatter .value = matter
94
- }
95
-
96
- function parseFrontMatter () {
97
- const { data, content : c, ... rest } = matter (content .value )
98
-
99
- // unflatten frontmatter data
100
- // convert `parent.child` keys into `parent: { child: ... }`
101
- const unflattenData = unflatten (data || {}, {})
102
-
103
- return {
104
- content: c .replace (/ ^ \n / , ' ' ),
105
- matter: unflattenData ,
106
- ... rest
107
- }
108
- }
109
-
110
- function stringifyFrontMatter (content , data = {}) {
111
- // flatten frontmatter data
112
- // convert `parent: { child: ... }` into flat keys `parent.child`
113
- data = flatten (data , {
114
- // preserve arrays and their contents as is and do not waltk through arrays
115
- // flatten array will be like `parent.0.child` and `parent.1.child` with is not readable
116
- safe: true
117
- })
118
-
119
- // eslint-disable-next-line import/no-named-as-default-member
120
- return matter .stringify (` \n ${content }` , data )
121
- }
88
+ }, { immediate: true })
122
89
123
90
function saveContent (content ) {
124
91
updatedContent .value = stringifyFrontMatter (content , parsedMatter .value )
@@ -142,6 +109,5 @@ function selectFile (f: File) {
142
109
143
110
function selectBranch (b : Branch ) {
144
111
branch .value = b
145
- localStorage .setItem (` projects-${props .project .id }-branch ` , branch .value ?.name )
146
112
}
147
113
</script >
0 commit comments