All files / json-crdt-extensions/peritext/lazy export-html.ts

100% Statements 25/25
81.81% Branches 9/11
100% Functions 3/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 292x 2x 2x         2x 792x 444x 444x 444x 444x 444x 444x 696x 444x     2x 84x   84x 84x     2x 36x  
import {SliceTypeName} from '../slice';
import {toText as _toHtml} from 'very-small-parser/lib/html/toText';
import {toHast as _toHast} from 'very-small-parser/lib/html/json-ml/toHast';
import type {JsonMlNode} from 'very-small-parser/lib/html/json-ml/types';
import type {THtmlToken} from 'very-small-parser/lib/html/types';
import type {PeritextMlNode} from '../block/types';
 
export const toJsonMl = (json: PeritextMlNode): JsonMlNode => {
  if (typeof json === 'string') return json;
  const [tag, attr, ...children] = json;
  const namedTag = tag === '' ? tag : SliceTypeName[tag as any];
  const htmlTag = namedTag ?? (attr?.inline ? 'span' : 'div');
  const htmlAttr = attr && attr.data !== void 0 ? {'data-attr': JSON.stringify(attr.data)} : null;
  const htmlNode: JsonMlNode = [htmlTag, htmlAttr];
  const length = children.length;
  for (let i = 0; i < length; i++) htmlNode.push(toJsonMl(children[i]));
  return htmlNode;
};
 
export const toHast = (json: PeritextMlNode): THtmlToken => {
  const jsonml = toJsonMl(json);
  // console.log(jsonml);
  const hast = _toHast(jsonml);
  return hast;
};
 
export const toHtml = (json: PeritextMlNode, tab?: string, indent?: string): string =>
  _toHtml(toHast(json), tab, indent);