Comparing version 1.0.1 to 1.0.2
36
index.js
@@ -5,6 +5,8 @@ 'use strict'; | ||
var marked = require('marked'); | ||
var traverse = require('traverse'); | ||
var parse = function(mdContent) { | ||
var json = marked.lexer(mdContent); | ||
var currentHeading, headings = []; | ||
var currentHeading, headings = [], | ||
isOrdered = true; | ||
var output = json.reduce(function(result, item, index, array) { | ||
@@ -26,4 +28,8 @@ switch (item.type) { | ||
break; | ||
case 'list_start': | ||
isOrdered = item.ordered; | ||
break; | ||
case 'text': | ||
var text = '- ' + item.text + '\n'; | ||
var ordered = isOrdered ? '1. ' : '- '; | ||
var text = ordered + item.text + '\n'; | ||
currentHeading.raw = currentHeading.raw ? currentHeading.raw + text : text; | ||
@@ -56,4 +62,4 @@ break; | ||
var parent, index = item.depth - 1; | ||
var curreHeading = headings[index]; | ||
if (curreHeading) { | ||
var currentHeading = headings[index]; | ||
if (currentHeading) { | ||
headings.splice(index, headings.length - index); | ||
@@ -92,2 +98,24 @@ } | ||
return '| ' + tableHeader + '\n|: ' + separator + '\n| ' + tableContent + '\n'; | ||
} | ||
function toMd(jsonObject) { | ||
var mdText = ''; | ||
traverse(jsonObject).reduce(function(acc, value) { | ||
if (this.isLeaf && this.key === 'raw') { | ||
mdText += value; | ||
} else { | ||
mdText += getHash(this.level) + ' ' + this.key + '\n\n'; | ||
} | ||
return; | ||
}); | ||
return mdText; | ||
} | ||
exports.toMd = toMd; | ||
function getHash(level) { | ||
var hash = ''; | ||
for (var i = 0; i < level; i++) { | ||
hash += '#'; | ||
} | ||
return hash; | ||
} |
{ | ||
"name": "md-2-json", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Markdown to JSON convertor", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"marked": "^0.3.6" | ||
"marked": "^0.3.6", | ||
"traverse": "^0.6.6" | ||
}, | ||
@@ -22,0 +23,0 @@ "repository": { |
@@ -7,3 +7,3 @@ # MARKDOWN-TO-JSON | ||
Simple content | ||
### Simple content | ||
@@ -24,3 +24,3 @@ ```js | ||
Multiline Content | ||
### Multiline Content | ||
@@ -55,2 +55,28 @@ ```js | ||
``` | ||
### Converting JSON to MD string | ||
The method `toMd` can be used to convert the JSON Object to Markdown string. | ||
```js | ||
var md2json = require('md-2-json'); | ||
var json = { | ||
"Heading 1": { | ||
raw: "This is a para\n", | ||
} | ||
} | ||
md2json.toMd(json); | ||
/* output | ||
` | ||
# Heading 1 | ||
This is a para | ||
` | ||
*/ | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5282
110
79
2
3
+ Addedtraverse@^0.6.6
+ Addedarray-buffer-byte-length@1.0.1(transitive)
+ Addedarraybuffer.prototype.slice@1.0.3(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddata-view-buffer@1.0.1(transitive)
+ Addeddata-view-byte-length@1.0.1(transitive)
+ Addeddata-view-byte-offset@1.0.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-abstract@1.23.5(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedes-set-tostringtag@2.0.3(transitive)
+ Addedes-to-primitive@1.3.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunction.prototype.name@1.1.6(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-symbol-description@1.0.2(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-bigints@1.0.2(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinternal-slot@1.0.7(transitive)
+ Addedis-array-buffer@3.0.4(transitive)
+ Addedis-async-function@2.0.0(transitive)
+ Addedis-bigint@1.0.4(transitive)
+ Addedis-boolean-object@1.1.2(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-data-view@1.0.1(transitive)
+ Addedis-date-object@1.0.5(transitive)
+ Addedis-finalizationregistry@1.1.0(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-map@2.0.3(transitive)
+ Addedis-negative-zero@2.0.3(transitive)
+ Addedis-number-object@1.0.7(transitive)
+ Addedis-regex@1.1.4(transitive)
+ Addedis-set@2.0.3(transitive)
+ Addedis-shared-array-buffer@1.0.3(transitive)
+ Addedis-string@1.0.7(transitive)
+ Addedis-symbol@1.0.4(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedis-weakmap@2.0.2(transitive)
+ Addedis-weakref@1.0.2(transitive)
+ Addedis-weakset@2.0.3(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedreflect.getprototypeof@1.0.7(transitive)
+ Addedregexp.prototype.flags@1.5.3(transitive)
+ Addedsafe-array-concat@1.1.2(transitive)
+ Addedsafe-regex-test@1.0.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstring.prototype.trim@1.2.9(transitive)
+ Addedstring.prototype.trimend@1.0.8(transitive)
+ Addedstring.prototype.trimstart@1.0.8(transitive)
+ Addedtraverse@0.6.10(transitive)
+ Addedtyped-array-buffer@1.0.2(transitive)
+ Addedtyped-array-byte-length@1.0.1(transitive)
+ Addedtyped-array-byte-offset@1.0.3(transitive)
+ Addedtyped-array-length@1.0.7(transitive)
+ Addedtypedarray.prototype.slice@1.0.3(transitive)
+ Addedunbox-primitive@1.0.2(transitive)
+ Addedwhich-boxed-primitive@1.0.2(transitive)
+ Addedwhich-builtin-type@1.2.0(transitive)
+ Addedwhich-collection@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)