Giter VIP home page Giter VIP logo

json2markup's Introduction

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!

json2markup's People

Contributors

melgrove avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.