Skip to content

Commit

Permalink
feat: pass dataPrefix option as data.matterDataPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
bent10 committed Sep 30, 2023
1 parent ce769c7 commit af595a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
21 changes: 16 additions & 5 deletions packages/hook-frontmatter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export interface Options extends Pick<LoadOptions, 'schema' | 'json'> {
dataPrefix?: boolean | string
}

type UnknownData = {
[key: string]: unknown
}

/**
* A [sequential hook](https://github.com/bent10/marked-extensions/tree/main/packages/sequential-hooks) for marked to support frontmatter in Markdown
* documents.
Expand All @@ -26,12 +30,19 @@ export default function markedHookFrontmatter(
Object.assign(parseOptions, { filename: data.filename })
}

const { matter, content } = parse(markdown, parseOptions)
const { matter = {}, content } = parse(markdown, parseOptions)
const { matterDataPrefix = dataPrefix } = matter as UnknownData

if (typeof dataPrefix === 'boolean') {
Object.assign(data, dataPrefix ? { matter } : matter)
} else if (typeof dataPrefix === 'string') {
data[dataPrefix] = matter
if (typeof matterDataPrefix === 'boolean') {
Object.assign(
data,
matterDataPrefix
? { matter, matterDataPrefix: 'matter' }
: { ...(matter as UnknownData), matterDataPrefix }
)
} else if (typeof matterDataPrefix === 'string') {
data.matterDataPrefix = matterDataPrefix
data[matterDataPrefix] = matter
}

return pupa(content, data, { ignoreMissing: true })
Expand Down
23 changes: 18 additions & 5 deletions packages/hook-frontmatter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ it('should parse frontmatter and add it to the extended hook data property', ()
markdownHooks: [markedHookFrontmatter()],
htmlHooks: [
(html, data) => {
expect(data).toEqual({ foo: 'bar', baz: 'qux' })
expect(data).toEqual({
foo: 'bar',
baz: 'qux',
matterDataPrefix: false
})

return html
}
Expand All @@ -32,7 +36,7 @@ it('should replace placeholders in the content with data from the extended hook'
markdownHooks: [markedHookFrontmatter()],
htmlHooks: [
(html, data) => {
expect(data).toEqual({ foo: 'bar' })
expect(data).toEqual({ foo: 'bar', matterDataPrefix: false })

return html
}
Expand All @@ -52,7 +56,10 @@ it('should add frontmatter to the extended hook data property with a custom data
markdownHooks: [markedHookFrontmatter({ dataPrefix: 'page' })],
htmlHooks: [
(html, data) => {
expect(data).toEqual({ page: { foo: 'bar' } })
expect(data).toEqual({
page: { foo: 'bar' },
matterDataPrefix: 'page'
})

return html
}
Expand All @@ -72,7 +79,10 @@ it('should use `matter` as dataPrefix when the option set to true', () => {
markdownHooks: [markedHookFrontmatter({ dataPrefix: true })],
htmlHooks: [
(html, data) => {
expect(data).toEqual({ matter: { foo: 'bar' } })
expect(data).toEqual({
matter: { foo: 'bar' },
matterDataPrefix: 'matter'
})

return html
}
Expand All @@ -92,7 +102,10 @@ it('should support spaces nor tabs inside placehholder', () => {
markdownHooks: [markedHookFrontmatter({ dataPrefix: true })],
htmlHooks: [
(html, data) => {
expect(data).toEqual({ matter: { foo: 'bar' } })
expect(data).toEqual({
matter: { foo: 'bar' },
matterDataPrefix: 'matter'
})

return html
}
Expand Down

0 comments on commit af595a0

Please sign in to comment.