@godxjp/markdown 33.21.0 → 33.22.2

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.
@@ -0,0 +1,118 @@
1
+ "use client";
2
+
3
+ // src/highlight.ts
4
+ import { createLowlight } from "lowlight";
5
+ import bash from "highlight.js/lib/languages/bash";
6
+ import css from "highlight.js/lib/languages/css";
7
+ import diff from "highlight.js/lib/languages/diff";
8
+ import dockerfile from "highlight.js/lib/languages/dockerfile";
9
+ import go from "highlight.js/lib/languages/go";
10
+ import ini from "highlight.js/lib/languages/ini";
11
+ import java from "highlight.js/lib/languages/java";
12
+ import javascript from "highlight.js/lib/languages/javascript";
13
+ import json from "highlight.js/lib/languages/json";
14
+ import markdown from "highlight.js/lib/languages/markdown";
15
+ import php from "highlight.js/lib/languages/php";
16
+ import python from "highlight.js/lib/languages/python";
17
+ import rust from "highlight.js/lib/languages/rust";
18
+ import shell from "highlight.js/lib/languages/shell";
19
+ import sql from "highlight.js/lib/languages/sql";
20
+ import typescript from "highlight.js/lib/languages/typescript";
21
+ import xml from "highlight.js/lib/languages/xml";
22
+ import yaml from "highlight.js/lib/languages/yaml";
23
+ import { visit } from "unist-util-visit";
24
+ var lowlight = createLowlight({
25
+ bash,
26
+ css,
27
+ diff,
28
+ dockerfile,
29
+ go,
30
+ ini,
31
+ java,
32
+ javascript,
33
+ json,
34
+ markdown,
35
+ php,
36
+ python,
37
+ rust,
38
+ shell,
39
+ sql,
40
+ typescript,
41
+ xml,
42
+ yaml
43
+ });
44
+ var SCOPES = {
45
+ comment: "comment",
46
+ quote: "comment",
47
+ doctag: "keyword",
48
+ keyword: "keyword",
49
+ built_in: "keyword",
50
+ type: "keyword",
51
+ "selector-tag": "keyword",
52
+ tag: "punctuation",
53
+ name: "keyword",
54
+ section: "keyword",
55
+ string: "string",
56
+ char: "string",
57
+ regexp: "string-expression",
58
+ subst: "string-expression",
59
+ "template-variable": "string-expression",
60
+ "title.function": "function",
61
+ title: "function",
62
+ "title.class": "constant",
63
+ number: "constant",
64
+ literal: "constant",
65
+ symbol: "constant",
66
+ "variable.constant": "constant",
67
+ "variable.language": "constant",
68
+ "selector-id": "constant",
69
+ "selector-class": "constant",
70
+ bullet: "constant",
71
+ params: "parameter",
72
+ attr: "parameter",
73
+ attribute: "parameter",
74
+ property: "parameter",
75
+ "template-tag": "parameter",
76
+ punctuation: "punctuation",
77
+ operator: "punctuation",
78
+ link: "link",
79
+ addition: "inserted",
80
+ deletion: "deleted"
81
+ };
82
+ function tokenOf(className) {
83
+ if (!Array.isArray(className) || !String(className[0]).startsWith("hljs-")) return void 0;
84
+ const parts = className.map(
85
+ (name, i) => i === 0 ? String(name).slice(5) : String(name).replace(/_+$/, "")
86
+ );
87
+ for (let n = parts.length; n > 0; n--) {
88
+ const token = SCOPES[parts.slice(0, n).join(".")];
89
+ if (token) return token;
90
+ }
91
+ return void 0;
92
+ }
93
+ function retag(nodes) {
94
+ return nodes.flatMap((node) => {
95
+ if (node.type !== "element") return [node];
96
+ const children = retag(node.children);
97
+ const token = tokenOf(node.properties?.className);
98
+ return token ? [{ type: "element", tagName: "span", properties: { dataCodeToken: token }, children }] : children;
99
+ });
100
+ }
101
+ var SKIP = /* @__PURE__ */ new Set(["mermaid", "math"]);
102
+ function rehypeHighlight() {
103
+ return (tree) => {
104
+ visit(tree, "element", (node, _index, parent) => {
105
+ if (node.tagName !== "code" || parent?.type !== "element" || parent.tagName !== "pre") return;
106
+ const classes = node.properties?.className ?? [];
107
+ const language = classes.find((name) => name.startsWith("language-"))?.slice(9);
108
+ if (!language || SKIP.has(language) || !lowlight.registered(language)) return;
109
+ if (!node.children.every((child) => child.type === "text")) return;
110
+ const text = node.children.map((child) => child.type === "text" ? child.value : "").join("");
111
+ node.children = retag(lowlight.highlight(language, text).children);
112
+ });
113
+ };
114
+ }
115
+
116
+ export {
117
+ rehypeHighlight
118
+ };
@@ -1,45 +1,11 @@
1
+ import { d as Doc, D as DirectiveBlock, e as DocNode } from '../directive-DqwrNF2I.js';
2
+ export { C as CALLOUT_KINDS, a as COLUMNS_MAX, b as COLUMNS_MIN, c as CalloutKind, M as MODELLED_DIRECTIVES, f as Mark } from '../directive-DqwrNF2I.js';
3
+
1
4
  /**
2
- * The codec's document: plain JSON in the ProseMirror/Tiptap shape (`type` / `attrs` / `content` /
3
- * `text` / `marks`), so `@godxjp/block-editor` loads it with `setContent` and hands back what
4
- * `getJSON()` returns. No DOM, no editor instance — this module runs in a Worker (gh#1156).
5
- *
6
- * Node names follow Tiptap's StarterKit / Table / Details extensions where one exists:
7
- * doc · paragraph · heading{level} · blockquote · codeBlock{language} · horizontalRule ·
8
- * bulletList{tight} · orderedList{start,tight} · listItem · taskList{tight} · taskItem{checked} ·
9
- * table · tableRow · tableHeader{align} · tableCell{align} · image{src,alt,title} · hardBreak
10
- * and the kit's own:
11
- * callout{kind,title} · details · detailsSummary · detailsContent · columns · column{width} ·
12
- * embed{target} (block `![[x]]`) · wikilink{target,heading,label} · embedInline{target} ·
13
- * rawBlock{source} · rawInline{source} · frontmatter{source} (YAML, first block only)
14
- * Marks: bold · italic · strike · code · link{href,title}.
5
+ * The parts of a directive block's `source` (a `directive` node's attribute), or `null` when the
6
+ * text is not exactly one container directive. Pure — a host's editor preview calls it.
15
7
  */
