json2md

repository·master·Indexed 20 days ago

https://github.com/ionicabizau/json2md

A 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.

Tokens
4.2K
Snippets
11
Records
11
Agent score
69%

What's inside json2md

  1. Convert JSON to Markdown asynchronously

    master

    If 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)
  2. Convert JSON to Markdown with json2md()

    master

    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)
  3. Use the `json2md(data, prefix)` function

    master

    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' });
  4. Extend json2md with custom converters

    master

    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" });
  5. Use the async version of `json2md`

    master

    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' });
  6. Supported Markdown elements in json2md

    master

    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.                                                                |
  7. Supported JSON elements for markdown conversion

    master

    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.                                                               |
  8. Use json2md.async for asynchronous conversion

    master

    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);
    });
  9. Use the json2md function to convert JSON to Markdown

    master

    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."
    });
  10. Supported JSON elements for Markdown conversion

    master

    The following JSON keys are supported by the default converters:

    TypeElementData Format
    h1 to h6Headings{ h1: "text" }
    pParagraphs{ p: "text" } or { p: ["para 1", "para 2"] }
    blockquoteBlockquote{ blockquote: "text" } or { blockquote: ["quote 1", "quote 2"] }
    imgImage{ img: { title: "title", source: "url", alt: "alt text" } } or an array of these objects
    ulUnordered list{ ul: ["item 1", "item 2"] }
    olOrdered list{ ol: ["item 1", "item 2"] }
    hrSeparator{ hr: "" }
    codeCode block{ code: { language: "html", content: "<p>...</p>" } }
    tableTable{ table: { headers: ["a", "b"], rows: [{ a: "val1", b: "val2" }] } } or { table: { headers: ["a", "b"], rows: [["val1", "val2"]] } }
    linkLink{ 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 }]
      }
    };