Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): introduce _dir field in contents #1613

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/runtime/transformers/path-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ export default defineTransformer({
// Check first part for locale name
const _locale = locales.includes(parts[0]) ? parts.shift() : defaultLocale

const filePath = parts.join('/')
const filePath = generatePath(parts.join('/'))

return <ParsedContent> {
_path: generatePath(filePath),
_draft: isDraft(filePath),
_partial: isPartial(filePath),
_path: filePath,
_dir: filePath.split('/').slice(-2)[0],
_draft: isDraft(_path),
_partial: isPartial(_path),
_locale,
...content,
// TODO: move title to Markdown parser
Expand Down
33 changes: 24 additions & 9 deletions test/features/transformer-path-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,41 @@ const testCases = {
title: '',
_draft: false,
_partial: false,
_path: '/'
_path: '/',
_dir: ''
},
'content:3.index.draft.md': {
__description: 'Index file with position [Draft]',
title: '',
_draft: true,
_partial: false,
_path: '/'
_path: '/',
_dir: ''
},
'content:1.blog:3.index.draft.md': {
__description: 'Blog Index file with position [Draft]',
title: '',
_draft: true,
_partial: false,
_path: '/blog'
_path: '/blog',
_dir: ''
},
'content:1.blog:_4.the-post.md': {
__description: 'Blog post file with position [Partial]',
title: '4The Post',
_draft: false,
_partial: true,
_path: '/blog/_4.the-post'
_path: '/blog/_4.the-post',
_dir: 'blog'
},
...['1.0.0', '1.1', '1', '1.x', '1.0.x', '1.0.0.x'].reduce((map, semver) => {
map[`content:${semver}:doc.md`] = {
title: 'Doc',
_draft: false,
_partial: false,
_path: `/${semver}/doc`,
_source: 'content'
_source: 'content',
_dir: semver
}
return map
}, {}),
Expand All @@ -45,28 +50,32 @@ const testCases = {
title: 'Doc',
_draft: false,
_partial: false,
_path: '/one/two/three/four/five/doc'
_path: '/one/two/three/four/five/doc',
_dir: 'five'
},
'content:1.one:file?param=value#hash.md': {
__description: 'Handle special chars in file name',
title: 'File?param=value#hash',
_draft: false,
_partial: false,
_path: '/one/fileparamvaluehash'
_path: '/one/fileparamvaluehash',
_dir: 'one'
},
'content:indexer.md': {
__description: 'non-index file with index substring',
title: 'Indexer',
_draft: false,
_partial: false,
_path: '/indexer'
_path: '/indexer',
_dir: ''
},
'content:indexer.draft.md': {
__description: 'non-index file with index substring',
title: 'Indexer',
_draft: true,
_partial: false,
_path: '/indexer'
_path: '/indexer',
_dir: ''
}
}

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

expect(transformed).toHaveProperty('_dir')
assert(
transformed._dir === expected._dir,
`directory is not equal, recieved: ${transformed._dir}`
)

expect(transformed).toHaveProperty('_path')
assert(
fullPath.startsWith(`${transformed._source}/${transformed._file}`),
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/basic/server/api/parse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineEventHandler, useBody } from 'h3'
import { eventHandler, readBody } from 'h3'
import { parseContent } from '#content/server'

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

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