16
- type Mark = {
17
- type: "bold" | "italic" | "strike" | "code";
18
- } | {
19
- type: "link";
20
- attrs: {
21
- href: string;
22
- title: string | null;
23
- };
24
- };
25
- type DocNode = {
26
- type: string;
27
- attrs?: Record<string, unknown>;
28
- content?: DocNode[];
29
- text?: string;
30
- marks?: Mark[];
31
- };
32
- type Doc = DocNode & {
33
- type: "doc";
34
- content: DocNode[];
35
- };
36
- /** GitHub alert kinds, the only callouts v1 knows. Serialized uppercase. */
37
- declare const CALLOUT_KINDS: readonly ["note", "tip", "important", "warning", "caution"];
38
- type CalloutKind = (typeof CALLOUT_KINDS)[number];
39
- /** Columns per `:::columns` block (grammar v1). */
40
- declare const COLUMNS_MIN = 2;
41
- declare const COLUMNS_MAX = 4;
42
-
8
+ declare function readDirective(source: string): DirectiveBlock | null;
43
9
  /** Markdown → document (plain JSON, ProseMirror/Tiptap shape). Pure: no DOM. */
44
10
  declare function parse(markdown: string): Doc;
45
11
 
@@ -68,4 +34,4 @@ declare const CODEC_ID = "kit-md@2";
68
34
  /** `serialize(parse(markdown))` — the canonical form of a body, exactly what the editor saves. */
