Skip to content

Commit

Permalink
fix: resolves markdown without frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
bent10 committed Oct 1, 2023
1 parent 7f982e5 commit 1799a0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/hook-frontmatter/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import yaml, { type LoadOptions } from 'js-yaml'
// Define a lexer for frontmatter and content.
const lexer = moo.states({
main: {
openTag: { match: /^---\n/, lineBreaks: true, push: 'matter' }
openTag: { match: /^---\n/, lineBreaks: true, push: 'matter' },
chunk: moo.fallback
},
matter: {
closeTag: { match: /^---\n/, lineBreaks: true, push: 'content' },
Expand Down
24 changes: 24 additions & 0 deletions packages/hook-frontmatter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ it('should closed with double lines', () => {
expect(html).toBe('<hr>\n<h1>Content</h1>\n')
})

it('should handle markdown without frontmatter', () => {
const html = new Marked()
.use(
markedSequentialHooks({
markdownHooks: [markedHookFrontmatter()]
})
)
.parse('# Content\nfoo\n')

expect(html).toBe('<h1>Content</h1>\n<p>foo</p>\n')
})

it('should pass the filename option when available in data', () => {
const md = '---\nfoo: bar\nfoo: baz\n---\nContent'

Expand Down Expand Up @@ -173,3 +185,15 @@ it('should ends `closeTag` with newline', () => {
.parse('---\nfoo: bar\n------\n')
).toThrow(/can not read a block mapping entry/)
})

it('should ends `closeTag` with newline', () => {
expect(() =>
new Marked()
.use(
markedSequentialHooks({
markdownHooks: [markedHookFrontmatter()]
})
)
.parse('---\nfoo: bar\n------\n')
).toThrow(/can not read a block mapping entry/)
})

0 comments on commit 1799a0e

Please sign in to comment.