Skip to content

Commit 98271a7

Browse files
authored
feat(parser): introduce _dir field in contents (#1613)
1 parent 5ec049d commit 98271a7

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

src/runtime/transformers/path-meta.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ export default defineTransformer({
3232
// Check first part for locale name
3333
const _locale = locales.includes(parts[0]) ? parts.shift() : defaultLocale
3434

35-
const filePath = parts.join('/')
35+
const filePath = generatePath(parts.join('/'))
3636

3737
return <ParsedContent> {
38-
_path: generatePath(filePath),
39-
_draft: isDraft(filePath),
40-
_partial: isPartial(filePath),
38+
_path: filePath,
39+
_dir: filePath.split('/').slice(-2)[0],
40+
_draft: isDraft(_path),
41+
_partial: isPartial(_path),
4142
_locale,
4243
...content,
4344
// TODO: move title to Markdown parser

test/features/transformer-path-meta.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,41 @@ const testCases = {
77
title: '',
88
_draft: false,
99
_partial: false,
10-
_path: '/'
10+
_path: '/',
11+
_dir: ''
1112
},
1213
'content:3.index.draft.md': {
1314
__description: 'Index file with position [Draft]',
1415
title: '',
1516
_draft: true,
1617
_partial: false,
17-
_path: '/'
18+
_path: '/',
19+
_dir: ''
1820
},
1921
'content:1.blog:3.index.draft.md': {
2022
__description: 'Blog Index file with position [Draft]',
2123
title: '',
2224
_draft: true,
2325
_partial: false,
24-
_path: '/blog'
26+
_path: '/blog',
27+
_dir: ''
2528
},
2629
'content:1.blog:_4.the-post.md': {
2730
__description: 'Blog post file with position [Partial]',
2831
title: '4The Post',
2932
_draft: false,
3033
_partial: true,
31-
_path: '/blog/_4.the-post'
34+
_path: '/blog/_4.the-post',
35+
_dir: 'blog'
3236
},
3337
...['1.0.0', '1.1', '1', '1.x', '1.0.x', '1.0.0.x'].reduce((map, semver) => {
3438
map[`content:${semver}:doc.md`] = {
3539
title: 'Doc',
3640
_draft: false,
3741
_partial: false,
3842
_path: `/${semver}/doc`,
39-
_source: 'content'
43+
_source: 'content',
44+
_dir: semver
4045
}
4146
return map
4247
}, {}),
@@ -45,28 +50,32 @@ const testCases = {
4550
title: 'Doc',
4651
_draft: false,
4752
_partial: false,
48-
_path: '/one/two/three/four/five/doc'
53+
_path: '/one/two/three/four/five/doc',
54+
_dir: 'five'
4955
},
5056
'content:1.one:file?param=value#hash.md': {
5157
__description: 'Handle special chars in file name',
5258
title: 'File?param=value#hash',
5359
_draft: false,
5460
_partial: false,
55-
_path: '/one/fileparamvaluehash'
61+
_path: '/one/fileparamvaluehash',
62+
_dir: 'one'
5663
},
5764
'content:indexer.md': {
5865
__description: 'non-index file with index substring',
5966
title: 'Indexer',
6067
_draft: false,
6168
_partial: false,
62-
_path: '/indexer'
69+
_path: '/indexer',
70+
_dir: ''
6371
},
6472
'content:indexer.draft.md': {
6573
__description: 'non-index file with index substring',
6674
title: 'Indexer',
6775
_draft: true,
6876
_partial: false,
69-
_path: '/indexer'
77+
_path: '/indexer',
78+
_dir: ''
7079
}
7180
}
7281

@@ -110,6 +119,12 @@ export const testPathMetaTransformer = () => {
110119
`source is not equal, recieved: ${transformed._source}`
111120
)
112121

122+
expect(transformed).toHaveProperty('_dir')
123+
assert(
124+
transformed._dir === expected._dir,
125+
`directory is not equal, recieved: ${transformed._dir}`
126+
)
127+
113128
expect(transformed).toHaveProperty('_path')
114129
assert(
115130
fullPath.startsWith(`${transformed._source}/${transformed._file}`),

test/fixtures/basic/server/api/parse.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { defineEventHandler, useBody } from 'h3'
1+
import { eventHandler, readBody } from 'h3'
22
import { parseContent } from '#content/server'
33

4-
export default defineEventHandler(async (event) => {
5-
const { id, content, options } = await useBody(event)
4+
export default eventHandler(async (event) => {
5+
const { id, content, options } = await readBody(event)
66

77
// @ts-ignore
88
const parsedContent = await parseContent(id, content, options)

0 commit comments

Comments
 (0)