69
35
  declare function normalize(markdown: string): string;
70
36
 
71
- export { CALLOUT_KINDS, CODEC_ID, COLUMNS_MAX, COLUMNS_MIN, type CalloutKind, type Doc, type DocNode, type Mark, type OutlineEntry, frontmatterOf, normalize, outline, parse, serialize };
37
+ export { CODEC_ID, DirectiveBlock, Doc, DocNode, type OutlineEntry, frontmatterOf, normalize, outline, parse, readDirective, serialize };
@@ -1,73 +1,15 @@
1
1
  // src/codec/parse.ts
2
2
  import { fromMarkdown } from "mdast-util-from-markdown";
3
- import { toMarkdown } from "mdast-util-to-markdown";
3
+ import { toMarkdown as toMarkdown2 } from "mdast-util-to-markdown";
4
4
 
5
5
  // src/codec/model.ts
6
6
  var CALLOUT_KINDS = ["note", "tip", "important", "warning", "caution"];
7
7
  var COLUMNS_MIN = 2;
8
8
  var COLUMNS_MAX = 4;
9
+ var MODELLED_DIRECTIVES = ["toggle", "columns", "column"];
9
10
 
10
- // src/codec/readers.ts
11
- var CALLOUT = /^\[!([A-Za-z]+)\](?:[ \t]+([^\n]*))?$/;
12
- function readCallout(node) {
13
- const [first, ...rest] = node.children;
14
- if (first?.type !== "paragraph") return null;
15
- const [head, ...tail] = first.children;
16
- if (head?.type !== "text") return null;
17
- const newline = head.value.indexOf("\n");
18
- if (newline === -1 && tail.length > 0) return null;
19
- const line = newline === -1 ? head.value : head.value.slice(0, newline);
20
- const match = CALLOUT.exec(line);
21
- const kind = match?.[1]?.toLowerCase();
22
- if (!match || !kind || !CALLOUT_KINDS.includes(kind)) return null;
23
- const remainder = [
24
- ...newline === -1 ? [] : [{ ...head, value: head.value.slice(newline + 1) }],
25
- ...tail
26
- ].filter((n) => !(n.type === "text" && n.value === ""));
27
- return {
28
- kind,
29
- title: match[2]?.trim() || null,
30
- body: [
31
- ...remainder.length ? [{ type: "paragraph", children: remainder }] : [],
32
- ...rest
33
- ]
34
- };
35
- }
36
- function labelOf(node) {
37
- const first = node.children[0];
38
- return first?.type === "paragraph" && first.data?.directiveLabel ? first : null;
39
- }
40
- function readToggle(node) {
41
- if (node.name !== "toggle" || Object.keys(node.attributes ?? {}).length > 0) return null;
42
- const label = labelOf(node);
43
- return {
44
- summary: label?.children ?? [],
45
- body: label ? node.children.slice(1) : node.children
46
- };
47
- }
48
- function readColumns(node, nested) {
49
- if (node.name !== "columns" || nested || labelOf(node) || Object.keys(node.attributes ?? {}).length) {
50
- return null;
51
- }
52
- const columns = node.children;
53
- if (columns.length < COLUMNS_MIN || columns.length > COLUMNS_MAX) return null;
54
- const parts = [];
55
- for (const column of columns) {
56
- if (column.type !== "containerDirective" || column.name !== "column" || labelOf(column))
57
- return null;
58
- const attributes = Object.entries(column.attributes ?? {});
59
- if (attributes.length > 1) return null;
60
- let width = null;
61
- if (attributes.length === 1) {
62
- const [key, value] = attributes[0];
63
- if (key !== "width" || typeof value !== "string" || !/^(?:[1-9]\d?|100)$/.test(value))
64
- return null;
65
- width = Number(value);
66
- }
67
- parts.push({ width, body: column.children });
68
- }
69
- return parts;
70
- }
11
+ // src/codec/directive.ts
12
+ import { toMarkdown } from "mdast-util-to-markdown";
71
13
 
