From 03c918fec065a0fcc9d00b7062af267bfcd81136 Mon Sep 17 00:00:00 2001 From: Sean Lang Date: Fri, 13 May 2016 21:09:42 -0400 Subject: [PATCH] fix handling bad front-matter & add test --- lib/index.coffee | 15 ++++++++++----- test/index.coffee | 11 +++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index 5c25102..8ffd88c 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -243,11 +243,16 @@ module.exports = (dirtyMarkdown, options = {}) -> out = '' # handle yaml front-matter - content = fm(dirtyMarkdown) - if Object.keys(content.attributes).length isnt 0 - out += '---\n' + yaml.safeDump(content.attributes).trim() + '\n---\n\n' - - ast = marked.lexer(content.body) + try + content = fm(dirtyMarkdown) + if Object.keys(content.attributes).length isnt 0 + out += '---\n' + yaml.safeDump(content.attributes).trim() + '\n---\n\n' + content = content.body + catch + # parsing failed, just ignore front-matter + content = dirtyMarkdown + + ast = marked.lexer(content) rawLinks = ast.links # see issue: https://github.com/chjj/marked/issues/472 links = [] diff --git a/test/index.coffee b/test/index.coffee index 8af5a8a..4c0560a 100644 --- a/test/index.coffee +++ b/test/index.coffee @@ -761,6 +761,17 @@ describe 'front-matter', -> My content ''') + it 'should ignore stuff that looks like (but isn\'t) front-matter', -> + tidyMdSnippet(''' + --- + `hi` + --- + ''').should.equal(''' + -------------------------------------------------------------------------------- + + # `hi` + ''') + describe 'comments', -> it 'should handle comments', -> tidyMdSnippet('''