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