72
14
  // src/codec/syntax.ts
73
15
  import { directive } from "micromark-extension-directive";
@@ -186,6 +128,87 @@ function toMarkdownOptions() {
186
128
  };
187
129
  }
188
130
 
131
+ // src/codec/directive.ts
132
+ var markdownOf = (node) => toMarkdown(node, toMarkdownOptions()).replace(/\n+$/, "");
133
+ function directiveParts(node) {
134
+ const [first, ...rest] = node.children;
135
+ const hasLabel = first?.type === "paragraph" && Boolean(first.data?.directiveLabel);
136
+ const attributes = {};
137
+ for (const [key, value] of Object.entries(node.attributes ?? {})) attributes[key] = value ?? "";
138
+ return {
139
+ name: node.name,
140
+ label: hasLabel ? markdownOf({ type: "paragraph", children: first.children }) : null,
141
+ attributes,
142
+ source: markdownOf(node),
143
+ body: markdownOf({
144
+ type: "root",
145
+ children: hasLabel ? rest : node.children
146
+ })
147
+ };
148
+ }
149
+
150
+ // src/codec/readers.ts
151
+ var CALLOUT = /^\[!([A-Za-z]+)\](?:[ \t]+([^\n]*))?$/;
152
+ function readCallout(node) {
153
+ const [first, ...rest] = node.children;
154
+ if (first?.type !== "paragraph") return null;
155
+ const [head, ...tail] = first.children;
156
+ if (head?.type !== "text") return null;
157
+ const newline = head.value.indexOf("\n");
158
+ if (newline === -1 && tail.length > 0) return null;
159
+ const line = newline === -1 ? head.value : head.value.slice(0, newline);
160
+ const match = CALLOUT.exec(line);
161
+ const kind = match?.[1]?.toLowerCase();
162
+ if (!match || !kind || !CALLOUT_KINDS.includes(kind)) return null;
163
+ const remainder = [
164
+ ...newline === -1 ? [] : [{ ...head, value: head.value.slice(newline + 1) }],
165
+ ...tail
166
+ ].filter((n) => !(n.type === "text" && n.value === ""));
167
+ return {
168
+ kind,
169
+ title: match[2]?.trim() || null,
170
+ body: [
171
+ ...remainder.length ? [{ type: "paragraph", children: remainder }] : [],
172
+ ...rest
173
+ ]
174
+ };
175
+ }
176
+ function labelOf(node) {
177
+ const first = node.children[0];
178
+ return first?.type === "paragraph" && first.data?.directiveLabel ? first : null;
179
+ }
180
+ function readToggle(node) {
181
+ if (node.name !== "toggle" || Object.keys(node.attributes ?? {}).length > 0) return null;
182
+ const label = labelOf(node);
183
+ return {
184
+ summary: label?.children ?? [],
185
+ body: label ? node.children.slice(1) : node.children
186
+ };
187
+ }
188
+ function readColumns(node, nested) {
189
+ if (node.name !== "columns" || nested || labelOf(node) || Object.keys(node.attributes ?? {}).length) {
190
+ return null;
191
+ }
192
+ const columns = node.children;
193
+ if (columns.length < COLUMNS_MIN || columns.length > COLUMNS_MAX) return null;
194
+ const parts = [];
195
+ for (const column of columns) {
196
+ if (column.type !== "containerDirective" || column.name !== "column" || labelOf(column))
197
+ return null;
198
+ const attributes = Object.entries(column.attributes ?? {});
199
+ if (attributes.length > 1) return null;
200
+ let width = null;
201
+ if (attributes.length === 1) {
202
+ const [key, value] = attributes[0];
203
+ if (key !== "width" || typeof value !== "string" || !/^(?:[1-9]\d?|100)$/.test(value))
204
+ return null;
205
+ width = Number(value);
206
+ }
207
+ parts.push({ width, body: column.children });
208
+ }
209
+ return parts;
210
+ }
211
+
189
212
  // src/codec/parse.ts
