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

94.31% Statements 83/88
82.85% Branches 29/35
100% Functions 7/7
97.33% Lines 73/75

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1182x 2x 2x 2x 2x 2x                       6x 6x   6x     25x 25x 12x 10x 10x   13x 13x 13x 13x 13x 7x 7x 7x 6x 6x 6x       6x 6x 6x 6x     19x 13x 7x 7x 7x 1x 1x 1x       1x 1x 1x 1x           6x 6x 6x       2x 6x   2x 42x 22x 22x 22x 33x 22x 22x 3x 3x   19x 19x 19x 19x         19x 19x   22x 3x 3x 3x   22x     2x 9x 9x     2x 9x 9x    
import {html as _html} from 'very-small-parser/lib/html';
import {fromHast as _fromHast} from 'very-small-parser/lib/html/json-ml/fromHast';
import {SliceTypeName} from '../slice';
import {registry as defaultRegistry} from '../registry/registry';
import {SliceBehavior, SliceHeaderShift} from '../slice/constants';
import {Anchor} from '../rga/constants';
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';
import type {SliceRegistry} from '../registry/SliceRegistry';
import type {ViewRange, ViewSlice} from '../editor/types';
 
/**
 * Flattens a {@link PeritextMlNode} tree structure into a {@link ViewRange}
 * flat string with annotation ranges.
 */
class ViewRangeBuilder {
  private text = '';
  private slices: ViewSlice[] = [];
 
  constructor(private registry: SliceRegistry) {}
 
  private build0(node: PeritextMlNode, depth = 0): void {
    const skipWhitespace = depth < 2;
    if (typeof node === 'string') {
      if (skipWhitespace && !node.trim()) return;
      this.text += node;
      return;
    }
    const [type, attr] = node;
    const start = this.text.length;
    const length = node.length;
    const inline = !!attr?.inline;
    if (!!type || type === 0) {
      let end: number = 0,
        header: number = 0;
      if (!inline) {
        this.text += '\n';
        end = start;
        header =
          (SliceBehavior.Marker << SliceHeaderShift.Behavior) +
          (Anchor.Before << SliceHeaderShift.X1Anchor) +
          (Anchor.Before << SliceHeaderShift.X2Anchor);
        const slice: ViewSlice = [header, start, end, type];
        const data = attr?.data;
        Iif (data) slice.push(data);
        this.slices.push(slice);
      }
    }
    for (let i = 2; i < length; i++) this.build0(node[i] as PeritextMlNode, depth + 1);
    if (!!type || type === 0) {
      let end: number = 0,
        header: number = 0;
      if (inline) {
        end = this.text.length;
        const behavior: SliceBehavior = attr?.behavior ?? SliceBehavior.Many;
        header =
          (behavior << SliceHeaderShift.Behavior) +
          (Anchor.Before << SliceHeaderShift.X1Anchor) +
          (Anchor.After << SliceHeaderShift.X2Anchor);
        const slice: ViewSlice = [header, start, end, type];
        const data = attr?.data;
        Iif (data) slice.push(data);
        this.slices.push(slice);
      }
    }
  }
 
  public build(node: PeritextMlNode): ViewRange {
    this.build0(node);
    const view: ViewRange = [this.text, 0, this.slices];
    return view;
  }
}
 
export const toViewRange = (node: PeritextMlNode, registry: SliceRegistry = defaultRegistry): ViewRange =>
  new ViewRangeBuilder(registry).build(node);
 
export const fromJsonMl = (jsonml: JsonMlNode, registry: SliceRegistry = defaultRegistry): PeritextMlNode => {
  if (typeof jsonml === 'string') return jsonml;
  const tag = jsonml[0];
  const length = jsonml.length;
  const node: PeritextMlNode = [tag, null];
  for (let i = 2; i < length; i++) node.push(fromJsonMl(jsonml[i] as JsonMlNode, registry));
  const res = registry.fromHtml(jsonml);
  if (res) {
    node[0] = res[0];
    node[1] = res[1];
  } else {
    node[0] = SliceTypeName[tag as any] ?? tag;
    const attr = jsonml[1] || {};
    let data = null;
    Iif (attr['data-attr'] !== void 0) {
      try {
        data = JSON.parse(attr['data-attr']);
      } catch {}
    }
    const inline = attr['data-inline'] === 'true';
    Iif (data || inline) node[1] = {data, inline};
  }
  if (typeof node[0] === 'number' && node[0] < 0) {
    const attr = node[1] || {};
    attr.inline = true;
    node[1] = attr;
  }
  return node;
};
 
export const fromHast = (hast: THtmlToken, registry?: SliceRegistry): PeritextMlNode => {
  const jsonml = _fromHast(hast);
  return fromJsonMl(jsonml, registry);
};
 
export const fromHtml = (html: string, registry?: SliceRegistry): PeritextMlNode => {
  const hast = _html.parsef(html);
  return fromHast(hast, registry);
};