@benglynn/md2json v0.0.7
@benglynn/md2json
Converts markdown articles into JSON articles.
Why?
So that the articles can be easily consumed by a JavaScript web app.
How?
md2json uses the showdown convertor, with its metadata
option. Metadata is parsed with js-yaml, it can be anything you like. It's added as a metadata
property.
Along with metadata
two other properties are added to the resulting JSON: name
and html
.
The name comes from the name of the file, so example-article.md
gets the name example-article
(and becomes example-article.json
).
Any code blocks in the HTML returned from showdown are parsed with prism-js, but only if written in a default supported language, To support other code see prism's loadLanguages()
.
The HTML is then base64 encoded for transit, and can be decoded in your JavaScript app with atob()
.
An example transofmation
example-article.md
---
headline: 'Example article'
description: 'The metadata is valid yaml'
---
# Markdown syntax from here on
Here's an is example article __in markdown__.
```javascript
var codeBlocks = 'highlighted'
```
Becomes example-article.json
(html value truncated)
{
"metadata": {
"headline": "Example article",
"description": "The metadata is valid yaml",
},
"html": "PGgxIGlkPSJtYXJrZG93bnN5bnRheGZyb21oZXJ...",
"name": "example-article"
}
Decoding the html value with atob()
<h1 id="markdownsyntaxfromhereon">Markdown syntax from here on</h1>
<p>Here's an is example article <strong>in markdown</strong>.</p>
<pre class="javascript language-javascript"><code class="javascript language-javascript"><span class="token keyword">var</span> codeBlocks <span class="token operator">=</span> <span class="token string">'highlighted'</span>
</code></pre>
Usage
Install as a dev dependency
npm i -D @benglynn/md2json
Parse an entire directory of markdown files and crate corresponding JSON files in another directory. Note that the destination directory will be emptied before the new json files are added.
const md2json = require('@benglynn/md2json')
const path = require('path')
const srcMarkdownDir = path.join(__dirname, 'posts')
const destJsonDir = path.join(__dirname, "dist")
md2json(srcMarkdownDir, destJsonDir)
Tests
npm test