190
213
  var WIKILINK = /!\[\[([^[\]\n]+)\]\]|\[\[([^[\]\n|#]+)(?:#([^[\]\n|]+))?(?:\|([^[\]\n]+))?\]\]/g;
191
214
  function parseMdast(markdown) {
@@ -195,7 +218,7 @@ function parseMdast(markdown) {
195
218
  });
196
219
  }
197
220
  function raw(node, inline2) {
198
- const source = node.type === "html" ? node.value : toMarkdown(node, toMarkdownOptions()).replace(/\n+$/, "");
221
+ const source = node.type === "html" ? node.value : toMarkdown2(node, toMarkdownOptions()).replace(/\n+$/, "");
199
222
  return { type: inline2 ? "rawInline" : "rawBlock", attrs: { source } };
200
223
  }
201
224
  function withMark(marks, mark) {
@@ -342,7 +365,12 @@ function directiveBlock(node, depth) {
342
365
  }))
343
366
  };
344
367
  }
345
- return raw(node, false);
368
+ if (MODELLED_DIRECTIVES.includes(node.name)) return raw(node, false);
369
+ return { type: "directive", attrs: { name: node.name, source: directiveParts(node).source } };
370
+ }
371
+ function readDirective(source) {
372
+ const [only, ...rest] = parseMdast(source.replace(/\r\n?/g, "\n")).children;
373
+ return only?.type === "containerDirective" && rest.length === 0 ? directiveParts(only) : null;
346
374
  }
