@simple-mark/codemirror 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +20 -76
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -2,7 +2,7 @@ import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
2
2
  import { commonmarkLanguage, markdown } from "@codemirror/lang-markdown";
3
3
  import { EditorState, Facet, Prec, StateEffect, StateField } from "@codemirror/state";
4
4
  import { Decoration, EditorView, ViewPlugin, WidgetType, hoverTooltip, keymap, showTooltip } from "@codemirror/view";
5
- import { cellAt, charWidth, defaultTheme, markdownExtensions, markdownStyles, parseTable, renderTable, resolveLineLook, resolveLook, rowLine } from "@simple-mark/model";
5
+ import { cellAt, charWidth, defaultTheme, markdownExtensions, markdownStyles, mathSource, parseTable, previewBlocks, renderTable, resolveLineLook, resolveLook, rowLine } from "@simple-mark/model";
6
6
  import { ensureSyntaxTree, syntaxTree } from "@codemirror/language";
7
7
  //#region packages/codemirror/src/decorations.ts
8
8
  /** The theme the editor draws with. */
@@ -549,56 +549,24 @@ var MathBlockWidget = class extends WidgetType {
549
549
  return dom;
550
550
  }
551
551
  };
552
- /** The TeX of a math node: the text between its delimiters, without quote markers. */
553
- function mathTex(state, node) {
554
- const marks = node.getChildren("MathMark");
555
- if (marks.length < 2) return null;
556
- if (node.name === "InlineMath") return state.sliceDoc(marks[0].to, marks[1].from);
557
- return node.getChildren("MathText").map((text) => state.sliceDoc(text.from, text.to)).join("\n");
558
- }
559
- const normalizeLabel = (label) => label.slice(1, -1).trim().replace(/\s+/g, " ").toLowerCase();
560
- /** A link destination as written, without the optional angle brackets (`<a b.png>`). */
561
- const destination = (state, url) => state.sliceDoc(url.from, url.to).replace(/^<(.*)>$/, "$1");
562
- /** Link reference definitions (`[label]: url`) of the document, by normalized label. */
563
- function linkDefinitions(state, tree) {
564
- const definitions = /* @__PURE__ */ new Map();
565
- for (let node = tree.topNode.firstChild; node; node = node.nextSibling) {
566
- if (node.name !== "LinkReference") continue;
567
- const label = node.getChild("LinkLabel");
568
- const url = node.getChild("URL");
569
- if (label && url) {
570
- const key = normalizeLabel(state.sliceDoc(label.from, label.to));
571
- if (!definitions.has(key)) definitions.set(key, destination(state, url));
572
- }
573
- }
574
- return definitions;
575
- }
576
- function imageRef(state, node, definitions, resolve) {
577
- const marks = node.getChildren("LinkMark");
578
- const alt = marks.length >= 2 ? state.sliceDoc(marks[0].to, marks[1].from) : "";
579
- const url = node.getChild("URL");
580
- let raw = url ? destination(state, url) : void 0;
581
- if (raw === void 0) {
582
- const labelNode = node.getChild("LinkLabel");
583
- const labelText = labelNode && labelNode.to - labelNode.from > 2 ? state.sliceDoc(labelNode.from, labelNode.to) : `[${alt}]`;
584
- raw = definitions.get(normalizeLabel(labelText));
585
- if (raw === void 0) return {
586
- raw: labelText,
587
- src: null,
588
- alt,
589
- problem: `No definition for ${labelText}`
590
- };
591
- }
592
- const src = resolve(raw);
552
+ /** An image from the document with its path resolved by the host. */
553
+ function imageRef(image, resolve) {
554
+ if (image.src === null) return {
555
+ raw: image.raw,
556
+ src: null,
557
+ alt: image.alt,
558
+ problem: `No definition for ${image.raw}`
559
+ };
560
+ const src = resolve(image.src);
593
561
  return src === null ? {
594
- raw,
562
+ raw: image.raw,
595
563
  src,
596
- alt,
597
- problem: `Can't load ${raw}`
564
+ alt: image.alt,
565
+ problem: `Can't load ${image.raw}`
598
566
  } : {
599
- raw,
567
+ raw: image.raw,
600
568
  src,
601
- alt
569
+ alt: image.alt
602
570
  };
603
571
  }
604
572
  var ImageWidget = class extends WidgetType {
@@ -672,35 +640,11 @@ var ImageWidget = class extends WidgetType {
672
640
  };
673
641
  /** What to preview below which lines: the images of each line, and closed non-empty $$ blocks. */
674
642
  function blockPreviewSources(state) {
675
- const tree = syntaxTree(state);
676
643
  const resolve = state.facet(imageResolver);
677
- const definitions = linkDefinitions(state, tree);
678
- const imagesByLine = /* @__PURE__ */ new Map();
679
- const previews = [];
680
- tree.iterate({ enter(node) {
681
- if (node.name === "Image") {
682
- const at = state.doc.lineAt(node.to).to;
683
- let images = imagesByLine.get(at);
684
- if (!images) {
685
- imagesByLine.set(at, images = []);
686
- previews.push({
687
- at,
688
- images
689
- });
690
- }
691
- images.push(imageRef(state, node.node, definitions, resolve));
692
- return false;
693
- }
694
- if (node.name === "BlockMath") {
695
- const tex = mathTex(state, node.node);
696
- if (tex !== null && tex.trim()) previews.push({
697
- at: state.doc.lineAt(node.to).to,
698
- tex
699
- });
700
- return false;
701
- }
702
- } });
703
- return previews;
644
+ return previewBlocks(syntaxTree(state), state.doc.toString()).map((block) => "tex" in block ? block : {
645
+ at: block.at,
646
+ images: block.images.map((image) => imageRef(image, resolve))
647
+ });
704
648
  }
705
649
  function blockPreviews(state) {
706
650
  const ranges = blockPreviewSources(state).map((preview) => {
@@ -728,7 +672,7 @@ function inlineMathAt(state, pos) {
728
672
  }
729
673
  function mathTooltip(state, pos) {
730
674
  const node = inlineMathAt(state, pos);
731
- const tex = node && mathTex(state, node);
675
+ const tex = node && mathSource(node, (from, to) => state.sliceDoc(from, to));
732
676
  if (!node || !tex) return null;
733
677
  const display = node.getChild("MathMark").to - node.from === 2;
734
678
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simple-mark/codemirror",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "CodeMirror 6 editor that styles Markdown without hiding its syntax.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -28,7 +28,7 @@
28
28
  "@codemirror/state": "^6.7.6",
29
29
  "@codemirror/view": "^6.43.15",
30
30
  "@lezer/common": "^1.5.3",
31
- "@simple-mark/math": "^0.1.0",
32
- "@simple-mark/model": "^0.1.0"
31
+ "@simple-mark/math": "^0.2.0",
32
+ "@simple-mark/model": "^0.2.0"
33
33
  }
34
34
  }