Skip to content

Commit

Permalink
add support for front-matter
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
notslang committed Mar 17, 2015
1 parent bca7db5 commit 9d4a947
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ marked = require 'marked'
Entities = require('html-entities').AllHtmlEntities
indent = require 'indent'
pad = require 'pad'
yaml = require 'js-yaml'
fm = require 'front-matter'


htmlEntities = new Entities()

Expand Down Expand Up @@ -185,12 +188,18 @@ fixHeaders = (ast) ->
return ast

module.exports = (dirtyMarkdown) ->
ast = marked.lexer(dirtyMarkdown)
out = []

# handle yaml front-matter
content = fm(dirtyMarkdown)
if Object.keys(content.attributes).length isnt 0
out.push '---', yaml.safeDump(content.attributes).trim(), '---\n'

ast = marked.lexer(content.body)

# see issue: https://github.com/chjj/marked/issues/472
links = ast.links

out = []
previousToken = undefined

# remove all the `space` and `list_end` tokens - they're useless
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"tidy-markdown": "./bin/index.js"
},
"dependencies": {
"front-matter": "^1.0.0",
"html-entities": "^1.0.10",
"indent": "0.0.1",
"js-yaml": "^3.2.7",
"marked": "^0.3.2",
"pad": "0.0.5"
},
Expand Down
17 changes: 17 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,23 @@ describe 'html', ->
</dl>
''')


describe 'front-matter', ->
it 'should handle front-matter', ->
tidyMdSnippet('''
---
title: "Awesome markdown file"
---
My content
''').should.equal('''
---
title: Awesome markdown file
---
My content
''')


describe 'full documents', ->
it 'should reformat to match expected', ->
for file in fs.readdirSync('./test/cases')
Expand Down

0 comments on commit 9d4a947

Please sign in to comment.