Skip to content

Commit 52a62a0

Browse files
committed
up
1 parent c2f6c62 commit 52a62a0

File tree

4 files changed

+47
-44
lines changed

4 files changed

+47
-44
lines changed

components/molecules/FilesTree.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div
55
class="group border-r-2 py-2 pr-6 flex items-center text-sm font-medium focus:u-bg-gray-50 focus:outline-none w-full cursor-pointer"
66
:class="{
7-
[`pl-${6 + (level * 3)}`]: true,
7+
[`pl-[${24 + (level * 12)}px]`]: true,
88
'u-bg-gray-100 u-border-gray-800 u-text-gray-900': isSelected(file),
99
'border-transparent u-text-gray-500 hover:u-text-gray-900 hover:u-bg-gray-50': !isSelected(file)
1010
}"

composables/useMarkdown.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as matter from 'gray-matter'
2+
import flat from 'flat'
3+
4+
export const useMarkdown = () => {
5+
function parseFrontMatter (content) {
6+
// @ts-ignore
7+
const { data, content: c, ...rest } = matter.default ? matter.default(content) : matter(content)
8+
9+
// unflatten frontmatter data
10+
// convert `parent.child` keys into `parent: { child: ... }`
11+
const unflattenData = flat.unflatten(data || {}, {})
12+
13+
return {
14+
content: c.replace(/^\n/, ''),
15+
matter: unflattenData,
16+
...rest
17+
}
18+
}
19+
20+
function stringifyFrontMatter (content, data = {}) {
21+
// flatten frontmatter data
22+
// convert `parent: { child: ... }` into flat keys `parent.child`
23+
data = flat.flatten(data, {
24+
// preserve arrays and their contents as is and do not waltk through arrays
25+
// flatten array will be like `parent.0.child` and `parent.1.child` with is not readable
26+
safe: true
27+
})
28+
29+
// eslint-disable-next-line import/no-named-as-default-member
30+
return matter.stringify(`\n${content}`, data)
31+
}
32+
33+
return {
34+
parseFrontMatter,
35+
stringifyFrontMatter
36+
}
37+
}

nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default defineNuxtConfig({
7272
require('@tailwindcss/typography')
7373
],
7474
content: ['presets/*.ts'],
75-
safelist: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30].map(number => `pl-${number}`)
75+
safelist: [24, 36, 48, 60, 72, 84, 96, 108, 120].map(number => `pl-[${number}px]`)
7676
}
7777
}
7878
})

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

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
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'
2725
import type { Team, Project, File, Branch } from '~/types'
2826
2927
const props = defineProps({
@@ -38,6 +36,7 @@ const props = defineProps({
3836
})
3937
4038
const client = useStrapiClient()
39+
const { parseFrontMatter, stringifyFrontMatter } = useMarkdown()
4140
4241
const branch: Ref<Branch> = ref(null)
4342
const file: Ref<File> = ref(null)
@@ -46,18 +45,14 @@ const updatedContent: Ref<string> = ref('')
4645
const parsedContent: Ref<string> = ref('')
4746
const parsedMatter: Ref<string> = ref('')
4847
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`))
5049
5150
const { data: files, refresh: refreshFiles } = await useAsyncData('files', () => client<File[]>(`/projects/${props.project.id}/files`, {
5251
params: {
5352
branch: branch.value?.name
5453
}
5554
}))
5655
57-
watch(content, () => {
58-
setupEditor()
59-
}, { immediate: true })
60-
6156
// Select file when files changes
6257
watch(files, () => {
6358
const currentFile = file.value?.path ? findFileFromPath(file.value.path, files.value) : null
@@ -67,9 +62,7 @@ watch(files, () => {
6762
6863
// Select branch when branches changes
6964
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])
7366
}, { immediate: true })
7467
7568
// Fetch content when file changes
@@ -86,39 +79,13 @@ watch(file, async () => {
8679
// Fetch files when branch changes
8780
watch(branch, async () => await refreshFiles())
8881
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)
9185
92-
parsedContent.value = content
86+
parsedContent.value = c
9387
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 })
12289
12390
function saveContent (content) {
12491
updatedContent.value = stringifyFrontMatter(content, parsedMatter.value)
@@ -142,6 +109,5 @@ function selectFile (f: File) {
142109
143110
function selectBranch (b: Branch) {
144111
branch.value = b
145-
localStorage.setItem(`projects-${props.project.id}-branch`, branch.value?.name)
146112
}
147113
</script>

0 commit comments

Comments
 (0)