@glossarist/concept-browser 0.7.35 → 0.7.41

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 (38) hide show
  1. package/package.json +2 -2
  2. package/scripts/build-edges.js +16 -8
  3. package/scripts/generate-data.mjs +284 -86
  4. package/src/__tests__/citation-display.test.ts +165 -3
  5. package/src/__tests__/cite-ref.test.ts +112 -0
  6. package/src/__tests__/concept-detail-interaction.test.ts +1 -1
  7. package/src/__tests__/{math.test.ts → content-renderer.test.ts} +113 -29
  8. package/src/__tests__/escape.test.ts +76 -0
  9. package/src/__tests__/graph-data-source.test.ts +155 -0
  10. package/src/__tests__/model-bridge-bridges.test.ts +150 -0
  11. package/src/__tests__/model-bridge-citation.test.ts +163 -0
  12. package/src/__tests__/reference-resolver-cite.test.ts +122 -0
  13. package/src/__tests__/reference-resolver.test.ts +12 -7
  14. package/src/__tests__/resolve-view.test.ts +1 -1
  15. package/src/__tests__/sidebar-nav-highlighting.test.ts +178 -0
  16. package/src/__tests__/source-refs.test.ts +9 -6
  17. package/src/__tests__/test-helpers.ts +20 -0
  18. package/src/__tests__/uri-router.test.ts +39 -12
  19. package/src/adapters/DatasetAdapter.ts +12 -0
  20. package/src/adapters/GraphDataSource.ts +3 -3
  21. package/src/adapters/ReferenceResolver.ts +85 -55
  22. package/src/adapters/UriRouter.ts +82 -10
  23. package/src/adapters/factory.ts +34 -10
  24. package/src/adapters/model-bridge.ts +121 -71
  25. package/src/adapters/types.ts +3 -0
  26. package/src/components/AppSidebar.vue +7 -4
  27. package/src/components/CitationDisplay.vue +86 -30
  28. package/src/components/ConceptDetail.vue +56 -20
  29. package/src/components/LanguageDetail.vue +6 -6
  30. package/src/composables/use-concept-content.ts +8 -8
  31. package/src/composables/use-concept-edges.ts +2 -1
  32. package/src/composables/use-render-options.ts +1 -1
  33. package/src/graph/GraphEngine.ts +3 -3
  34. package/src/stores/vocabulary.ts +2 -2
  35. package/src/style.css +29 -0
  36. package/src/utils/content-renderer.ts +312 -0
  37. package/src/utils/markdown-lite.ts +2 -2
  38. package/src/utils/math.ts +0 -189
