forked from yoshuawuyts/mdjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (26 loc) · 786 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const assert = require('assert')
const marked = require('marked')
const clone = require('clone')
const lexer = new marked.Lexer()
module.exports = toObj
// map a markdown string to an object
// with `html` and `raw` fields
// str -> obj
function toObj (txt) {
assert.equal(typeof txt, 'string', 'input should be a markdown string')
const tokens = lexer.lex(txt)
const parsed = marked.parser(clone(tokens)).split('\n')
const res = {}
var key = ''
tokens.forEach(function (token, i) {
if (i === 0) assert.equal(token.type, 'heading', 'files should start with a heading')
if (token.type === 'heading') {
key = token.text
res[key] = {html: '', raw: ''}
return
}
res[key].html += parsed[i]
res[key].raw += token.text
})
return res
}