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

json2md

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json2md - npm Package Compare versions

Comparing version 1.4.1 to 1.5.0

24

lib/converters.js

@@ -91,1 +91,25 @@ // converters

};
converters.table = function(input, json2md) {
if (typeof input != 'object') {
return '';
}
if (!input.hasOwnProperty('headers') || !input.hasOwnProperty('rows')) {
return '';
}
var header = input.headers.join(' | ');
var spaces = input.headers.map(function() { return '---'; }).join(' | ');
var data = input.rows.map(function(r) {
if (Array.isArray(r)) {
return r.map(function(el) {
return parseTextFormat(json2md(el));
}).join(' | ');
} else {
return input.headers.map(function(h) {
return parseTextFormat(json2md(r[h]));
}).join(' | ');
}
}).join('\n');
return [header, spaces, data].join('\n');
};

32

lib/index.js

@@ -10,17 +10,19 @@ // Dependencies

*
* | Type | Element | Data | Example |
* |--------------|--------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
* | `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` |
* | `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` |
* | `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` |
* | `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` |
* | `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` |
* | `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` |
* | `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` |
* | `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` |
* | `img` | Image | An object or an array of objects containing the `title` and `source` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png" } }` |
* | `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` |
* | `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` |
* | `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. | `{ code: { "language": "html", "content": "<script src='dummy.js'></script>" } } |
* | Type | Element | Data | Example |
* |--------------|--------------------|--------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
* | `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` |
* | `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` |
* | `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` |
* | `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` |
* | `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` |
* | `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` |
* | `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` |
* | `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` |
* | `img` | Image | An object or an array of objects containing the `title` and `source` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png" } }` |
* | `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` |
* | `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` |
* | `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. | `{ code: { "language": "html", "content": "<script src='dummy.js'></script>" } }` |
* | `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s). | `{ table: { headers: ["a", "b"], rows: [{ a: "col1", b: "col2" }] } }` or `{ table: { headers: ["a", "b"], rows: [["col1", "col2"]] } }` |
*
*
* You can extend the `json2md.converters` object to support your custom types.

@@ -49,3 +51,3 @@ *

