Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including embed-json with all npm packages installed. Try it out:

var embedJson = require("embed-json")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

embed-json v1.1.2

Inline/Embed JSON data from files into HTML.

embed-json

Node.js module to inline/embed JSON data from files into html.

NPM

Build Status

Installation

NPM

npm i --save embed-json

Yarn

yarn add embed-json

Usage

Synopsis

/**
 * Embed JSON data from referenced files into the given HTML string.
 * When embedJson() encounters a script tag with JSON mime type and a src attribute,
 * e.g.
 *  <script type="application/json" src="data.json"></script>
 * it will retrieve the data from the file and embed it into the tag, while removing
 * the src attribute.
 */
embedJson(htmlString, options);

Example

const embedJson = require('embed-json');
const fs = require('fs');

const html = fs.readFileSync('index.html', 'utf8');
embedJson(html);

Options

encoding string

Default: utf8

Specify the encoding of the JSON files.

mimeTypes Array<string> | string

Default: ['application/json', 'application/ld+json']

Specify the mime type(s) of scripts to embed.

minify boolean

Default: true

Specify whether or not to minify the embedded JSON data.

root string

Default: __dirname

Specify the directory in which the JSON files are stored.

Changelog

License

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free