Skip to content

Commit 2b048fd

Browse files
committed
up
1 parent e21375a commit 2b048fd

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
@@ -20,8 +20,6 @@
2020

2121
<script setup lang="ts">
2222
import type { PropType, Ref } from 'vue'
23-
import matter from 'gray-matter'
24-
import { unflatten, flatten } from 'flat'
2523
import type { Team, Project, File, Branch } from '~/types'
2624
2725
const props = defineProps({
@@ -36,6 +34,7 @@ const props = defineProps({
3634
})
3735
3836
const client = useStrapiClient()
37+
const { parseFrontMatter, stringifyFrontMatter } = useMarkdown()
3938
4039
const branch: Ref<Branch> = ref(null)
4140
const file: Ref<File> = ref(null)
@@ -44,18 +43,14 @@ const updatedContent: Ref<string> = ref('')
4443
const parsedContent: Ref<string> = ref('')
4544
const parsedMatter: Ref<string> = ref('')
4645
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`))
4847
4948
const { data: files, refresh: refreshFiles } = await useAsyncData('files', () => client<File[]>(`/projects/${props.project.id}/files`, {
5049
params: {
5150
branch: branch.value?.name
5251
}
5352
}))
5453
55-
watch(content, () => {
56-
setupEditor()
57-
}, { immediate: true })
58-
5954
// Select file when files changes
6055
watch(files, () => {
6156
const currentFile = file.value?.path ? findFileFromPath(file.value.path, files.value) : null
@@ -65,9 +60,7 @@ watch(files, () => {
6560
6661
// Select branch when branches changes
6762
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])
7164
}, { immediate: true })
7265
7366
// Fetch content when file changes
@@ -84,39 +77,13 @@ watch(file, async () => {
8477
// Fetch files when branch changes
8578
watch(branch, async () => await refreshFiles())
8679
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)
8983
90-
parsedContent.value = content
84+
parsedContent.value = c
9185
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 })
12087
12188
function saveContent (content) {
12289
updatedContent.value = stringifyFrontMatter(content, parsedMatter.value)
@@ -140,6 +107,5 @@ function selectFile (f: File) {
140107
141108
function selectBranch (b: Branch) {
142109
branch.value = b
143-
localStorage.setItem(`projects-${props.project.id}-branch`, branch.value?.name)
144110
}
145111
</script>

0 commit comments

Comments
 (0)