prefix = prefix || "";
if (typeof data === "string") {
if (typeof data === "string" || typeof data === 'number') {
return prefix + data;

@@ -52,0 +54,0 @@ }

{
"name": "json2md",
"version": "1.4.1",
"version": "1.5.0",
"description": "A JSON to Markdown converter.",

@@ -25,3 +25,4 @@ "main": "lib/index.js",

"contributors": [
"Crisoforo Gaspar <github@crisoforo.com>"
"Crisoforo Gaspar <github@crisoforo.com>",
"Dmitry Tsvettsikh <me@reklatsmasters.com>"
],

@@ -28,0 +29,0 @@ "license": "MIT",

@@ -98,16 +98,17 @@ [![json2md](http://i.imgur.com/uj64JFw.png)](#)

| Type | Element | Data | Example |
|--------------|--------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
| `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` |
| `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` |
| `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` |
| `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` |
| `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` |
| `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` |
| `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` |
| `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` |
| `img` | Image | An object or an array of objects containing the `title` and `source` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png" } }` |
| `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` |
| `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` |
| `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. | `{ code: { "language": "html", "content": "<script src='dummy.js'></script>" } } |
| Type | Element | Data | Example |
|--------------|--------------------|--------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` |
| `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` |
| `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` |
| `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` |
| `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` |
| `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` |
| `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` |
| `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` |
| `img` | Image | An object or an array of objects containing the `title` and `source` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png" } }` |
| `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` |
| `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` |
| `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. | `{ code: { "language": "html", "content": "<script src='dummy.js'></script>" } }` |
| `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s). | `{ table: { headers: ["a", "b"], rows: [{ a: "col1", b: "col2" }] } }` or `{ table: { headers: ["a", "b"], rows: [["col1", "col2"]] } }` |

@@ -114,0 +115,0 @@ You can extend the `json2md.converters` object to support your custom types.

// Dependencies
var json2md = require("../lib")
, tester = require("tester")
;
var json2md = require("../lib"),
tester = require("tester");
tester.describe("json2md", test => {
// Headings
test.it("should support headings", function (cb) {
test.expect(json2md({ h1: "Heading 1" })).toBe("# Heading 1");
test.expect(json2md({ h2: "Heading 2" })).toBe("## Heading 2");
test.expect(json2md({ h3: "Heading 3" })).toBe("### Heading 3");
test.expect(json2md({ h4: "Heading 4" })).toBe("#### Heading 4");
test.expect(json2md({ h5: "Heading 5" })).toBe("##### Heading 5");
test.expect(json2md({ h6: "Heading 6" })).toBe("###### Heading 6");
test.it("should support headings", function(cb) {
test.expect(json2md({
h1: "Heading 1"
})).toBe("# Heading 1");
test.expect(json2md({
h2: "Heading 2"
})).toBe("## Heading 2");
test.expect(json2md({
h3: "Heading 3"
})).toBe("### Heading 3");
test.expect(json2md({
h4: "Heading 4"
})).toBe("#### Heading 4");
test.expect(json2md({
h5: "Heading 5"
})).toBe("##### Heading 5");
test.expect(json2md({
h6: "Heading 6"
})).toBe("###### Heading 6");
cb();

@@ -19,4 +30,6 @@ });

// Blockquote
test.it("should support blockquotes", function (cb) {
test.expect(json2md({ blockquote: "Some content" })).toBe("> Some content\n");
test.it("should support blockquotes", function(cb) {
test.expect(json2md({
blockquote: "Some content"
})).toBe("> Some content\n");
cb();

@@ -26,7 +39,7 @@ });

// Images
test.it("should support images", function (cb) {
test.it("should support images", function(cb) {
test.expect(json2md({
img: {
source: "source"
, title: "title"
source: "source",
title: "title"
}

@@ -38,7 +51,6 @@ })).toBe("![title](source)\n");

// Unordered lists
test.it("should support unordered lists", function (cb) {
test.it("should support unordered lists", function(cb) {
test.expect(json2md({
ul: [
"item 1"
, "item 2"
"item 1", "item 2"
]

@@ -49,7 +61,6 @@ })).toBe("\n - item 1\n - item 2\n");

test.it("should support unordered lists with emphasis format", function (cb) {
test.it("should support unordered lists with emphasis format", function(cb) {
test.expect(json2md({
ul: [
"<em>item 1</em>"
, "<bold>item 2</bold>"
"<em>item 1</em>", "<bold>item 2</bold>"
]

@@ -61,7 +72,6 @@ })).toBe("\n - *item 1*\n - **item 2**\n");

// Ordered lists
test.it("should support ordered lists", function (cb) {
test.it("should support ordered lists", function(cb) {
test.expect(json2md({
ol: [
"item 1"
, "item 2"
"item 1", "item 2"
]

@@ -73,11 +83,8 @@ })).toBe("\n 1. item 1\n 2. item 2\n");

// Code blocks
test.it("should support code blocks", function (cb) {
test.it("should support code blocks", function(cb) {
test.expect(json2md({
code: {
language: "js"
, content: [
"function sum (a, b) {"
, " return a + b;"
, "}"
, "sum(1, 2);"
language: "js",
content: [
"function sum (a, b) {", " return a + b;", "}", "sum(1, 2);"
]

@@ -90,7 +97,6 @@ }

// Paragraphs
test.it("should support paragraphs", function (cb) {
test.it("should support paragraphs", function(cb) {
test.expect(json2md({
p: [
"Two"
, "Paragraphs"
"Two", "Paragraphs"
]

@@ -101,7 +107,6 @@ })).toBe("\nTwo\n\nParagraphs\n");

test.it("should support paragraphs with bold text", function (cb) {
test.it("should support paragraphs with bold text", function(cb) {
test.expect(json2md({
p: [
"Two <bold>more words</bold>"
, "in this paragraph, <strong>right?</strong>"
"Two <bold>more words</bold>", "in this paragraph, <strong>right?</strong>"
]

@@ -113,7 +118,9 @@ })).toBe("\nTwo **more words**\n\nin this paragraph, **right?**\n");

// Custom converters
test.it("should support custom types", function (cb) {
json2md.converters.sayHello = function (input, json2md) {
test.it("should support custom types", function(cb) {
json2md.converters.sayHello = function(input, json2md) {
return "Hello " + input + "!";
};
test.expect(json2md({ sayHello: "World" })).toBe("Hello World!")
test.expect(json2md({
sayHello: "World"
})).toBe("Hello World!")
cb();

@@ -123,15 +130,11 @@ });

// Code blocks in lists
test.it("should correctly indent code blocks in lists", function (cb) {
test.it("should correctly indent code blocks in lists", function(cb) {
test.expect(json2md({
ol: [
[
"Copy the code below:",
{
"Copy the code below:", {
code: {
language: "js"
, content: [
"function sum (a, b) {"
, " return a + b;"
, "}"
, "sum(1, 2);"
language: "js",
content: [
"function sum (a, b) {", " return a + b;", "}", "sum(1, 2);"
]

@@ -154,15 +157,11 @@ }

test.it("should correctly indent code blocks in unordered lists", function (cb) {
test.it("should correctly indent code blocks in unordered lists", function(cb) {
test.expect(json2md({
ul: [
[
"Copy the code below:",
{
"Copy the code below:", {
code: {
language: "js"
, content: [
"function sum (a, b) {"
, " return a + b;"
, "}"
, "sum(1, 2);"
language: "js",
content: [
"function sum (a, b) {", " return a + b;", "}", "sum(1, 2);"
]

@@ -184,2 +183,34 @@ }

});
test.it("should work when input is number", function(cb) {
test.expect(json2md({
blockquote: 123
})).toBe("> 123\n");
cb();
});
test.it("should support tables, rows is objects", function(cb) {
test.expect(json2md({
table: {
headers: ["a", "b"],
rows: [{
a: "col1",
b: "col2"
}]
}
})).toBe("a | b\n--- | ---\ncol1 | col2");
cb();
})
test.it("should support tables, rows is arrays", function(cb) {
test.expect(json2md({
table: {
headers: ["a", "b"],
rows: [
["col1", "col2"]
]
}
})).toBe("a | b\n--- | ---\ncol1 | col2");
cb();
})
});
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