-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
28 lines (22 loc) · 871 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
const md = require('markdown-it')()
const md2nestedjson = markdown => {
const tokens = md.parse(markdown)
const isOnlyCodeBlocks = tokens.every(token => token.type === 'code_block')
const json = tokens.map(token =>
token.type === 'code_block' ? md2nestedjson(token.content)
: token
)
console.log('json', JSON.stringify(json, null, 2))
// .filter(token => token.content)
// .map(token => {
// const trimmed = token.content.trim()
// return {
// token,
// value: token.content.replace(/[#-]+\s*/g, ''),
// ...trimmed.startsWith('#') ? { headingLevel: token.content.replace(/[^#]/g, '').length } : null,
// ...trimmed.startsWith('-') ? { listLevel: token.content.match(/^( *)-.*/g, '') } : null,
// }
// })
return isOnlyCodeBLocks ? json.flat() : json
}
module.exports = md2nestedjson