UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

48 lines (47 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.exportStyle = exports.exportHtml = exports.toHtml = exports.toHast = exports.toJsonMl = void 0; const slice_1 = require("../slice"); const toText_1 = require("very-small-parser/lib/html/toText"); const toHast_1 = require("very-small-parser/lib/html/json-ml/toHast"); const toBase64_1 = require("@jsonjoy.com/base64/lib/toBase64"); const toJsonMl = (json) => { if (typeof json === 'string') return json; const [tag, attr, ...children] = json; const namedTag = tag === '' ? tag : slice_1.SliceTypeName[tag]; const htmlTag = namedTag ?? (attr?.inline ? 'span' : 'div'); const htmlAttr = attr && attr.data !== void 0 ? { 'data-attr': JSON.stringify(attr.data) } : null; const htmlNode = [htmlTag, htmlAttr]; const length = children.length; for (let i = 0; i < length; i++) htmlNode.push((0, exports.toJsonMl)(children[i])); return htmlNode; }; exports.toJsonMl = toJsonMl; const toHast = (json) => { const jsonml = (0, exports.toJsonMl)(json); // console.log(jsonml); const hast = (0, toHast_1.toHast)(jsonml); return hast; }; exports.toHast = toHast; const toHtml = (json, tab, indent) => (0, toText_1.toText)((0, exports.toHast)(json), tab, indent); exports.toHtml = toHtml; const base64Str = (str) => (0, toBase64_1.toBase64)(new TextEncoder().encode(str)); const exportHtml = (view, node) => { const data = { view }; const json = JSON.stringify(data); const jsonBase64 = base64Str(json); const html = (0, exports.toHtml)(node) + '<b data-json-joy-peritext="' + jsonBase64 + '"/>'; return html; }; exports.exportHtml = exportHtml; const exportStyle = (style) => { const data = { style }; const json = JSON.stringify(data); const jsonBase64 = base64Str(json); const html = '<b data-json-joy-peritext="' + jsonBase64 + '"/>'; return html; }; exports.exportStyle = exportStyle;