@@ -0,0 +1,312 @@
1
+ /**
2
+ * Content renderer: transforms Glossarist inline content notation to HTML.
3
+ *
4
+ * Handles ALL inline rendering — mentions, cross-references, citations,
5
+ * math placeholders, tables, lists, and text formatting. This is the single
6
+ * source of truth for content rendering in the browser.
7
+ *
8
+ * Math-specific helpers (replaceBracketed, mathPlaceholder) are internal.
9
+ * The v-math directive upgrades the placeholders to KaTeX at runtime.
10
+ */
11
+ import { escapeHtml, escapeAttr } from './escape';
12
+ import { parseMention } from 'glossarist';
13
+
14
+ // ── Resolver types ────────────────────────────────────────────────────────
15
+
16
+ export type XrefResolver = (uri: string, term: string) => string;
17
+ export type BibResolver = (refId: string, title: string) => string;
18
+ export type FigResolver = (figId: string) => string;
19
+ export type CiteResolver = (key: string, label: string | null) => string;
20
+ export type ConceptRefResolver = (conceptId: string, term: string) => string;
21
+ export type UrnRefResolver = (uri: string, term: string) => string;
22
+
23
+ export interface RenderOptions {
24
+ xrefResolver?: XrefResolver;
25
+ bibResolver?: BibResolver;
26
+ figResolver?: FigResolver;
27
+ conceptRefResolver?: ConceptRefResolver;
28
+ citeResolver?: CiteResolver;
29
+ urnRefResolver?: UrnRefResolver;
30
+ }
31
+
32
+ // ── Math placeholders ────────────────────────────────────────────────────
33
+
34
+ function replaceBracketed(text: string, prefix: string, handler: (content: string, bold: boolean) => string): string {
35
+ let result = '';
36
+ let i = 0;
37
+ const boldPrefix = '*' + prefix;
38
+ while (i < text.length) {
39
+ if (text.startsWith(boldPrefix + '[', i)) {
40
+ i += boldPrefix.length + 1;
41
+ let j = i;
42
+ let d = 1;
43
+ while (j < text.length && d > 0) {
44
+ if (text[j] === '[') d++;
45
+ else if (text[j] === ']') d--;
46
+ j++;
47
+ }
48
+ const content = text.slice(i, j - 1);
49
+ let end = j;
50
+ if (end < text.length && text[end] === '*') end++;
51
+ result += handler(content, true);
52
+ i = end;
53
+ } else if (text.startsWith(prefix + '[', i)) {
54
+ i += prefix.length + 1;
55
+ let j = i;
56
+ let d = 1;
57
+ while (j < text.length && d > 0) {
58
+ if (text[j] === '[') d++;
59
+ else if (text[j] === ']') d--;
60
+ j++;
61
+ }
62
+ const content = text.slice(i, j - 1);
63
+ result += handler(content, false);
64
+ i = j;
65
+ } else {
66
+ result += text[i];
67
+ i++;
68
+ }
69
+ }
70
+ return result;
71
+ }
72
+
73
+ function mathPlaceholder(expr: string, format: string, bold: boolean): string {
74
+ return `<span class="math-pending${bold ? ' math-bold' : ''}" data-expr="${escapeAttr(expr)}" data-format="${format}">${escapeAttr(expr)}</span>`;
75
+ }
76
+
77
+ // ── Block transforms ─────────────────────────────────────────────────────
78
+
79
+ function convertAsciiDocTables(text: string): string {
80
+ return text.replace(/\n?\|===\n([\s\S]*?)\n\|===/g, (_: string, body: string) => {
81
+ const rows: string[] = body.split('\n').filter((line: string) => line.trim() !== '');
82
+ if (!rows.length) return '';
83
+
84
+ const parsedRows: string[][] = rows.map((row: string) => {
85
+ const cellText = row.replace(/^\s*\|/, '').trim();
86
+ const cells = cellText.split(/\s*\|\s*/).map((c: string) => c.trim()).filter((c: string) => c !== '');
87
+ return cells;
88
+ }).filter((r: string[]) => r.length > 0);
89
+
90
+ if (!parsedRows.length) return '';
91
+
92
+ const maxCols = Math.max(...parsedRows.map((r: string[]) => r.length));
93
+ const normalized = parsedRows.map((r: string[]) => {
94
+ while (r.length < maxCols) r.push('');
95
+ return r;
96
+ });
97
+
98
+ const thead = normalized[0].map((c: string) => `<th>${escapeHtml(c)}</th>`).join('');
99
+ const tbody = normalized.slice(1).map((r: string[]) =>
100
+ `<tr>${r.map((c: string) => `<td>${escapeHtml(c)}</td>`).join('')}</tr>`
101
+ ).join('');
102
+
103
+ return `\n<table class="concept-table"><thead><tr>${thead}</tr></thead><tbody>${tbody}</tbody></table>`;
104
+ });
105
+ }
106
+
107
+ function convertLists(text: string): string {
108
+ let result = text.replace(/(?:^|\n)((?:[ \t]*\* [^\n]+)(?:\n[ \t]*\* [^\n]+)*)/g, (_, block) => {
109
+ if (/^\*stem:\[/.test(block.trimStart())) return _;
110
+ const items: string[] = [];
111
+ const re = /[ \t]*\* ([^\n]+)/g;
112
+ let m;
113
+ while ((m = re.exec(block)) !== null) {
114
+ items.push(m[1].trim());
115
+ }
116
+ if (!items.length) return _;
117
+ const lis = items.map(item => `<li>${item}</li>`).join('');
118
+ return `\n<ul class="concept-list">${lis}</ul>`;
119
+ });
120
+
121
+ result = result.replace(/(?:^|\n)((?:[ \t]*\d+[).][ \t]+[^\n]+)(?:\n[ \t]*\d+[).][ \t]+[^\n]+)*)/g, (_, block) => {
122
+ const items: string[] = [];
123
+ const re = /[ \t]*\d+[).][ \t]+([^\n]+)/g;
124
+ let m;
125
+ while ((m = re.exec(block)) !== null) {
126
+ items.push(m[1].trim());
127
+ }
128
+ if (!items.length) return _;
129
+ const lis = items.map(item => `<li>${item}</li>`).join('');
130
+ return `\n<ol class="concept-list concept-list-ordered">${lis}</ol>`;
131
+ });
132
+
133
+ return result;
134
+ }
135
+
136
+ // ── Inline reference resolution ──────────────────────────────────────────
137
+
138
+ function resolveBibRefs(text: string, opts: RenderOptions): string {
139
+ return text.replace(/<<([^,>]+),([^>]+)>>/g, (_, refId, title) => {
140
+ if (opts.bibResolver) {
141
+ return opts.bibResolver(refId.trim(), title.trim());
142
+ }
143
+ return `<span class="bib-ref">${escapeHtml(title.trim())}</span>`;
144
+ });
145
+ }
146
+
147
+ function resolveFigRefs(text: string, opts: RenderOptions): string {
148
+ return text.replace(/<<(fig_[^>]+)>>/g, (_, figId) => {
149
+ if (opts.figResolver) {
150
+ return opts.figResolver(figId.trim());
151
+ }
152
+ return `<span class="fig-ref">${escapeHtml(figId.trim())}</span>`;
153
+ });
154
+ }
155
+
156
+ function resolveUrnRefs(text: string, opts: RenderOptions): string {
157
+ // Double-brace URN refs: {{urn:...,term}} or {{urn:...,term,display}}
158
+ // Note: glossarist ≥ 0.3.7 parseMention handles these as 'urn-ref', but we
159
+ // keep this handler for when parseMention returns 'unresolved' (glossarist < 0.3.7)
160
+ let result = text.replace(/\{\{(urn:[^,}]+),([^,}]+)(?:,([^}]+))?\}\}/g, (_, uri, term, display) => {
161
+ const t = (display || term).trim();
162
+ if (opts.xrefResolver) {
163
+ return opts.xrefResolver(uri, t);
164
+ }
165
+ return t;
166
+ });
167
+
168
+ // Single-brace URN refs: {urn:...,term} or {urn:...,term,display}
169
+ result = result.replace(/\{(urn:[^,}]+),([^,}]+)(?:,([^}]+))?\}/g, (_, uri, term, display) => {
170
+ const t = (display || term).trim();
171
+ if (opts.xrefResolver) {
172
+ return opts.xrefResolver(uri, t);
173
+ }
174
+ return t;
175
+ });
176
+
177
+ return result;
178
+ }
179
+
180
+ function resolveMentions(text: string, opts: RenderOptions): string {
181
+ // Single-pass {{...}} mention dispatcher via parseMention (SSOT)
182
+ return text.replace(/\{\{([^{}]+?)\}\}/g, (_orig, body) => {
183
+ const parsed = parseMention(body);
184
+
185
+ // cite:key[,render term] — bibliography citation
186
+ if (parsed.kind === 'cite-ref') {
187
+ const key = parsed.key!;
188
+ const label = parsed.label ?? null;
189
+ if (opts.citeResolver) return opts.citeResolver(key, label);
190
+ return `<span class="bib-ref">${escapeHtml(label ?? key)}</span>`;
191
+ }
192
+
193
+ // urn:...[,render term] — URN cross-reference (glossarist ≥ 0.3.7)
194
+ const anyParsed = parsed as Record<string, unknown>;
195
+ if ((anyParsed.kind as string) === 'urn-ref') {
196
+ const uri = anyParsed.uri as string;
197
+ const label = (anyParsed.label as string) ?? uri;
198
+ if (opts.urnRefResolver) return opts.urnRefResolver(uri, label);
199
+ if (opts.xrefResolver) return opts.xrefResolver(uri, label);
200
+ return escapeHtml(label);
201
+ }
202
+
203
+ // numeric_id[,render term] — local concept ID
204
+ if (parsed.kind === 'numeric') {
205
+ const id = parsed.id!;
206
+ const label = parsed.label;
207
+ if (label && opts.conceptRefResolver) {
208
+ return opts.conceptRefResolver(id, label);
209
+ }
210
+ return `<span class="gl-mention">${escapeHtml(id)}</span>`;
211
+ }
212
+
213
+ // designation[,render term] — designation matching (glossarist ≥ 0.3.7)
214
+ if ((anyParsed.kind as string) === 'designation') {
215
+ const designation = anyParsed.id as string;
216
+ const label = (anyParsed.label as string) ?? designation;
217
+ if (opts.conceptRefResolver) {
218
+ return opts.conceptRefResolver(designation, label);
219
+ }
220
+ return escapeHtml(label);
221
+ }
222
+
223
+ // Fallback for unresolved: handle two-arg form or render as plain text
224
+ // This handles cases where parseMention doesn't recognize the kind
225
+ // (e.g. glossarist < 0.3.7 before urn-ref/designation kinds were added)
226
+ const commaIdx = body.indexOf(',');
227
+ if (commaIdx > 0) {
228
+ const id = body.slice(0, commaIdx).trim();
229
+ const display = body.slice(commaIdx + 1).trim();
230
+ if (opts.conceptRefResolver) return opts.conceptRefResolver(id, display);
231
+ return escapeHtml(display);
232
+ }
233
+
234
+ return `<span class="gl-mention">${escapeHtml(body.trim())}</span>`;
235
+ });
236
+ }
237
+
238
+ // ── Public API ───────────────────────────────────────────────────────────
239
+
240
+ /**
241
+ * Render Glossarist inline content notation to HTML.
242
+ *
243
+ * Pipeline stages (in order):
244
+ * 1. Math placeholders (stem:, latexmath:)
245
+ * 2. AsciiDoc tables
246
+ * 3. Bullet and numbered lists
247
+ * 4. Text formatting (bold, italic, subscript)
248
+ * 5. Bibliography cross-references (<<ref,title>>)
249
+ * 6. Figure references (<<fig_...>>)
250
+ * 7. Single-brace URN inline references ({urn:...})
251
+ * 8. Mention dispatcher via parseMention (cite-ref, urn-ref, numeric, designation)
252
+ */
253
+ export function renderContent(text: string, xrefResolverOrOpts?: XrefResolver | RenderOptions): string {
254
+ if (!text) return '';
255
+ let result = text;
256
+
257
+ const opts: RenderOptions = typeof xrefResolverOrOpts === 'function'
258
+ ? { xrefResolver: xrefResolverOrOpts }
259
+ : (xrefResolverOrOpts ?? {});
260
+
261
+ // Stage 1: Math expressions → placeholders for v-math directive
262
+ result = replaceBracketed(result, 'stem:', (expr, bold) => mathPlaceholder(expr, 'asciimath', bold));
263
+ result = replaceBracketed(result, 'latexmath:', (expr, bold) => mathPlaceholder(expr, 'latex', bold));
264
+
265
+ // Stage 2: Block structures
266
+ result = convertAsciiDocTables(result);
267
+ result = convertLists(result);
268
+
269
+ // Stage 3: Inline formatting
270
+ result = result.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
271
+ result = result.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, '<em>$1</em>');
272
+ result = result.replace(/~([^~]+)~/g, '<sub>$1</sub>');
273
+
274
+ // Stage 4: Reference resolution
275
+ result = resolveBibRefs(result, opts);
276
+ result = resolveFigRefs(result, opts);
277
+ result = resolveUrnRefs(result, opts);
278
+
279
+ // Stage 5: Mention dispatcher (parseMention SSOT)
280
+ result = resolveMentions(result, opts);
281
+
282
+ return result;
283
+ }
284
+
285
+ /**
286
+ * Strip all inline notation to produce plain text.
287
+ * Used for search indexing, previews, and accessibility.
288
+ */
289
+ export function cleanContent(text: string): string {
290
+ if (!text) return '';
291
+ let result = text
292
+ .replace(/<[^>]+>/g, '')
293
+ .replace(/\*\*([^*]+)\*\*/g, '$1')
294
+ .replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, '$1')
295
+ .replace(/~([^~]+)~/g, '_$1')
296
+ .replace(/\n[ \t]*\* /g, '; ')
297
+ .replace(/<<([^,>]+),([^>]+)>>/g, '$2')
298
+ .replace(/<<(fig_[^>]+)>>/g, '$1')
299
+ // URN refs — show render term (second part for two-arg, third part for three-arg)
300
+ .replace(/\{\{urn:[^,}]+,([^,}]+),([^}]+)\}\}/g, '$1') // three-arg: {{urn:...,term,display}} → term
301
+ .replace(/\{\{urn:[^,}]+(?:,([^}]+))?\}\}/g, (_, label) => label ? label.trim() : '') // two-arg or bare
302
+ .replace(/\{urn:[^,}]+,([^,}]+)(?:,[^}]+)?\}/g, '$1')
303
+ // Cite refs — show render term (or empty if bare)
304
+ .replace(/\{\{cite:[^,}]+(?:,([^}]+))?\}\}/g, (_, label) => label ? label.trim() : '')
305
+ // Two-arg mentions: show render term (second part)
306
+ .replace(/\{\{([^,}]+),\s*([^}]+)\}\}/g, '$2')
307
+ // One-arg mentions: show the identifier
308
+ .replace(/\{\{([^,}]+)\}\}/g, '$1')
309
+ .replace(/(?:\*?)stem:\[([^\]]*)\]/g, '$1')
310
+ .replace(/(?:\*?)latexmath:\[([^\]]*)\]/g, '$1');
311
+ return result;
312
+ }
@@ -1,3 +1,5 @@
1
+ import { escapeHtml } from './escape';
2
+
1
3
  const INLINE_PATTERNS: [RegExp, (m: RegExpMatchArray) => string][] = [
2
4
  [/\*\*(.+?)\*\*/g, m => `<strong>${m[1]}</strong>`],
3
5
  [/(?<!\*)\*([^*]+?)\*(?!\*)/g, m => `<em>${m[1]}</em>`],
@@ -118,5 +120,3 @@ export function renderMarkdown(input: string): string {
118
120
 
119
121
  return blocks.join('\n');
120
122
  }
121
-
122
- import { escapeHtml } from './escape';
package/src/utils/math.ts DELETED
@@ -1,189 +0,0 @@
1
- import { escapeHtml, escapeAttr } from './escape';
2
-
3
- export type XrefResolver = (uri: string, term: string) => string;
4
- export type BibResolver = (refId: string, title: string) => string;
5
- export type FigResolver = (figId: string) => string;
6
-
7
- export type ConceptRefResolver = (conceptId: string, term: string) => string;
8
-
9
- export interface RenderOptions {
10
- xrefResolver?: XrefResolver;
11
- bibResolver?: BibResolver;
12
- figResolver?: FigResolver;
13
- conceptRefResolver?: ConceptRefResolver;
14
- }
15
-
16
- function replaceBracketed(text: string, prefix: string, handler: (content: string, bold: boolean) => string): string {
17
- let result = '';
18
- let i = 0;
19
- const boldPrefix = '*' + prefix;
20
- while (i < text.length) {
21
- if (text.startsWith(boldPrefix + '[', i)) {
22
- i += boldPrefix.length + 1;
23
- let j = i;
24
- let d = 1;
25
- while (j < text.length && d > 0) {
26
- if (text[j] === '[') d++;
27
- else if (text[j] === ']') d--;
28
- j++;
29
- }
30
- const content = text.slice(i, j - 1);
31
- let end = j;
32
- if (end < text.length && text[end] === '*') end++;
33
- result += handler(content, true);
34
- i = end;
35
- } else if (text.startsWith(prefix + '[', i)) {
36
- i += prefix.length + 1;
37
- let j = i;
38
- let d = 1;
39
- while (j < text.length && d > 0) {
40
- if (text[j] === '[') d++;
41
- else if (text[j] === ']') d--;
42
- j++;
43
- }
44
- const content = text.slice(i, j - 1);
45
- result += handler(content, false);
46
- i = j;
47
- } else {
48
- result += text[i];
49
- i++;
50
- }
51
- }
52
- return result;
53
- }
54
-
55
- function mathPlaceholder(expr: string, format: string, bold: boolean): string {
56
- return `<span class="math-pending${bold ? ' math-bold' : ''}" data-expr="${escapeAttr(expr)}" data-format="${format}">${escapeAttr(expr)}</span>`;
57
- }
58
-
59
- function convertAsciiDocTables(text: string): string {
60
- return text.replace(/\n?\|===\n([\s\S]*?)\n\|===/g, (_: string, body: string) => {
61
- const rows: string[] = body.split('\n').filter((line: string) => line.trim() !== '');
62
- if (!rows.length) return '';
63
-
64
- const parsedRows: string[][] = rows.map((row: string) => {
65
- const cellText = row.replace(/^\s*\|/, '').trim();
66
- const cells = cellText.split(/\s*\|\s*/).map((c: string) => c.trim()).filter((c: string) => c !== '');
67
- return cells;
68
- }).filter((r: string[]) => r.length > 0);
69
-
70
- if (!parsedRows.length) return '';
71
-
72
- const maxCols = Math.max(...parsedRows.map((r: string[]) => r.length));
73
- const normalized = parsedRows.map((r: string[]) => {
74
- while (r.length < maxCols) r.push('');
75
- return r;
76
- });
77
-
78
- const thead = normalized[0].map((c: string) => `<th>${escapeHtml(c)}</th>`).join('');
79
- const tbody = normalized.slice(1).map((r: string[]) =>
80
- `<tr>${r.map((c: string) => `<td>${escapeHtml(c)}</td>`).join('')}</tr>`
81
- ).join('');
82
-
83
- return `\n<table class="concept-table"><thead><tr>${thead}</tr></thead><tbody>${tbody}</tbody></table>`;
84
- });
85
- }
86
-
87
- function convertLists(text: string): string {
88
- let result = text.replace(/(?:^|\n)((?:[ \t]*\* [^\n]+)(?:\n[ \t]*\* [^\n]+)*)/g, (_, block) => {
89
- if (/^\*stem:\[/.test(block.trimStart())) return _;
90
- const items: string[] = [];
91
- const re = /[ \t]*\* ([^\n]+)/g;
92
- let m;
93
- while ((m = re.exec(block)) !== null) {
94
- items.push(m[1].trim());
95
- }
96
- if (!items.length) return _;
97
- const lis = items.map(item => `<li>${item}</li>`).join('');
98
- return `\n<ul class="concept-list">${lis}</ul>`;
99
- });
100
-
101
- // Numbered lists: 1) item or 1. item
102
- result = result.replace(/(?:^|\n)((?:[ \t]*\d+[).][ \t]+[^\n]+)(?:\n[ \t]*\d+[).][ \t]+[^\n]+)*)/g, (_, block) => {
103
- const items: string[] = [];
104
- const re = /[ \t]*\d+[).][ \t]+([^\n]+)/g;
105
- let m;
106
- while ((m = re.exec(block)) !== null) {
107
- items.push(m[1].trim());
108
- }
109
- if (!items.length) return _;
110
- const lis = items.map(item => `<li>${item}</li>`).join('');
111
- return `\n<ol class="concept-list concept-list-ordered">${lis}</ol>`;
112
- });
113
-
114
- return result;
115
- }
116
-
117
- export function renderMath(text: string, xrefResolverOrOpts?: XrefResolver | RenderOptions): string {
118
- if (!text) return '';
119
- let result = text;
120
-
121
- const opts: RenderOptions = typeof xrefResolverOrOpts === 'function'
122
- ? { xrefResolver: xrefResolverOrOpts }
123
- : (xrefResolverOrOpts ?? {});
124
-
125
- // Math expressions: output placeholders for v-math directive to upgrade
126
- result = replaceBracketed(result, 'stem:', (expr, bold) => mathPlaceholder(expr, 'asciimath', bold));
127
- result = replaceBracketed(result, 'latexmath:', (expr, bold) => mathPlaceholder(expr, 'latex', bold));
128
-
129
- result = convertAsciiDocTables(result);
130
- result = convertLists(result);
131
- result = result.replace(/\*([^*]+)\*/g, '<em>$1</em>');
132
- result = result.replace(/~([^~]+)~/g, '<sub>$1</sub>');
133
-
134
- result = result.replace(/<<([^,>]+),([^>]+)>>/g, (_, refId, title) => {
135
- if (opts.bibResolver) {
136
- return opts.bibResolver(refId.trim(), title.trim());
137
- }
138
- return `<span class="bib-ref">${escapeHtml(title.trim())}</span>`;
139
- });
140
-
141
- result = result.replace(/<<(fig_[^>]+)>>/g, (_, figId) => {
142
- if (opts.figResolver) {
143
- return opts.figResolver(figId.trim());
144
- }
145
- return `<span class="fig-ref">${escapeHtml(figId.trim())}</span>`;
146
- });
147
-
148
- result = result.replace(/\{\{(urn:[^,}]+),([^,}]+)(?:,([^}]+))?\}\}/g, (_, uri, term, display) => {
149
- const t = (display || term).trim();
150
- if (opts.xrefResolver) {
151
- return opts.xrefResolver(uri, t);
152
- }
153
- return t;
154
- });
155
-
156
- result = result.replace(/\{(urn:[^,}]+),([^,}]+)(?:,([^}]+))?\}/g, (_, uri, term, display) => {
157
- const t = (display || term).trim();
158
- if (opts.xrefResolver) {
159
- return opts.xrefResolver(uri, t);
160
- }
161
- return t;
162
- });
163
-
164
- result = result.replace(/\{\{([^,}]+),\s*([^}]+)\}\}/g, (_, term, id) => {
165
- if (opts.conceptRefResolver) {
166
- return opts.conceptRefResolver(id.trim(), term.trim());
167
- }
168
- return term.trim();
169
- });
170
-
171
- return result;
172
- }
173
-
174
- export function cleanContent(text: string): string {
175
- if (!text) return '';
176
- let result = text
177
- .replace(/<[^>]+>/g, '')
178
- .replace(/\*([^*]+)\*/g, '$1')
179
- .replace(/~([^~]+)~/g, '_$1')
180
- .replace(/\n[ \t]*\* /g, '; ')
181
- .replace(/<<([^,>]+),([^>]+)>>/g, '$2')
182
- .replace(/<<(fig_[^>]+)>>/g, '$1')
183
- .replace(/\{\{urn:[^,}]+,([^,}]+)(?:,[^}]+)?\}\}/g, '$1')
184
- .replace(/\{urn:[^,}]+,([^,}]+)(?:,[^}]+)?\}/g, '$1')
185
- .replace(/\{\{([^,}]+)(?:,\s*[^}]+)?\}\}/g, '$1')
186
- .replace(/(?:\*?)stem:\[([^\]]*)\]/g, '$1')
187
- .replace(/(?:\*?)latexmath:\[([^\]]*)\]/g, '$1');
188
- return result;
189
- }