Skip to content

melgrove/json2markup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json2markup

NPM version

Converts JSON into formatted markup. Creates nested ordered and unordered lists in either HTML or Markdown. Has an extremely simple API with optional HTML sanitization. Arrays are interpreted as ordered lists and objects are interpreted as unordered lists.

Usage

const json2markup = require('json2markup');

json2markup(objectToConvert, config)

objectToConvert required (string, object, or array): Strings are interpreted as JSON and parsed, objects and arrays are interpreted literally

config optional (object): Configuration properties

  • return ('html' or 'markdown', default 'html'): Output file type
  • sanitize (boolean): Sanitize HTML?

Returns (string): HTML or Markdown string


Examples

example.json

{
	"unordered lists are created from objects": {
        "foo": true,
        "bar": false
    },
    "ordered lists are created from arrays": [
        "this is an example",
        {
            "nested": true,
            "more nesting": [
                true,
                [
                    "even",
                    {
                        "more": "and",
                        "even": "more"
                    }
                ]
            ]
        },
        "another element"
    ]
}

example.js

const json2markup = require('json2markup');
const fs = require('fs')

let myJSON = fs.readFileSync('example.json', 'utf8')

json2markup(myJSON, {return: 'markdown'})

Returns:

  • unordered lists are created from objects:
    • foo: true
    • bar: false
  • ordered lists are created from arrays:
    1. this is an example
      • nested: true
      • more nesting:
        1. true
          1. even
            • more: and
            • even: more
    2. another element

Or with HTML:

json2markup(myJSON)

Returns:

<ul><li>unordered lists are created from objects: <ul><li>foo: true</li><li>bar: false</li></ul></li><li>ordered lists are created from arrays: <ol><li>this is an example</li><li><ul><li>nested: true</li><li>more nesting: <ol><li>true</li><li><ol><li>even</li><li><ul><li>more: and</li><li>even: more</li></ul></li></ol></li></ol></li></ul></li><li>another element</li></ol></li></ul>

You can also pass JavaScript objects and arrays, and sanitize HTML

const obj = {
    one: ['foo', 'bar', 'baz'],
    two: "don't mind<body onafterprint=alert(1)> me!"
}

json2markup(obj, {return: 'markdown', sanitize: true})

Returns:

  • one:
    1. foo
    2. bar
    3. baz
  • two: don't mind me!

About

Converts JSON into formatted markup. Creates nested ordered and unordered lists in either HTML or Markdown.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published