Skip to content

Commit

Permalink
feat: add interpolation option
Browse files Browse the repository at this point in the history
  • Loading branch information
bent10 committed Oct 1, 2023
1 parent 48b8c21 commit 2f2049f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/hook-frontmatter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export interface Options extends Pick<LoadOptions, 'schema' | 'json'> {
* string is provided, the data will be added with that string as the key.
*/
dataPrefix?: boolean | string

/**
* Set to `false` to disable data interpolation in the markdown content.
*/
interpolation?: boolean
}

type UnknownData = {
Expand All @@ -23,7 +28,7 @@ type UnknownData = {
export default function markedHookFrontmatter(
options: Options = {}
): MarkdownHook {
const { dataPrefix = false, ...parseOptions } = options
const { dataPrefix = false, interpolation = true, ...parseOptions } = options

return (markdown, data) => {
if (data.filename) {
Expand All @@ -45,6 +50,8 @@ export default function markedHookFrontmatter(
data[matterDataPrefix] = matter
}

return pupa(content, data, { ignoreMissing: true })
return interpolation
? pupa(content, data, { ignoreMissing: true })
: content
}
}
13 changes: 13 additions & 0 deletions packages/hook-frontmatter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ it('should use `matter` as dataPrefix when the option set to true', () => {
expect(html).toBe('<p>Hello, bar!</p>\n')
})

it('should disable interpolation when the option set to false', () => {
const md = '---\nfoo: bar\n---\nHello, { foo}!'
const html = new Marked()
.use(
markedSequentialHooks({
markdownHooks: [markedHookFrontmatter({ interpolation: false })]
})
)
.parse(md)

expect(html).toBe('<p>Hello, {foo}!</p>\n')
})

it('should support spaces nor tabs inside placehholder', () => {
const md = '---\nfoo: bar\n---\nHello, { matter.foo }!'
const html = new Marked()
Expand Down

0 comments on commit 2f2049f

Please sign in to comment.