347
375
  function nonEmpty(content2) {
348
376
  return content2.length ? content2 : [{ type: "paragraph" }];
@@ -421,7 +449,7 @@ function parse(markdown) {
421
449
  }
422
450
 
423
451
  // src/codec/serialize.ts
424
- import { toMarkdown as toMarkdown2 } from "mdast-util-to-markdown";
452
+ import { toMarkdown as toMarkdown3 } from "mdast-util-to-markdown";
425
453
  var MARK_RANK = ["link", "bold", "italic", "strike"];
426
454
  var hasMark = (node, type) => node.marks?.some((m) => m.type === type) ?? false;
427
455
  var linkOf = (node) => node.marks?.find((m) => m.type === "link");
@@ -601,6 +629,7 @@ function block2(node) {
601
629
  case "embed":
602
630
  return [{ type: "html", value: `![[${String(attrs.target)}]]` }];
603
631
  case "rawBlock":
632
+ case "directive":
604
633
  return [{ type: "html", value: String(attrs.source) }];
605
634
  case "frontmatter":
606
635
  return [{ type: "yaml", value: String(attrs.source) }];
@@ -635,7 +664,7 @@ function decodeDollarEscapes(markdown) {
635
664
  }
636
665
  function serialize(doc) {
637
666
  const root = { type: "root", children: flow(doc.content ?? []) };
638
- const markdown = toMarkdown2(root, toMarkdownOptions());
667
+ const markdown = toMarkdown3(root, toMarkdownOptions());
639
668
  if (markdown.trim() === "") return "";
640
669
  const tidy = markdown.replace(/\n{3,}/g, "\n\n").replace(/^\n+/, "").replace(/\n+$/, "\n");
641
670
  return decodeDollarEscapes(decodeAttentionReferences(tidy));
@@ -675,9 +704,11 @@ export {
675
704
  CODEC_ID,
676
705
  COLUMNS_MAX,
677
706
  COLUMNS_MIN,
707
+ MODELLED_DIRECTIVES,
678
708
  frontmatterOf,
679
709
  normalize,
680
710
  outline,
681
711
  parse,
712
+ readDirective,
682
713
  serialize
683
714
  };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * The codec's document: plain JSON in the ProseMirror/Tiptap shape (`type` / `attrs` / `content` /
3
+ * `text` / `marks`), so `@godxjp/block-editor` loads it with `setContent` and hands back what
4
+ * `getJSON()` returns. No DOM, no editor instance — this module runs in a Worker (gh#1156).
5
+ *
6
+ * Node names follow Tiptap's StarterKit / Table / Details extensions where one exists:
7
+ * doc · paragraph · heading{level} · blockquote · codeBlock{language} · horizontalRule ·
8
+ * bulletList{tight} · orderedList{start,tight} · listItem · taskList{tight} · taskItem{checked} ·
9
+ * table · tableRow · tableHeader{align} · tableCell{align} · image{src,alt,title} · hardBreak
10
+ * and the kit's own:
11
+ * callout{kind,title} · details · detailsSummary · detailsContent · columns · column{width} ·
12
+ * embed{target} (block `![[x]]`) · wikilink{target,heading,label} · embedInline{target} ·
13
+ * directive{name,source} (a container directive the kit does not model: `:::poll[…]{…}`) ·
14
+ * rawBlock{source} · rawInline{source} · frontmatter{source} (YAML, first block only)
15
+ * Marks: bold · italic · strike · code · link{href,title}.
16
+ */
17
+ type Mark = {
18
+ type: "bold" | "italic" | "strike" | "code";
19
+ } | {
20
+ type: "link";
21
+ attrs: {
22
+ href: string;
23
+ title: string | null;
24
+ };
25
+ };
26
+ type DocNode = {
27
+ type: string;
28
+ attrs?: Record<string, unknown>;
29
+ content?: DocNode[];
30
+ text?: string;
31
+ marks?: Mark[];
32
+ };
33
+ type Doc = DocNode & {
34
+ type: "doc";
35
+ content: DocNode[];
36
+ };
37
+ /** GitHub alert kinds, the only callouts v1 knows. Serialized uppercase. */
38
+ declare const CALLOUT_KINDS: readonly ["note", "tip", "important", "warning", "caution"];
39
+ type CalloutKind = (typeof CALLOUT_KINDS)[number];
40
+ /** Columns per `:::columns` block (grammar v1). */
41
+ declare const COLUMNS_MIN = 2;
42
+ declare const COLUMNS_MAX = 4;
43
+ /** Directive names the kit draws itself (grammar v1). A host cannot register a renderer for them. */
44
+ declare const MODELLED_DIRECTIVES: readonly string[];
45
+
46
+ /**
47
+ * DIRECTIVE BLOCKS: a container directive the kit does not model itself (`:::poll[…]{…}`,
48
+ * `::::form`), read once so the codec, the renderer and the block editor hand a host the same
49
+ * parts. Here everything is the CANONICAL Markdown the codec writes (what the editor saves);
50
+ * `<Markdown directives>` slices the same parts from the body as written, which is the same text for
51
+ * a body the editor saved.
52
+ */
53
+ type DirectiveBlock = {
54
+ /** The directive's name: `poll` for `:::poll`. */
55
+ name: string;
56
+ /** The `[label]` as inline Markdown, or `null` when the directive has none. */
57
+ label: string | null;
58
+ /** The `{…}` attributes; a bare flag (`{anonymous}`) is `""`, `{#x}` is `id`, `{.x}` is `class`. */
59
+ attributes: Record<string, string>;
60
+ /** The whole directive, fences included: canonical from the codec, as written from `<Markdown>`. */
61
+ source: string;
62
+ /** The content between the fences (without the label) as Markdown; `""` when empty. */
63
+ body: string;
64
+ };
65
+
66
+ export { CALLOUT_KINDS as C, type DirectiveBlock as D, MODELLED_DIRECTIVES as M, COLUMNS_MAX as a, COLUMNS_MIN as b, type CalloutKind as c, type Doc as d, type DocNode as e, type Mark as f };