Skip to content

Commit

Permalink
feat(parser): introduce _dir field in contents (#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 18, 2022
1 parent 5ec049d commit 98271a7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
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

0 comments on commit 98271a7

Please sign in to comment.