stringify-json-script-content
v0.0.2
Published
Stringify object to json that can use in script tags.
Downloads
4
Readme
stringify-json-script-content
Stringify object to json that can use in script tags.
Installation
npm
npm install stringify-json-script-content
Usage
import fs from "node:fs";
import stringifyJsonScriptContent from "stringify-json-script-content";
const jsonScriptContent = stringifyJsonScriptContent({
hello: "world<script>!</script>",
});
const html = `
<!DOCTYPE html>
<script id="json-data" type="application/json">
${jsonScriptContent}
</script>
<script>
console.log(JSON.parse(document.getElementById("json-data").textContent));
</script>
`;
fs.writeFileSync("test.html", html);
API
stringifyJsonScriptContent(value, replacer, space)
(default export)
The parameters of stringifyJsonScriptContent()
is same as JSON.stringify() and it returns like JSON.stringify() and can use in script tags.
encodeJsonScriptContent(jsonString)
The encodeJsonScriptContent()
encode chars that shouldn't use in script tags.
Example
import { encodeJsonScriptContent } from "stringify-json-script-content";
const jsonString = JSON.stringify({ hello: "world<script>!</script>" });
const encodeJsonString = encodeJsonScriptContent(jsonString);
console.log(encodeJsonString);
Release Notes
[TODO]
[Unreleased]
v0.0.2
- Fix packed files.
v0.0.1
- Added features stringify object to json that can use in script tags.