Install json2md via npm or yarn
masterYou can install json2md using either npm or yarn to use it in your project.
# Using npm
npm install --save json2md
# Using yarn
yarn add json2mdrepository·master·Indexed 20 days ago
https://github.com/ionicabizau/json2mdA JSON to Markdown converter utility (version 2.0.3) that transforms JSON data structures into Markdown strings. It supports standard elements including headings (h1-h6), paragraphs, blockquotes, images, lists, separators, code blocks, tables, and links. The library provides both synchronous and asynchronous conversion methods and allows for extensibility via custom converters in the json2md.converters object.
You can install json2md using either npm or yarn to use it in your project.
# Using npm
npm install --save json2md
# Using yarn
yarn add json2mdIf you need to perform the conversion asynchronously, you can use the async version of the library. It accepts the same parameters as the synchronous version but returns a Promise that resolves to the Markdown string.
// Returns Promise<String>
const markdown = await json2md(data, prefix)The primary function json2md(data, prefix) converts a JSON structure into a Markdown string.
data: The input JSON data (can be an Array, Object, or String).prefix: (Optional) A string snippet to add before each line of the generated output.Returns a String containing the generated Markdown.
const json2md = require("json2md")
const markdown = json2md([
{ h1: "JSON To Markdown" }
, { blockquote: "A JSON to Markdown converter." }
])
console.log(markdown)The core function json2md(data, prefix) converts a JSON input into a Markdown string.
Parameters:
data (Array|Object|String): The input JSON data to be converted.prefix (String, optional): A snippet to add before each line of the generated markdown.Returns:
String: The generated markdown result.// Basic usage
const markdown = json2md({ h1: 'Hello World' });You can add support for custom JSON types by extending the json2md.converters object. A converter is a function that takes the input and the json2md instance, and returns a string.
Example implementation:
json2md.converters.sayHello = function (input, json2md) {
return "Hello " + input + "!";
}Usage:
json2md({ sayHello: "World" });
// => "Hello World!"json2md.converters.sayHello = function (input, json2md) {
return "Hello " + input + "!";
}
json2md({ sayHello: "World" });For asynchronous operations, json2md returns a Promise that resolves to the generated markdown string.
Parameters:
data (Array|Object|String): The input JSON data.prefix (String, optional): A snippet to add before each line.Returns:
Promise<String>: A promise that resolves to the generated markdown result.// Async usage
const markdown = await json2md({ h1: 'Hello World' });The following JSON keys are supported for generating Markdown elements:
| Type | Element | Data |
|--------------|--------------------|--------------------------------------------------------------------------------------------------------------------------|
| `h1` | Heading 1 | The heading text as string. |
| `h2` | Heading 2 | The heading text as string. |
| `h3` | Heading 3 | The heading text as string. |
| `h4` | Heading 4 | The heading text as string. |
| `h5` | Heading 5 | The heading text as string. |
| `h6` | Heading 6 | The heading text as string. |
| `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). |
| `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) |
| `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. |
| `ul` | Unordered list | An array of strings or lists representing the items. |
| `ol` | Ordered list | An array of strings or lists representing the items. |
| `hr` | Separator | None |
| `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. |
| `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s). |
| `link` | Link | An object containing the `title` and the `source` fields. |The following JSON keys are supported for generating specific Markdown elements:
| Type | Element | Data |
|--------------|--------------------|--------------------------------------------------------------------------------------------------------------------------|
| `h1` | Heading 1 | The heading text as string. |
| `h2` | Heading 2 | The heading text as string. |
| `h3` | Heading 3 | The heading text as string. |
| `h4` | Heading 4 | The heading text as string. |
| `h5` | Heading 5 | The heading text as string. |
| `h6` | Heading 6 | The heading text as string. |
| `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). |
| `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) |
| `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. |
| `ul` | Unordered list | An array of strings or lists representing the items. |
| `ol` | Ordered list | An array of strings or lists representing the items. |
| `hr` | Separator | None |
| `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. |
| `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s). |
| `link` | Link | An object containing the `title` and the `source` fields. |If you are processing large datasets or want to avoid blocking the event loop, use json2md.async. It returns a Promise that resolves to the Markdown string.
Parameters:
data (Array|Object|String): The input JSON data.prefix (String, optional): A snippet to add before each line._type (String, optional): Internal use.Returns:
Promise<String>: A promise that resolves to the generated markdown result.const json2md = require('json2md');
json2md.async({
h1: "Async Heading",
ul: ["Item 1", "Item 2"]
}).then(markdown => {
console.log(markdown);
}).catch(err => {
console.error(err);
});The primary json2md function converts a JSON object, array, or string into a Markdown string.
Parameters:
data (Array|Object|String): The input JSON data to be converted.prefix (String, optional): A snippet to add before each line (useful for indentation or prefixing)._type (String, optional): Internal use for specific type handling.Returns:
String: The generated markdown result.const json2md = require('json2md');
// Example usage with an object
const markdown = json2md({
h1: "Heading 1",
p: "This is a paragraph."
});The following JSON keys are supported by the default converters:
| Type | Element | Data Format |
|---|---|---|
h1 to h6 | Headings | { h1: "text" } |
p | Paragraphs | { p: "text" } or { p: ["para 1", "para 2"] } |
blockquote | Blockquote | { blockquote: "text" } or { blockquote: ["quote 1", "quote 2"] } |
img | Image | { img: { title: "title", source: "url", alt: "alt text" } } or an array of these objects |
ul | Unordered list | { ul: ["item 1", "item 2"] } |
ol | Ordered list | { ol: ["item 1", "item 2"] } |
hr | Separator | { hr: "" } |
code | Code block | { code: { language: "html", content: "<p>...</p>" } } |
table | Table | { table: { headers: ["a", "b"], rows: [{ a: "val1", b: "val2" }] } } or { table: { headers: ["a", "b"], rows: [["val1", "val2"]] } } |
link | Link | { link: { title: "text", source: "url" } } |
// Example of a complex object
const data = {
h1: "My Document",
table: {
headers: ["Name", "Age"],
rows: [{ Name: "Alice", Age: 30 }, { Name: "Bob", Age: 25 }]
}
};