@opendikw/web-client 0.1.2 → 0.1.3

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.
@@ -1,4 +1,5 @@
1
1
  export * from "./markdown.js";
2
+ export * from "./scientific-inline.js";
2
3
  export * from "./markdown-blocks.js";
3
4
  export * from "./source-inline-refs.js";
4
5
  export * from "./lang.js";
@@ -1,4 +1,5 @@
1
1
  export * from "./markdown.js";
2
+ export * from "./scientific-inline.js";
2
3
  export * from "./markdown-blocks.js";
3
4
  export * from "./source-inline-refs.js";
4
5
  export * from "./lang.js";
@@ -1,4 +1,5 @@
1
1
  import MarkdownIt from "markdown-it";
2
+ import { installScientificInline } from "./scientific-inline.js";
2
3
  export function parseMarkdownDocument(source, options = {}) {
3
4
  const normalized = source.replace(/\r\n/g, "\n");
4
5
  const frontmatter = extractFrontmatter(normalized);
@@ -110,7 +111,7 @@ function stripWikilinkSyntax(value) {
110
111
  return value.replace(/\[\[([^\]|]+)\|([^\]]+)\]\]/g, "$2").replace(/\[\[([^\]]+)\]\]/g, "$1");
111
112
  }
112
113
  export function slugifyHeading(value) {
113
- return stripWikilinkSyntax(value)
114
+ return stripWikilinkSyntax(stripScientificInlineTags(value))
114
115
  .trim()
115
116
  .toLowerCase()
116
117
  .normalize("NFKD")
@@ -153,6 +154,22 @@ export function parseDetailsOpenAttribute(attributes) {
153
154
  // parser config used by MarkdownView so the slugs produced here match the
154
155
  // `id` attributes that MarkdownView writes onto the rendered DOM.
155
156
  const headingParser = new MarkdownIt({ html: false, linkify: true, typographer: true });
157
+ installScientificInline(headingParser);
158
+ // Remove only markup that the reader actually accepts; code spans and unsafe
159
+ // HTML remain literal. Keep the existing Markdown/wikilink heading contract.
160
+ function stripScientificInlineTags(value) {
161
+ if (!/<(?:sup|sub)>/i.test(value))
162
+ return value;
163
+ const tokens = headingParser.parseInline(value, {})[0]?.children ?? [];
164
+ let result = value;
165
+ for (const token of [...tokens].reverse()) {
166
+ if (token.type === "scientific_open" || token.type === "scientific_close") {
167
+ const { start, end } = token.meta;
168
+ result = result.slice(0, start) + result.slice(end);
169
+ }
170
+ }
171
+ return result;
172
+ }
156
173
  // MarkdownView renders any details block whose `open` attribute parses as
157
174
  // safe inside its own recursive renderMarkdown() call with a fresh slug
158
175
  // counter. If extractHeadingsWithSlugs included headings from those blocks in
@@ -185,7 +202,7 @@ export function extractHeadingsWithSlugs(body) {
185
202
  if (!inline || inline.type !== "inline") {
186
203
  continue;
187
204
  }
188
- const title = (inline.content ?? "").trim();
205
+ const title = stripScientificInlineTags(inline.content ?? "").trim();
189
206
  if (!title) {
190
207
  continue;
191
208
  }
@@ -0,0 +1,3 @@
1
+ import type { MarkdownIt } from "markdown-it";
2
+ /** Attribute-free, single-line scientific marks without enabling raw HTML. */
3
+ export declare function installScientificInline(md: MarkdownIt): void;
@@ -0,0 +1,75 @@
1
+ // Index balanced pairs once per inline source. Repeated unclosed tags must not
2
+ // repeatedly scan the line tail, and rejected nested pairs must stay literal.
3
+ function indexPairs(source) {
4
+ const pairs = new Map();
5
+ const stack = [];
6
+ for (const match of source.matchAll(/\n|<(\/?)(sup|sub)>/gi)) {
7
+ if (match[0] === "\n") {
8
+ stack.length = 0;
9
+ }
10
+ else if (!match[1]) {
11
+ stack.push({ start: match.index, tag: match[2].toLowerCase() });
12
+ }
13
+ else {
14
+ const open = stack.pop();
15
+ if (open?.tag === match[2].toLowerCase())
16
+ pairs.set(open.start, match.index);
17
+ else
18
+ stack.length = 0;
19
+ }
20
+ }
21
+ return pairs;
22
+ }
23
+ /** Attribute-free, single-line scientific marks without enabling raw HTML. */
24
+ export function installScientificInline(md) {
25
+ const sources = new WeakMap();
26
+ md.inline.ruler.before("html_inline", "scientific_inline", (state, silent) => {
27
+ const start = state.pos;
28
+ // Link-label lookahead must still inspect inner brackets for nested links.
29
+ if (silent || state.src[start] !== "<")
30
+ return false;
31
+ const opener = /^<(sup|sub)>/i.exec(state.src.slice(start, start + 5));
32
+ if (!opener)
33
+ return false;
34
+ let source = sources.get(state);
35
+ if (!source) {
36
+ source = { pairs: indexPairs(state.src), rejectedUntil: -1 };
37
+ sources.set(state, source);
38
+ }
39
+ if (start < source.rejectedUntil)
40
+ return false;
41
+ const contentEnd = source.pairs.get(start);
42
+ if (contentEnd === undefined || contentEnd + 6 > state.posMax) {
43
+ // An actual unbalanced opener keeps its remainder literal, including any
44
+ // otherwise balanced descendants. Code-span lookalikes never call here.
45
+ const newline = state.src.indexOf("\n", start);
46
+ source.rejectedUntil = newline < 0 ? state.posMax : Math.min(newline, state.posMax);
47
+ return false;
48
+ }
49
+ const end = contentEnd + 6;
50
+ const content = state.src.slice(start + 5, contentEnd);
51
+ // Consume rejected pairs as text too, so a nested tag cannot become live.
52
+ if (/[<>]/.test(content)) {
53
+ state.push("text", "", 0).content = state.src.slice(start, end);
54
+ }
55
+ else {
56
+ const inner = new md.inline.State(content, md, state.env, []);
57
+ inner.linkLevel = state.linkLevel;
58
+ inner.level = state.level + 1;
59
+ // A bounded src also confines custom rules that search past posMax.
60
+ md.inline.tokenize(inner);
61
+ for (const rule of md.inline.ruler2.getRules(""))
62
+ rule(inner);
63
+ const tag = opener[1].toLowerCase();
64
+ state.push("scientific_open", tag, 1).meta = { start, end: start + 5 };
65
+ // Keep ordinary tokens visible to image-alt, linkify and typography rules.
66
+ for (const child of inner.tokens) {
67
+ const token = state.push(child.type, child.tag, child.nesting);
68
+ Object.assign(token, child, { level: token.level });
69
+ }
70
+ state.push("scientific_close", tag, -1).meta = { start: contentEnd, end };
71
+ }
72
+ state.pos = end;
73
+ return true;
74
+ });
75
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opendikw/web-client",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "engines": {