Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

md-2-json

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

md-2-json - npm Package Compare versions

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;
}

5

package.json
{
"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
`
*/
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc