@canofold/markdown 0.2.1 → 0.3.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.
@@ -2,5 +2,5 @@
2
2
  export { detectMarkdownAssets } from '../compiler/assets';
3
3
  export { analyzeMarkdown } from '../compiler/analyze';
4
4
  export { analyzeMdxModuleBoundary } from '../compiler/mdxModules';
5
- export type { AnalyzeMarkdownOptions, MarkdownAnalysis, MarkdownCodeExample, MarkdownHeading } from '../compiler/analyze';
5
+ export type { AnalyzeMarkdownOptions, MarkdownAnalysis, MarkdownCodeExample, MarkdownDirective, MarkdownHeading } from '../compiler/analyze';
6
6
  export type { MdxImportBinding, MdxImportDeclaration, MdxModuleBoundary } from '../compiler/mdxModules';
@@ -1,132 +1,150 @@
1
- import { n as k, c as w } from "../directiveValidation-CwXRsn5k.js";
2
- import { d as K } from "../directiveValidation-CwXRsn5k.js";
3
- import x from "github-slugger";
4
- import { remarkDefinitionList as b } from "remark-definition-list";
5
- import D from "remark-directive";
6
- import M from "remark-gfm";
1
+ import { n as w, c as x } from "../directiveValidation-BdVbgcY_.js";
2
+ import { d as V } from "../directiveValidation-BdVbgcY_.js";
3
+ import b from "github-slugger";
4
+ import { remarkDefinitionList as D } from "remark-definition-list";
5
+ import M from "remark-directive";
6
+ import E from "remark-gfm";
7
7
  import S from "remark-parse";
8
- import { unified as E } from "unified";
9
- import { visit as y } from "unist-util-visit";
10
- import { a as I, n as v, m as C } from "../plugins-CeQ-Bo89.js";
11
- import { createProcessor as A } from "@mdx-js/mdx";
12
- function L(t) {
8
+ import { unified as I } from "unified";
9
+ import { visit as k } from "unist-util-visit";
10
+ import { a as j, n as C, m as A } from "../plugins-StkCS2su.js";
11
+ import { createProcessor as L } from "@mdx-js/mdx";
12
+ function P(t) {
13
13
  return "children" in t && Array.isArray(t.children);
14
14
  }
15
- function f(t) {
16
- return t.type === "text" || t.type === "inlineCode" ? String(t.value ?? "") : t.type === "image" ? String(t.alt ?? "") : t.type === "code" || t.type === "html" || t.type === "definition" ? "" : L(t) ? t.children.map(f).join(" ") : "";
15
+ function l(t) {
16
+ return t.type === "text" || t.type === "inlineCode" ? String(t.value ?? "") : t.type === "image" ? String(t.alt ?? "") : t.type === "code" || t.type === "html" || t.type === "definition" ? "" : P(t) ? t.children.map(l).join(" ") : "";
17
17
  }
18
- function q(t, e = {}) {
19
- const s = E().use(S).use(b).use(M).use(D).parse(k(t)), c = new x(), m = [], o = [], r = [], l = [];
18
+ function H(t, i = {}) {
19
+ const n = I().use(S).use(D).use(E).use(M).parse(w(t)), c = new b(), f = [], s = [], o = [], m = [], y = [];
20
20
  let g = 0;
21
21
  const u = /* @__PURE__ */ new Map();
22
- y(s, "definition", (n) => {
23
- n.url && u.set(n.identifier.toLowerCase(), n.url);
24
- }), y(s, (n) => {
25
- if (n.type === "heading") {
26
- const i = n, a = f(i).replace(/\s+/g, " ").trim();
27
- m.push({ level: i.depth, text: a, slug: c.slug(a) });
22
+ k(n, "definition", (r) => {
23
+ r.url && u.set(r.identifier.toLowerCase(), r.url);
24
+ }), k(n, (r) => {
25
+ if (r.type === "heading") {
26
+ const e = r, a = l(e).replace(/\s+/g, " ").trim();
27
+ f.push({ level: e.depth, text: a, slug: c.slug(a) });
28
28
  }
29
- if (n.type === "code") {
30
- const i = n;
31
- i.lang || (g += 1), o.push({
32
- language: i.lang || "text",
33
- ...i.meta ? { meta: i.meta } : {},
34
- code: i.value
29
+ if (r.type === "code") {
30
+ const e = r;
31
+ e.lang || (g += 1), s.push({
32
+ language: e.lang || "text",
33
+ ...e.meta ? { meta: e.meta } : {},
34
+ code: e.value
35
35
  });
36
36
  }
37
- if (n.type === "link") {
38
- const i = n;
39
- i.url && r.push(i.url);
37
+ if (r.type === "link") {
38
+ const e = r;
39
+ e.url && o.push(e.url);
40
+ }
41
+ if (r.type === "image") {
42
+ const e = r;
43
+ e.url && m.push(e.url);
40
44
  }
41
- if (n.type === "image") {
42
- const i = n;
43
- i.url && l.push(i.url);
45
+ if (r.type === "linkReference") {
46
+ const e = r, a = u.get(e.identifier.toLowerCase());
47
+ a && o.push(a);
44
48
  }
45
- if (n.type === "linkReference") {
46
- const i = n, a = u.get(i.identifier.toLowerCase());
47
- a && r.push(a);
49
+ if (r.type === "imageReference") {
50
+ const e = r, a = u.get(e.identifier.toLowerCase());
51
+ a && m.push(a);
48
52
  }
49
- if (n.type === "imageReference") {
50
- const i = n, a = u.get(i.identifier.toLowerCase());
51
- a && l.push(a);
53
+ if (r.type === "containerDirective" || r.type === "leafDirective" || r.type === "textDirective") {
54
+ const e = r;
55
+ y.push({
56
+ name: e.name,
57
+ type: e.type,
58
+ label: l(e).replace(/\s+/g, " ").trim(),
59
+ attributes: Object.fromEntries(
60
+ Object.entries(e.attributes ?? {}).flatMap(
61
+ ([a, h]) => typeof h == "string" ? [[a, h]] : []
62
+ )
63
+ ),
64
+ ...e.position?.start.line === void 0 ? {} : { line: e.position.start.line },
65
+ ...e.position?.start.column === void 0 ? {} : { column: e.position.start.column },
66
+ ...e.position?.start.offset === void 0 ? {} : { offset: e.position.start.offset },
67
+ ...e.position?.end.offset === void 0 ? {} : { endOffset: e.position.end.offset }
68
+ });
52
69
  }
53
70
  });
54
- const h = I(v(e.plugins), {
71
+ const v = j(C(i.plugins), {
55
72
  source: t,
56
73
  mode: "markdown"
57
74
  });
58
75
  return {
59
- text: f(s).replace(/\s+/g, " ").trim(),
60
- headings: m,
61
- codeExamples: o,
62
- links: r,
63
- images: l,
76
+ text: l(n).replace(/\s+/g, " ").trim(),
77
+ headings: f,
78
+ codeExamples: s,
79
+ links: o,
80
+ images: m,
64
81
  missingCodeBlockLanguages: g,
65
- directiveIssues: w(s, C(h)).map(
66
- ({ message: n, line: i, column: a }) => ({
67
- message: n,
68
- line: i,
82
+ directives: y,
83
+ directiveIssues: x(n, A(v)).map(
84
+ ({ message: r, line: e, column: a }) => ({
85
+ message: r,
86
+ line: e,
69
87
  column: a
70
88
  })
71
89
  )
72
90
  };
73
91
  }
74
- function P(t) {
75
- return t.specifiers.map((e) => e.type === "ImportDefaultSpecifier" ? { imported: "default", local: e.local.name } : e.type === "ImportNamespaceSpecifier" ? { imported: "*", local: e.local.name, namespace: !0 } : {
76
- imported: e.type === "ImportSpecifier" ? "name" in e.imported ? e.imported.name : String(e.imported.value ?? "") : "",
77
- local: e.local.name
92
+ function O(t) {
93
+ return t.specifiers.map((i) => i.type === "ImportDefaultSpecifier" ? { imported: "default", local: i.local.name } : i.type === "ImportNamespaceSpecifier" ? { imported: "*", local: i.local.name, namespace: !0 } : {
94
+ imported: i.type === "ImportSpecifier" ? "name" in i.imported ? i.imported.name : String(i.imported.value ?? "") : "",
95
+ local: i.local.name
78
96
  });
79
97
  }
80
- function j(t) {
98
+ function R(t) {
81
99
  return !!(t && typeof t == "object" && typeof t.type == "string");
82
100
  }
83
- function R(t) {
84
- const e = t;
85
- return typeof e.start == "number" && typeof e.end == "number";
101
+ function z(t) {
102
+ const i = t;
103
+ return typeof i.start == "number" && typeof i.end == "number";
86
104
  }
87
- function d(t, e) {
105
+ function d(t, i) {
88
106
  if (!t || typeof t != "object") return;
89
107
  if (Array.isArray(t)) {
90
- for (const s of t) d(s, e);
108
+ for (const n of t) d(n, i);
91
109
  return;
92
110
  }
93
111
  const p = t;
94
- j(t) && e(t);
95
- for (const [s, c] of Object.entries(p))
96
- s === "loc" || s === "position" || d(c, e);
112
+ R(t) && i(t);
113
+ for (const [n, c] of Object.entries(p))
114
+ n === "loc" || n === "position" || d(c, i);
97
115
  }
98
- function F(t) {
99
- const e = A().parse(t), p = [], s = [], c = [];
100
- d(e, (o) => {
101
- if (o.type === "ImportExpression") {
102
- const r = o.source.type === "Literal" && typeof o.source.value == "string" ? o.source.value : void 0;
103
- s.push({
116
+ function J(t) {
117
+ const i = L().parse(t), p = [], n = [], c = [];
118
+ d(i, (s) => {
119
+ if (s.type === "ImportExpression") {
120
+ const o = s.source.type === "Literal" && typeof s.source.value == "string" ? s.source.value : void 0;
121
+ n.push({
104
122
  kind: "dynamic-import",
105
- specifier: r
123
+ specifier: o
106
124
  });
107
125
  }
108
126
  });
109
- for (const o of e.children ?? [])
110
- if (o.type === "mdxjsEsm")
111
- for (const r of o.data?.estree?.body ?? [])
112
- if (r.type === "ImportDeclaration") {
127
+ for (const s of i.children ?? [])
128
+ if (s.type === "mdxjsEsm")
129
+ for (const o of s.data?.estree?.body ?? [])
130
+ if (o.type === "ImportDeclaration") {
113
131
  if (p.push({
114
- specifier: String(r.source.value),
115
- bindings: P(r)
116
- }), !R(r))
132
+ specifier: String(o.source.value),
133
+ bindings: O(o)
134
+ }), !z(o))
117
135
  throw new Error("Unable to locate an MDX import declaration in the source document");
118
- c.push({ start: r.start, end: r.end });
119
- } else (r.type === "ExportNamedDeclaration" || r.type === "ExportAllDeclaration") && r.source && s.push({
136
+ c.push({ start: o.start, end: o.end });
137
+ } else (o.type === "ExportNamedDeclaration" || o.type === "ExportAllDeclaration") && o.source && n.push({
120
138
  kind: "export-from",
121
- specifier: String(r.source.value)
139
+ specifier: String(o.source.value)
122
140
  });
123
- const m = [
124
- ...new Map(c.map((o) => [`${o.start}:${o.end}`, o])).values()
125
- ].sort((o, r) => r.start - o.start).reduce((o, r) => `${o.slice(0, r.start)}${o.slice(r.end)}`, t).replace(/^\s*\n/, "");
126
- return { imports: p, sourceWithoutImports: m, unsupportedReferences: s };
141
+ const f = [
142
+ ...new Map(c.map((s) => [`${s.start}:${s.end}`, s])).values()
143
+ ].sort((s, o) => o.start - s.start).reduce((s, o) => `${s.slice(0, o.start)}${s.slice(o.end)}`, t).replace(/^\s*\n/, "");
144
+ return { imports: p, sourceWithoutImports: f, unsupportedReferences: n };
127
145
  }
128
146
  export {
129
- q as analyzeMarkdown,
130
- F as analyzeMdxModuleBoundary,
131
- K as detectMarkdownAssets
147
+ H as analyzeMarkdown,
148
+ J as analyzeMdxModuleBoundary,
149
+ V as detectMarkdownAssets
132
150
  };
package/dist/server.js CHANGED
@@ -1,63 +1,68 @@
1
- import * as p from "react/jsx-runtime";
1
+ import * as f from "react/jsx-runtime";
2
2
  import { jsx as i } from "react/jsx-runtime";
3
- import { evaluate as w } from "@mdx-js/mdx";
4
- import { c as f, a as h, b as k } from "./Markdown-DgolXL7g.js";
5
- import { a as M, n as P } from "./directiveValidation-CwXRsn5k.js";
6
- import { n as x, d as v, c as y, a as N, p as L } from "./prepareMarkdown-Bug1ncYk.js";
7
- import { a as b, b as S, s as C, c as T } from "./plugins-CeQ-Bo89.js";
8
- import { d as G } from "./plugins-CeQ-Bo89.js";
9
- import { createHash as z } from "node:crypto";
10
- async function O(a = {}, r, t = { warnedLanguages: /* @__PURE__ */ new Set() }) {
11
- const n = x(a), e = M(), s = r ?? "", o = b(n.plugins, { source: s, mode: "mdx" }), l = S(o), d = { ...n, plugins: o }, u = v(s, l);
12
- o.forEach((g) => e.markPluginAssets(g));
13
- const m = u.highlightedCode ? await y(
14
- u.codeLanguages,
3
+ import { evaluate as h } from "@mdx-js/mdx";
4
+ import { c as k, a as M, b as P } from "./Markdown-C7O01MRv.js";
5
+ import { a as x, n as v } from "./directiveValidation-BdVbgcY_.js";
6
+ import { n as y, d as L, c as N, a as S, p as b } from "./prepareMarkdown-D-bFl1_V.js";
7
+ import { a as C, b as T, c as z, s as H, e as O } from "./plugins-StkCS2su.js";
8
+ import { d as W } from "./plugins-StkCS2su.js";
9
+ import { createHash as R } from "node:crypto";
10
+ async function A(a = {}, r, s = { warnedLanguages: /* @__PURE__ */ new Set() }) {
11
+ const n = y(a), e = x(), t = r ?? "", o = C(n.plugins, { source: t, mode: "mdx" }), l = T(o), d = { ...n, plugins: o }, g = L(t, l), m = [
12
+ .../* @__PURE__ */ new Set([...g.codeLanguages, ...z(o)])
13
+ ], c = {
14
+ highlightedCode: m.length > 0,
15
+ codeLanguages: m
16
+ };
17
+ o.forEach((w) => e.markPluginAssets(w));
18
+ const p = c.highlightedCode ? await N(
19
+ c.codeLanguages,
15
20
  d,
16
- t.warnedLanguages,
21
+ s.warnedLanguages,
17
22
  l
18
- ) : void 0, c = N("mdx", e, d, u, m);
23
+ ) : void 0, u = S("mdx", e, d, c, p);
19
24
  return {
20
- remarkPlugins: c.remarkPlugins,
21
- rehypePlugins: c.rehypePlugins,
22
- remarkRehypeOptions: { handlers: c.handlers },
25
+ remarkPlugins: u.remarkPlugins,
26
+ rehypePlugins: u.rehypePlugins,
27
+ remarkRehypeOptions: { handlers: u.handlers },
23
28
  getAssets: () => e.snapshot()
24
29
  };
25
30
  }
26
- function R(a, r) {
27
- const { plugins: t, ...n } = r ?? {}, e = C({ ...n, plugins: T(t) });
31
+ function F(a, r) {
32
+ const { plugins: s, ...n } = r ?? {}, e = H({ ...n, plugins: O(s) });
28
33
  if (e !== void 0)
29
- return z("sha256").update(a).update("\0").update(e).digest("hex");
34
+ return R("sha256").update(a).update("\0").update(e).digest("hex");
30
35
  }
31
- function A(a, r, t, n) {
32
- for (a.delete(r), a.set(r, t); a.size > n; ) {
36
+ function D(a, r, s, n) {
37
+ for (a.delete(r), a.set(r, s); a.size > n; ) {
33
38
  const e = a.keys().next().value;
34
39
  if (e === void 0) break;
35
40
  a.delete(e);
36
41
  }
37
- return t;
42
+ return s;
38
43
  }
39
- function H(a, r, t, n) {
40
- if (!r) return t();
44
+ function J(a, r, s, n) {
45
+ if (!r) return s();
41
46
  const e = a.get(r);
42
47
  if (e)
43
48
  return a.delete(r), a.set(r, e), e;
44
- const s = t().catch((o) => {
45
- throw a.get(r) === s && a.delete(r), o;
49
+ const t = s().catch((o) => {
50
+ throw a.get(r) === t && a.delete(r), o;
46
51
  });
47
- return A(a, r, s, n);
52
+ return D(a, r, t, n);
48
53
  }
49
- function F({
54
+ function K({
50
55
  maxEntries: a = 64,
51
56
  warningState: r = /* @__PURE__ */ new Set()
52
57
  } = {}) {
53
- const t = Number.isFinite(a) && a > 0 ? Math.floor(a) : 64, n = /* @__PURE__ */ new Map();
58
+ const s = Number.isFinite(a) && a > 0 ? Math.floor(a) : 64, n = /* @__PURE__ */ new Map();
54
59
  return {
55
- prepare(e, s) {
56
- return H(
60
+ prepare(e, t) {
61
+ return J(
57
62
  n,
58
- R(e, s),
59
- () => L(e, s, { warnedLanguages: r }),
60
- t
63
+ F(e, t),
64
+ () => b(e, t, { warnedLanguages: r }),
65
+ s
61
66
  );
62
67
  },
63
68
  clear() {
@@ -65,16 +70,16 @@ function F({
65
70
  }
66
71
  };
67
72
  }
68
- function q(a = {}) {
69
- const r = /* @__PURE__ */ new Set(), t = F({ ...a, warningState: r });
73
+ function G(a = {}) {
74
+ const r = /* @__PURE__ */ new Set(), s = K({ ...a, warningState: r });
70
75
  return {
71
76
  async render(n, e = {}) {
72
- const s = await t.prepare(n, e.markdown);
77
+ const t = await s.prepare(n, e.markdown);
73
78
  return {
74
79
  content: /* @__PURE__ */ i(
75
- k,
80
+ P,
76
81
  {
77
- document: s.document,
82
+ document: t.document,
78
83
  classNames: e.classNames,
79
84
  components: e.components,
80
85
  slots: e.slots,
@@ -84,27 +89,27 @@ function q(a = {}) {
84
89
  urlTransform: e.urlTransform
85
90
  }
86
91
  ),
87
- assets: s.assets
92
+ assets: t.assets
88
93
  };
89
94
  },
90
95
  async renderMdx(n, e = {}) {
91
96
  if (e.markdown?.html && e.markdown.html !== "trusted")
92
97
  throw new Error("MDX executes trusted JSX and does not support sanitize or strip HTML policies");
93
- const s = await O(e.markdown, n, {
98
+ const t = await A(e.markdown, n, {
94
99
  warnedLanguages: r
95
- }), l = (await w(
100
+ }), l = (await h(
96
101
  {
97
- value: P(n),
102
+ value: v(n),
98
103
  data: { canofoldLocale: e.markdown?.locale?.trim() || void 0 }
99
104
  },
100
105
  {
101
- ...p,
102
- remarkPlugins: s.remarkPlugins,
103
- rehypePlugins: s.rehypePlugins,
104
- remarkRehypeOptions: s.remarkRehypeOptions,
106
+ ...f,
107
+ remarkPlugins: t.remarkPlugins,
108
+ rehypePlugins: t.rehypePlugins,
109
+ remarkRehypeOptions: t.remarkRehypeOptions,
105
110
  baseUrl: e.baseUrl ?? import.meta.url
106
111
  }
107
- )).default, d = f(
112
+ )).default, d = k(
108
113
  e.classNames,
109
114
  e.slots,
110
115
  e.components,
@@ -112,7 +117,7 @@ function q(a = {}) {
112
117
  );
113
118
  return {
114
119
  content: /* @__PURE__ */ i(
115
- h,
120
+ M,
116
121
  {
117
122
  "data-cf-runtime": "static",
118
123
  as: e.as,
@@ -123,15 +128,15 @@ function q(a = {}) {
123
128
  children: /* @__PURE__ */ i(l, { components: d })
124
129
  }
125
130
  ),
126
- assets: s.getAssets()
131
+ assets: t.getAssets()
127
132
  };
128
133
  },
129
134
  clear() {
130
- t.clear();
135
+ s.clear();
131
136
  }
132
137
  };
133
138
  }
134
139
  export {
135
- q as createMarkdownRenderer,
136
- G as defineMarkdownPlugin
140
+ G as createMarkdownRenderer,
141
+ W as defineMarkdownPlugin
137
142
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canofold/markdown",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "An interactive, themeable Markdown renderer for knowledge and documentation sites.",
5
5
  "license": "MIT",
6
6
  "author": "Canofold Contributors",
@@ -90,7 +90,7 @@
90
90
  "@types/mdast": "^4.0.4",
91
91
  "@types/node": "^22.10.0",
92
92
  "@types/react": "^19.2.18",
93
- "@types/react-dom": "^19.2.5",
93
+ "@types/react-dom": "^19.2.7",
94
94
  "@types/unist": "^3.0.3",
95
95
  "mdast-util-directive": "^3.1.0",
96
96
  "mdast-util-to-hast": "^13.2.1",