### Install json2md via Package Managers
Source: https://github.com/ionicabizau/json2md/blob/master/README.md
Commands to install the json2md library using common JavaScript package managers.
```sh
# Using npm
npm install --save json2md
# Using yarn
yarn add json2md
```
--------------------------------
### Generate Markdown Paragraphs (p) with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
This example shows how to create Markdown paragraphs using json2md. It supports single paragraphs as strings, multiple paragraphs as an array, and conversion of basic HTML formatting tags to Markdown equivalents.
```javascript
const json2md = require("json2md");
const markdown = json2md([
{ p: "This is a single paragraph." },
{ p: [
"This is the first paragraph.",
"This is the second paragraph."
]},
{ p: "Text with bold, italic, underline, and strikethrough." }
]);
console.log(markdown);
```
--------------------------------
### Extend json2md with custom converters
Source: https://github.com/ionicabizau/json2md/blob/master/README.md
Demonstrates how to add a custom converter to the json2md.converters object to handle specific data types. The example shows a 'sayHello' function that takes input and returns a formatted string, which can then be invoked via the main json2md function.
```javascript
json2md.converters.sayHello = function (input, json2md) {
return "Hello " + input + "!"
}
// Usage:
json2md({ sayHello: "World" })
// => "Hello World!"
```
--------------------------------
### Generate Tables with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Demonstrates creating Markdown tables using objects or arrays for rows. Features include column alignment, pretty formatting, and nested elements like links.
```javascript
const json2md = require("json2md");
const basicTable = json2md({
table: {
headers: ["Name", "Age", "City"],
rows: [
{ Name: "Alice", Age: "30", City: "New York" },
{ Name: "Bob", Age: "25", City: "London" }
]
}
});
const prettyTable = json2md({
table: {
pretty: true,
headers: ["Product", "Price", "Stock"],
aligns: ["left", "right", "center"],
rows: [
["Widget", "$10.00", "In Stock"],
["Gadget", "$25.50", "Limited"]
]
}
});
const tableWithLinks = json2md({
table: {
headers: ["Name", "Website"],
rows: [
{
Name: "Example",
Website: {
link: {
title: "Visit Site",
source: "https://example.com"
}
}
}
]
}
});
```
--------------------------------
### Generate Task Lists with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Creates GitHub-flavored Markdown task lists, supporting both incomplete and completed states via the isDone property.
```javascript
const json2md = require("json2md");
const markdown = json2md({
taskLists: [
{ title: "Incomplete task" },
{ title: "Completed task", isDone: true },
"Another incomplete task"
]
});
console.log(markdown);
```
--------------------------------
### Extend json2md with custom converters
Source: https://github.com/ionicabizau/json2md/blob/master/DOCUMENTATION.md
Demonstrates how to add custom functionality to json2md by defining a new converter function within the json2md.converters object. This allows users to define custom JSON-to-Markdown transformation logic for specific keys.
```javascript
json2md.converters.sayHello = function (input, json2md) {
return "Hello " + input + "!"
}
json2md({ sayHello: "World" })
// => "Hello World!"
```
--------------------------------
### Add Contributor to package.json
Source: https://github.com/ionicabizau/json2md/blob/master/CONTRIBUTING.md
When fixing issues in the json2md project, if a package.json or bower.json file exists, you should add yourself to the 'contributors' array. This JSON snippet shows the expected format for adding a contributor.
```json
{
"contributors": [
"Your Name (http://your.website)"
]
}
```
--------------------------------
### POST /json2md/async
Source: https://github.com/ionicabizau/json2md/blob/master/README.md
Asynchronously converts a provided JSON object into a Markdown string, returning a Promise.
```APIDOC
## POST /json2md/async
### Description
Performs an asynchronous conversion of JSON data to Markdown, useful for non-blocking operations.
### Method
POST
### Endpoint
/json2md/async
### Parameters
#### Request Body
- **data** (Array|Object|String) - Required - The input JSON data to convert.
- **prefix** (String) - Optional - A string snippet to prepend to each line of the output.
### Request Example
{
"data": { "link": { "title": "Home", "source": "https://example.com" } }
}
### Response
#### Success Response (200)
- **result** (Promise) - A promise that resolves to the generated markdown string.
```
--------------------------------
### Generate Unordered Lists with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Demonstrates the creation of bulleted lists, including support for nested lists and text formatting like italics and bold.
```javascript
const json2md = require("json2md");
const markdown = json2md({
ul: [
"Simple item",
"Italic item",
"Bold item",
[
"Nested content:",
{ ul: ["Sub-item 1", "Sub-item 2"] }
]
]
});
console.log(markdown);
```
--------------------------------
### Generate Ordered Lists with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Shows how to create numbered lists, including the ability to embed code blocks within specific list items.
```javascript
const json2md = require("json2md");
const markdown = json2md({
ol: [
"First step",
"Second step",
[
"Copy the code below:",
{ code: {
language: "js",
content: [
"function greet(name) {",
" return 'Hello, ' + name;",
"}"
]
}}
],
"Final step"
]
});
console.log(markdown);
```
--------------------------------
### Generate Code Blocks with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Generates fenced code blocks with optional syntax highlighting. Supports content as a single string or an array of lines.
```javascript
const json2md = require("json2md");
const markdown = json2md([
{ code: {
language: "javascript",
content: "const message = 'Hello World';"
}},
{ code: {
language: "python",
content: [
"def fibonacci(n):",
" if n <= 1:",
" return n",
" return fibonacci(n-1) + fibonacci(n-2)"
]
}},
{ code: {
content: "Plain code without language"
}}
]);
console.log(markdown);
```
--------------------------------
### Asynchronous JSON to Markdown Conversion with json2md.async
Source: https://context7.com/ionicabizau/json2md/llms.txt
The asynchronous version of json2md, json2md.async, returns a Promise. This is useful for custom converters that involve asynchronous operations. It can handle a mix of synchronous and asynchronous elements.
```javascript
const json2md = require("json2md");
// Define an async custom converter
json2md.converters.fetchTitle = async function(url, json2md) {
// Simulating async operation
return new Promise((resolve) => {
setTimeout(() => {
resolve("# Fetched Title from " + url);
}, 100);
});
};
// Use async API with mixed sync and async elements
json2md.async([
{ h1: "Async Document" },
{ fetchTitle: "https://example.com" },
{ p: "Content loaded asynchronously" }
]).then((markdown) => {
console.log(markdown);
}).catch((err) => {
console.error("Error:", err);
});
```
--------------------------------
### Generate Markdown Links with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Generates Markdown hyperlink syntax using json2md. It accepts single link objects or arrays of links, where each link can have a title and a source URL. The input can be a simple URL or a more detailed object.
```javascript
const json2md = require("json2md");
const markdown = json2md([
{ link: {
title: "Visit Example",
source: "https://example.com"
}},
{ link: "https://simple-link.com" },
{ link: [
{ title: "GitHub", source: "https://github.com" },
{ title: "npm", source: "https://npmjs.com" }
]}
]);
console.log(markdown);
```
--------------------------------
### Generate Markdown Headings (h1-h6) with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
This snippet demonstrates how to generate Markdown headings from level 1 to 6 using json2md. The value associated with each heading type (h1, h2, etc.) is the heading text.
```javascript
const json2md = require("json2md");
const markdown = json2md([
{ h1: "Main Title" },
{ h2: "Section" },
{ h3: "Subsection" },
{ h4: "Sub-subsection" },
{ h5: "Minor heading" },
{ h6: "Smallest heading" }
]);
console.log(markdown);
```
--------------------------------
### Generate Markdown Images with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Generates Markdown image syntax using json2md. It accepts single image objects or arrays of images with source, title, and alt attributes. The input can be a simple source URL or a more detailed object.
```javascript
const json2md = require("json2md");
const markdown = json2md([
// Single image with all attributes
{ img: {
source: "https://example.com/logo.png",
title: "Company Logo",
alt: "Logo"
}},
// Simple image (source only)
{ img: "https://example.com/photo.jpg" },
// Multiple images
{ img: [
{ source: "https://example.com/img1.png", title: "First", alt: "Image 1" },
{ source: "https://example.com/img2.png", title: "Second", alt: "Image 2" }
]]
]);
console.log(markdown);
```
--------------------------------
### Generate Markdown Blockquotes with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
This snippet illustrates how to generate Markdown blockquotes using json2md. It can handle single blockquotes as strings or multiple blockquotes provided as an array.
```javascript
const json2md = require("json2md");
const markdown = json2md([
{ blockquote: "This is a quoted text." },
{ blockquote: [
"First quote line",
"Second quote line"
]}
]);
console.log(markdown);
```
--------------------------------
### Convert JSON to Markdown using json2md
Source: https://github.com/ionicabizau/json2md/blob/master/README.md
The `json2md` function takes a JSON object and an optional prefix, converting it into a Markdown string. It supports various Markdown elements like headings, paragraphs, lists, images, blockquotes, horizontal rules, and code blocks.
```javascript
const json2md = require('json2md');
// Example: Heading 1
const data1 = { h1: 'heading 1' };
console.log(json2md(data1));
// Example: Paragraphs
const data2 = { p: ['Hello', 'World'] };
console.log(json2md(data2));
// Example: Image
const data3 = {
img: {
title: 'My image title',
source: 'http://example.com/image.png',
alt: 'My image alt'
}
};
console.log(json2md(data3));
// Example: Unordered list
const data4 = { ul: ['item 1', 'item 2'] };
console.log(json2md(data4));
// Example: Ordered list
const data5 = { ol: ['item 1', 'item 2'] };
console.log(json2md(data5));
// Example: Horizontal rule
const data6 = { hr: '' };
console.log(json2md(data6));
// Example: Code block
const data7 = {
code: {
language: 'html',
content: ""
}
};
console.log(json2md(data7));
// Example: Multiple elements
const data8 = [
{ h2: 'Another Heading' },
{ p: 'This is a paragraph.' },
{ blockquote: 'This is a blockquote.' },
{ ul: ['list item 1', 'list item 2'] }
];
console.log(json2md(data8));
```
--------------------------------
### POST /json2md
Source: https://github.com/ionicabizau/json2md/blob/master/README.md
Converts a provided JSON object into a Markdown string using the library's internal converters.
```APIDOC
## POST /json2md
### Description
Converts the input JSON data into a Markdown string. Supports standard types such as tables and links.
### Method
POST
### Endpoint
/json2md
### Parameters
#### Request Body
- **data** (Array|Object|String) - Required - The input JSON data to convert.
- **prefix** (String) - Optional - A string snippet to prepend to each line of the output.
### Request Example
{
"data": {
"table": {
"headers": ["Name", "Role"],
"rows": [["John", "Developer"]]
}
}
}
### Response
#### Success Response (200)
- **result** (String) - The generated markdown string.
#### Response Example
{
"result": "| Name | Role |\n| --- | --- |\n| John | Developer |"
}
```
--------------------------------
### Convert JSON to Markdown using json2md
Source: https://github.com/ionicabizau/json2md/blob/master/DOCUMENTATION.md
This snippet demonstrates how to use the json2md function to transform a JSON object into a Markdown string. It supports various elements including headings, paragraphs, lists, and code blocks.
```javascript
const json2md = require("json2md");
const data = [
{ h1: "Heading 1" },
{ p: "This is a paragraph." },
{ ul: ["Item 1", "Item 2"] },
{ code: { language: "javascript", content: "console.log('Hello World');" } }
];
const markdown = json2md(data);
console.log(markdown);
```
--------------------------------
### Convert JSON to Markdown using json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
The main json2md function converts a JSON structure into a Markdown string. It accepts an array or object defining Markdown elements like headings, lists, and code blocks.
```javascript
const json2md = require("json2md");
// Convert a JSON structure to Markdown
const markdown = json2md([
{ h1: "JSON To Markdown" },
{ blockquote: "A JSON to Markdown converter." },
{ h2: "Features" },
{ ul: [
"Easy to use",
"Programmatic Markdown generation",
"Extensible with custom converters"
]},
{ h2: "How to contribute" },
{ ol: [
"Fork the project",
"Create your branch",
"Raise a pull request"
]},
{ p: "Below you can see a code block example." },
{ code: {
language: "js",
content: [
"function sum (a, b) {",
" return a + b",
"}",
"sum(1, 2)"
]
}}
]);
console.log(markdown);
```
--------------------------------
### Generate Horizontal Rule with json2md
Source: https://context7.com/ionicabizau/json2md/llms.txt
Generates a Markdown horizontal rule separator using json2md. This is useful for visually separating content sections in Markdown documents. The input for 'hr' is typically an empty string.
```javascript
const json2md = require("json2md");
const markdown = json2md([
{ h2: "Section 1" },
{ p: "Content of section 1" },
{ hr: "" },
{ h2: "Section 2" },
{ p: "Content of section 2" }
]);
console.log(markdown);
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.