-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pipeline
committed
Sep 14, 2017
0 parents
commit 400761c
Showing
3 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# MARKDOWN-TO-JSON | ||
|
||
A simple library to convert markdown content to JSON object. | ||
|
||
## Usage | ||
|
||
Simple content | ||
|
||
```js | ||
|
||
var md2json = require('md-2-json'); | ||
|
||
md2json.parse('This is a markdown content'); | ||
|
||
/* output | ||
{ | ||
raw: "This is a markdown content\n" | ||
} | ||
*/ | ||
|
||
``` | ||
|
||
Multiline Content | ||
|
||
```js | ||
|
||
var md2json = require('md-2-json'); | ||
var mdContent = ` | ||
# Heading 1 | ||
This is a para | ||
- This is a list | ||
## Heading 2 | ||
This is a para | ||
` | ||
|
||
md2json.parse(mdContent); | ||
|
||
/* output | ||
{ | ||
"Heading 1": { | ||
raw: "This is a para\n - This is a list\n", | ||
"Heading 2": { | ||
raw: "This is a para\n" | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var marked = require('marked'); | ||
|
||
var parse = function(mdContent) { | ||
var json = marked.lexer(mdContent); | ||
var currentHeading, headings = []; | ||
var output = json.reduce(function(result, item, index, array) { | ||
switch (item.type) { | ||
case 'heading': | ||
if (!currentHeading || item.depth == 1) { | ||
headings = []; | ||
result[item.text] = {}; | ||
currentHeading = result[item.text]; | ||
headings.push(item.text); | ||
} else { | ||
var parentHeading = getParentHeading(headings, item, result); | ||
headings = parentHeading.headings; | ||
currentHeading = parentHeading.parent; | ||
currentHeading[item.text] = {}; | ||
currentHeading = currentHeading[item.text]; | ||
} | ||
break; | ||
case 'text': | ||
var text = '- ' + item.text + '\n'; | ||
currentHeading.raw = currentHeading.raw ? currentHeading.raw + text : text; | ||
break; | ||
case 'table': | ||
var tableContent = getTableContent(item); | ||
currentHeading.raw = currentHeading.raw ? currentHeading.raw + tableContent : tableContent; | ||
break; | ||
case 'space': | ||
currentHeading.raw = currentHeading.raw ? currentHeading.raw + '\n' : '\n'; | ||
break; | ||
case 'paragraph': | ||
if (!currentHeading) { | ||
currentHeading = result; | ||
} | ||
var para = item.text + '\n'; | ||
currentHeading.raw = currentHeading.raw ? currentHeading.raw + para : para; | ||
break; | ||
default: | ||
break; | ||
} | ||
return result; | ||
}, {}); | ||
return output; | ||
} | ||
exports.parse = parse; | ||
|
||
function getParentHeading(headings, item, result) { | ||
var parent, index = item.depth - 1; | ||
var curreHeading = headings[index]; | ||
if (curreHeading) { | ||
headings.splice(index, headings.length - index); | ||
} | ||
headings.push(item.text); | ||
for (var i = 0; i < index; i++) { | ||
if (!parent) { | ||
parent = result[headings[i]]; | ||
} else { | ||
parent = parent[headings[i]]; | ||
} | ||
} | ||
return { | ||
headings: headings, | ||
parent: parent | ||
}; | ||
} | ||
|
||
function getTableContent(item) { | ||
var tableHeader = '', | ||
tableContent = '', | ||
separator = ''; | ||
for (var i = 0; i < item.header.length; i++) { | ||
tableHeader += item.header[i] + ' | '; | ||
} | ||
for (var i = 0; i < item.align.length; i++) { | ||
separator += '---------:| '; | ||
} | ||
for (var i = 0; i < item.cells.length; i++) { | ||
var cells = item.cells[i]; | ||
for (var j = 0; j < cells.length; j++) { | ||
tableContent += cells[j] + ' | '; | ||
} | ||
} | ||
return '| ' + tableHeader + '\n|: ' + separator + '\n| ' + tableContent + '\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "md-2-json", | ||
"version": "1.0.1", | ||
"description": "Markdown to JSON convertor", | ||
"main": "index.js", | ||
"keywords": [ | ||
"md-to-json", | ||
"markdown", | ||
"markdown-to-json", | ||
"md2json", | ||
"md-2-json", | ||
"mdjson", | ||
"json", | ||
"parser" | ||
], | ||
"author": "ajithr", | ||
"license": "MIT", | ||
"dependencies": { | ||
"marked": "^0.3.6" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ajithr/md-2-json.git" | ||
} | ||
} |