Skip to content

Commit 33194cf

Browse files
committed
feat(project): support front-matter with Editor
1 parent 6a9840e commit 33194cf

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

pages/@[team]/[project]/content.vue

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
/>
1717
</template>
1818

19-
<DocusEditor v-model="content" />
19+
<DocusEditor :model-value="parsedContent" @update:model-value="saveContent" />
2020
</ProjectPage>
2121
</template>
2222

2323
<script setup lang="ts">
2424
import type { PropType, Ref } from 'vue'
25+
import matter from 'gray-matter'
26+
import { unflatten, flatten } from 'flat'
2527
import type { Team, Project, File, Branch } from '~/types'
2628
2729
const props = defineProps({
@@ -40,6 +42,8 @@ const client = useStrapiClient()
4042
const branch: Ref<Branch> = ref(null)
4143
const file: Ref<File> = ref(null)
4244
const content: Ref<string> = ref('')
45+
const parsedContent: Ref<string> = ref('')
46+
const parsedMatter: Ref<string> = ref('')
4347
4448
const { data: branches, refresh: refreshBranches } = await useAsyncData('files', () => client<Branch[]>(`/projects/${props.project.id}/branches`))
4549
@@ -49,6 +53,10 @@ const { data: files, refresh: refreshFiles } = await useAsyncData('files', () =>
4953
}
5054
}))
5155
56+
watch(content, () => {
57+
setupEditor()
58+
}, { immediate: true })
59+
5260
// Select file when files changes
5361
watch(files, () => {
5462
const currentFile = file.value?.path ? findFileFromPath(file.value.path, files.value) : null
@@ -77,6 +85,44 @@ watch(file, async () => {
7785
// Fetch files when branch changes
7886
watch(branch, async () => await refreshFiles())
7987
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+
80126
function findFileFromPath (path, files) {
81127
for (const file of files) {
82128
if (file.path === path) {

0 commit comments

Comments
 (0)