A functional library that takes a markdown body; and tries to create a JSON representation of the document.
- Headings
- Lists
- Paragraphs
Install:
npm install @connected-web/md2json
const md2json = require('@connected-web/md2json')
const fs = require('fs')
const filename = 'README.md'
const md = fs.readFileSync(filename, 'utf8')
const json = md2json(filename, md)
console.log('Markdown to Json:', json)
- Default example output:
examples/example-output.json
- Default tokens output:
examples/example-output-tokens.json
- json2md tokens output:
examples/example-output-json2md.json
- Test output:
examples/test-output.json
md2json(markdown)
md2json(title, markdown)
md2json({
title: 'README.md',
markdown: '# Heading\n\nContent'
})
The top level section of the JSON output.
The text string representing the markdown to be converted to JSON
The type of formatting to output.
{ outputFormat: 'default' }
: Empty string will default to default - a nested structure of markdown as JSON{ outputFormat: 'json2md' }
: Token format compatable with IonicaBizau/json2md (npm: json2md)
const md2json = require('@connected-web/md2json')
const tokens = md2json.tokens('# Heading\n\nContent\n\n## Heading 1.1')
console.log(tokens)
Output:
[
{
"name": "h1",
"text": "Heading"
},
{
"name": "p",
"text": "Content"
},
{
"name": "h2",
"text": "Heading 1.1"
}
]
Instead of using the outputFormat: json2md
option - which is compatable with IonicaBizau/json2md (npm: json2md), you can directly call this method:
const md2json = require('@connected-web/md2json')
const tokens = md2json.json2mdTokens('# Heading\n\nContent\n\n## Heading 1.1')
console.log(tokens)
Output:
[
{
"h1": "Heading"
},
{
"p": "Content"
},
{
"h2": "Heading 1.1"
}
]
You can then feed these tokens back into json2md
to create markdown again.
Not all formats and formatting are supported; this isn't guarenteed as a fully backwards compatable transformation - but please raise an issue with an example.
- Format parameters
- Render Markdown as HTML
- Create DOM from Markdown
- Create Tokens in the form:
{ name: el.tagName, text: el.text }
- Parse Tokens into Hierarchy
- Return JSON hierarchy
- Inline formats not fully supported, e.g.:
- bold and italic text will be reduced to plain text
- the markdown equivalent of a
<code>code block</code>
will duplicate the code text into a new block
Released under ISC.
- marked
- cheerio
- mocha
- chai
- standard
- Add options to support json2md compatible output
- Support a broader range of HTML tokens, including blockquote, img, code, table, tr, th, td
- Fix failing tests based on
marked
interface - Add PR checking to project
- Add
.tokens(markdown)
method to API
- First version ready for release
- Not perfect, but hopefully something people can work with