diff --git a/CHANGELOG.md b/CHANGELOG.md index 19782484..210d6606 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.7.5 + +* Fix handling frontMatter parsing ([#136](https://github.com/zhuochun/md-writer/issues/136)) + ## 2.7.4 * Add Critic-Markup support ([#221](https://github.com/zhuochun/md-writer/issues/221)) diff --git a/lib/helpers/front-matter.coffee b/lib/helpers/front-matter.coffee index 41cdd031..759b5615 100644 --- a/lib/helpers/front-matter.coffee +++ b/lib/helpers/front-matter.coffee @@ -25,8 +25,14 @@ class FrontMatter @_findFrontMatter (match) => try @content = yaml.safeLoad(match.match[1].trim()) || {} - @leadingFence = match.matchText.startsWith("---") - @isEmpty = false + + if typeof @content != "object" # error parsing #136 + @content = {} + @leadingFence = true + @isEmpty = true + else + @leadingFence = match.matchText.startsWith("---") + @isEmpty = false catch error @parseError = error @content = {} diff --git a/spec/helpers/front-matter-spec.coffee b/spec/helpers/front-matter-spec.coffee index 502fe257..dac921fa 100644 --- a/spec/helpers/front-matter-spec.coffee +++ b/spec/helpers/front-matter-spec.coffee @@ -38,6 +38,22 @@ describe "FrontMatter", -> expect(frontMatter.isEmpty).toBe(true) expect(frontMatter.content).toEqual({}) + it "is empty when editor has weird front matter", -> + editor.setText """ + --- + 1. [VLC media player](https://www.videolan.org/vlc/index.html) - VideoLAN, a project and a non-profit organization + 2. [Inkscape](https://inkscape.org/en/) - free and open-source vector graphics editor + 3. [GIMP](https://www.gimp.org/) - GNU Image Manipulation Program) is a free and open-source raster graphics editor' + --- + + some random text 1 + some random text 2 + """ + + frontMatter = new FrontMatter(editor) + expect(frontMatter.isEmpty).toBe(true) + expect(frontMatter.content).toEqual({}) + describe "editor with jekyll front matter", -> [editor, frontMatter] = []