Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Fix error parsing in FrontMatter
Browse files Browse the repository at this point in the history
Fix #136
  • Loading branch information
zhuochun committed Jul 23, 2018
1 parent f5bb58a commit 08cb6d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
10 changes: 8 additions & 2 deletions lib/helpers/front-matter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
16 changes: 16 additions & 0 deletions spec/helpers/front-matter-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []

Expand Down

0 comments on commit 08cb6d2

Please sign in to comment.