@cyanheads/cern-inspire-mcp-server 0.1.1 → 0.1.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.
Files changed (71) hide show
  1. package/AGENTS.md +11 -8
  2. package/CLAUDE.md +11 -8
  3. package/README.md +60 -33
  4. package/changelog/0.1.x/0.1.2.md +29 -0
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/mcp-server/resources/definitions/index.d.ts +13 -5
  8. package/dist/mcp-server/resources/definitions/index.d.ts.map +1 -1
  9. package/dist/mcp-server/resources/definitions/inspire-literature.resource.d.ts +13 -5
  10. package/dist/mcp-server/resources/definitions/inspire-literature.resource.d.ts.map +1 -1
  11. package/dist/mcp-server/resources/definitions/inspire-literature.resource.js +1 -1
  12. package/dist/mcp-server/resources/definitions/inspire-literature.resource.js.map +1 -1
  13. package/dist/mcp-server/tools/definitions/export-citations.tool.d.ts +10 -1
  14. package/dist/mcp-server/tools/definitions/export-citations.tool.d.ts.map +1 -1
  15. package/dist/mcp-server/tools/definitions/export-citations.tool.js +83 -19
  16. package/dist/mcp-server/tools/definitions/export-citations.tool.js.map +1 -1
  17. package/dist/mcp-server/tools/definitions/get-citation-summary.tool.d.ts +6 -2
  18. package/dist/mcp-server/tools/definitions/get-citation-summary.tool.d.ts.map +1 -1
  19. package/dist/mcp-server/tools/definitions/get-citation-summary.tool.js +71 -19
  20. package/dist/mcp-server/tools/definitions/get-citation-summary.tool.js.map +1 -1
  21. package/dist/mcp-server/tools/definitions/get-paper.tool.d.ts +26 -10
  22. package/dist/mcp-server/tools/definitions/get-paper.tool.d.ts.map +1 -1
  23. package/dist/mcp-server/tools/definitions/get-paper.tool.js +107 -40
  24. package/dist/mcp-server/tools/definitions/get-paper.tool.js.map +1 -1
  25. package/dist/mcp-server/tools/definitions/index.d.ts +30 -9
  26. package/dist/mcp-server/tools/definitions/index.d.ts.map +1 -1
  27. package/dist/mcp-server/tools/definitions/list-reference.tool.d.ts.map +1 -1
  28. package/dist/mcp-server/tools/definitions/list-reference.tool.js +27 -8
  29. package/dist/mcp-server/tools/definitions/list-reference.tool.js.map +1 -1
  30. package/dist/mcp-server/tools/definitions/search-authors.tool.d.ts.map +1 -1
  31. package/dist/mcp-server/tools/definitions/search-authors.tool.js +9 -9
  32. package/dist/mcp-server/tools/definitions/search-authors.tool.js.map +1 -1
  33. package/dist/mcp-server/tools/definitions/search-experiments.tool.d.ts.map +1 -1
  34. package/dist/mcp-server/tools/definitions/search-experiments.tool.js +6 -3
  35. package/dist/mcp-server/tools/definitions/search-experiments.tool.js.map +1 -1
  36. package/dist/mcp-server/tools/definitions/search-hepdata.tool.d.ts +2 -2
  37. package/dist/mcp-server/tools/definitions/search-hepdata.tool.d.ts.map +1 -1
  38. package/dist/mcp-server/tools/definitions/search-hepdata.tool.js +14 -12
  39. package/dist/mcp-server/tools/definitions/search-hepdata.tool.js.map +1 -1
  40. package/dist/mcp-server/tools/definitions/search-literature.tool.d.ts +1 -1
  41. package/dist/mcp-server/tools/definitions/search-literature.tool.d.ts.map +1 -1
  42. package/dist/mcp-server/tools/definitions/search-literature.tool.js +22 -11
  43. package/dist/mcp-server/tools/definitions/search-literature.tool.js.map +1 -1
  44. package/dist/mcp-server/tools/inputs.d.ts +17 -5
  45. package/dist/mcp-server/tools/inputs.d.ts.map +1 -1
  46. package/dist/mcp-server/tools/inputs.js +29 -10
  47. package/dist/mcp-server/tools/inputs.js.map +1 -1
  48. package/dist/services/inspire/identifiers.d.ts +17 -6
  49. package/dist/services/inspire/identifiers.d.ts.map +1 -1
  50. package/dist/services/inspire/identifiers.js +28 -7
  51. package/dist/services/inspire/identifiers.js.map +1 -1
  52. package/dist/services/inspire/inspire-service.d.ts +78 -18
  53. package/dist/services/inspire/inspire-service.d.ts.map +1 -1
  54. package/dist/services/inspire/inspire-service.js +293 -52
  55. package/dist/services/inspire/inspire-service.js.map +1 -1
  56. package/dist/services/inspire/markup-to-text.d.ts +32 -0
  57. package/dist/services/inspire/markup-to-text.d.ts.map +1 -0
  58. package/dist/services/inspire/markup-to-text.js +783 -0
  59. package/dist/services/inspire/markup-to-text.js.map +1 -0
  60. package/dist/services/inspire/normalize.d.ts +23 -6
  61. package/dist/services/inspire/normalize.d.ts.map +1 -1
  62. package/dist/services/inspire/normalize.js +94 -21
  63. package/dist/services/inspire/normalize.js.map +1 -1
  64. package/dist/services/inspire/types.d.ts +73 -5
  65. package/dist/services/inspire/types.d.ts.map +1 -1
  66. package/dist/utils/render.d.ts +34 -5
  67. package/dist/utils/render.d.ts.map +1 -1
  68. package/dist/utils/render.js +69 -16
  69. package/dist/utils/render.js.map +1 -1
  70. package/package.json +1 -1
  71. package/server.json +9 -3
@@ -0,0 +1,783 @@
1
+ /**
2
+ * @fileoverview Publisher markup in INSPIRE titles and abstracts → text.
3
+ * INSPIRE stores many publisher titles and abstracts as HTML, JATS, or MathML,
4
+ * and some entity-escaped. `markupToText()` keeps their text: scripts read
5
+ * `_x` / `^{xy}`, prescripts precede their base, fractions read `a/b`, and
6
+ * paragraphs are separated by a blank line. The whitespace removed markup leaves
7
+ * collapses to one space, except that a formula's trimmed inner edge keeps one
8
+ * only beside a letter or digit, and an HTML script binds to its neighbours
9
+ * across a line break. A tag counts only when its local name (after any prefix)
10
+ * is in a fixed HTML/JATS/MathML vocabulary and, unless it is void or
11
+ * self-closing, its closing tag follows; a void element never pairs. Every
12
+ * other `<` and `>` is prose, since comparisons and LaTeX are common in this
13
+ * text. Entities decode once, after tags, so decoded text is never read as
14
+ * markup; `containsMarkup()` tells whether that decode produced some. Every
15
+ * pass — the scan, the tree build, the render, and the whitespace pass — is
16
+ * iterative and linear in the input, unclosed and deeply nested markup included.
17
+ * @module services/inspire/markup-to-text
18
+ */
19
+ const group = (kind, names) => names.split(' ').map((name) => [name, kind]);
20
+ /** The recognized vocabulary, by local name. Names are case-sensitive, as in XML. */
21
+ const KINDS = new Map([
22
+ ...group('block', 'p div sec title list list-item'),
23
+ ...group('inline', 'b em i small span strong u bold italic sc monospace roman sans-serif underline overline styled-content named-content ext-link uri xref mixed-citation element-citation person-group string-name surname given-names article-title source volume issue year fpage lpage page-range'),
24
+ ...group('formula', 'math inline-formula'),
25
+ ...group('display', 'disp-formula'),
26
+ ...group('label', 'label'),
27
+ ...group('row', 'mrow mstyle merror mpadded menclose mtd'),
28
+ ...group('first', 'semantics maction alternatives'),
29
+ ...group('tex', 'tex-math'),
30
+ ...group('token', 'mi mn mo ms'),
31
+ ...group('mtext', 'mtext'),
32
+ ...group('drop', 'annotation annotation-xml mphantom pub-id issn'),
33
+ ...group('empty', 'none mprescripts malignmark maligngroup mglyph graphic inline-graphic'),
34
+ ...group('br', 'br'),
35
+ ...group('etal', 'etal'),
36
+ ...group('mspace', 'mspace'),
37
+ ...group('sub', 'sub'),
38
+ ...group('sup', 'sup'),
39
+ ...group('msub', 'msub munder'),
40
+ ...group('msup', 'msup'),
41
+ ...group('msubsup', 'msubsup munderover'),
42
+ ...group('mover', 'mover'),
43
+ ...group('mmultiscripts', 'mmultiscripts'),
44
+ ...group('mfrac', 'mfrac'),
45
+ ...group('msqrt', 'msqrt'),
46
+ ...group('mroot', 'mroot'),
47
+ ...group('mfenced', 'mfenced'),
48
+ ...group('mtable', 'mtable'),
49
+ ...group('mtr', 'mtr mlabeledtr'),
50
+ ]);
51
+ /**
52
+ * Elements read as empty. A start tag stands alone and is never paired, so what
53
+ * follows it is its sibling, and an end tag closes nothing: `</br>`, which
54
+ * sloppy HTML writes for a line break, is one, and any other is dropped.
55
+ */
56
+ const VOID = new Set('br none mprescripts mspace malignmark maligngroup mglyph graphic inline-graphic'.split(' '));
57
+ /** JATS `etal` may hold its own words, so it pairs like any element; left open, it is still markup. */
58
+ const OPTIONAL_CLOSE = 'etal';
59
+ /**
60
+ * MathML elements other than tokens: whitespace-only text directly inside one
61
+ * is layout, not content, and is dropped.
62
+ */
63
+ const MATHML = new Set('math mrow mstyle merror mpadded menclose mphantom mtd semantics maction msub munder msup msubsup munderover mover mmultiscripts mfrac msqrt mroot mfenced mtable mtr mlabeledtr'.split(' '));
64
+ /** IEEE closes `<italic>`, `<bold>`, and `<sc>` with `</i>`, `</b>`, and `</small>`. */
65
+ const CLOSE_ALIASES = new Map([
66
+ ['i', 'italic'],
67
+ ['b', 'bold'],
68
+ ['small', 'sc'],
69
+ ]);
70
+ /**
71
+ * Accents over a single character written in their spacing forms, as the
72
+ * combining mark that puts them there. An accent written as a combining mark
73
+ * (one `\p{Mn}` character) is its own mark.
74
+ */
75
+ const ACCENTS = new Map([
76
+ ['¯', '\u{304}'],
77
+ ['‾', '\u{304}'],
78
+ ['˜', '\u{303}'],
79
+ ['~', '\u{303}'],
80
+ ['^', '\u{302}'],
81
+ ['ˆ', '\u{302}'],
82
+ ['˙', '\u{307}'],
83
+ ['¨', '\u{308}'],
84
+ ['ˇ', '\u{30C}'],
85
+ ['→', '\u{20D7}'],
86
+ ]);
87
+ const COMBINING_MARK = /^\p{Mn}$/u;
88
+ /** Named entities that decode; any other name stays as written. */
89
+ const NAMED_ENTITIES = new Map([
90
+ ['amp', '&'],
91
+ ['lt', '<'],
92
+ ['gt', '>'],
93
+ ['quot', '"'],
94
+ ['apos', "'"],
95
+ ['nbsp', '\u{A0}'],
96
+ ['thinsp', '\u{2009}'],
97
+ ['hyphen', '\u{2010}'],
98
+ ['ndash', '–'],
99
+ ['mdash', '—'],
100
+ ['minus', '−'],
101
+ ['plusmn', '±'],
102
+ ['times', '×'],
103
+ ]);
104
+ // ─── Entities ───────────────────────────────────────────────────────────────
105
+ const ENTITY = /&(?:#(\d{1,7})|#[xX]([\dA-Fa-f]{1,6})|([A-Za-z][A-Za-z\d]{0,31}));/g;
106
+ /**
107
+ * True for a code point a numeric reference may produce: not a control
108
+ * character, a surrogate, out of range, or a Unicode tag character (which the
109
+ * service drops from every decoded string and must not come back here).
110
+ */
111
+ const decodable = (code) => code > 0x1f &&
112
+ !(code >= 0x7f && code <= 0x9f) &&
113
+ !(code >= 0xd800 && code <= 0xdfff) &&
114
+ !(code >= 0xe0000 && code <= 0xe007f) &&
115
+ code <= 0x10ffff;
116
+ /** One pass over `text`: the output is never scanned again, so `&amp;lt;` becomes `&lt;`. */
117
+ function decodeEntities(text) {
118
+ if (!text.includes('&'))
119
+ return text;
120
+ return text.replace(ENTITY, (whole, dec, hex, name) => {
121
+ if (name !== undefined)
122
+ return NAMED_ENTITIES.get(name) ?? whole;
123
+ const code = dec === undefined ? Number.parseInt(hex ?? '', 16) : Number.parseInt(dec, 10);
124
+ return decodable(code) ? String.fromCodePoint(code) : whole;
125
+ });
126
+ }
127
+ // ─── Scan ───────────────────────────────────────────────────────────────────
128
+ /**
129
+ * Start and end tags. Attribute values must be quoted and nothing in a tag may
130
+ * be `<`, so an attempt never reads past the next `<` and the scan stays linear.
131
+ */
132
+ const START_TAG = /<([A-Za-z_][\w.-]*)(?::([A-Za-z_][\w.-]*))?(?:[ \t\n\r]+[A-Za-z_][\w.:-]*[ \t\n\r]*=[ \t\n\r]*(?:"[^"<]*"|'[^'<]*'))*[ \t\n\r]*(\/?)>/y;
133
+ const END_TAG = /<\/([A-Za-z_][\w.-]*)(?::([A-Za-z_][\w.-]*))?[ \t\n\r]*>/y;
134
+ const PROCESSING_INSTRUCTION = /<\?[A-Za-z_][\w.:-]*(?:[ \t\n\r][^<]*?)?\?>/y;
135
+ const ATTRIBUTE = /([A-Za-z_][\w.:-]*)[ \t\n\r]*=[ \t\n\r]*(?:"([^"<]*)"|'([^'<]*)')/g;
136
+ /** True when a scan recognized anything other than text. */
137
+ const hasMarkup = (tokens) => tokens.some((token) => token.kind !== 'raw');
138
+ /** The local name of a qualified name. */
139
+ const localOf = (prefix, second) => second ?? prefix;
140
+ /**
141
+ * Tokenizes `s` and pairs its tags on a stack. A per-name count of open tags
142
+ * answers "is there anything to close?" in O(1); a matched end tag pops the
143
+ * tags opened after its partner, which become text (an `etal` stands alone), so
144
+ * each tag is pushed and popped once. Void elements never go on the stack.
145
+ * Comments and processing instructions are dropped; a comment or CDATA end is
146
+ * found with one cached `indexOf`, so repeated unterminated openers cost linear
147
+ * time in total.
148
+ */
149
+ function scan(s) {
150
+ const tokens = [];
151
+ const open = [];
152
+ const openCount = new Map();
153
+ let textStart = 0;
154
+ let i = 0;
155
+ let commentEnd = -2;
156
+ let cdataEnd = -2;
157
+ const adjustCount = (name, by) => openCount.set(name, (openCount.get(name) ?? 0) + by);
158
+ const unwind = (index) => {
159
+ const token = tokens[index];
160
+ adjustCount(token.name, -1);
161
+ if (token.local === OPTIONAL_CLOSE)
162
+ token.kind = 'empty';
163
+ else
164
+ tokens[index] = { kind: 'raw', start: token.start, end: token.end };
165
+ };
166
+ const emit = (token, at, next) => {
167
+ if (at > textStart)
168
+ tokens.push({ kind: 'raw', start: textStart, end: at });
169
+ tokens.push(token);
170
+ textStart = next;
171
+ };
172
+ const skip = (at, next) => emit({ kind: 'gap', start: at, end: next }, at, next);
173
+ for (;;) {
174
+ const lt = s.indexOf('<', i);
175
+ if (lt === -1)
176
+ break;
177
+ i = lt + 1;
178
+ const next = s.charCodeAt(lt + 1);
179
+ if (next === 0x21 /* ! */) {
180
+ if (s.startsWith('<!--', lt)) {
181
+ if (commentEnd !== -1 && commentEnd < lt + 4)
182
+ commentEnd = s.indexOf('-->', lt + 4);
183
+ if (commentEnd !== -1) {
184
+ skip(lt, commentEnd + 3);
185
+ i = commentEnd + 3;
186
+ }
187
+ }
188
+ else if (s.startsWith('<![CDATA[', lt)) {
189
+ if (cdataEnd !== -1 && cdataEnd < lt + 9)
190
+ cdataEnd = s.indexOf(']]>', lt + 9);
191
+ if (cdataEnd !== -1) {
192
+ emit({ kind: 'cdata', start: lt + 9, end: cdataEnd }, lt, cdataEnd + 3);
193
+ i = cdataEnd + 3;
194
+ }
195
+ }
196
+ continue;
197
+ }
198
+ if (next === 0x3f /* ? */) {
199
+ PROCESSING_INSTRUCTION.lastIndex = lt;
200
+ if (PROCESSING_INSTRUCTION.test(s)) {
201
+ skip(lt, PROCESSING_INSTRUCTION.lastIndex);
202
+ i = PROCESSING_INSTRUCTION.lastIndex;
203
+ }
204
+ continue;
205
+ }
206
+ if (next === 0x2f /* / */) {
207
+ END_TAG.lastIndex = lt;
208
+ const match = END_TAG.exec(s);
209
+ if (!match)
210
+ continue;
211
+ const local = localOf(match[1], match[2]);
212
+ if (!KINDS.has(local))
213
+ continue;
214
+ const name = match[2] === undefined ? local : `${match[1]}:${local}`;
215
+ if (VOID.has(local)) {
216
+ const end = END_TAG.lastIndex;
217
+ if (local === 'br')
218
+ emit({ kind: 'empty', start: lt, end, name, local, mate: -1 }, lt, end);
219
+ else
220
+ skip(lt, end);
221
+ i = end;
222
+ continue;
223
+ }
224
+ const alias = CLOSE_ALIASES.get(local);
225
+ const aliasName = alias === undefined ? undefined : match[2] === undefined ? alias : `${match[1]}:${alias}`;
226
+ const waiting = (openCount.get(name) ?? 0) +
227
+ (aliasName === undefined ? 0 : (openCount.get(aliasName) ?? 0));
228
+ if (waiting === 0)
229
+ continue; // nothing to close: the end tag is prose
230
+ const endIndex = tokens.length + (lt > textStart ? 1 : 0);
231
+ for (;;) {
232
+ const index = open.pop();
233
+ const token = tokens[index];
234
+ if (token.name === name || token.name === aliasName) {
235
+ adjustCount(token.name, -1);
236
+ token.mate = endIndex;
237
+ emit({ kind: 'end', start: lt, end: END_TAG.lastIndex, name, local, mate: index }, lt, END_TAG.lastIndex);
238
+ break;
239
+ }
240
+ unwind(index);
241
+ }
242
+ i = END_TAG.lastIndex;
243
+ continue;
244
+ }
245
+ START_TAG.lastIndex = lt;
246
+ const match = START_TAG.exec(s);
247
+ if (!match)
248
+ continue;
249
+ const local = localOf(match[1], match[2]);
250
+ if (!KINDS.has(local))
251
+ continue;
252
+ const name = match[2] === undefined ? local : `${match[1]}:${local}`;
253
+ const standsAlone = match[3] === '/' || VOID.has(local);
254
+ const token = {
255
+ kind: standsAlone ? 'empty' : 'start',
256
+ start: lt,
257
+ end: START_TAG.lastIndex,
258
+ name,
259
+ local,
260
+ mate: -1,
261
+ };
262
+ emit(token, lt, START_TAG.lastIndex);
263
+ if (!standsAlone) {
264
+ open.push(tokens.length - 1);
265
+ adjustCount(name, 1);
266
+ }
267
+ i = START_TAG.lastIndex;
268
+ }
269
+ if (s.length > textStart)
270
+ tokens.push({ kind: 'raw', start: textStart, end: s.length });
271
+ for (let k = open.length - 1; k >= 0; k--)
272
+ unwind(open[k]);
273
+ return tokens;
274
+ }
275
+ const XML_SPACE = /[ \t\n\r]+/g;
276
+ const LINE_BREAK = /[\n\r]/;
277
+ const isXmlSpace = (code) => code === 0x20 || code === 0x09 || code === 0x0a || code === 0x0d;
278
+ function isBlank(text) {
279
+ for (let k = 0; k < text.length; k++)
280
+ if (!isXmlSpace(text.charCodeAt(k)))
281
+ return false;
282
+ return true;
283
+ }
284
+ function element(kind, local, tag = { start: 0, end: 0 }) {
285
+ return { kind, local, tag, children: [], inList: false, text: '' };
286
+ }
287
+ /**
288
+ * The element tree, built in one pass. Text runs decode their entities on their
289
+ * own, never across a removed tag; `tex`, token, and `mtext` elements collect
290
+ * the text of their whole span; `drop` elements are skipped with their span,
291
+ * leaving a gap, as removed comments and processing instructions do.
292
+ */
293
+ function buildTree(s, tokens) {
294
+ const root = element('root', '');
295
+ const stack = [root];
296
+ /** Decoded text of the raw run starting at `from`, and the index after it. */
297
+ const rawRun = (from) => {
298
+ const first = tokens[from];
299
+ let end = first.end;
300
+ let next = from + 1;
301
+ for (let t = tokens[next]; t?.kind === 'raw' && t.start === end; t = tokens[++next])
302
+ end = t.end;
303
+ return [decodeEntities(s.slice(first.start, end)), next];
304
+ };
305
+ const collect = (from, to) => {
306
+ const parts = [];
307
+ let k = from;
308
+ while (k < to) {
309
+ const token = tokens[k];
310
+ if (token.kind === 'raw') {
311
+ const [text, next] = rawRun(k);
312
+ parts.push(text);
313
+ k = next;
314
+ }
315
+ else {
316
+ if (token.kind === 'cdata')
317
+ parts.push(s.slice(token.start, token.end));
318
+ k++;
319
+ }
320
+ }
321
+ return parts.join('');
322
+ };
323
+ const addText = (parent, text) => {
324
+ if (text === '' || (MATHML.has(parent.local) && isBlank(text)))
325
+ return;
326
+ parent.children.push(text);
327
+ };
328
+ /**
329
+ * A `drop` node where something was removed, so the whitespace around it
330
+ * collapses; none among MathML's positional children, whose whitespace is
331
+ * already layout, or where `first` picks its first element child.
332
+ */
333
+ const addGap = (parent) => {
334
+ if (!MATHML.has(parent.local) && parent.kind !== 'first') {
335
+ parent.children.push(element('drop', ''));
336
+ }
337
+ };
338
+ let k = 0;
339
+ while (k < tokens.length) {
340
+ const token = tokens[k];
341
+ const top = stack[stack.length - 1];
342
+ if (token.kind === 'raw') {
343
+ const [text, next] = rawRun(k);
344
+ addText(top, text);
345
+ k = next;
346
+ continue;
347
+ }
348
+ if (token.kind === 'cdata') {
349
+ addText(top, s.slice(token.start, token.end));
350
+ k++;
351
+ continue;
352
+ }
353
+ if (token.kind === 'gap') {
354
+ addGap(top);
355
+ k++;
356
+ continue;
357
+ }
358
+ if (token.kind === 'end') {
359
+ stack.pop();
360
+ k++;
361
+ continue;
362
+ }
363
+ const kind = KINDS.get(token.local);
364
+ const last = token.kind === 'start' ? token.mate : k;
365
+ if (kind === 'drop') {
366
+ addGap(top);
367
+ k = last + 1;
368
+ continue;
369
+ }
370
+ const node = element(kind, token.local, { start: token.start, end: token.end });
371
+ node.inList = token.local === 'p' && top.local === 'list-item';
372
+ top.children.push(node);
373
+ if (kind === 'tex' || kind === 'token' || kind === 'mtext') {
374
+ node.text = collect(k + 1, last);
375
+ k = last + 1;
376
+ continue;
377
+ }
378
+ if (token.kind === 'start')
379
+ stack.push(node);
380
+ k++;
381
+ }
382
+ return root;
383
+ }
384
+ // ─── Render ─────────────────────────────────────────────────────────────────
385
+ /**
386
+ * Whitespace markers between rendered pieces. `SOFT` collapses the whitespace
387
+ * run it sits in to one space; `PARA` and `LINE` make the run a blank line or a
388
+ * line break; `TRIM_OPEN` drops the whitespace after it in its run and
389
+ * `TRIM_CLOSE` the whitespace before it. `EDGE_OPEN` and `EDGE_CLOSE` trim a
390
+ * formula's edges the same way, except that trimmed whitespace leaves one space
391
+ * when the text beyond the edge is a letter or digit: IEEE writes the space that
392
+ * separates a formula from the next word inside `</tex-math>`. `BIND` sits
393
+ * beside an HTML script: a run holding a line break is pretty-printing layout
394
+ * and is dropped, so the script joins its base (`Nb\n <sub>3</sub>\n Sn` reads
395
+ * `Nb_3Sn`), and any other run collapses to one space, since a prescript often
396
+ * follows a written space (`of <sup>12</sup>C`). A run of whitespace with no
397
+ * marker in it is kept as written.
398
+ */
399
+ const SOFT = 0;
400
+ const PARA = 1;
401
+ const LINE = 2;
402
+ const TRIM_OPEN = 3;
403
+ const TRIM_CLOSE = 4;
404
+ const EDGE_OPEN = 5;
405
+ const EDGE_CLOSE = 6;
406
+ const BIND = 7;
407
+ /** Code points in `text` once XML whitespace is trimmed from both ends, capped at 2. */
408
+ function trimmedLength(text) {
409
+ let start = 0;
410
+ let end = text.length;
411
+ while (start < end && isXmlSpace(text.charCodeAt(start)))
412
+ start++;
413
+ while (end > start && isXmlSpace(text.charCodeAt(end - 1)))
414
+ end--;
415
+ const units = end - start;
416
+ if (units === 0)
417
+ return 0;
418
+ if (units === 1)
419
+ return 1;
420
+ const high = text.charCodeAt(start);
421
+ return units === 2 && high >= 0xd800 && high <= 0xdbff ? 1 : 2;
422
+ }
423
+ const collapse = (text) => text.replace(XML_SPACE, ' ');
424
+ function trimCollapse(text) {
425
+ const collapsed = collapse(text);
426
+ const start = collapsed.startsWith(' ') ? 1 : 0;
427
+ const end = collapsed.endsWith(' ') ? collapsed.length - 1 : collapsed.length;
428
+ return start >= end ? '' : collapsed.slice(start, end);
429
+ }
430
+ const firstElement = (children) => children.find((child) => typeof child !== 'string') ?? children[0];
431
+ /**
432
+ * How many code points `item` renders to, capped at 2 — whether a script or a
433
+ * fraction operand is one character. Walks rows and inline wrappers only; any
434
+ * other structure counts as 2 at once, so each node is walked by at most one
435
+ * enclosing operand.
436
+ */
437
+ function measure(item) {
438
+ let total = 0;
439
+ const pending = [item];
440
+ while (pending.length > 0 && total < 2) {
441
+ const next = pending.pop();
442
+ if (typeof next === 'string') {
443
+ total += trimmedLength(next);
444
+ continue;
445
+ }
446
+ switch (next.kind) {
447
+ case 'root':
448
+ case 'block':
449
+ case 'inline':
450
+ case 'formula':
451
+ case 'display':
452
+ case 'label':
453
+ case 'row':
454
+ for (const child of next.children)
455
+ pending.push(child);
456
+ break;
457
+ case 'first': {
458
+ const first = firstElement(next.children);
459
+ if (first !== undefined)
460
+ pending.push(first);
461
+ break;
462
+ }
463
+ case 'tex':
464
+ case 'token':
465
+ case 'mtext':
466
+ total += trimmedLength(next.text);
467
+ break;
468
+ case 'empty':
469
+ case 'br':
470
+ case 'mspace':
471
+ case 'drop':
472
+ break;
473
+ default:
474
+ total = 2;
475
+ }
476
+ }
477
+ return total >= 2 ? 2 : total;
478
+ }
479
+ /** The one character a one-character `item` renders to. */
480
+ function singleText(item) {
481
+ const pending = [item];
482
+ while (pending.length > 0) {
483
+ const next = pending.pop();
484
+ if (typeof next === 'string') {
485
+ if (trimmedLength(next) > 0)
486
+ return trimCollapse(next);
487
+ }
488
+ else if (next.kind === 'tex' || next.kind === 'token' || next.kind === 'mtext') {
489
+ if (trimmedLength(next.text) > 0)
490
+ return trimCollapse(next.text);
491
+ }
492
+ else if (next.kind === 'first') {
493
+ const first = firstElement(next.children);
494
+ if (first !== undefined)
495
+ pending.push(first);
496
+ }
497
+ else {
498
+ for (const child of next.children)
499
+ pending.push(child);
500
+ }
501
+ }
502
+ return '';
503
+ }
504
+ /** A list of children as one operand. */
505
+ const asGroup = (children) => ({ ...element('row', 'mrow'), children });
506
+ /** `_x` or `_{xy}` (likewise `^`); nothing for an empty or missing operand. */
507
+ function script(symbol, operand) {
508
+ if (operand === undefined)
509
+ return [];
510
+ const size = measure(operand);
511
+ if (size === 0)
512
+ return [];
513
+ return size === 1
514
+ ? [symbol, TRIM_OPEN, operand, TRIM_CLOSE]
515
+ : [symbol, '{', TRIM_OPEN, operand, TRIM_CLOSE, '}'];
516
+ }
517
+ /** An HTML script between `BIND` markers; nothing when the script is empty. */
518
+ const bound = (items) => (items.length === 0 ? items : [BIND, ...items, BIND]);
519
+ /** An operand, parenthesized when it is longer than one character. */
520
+ function operand(item) {
521
+ if (item === undefined)
522
+ return [];
523
+ const size = measure(item);
524
+ if (size === 0)
525
+ return [];
526
+ return size === 1 ? [TRIM_OPEN, item, TRIM_CLOSE] : ['(', TRIM_OPEN, item, TRIM_CLOSE, ')'];
527
+ }
528
+ const base = (item) => (item === undefined ? [] : [item]);
529
+ /** A start tag's attribute, decoded. */
530
+ function attribute(s, node, name) {
531
+ for (const match of s.slice(node.tag.start, node.tag.end).matchAll(ATTRIBUTE)) {
532
+ if (match[1] === name)
533
+ return decodeEntities(match[2] ?? match[3] ?? '');
534
+ }
535
+ return;
536
+ }
537
+ /** True when an `mspace` has a positive width (a named width unless it is a negative one). */
538
+ function hasWidth(s, node) {
539
+ const width = attribute(s, node, 'width')?.trim();
540
+ if (width === undefined || width === '')
541
+ return false;
542
+ const value = Number.parseFloat(width);
543
+ return Number.isNaN(value) ? !width.startsWith('negative') : value > 0;
544
+ }
545
+ /** `open child sep child … close`, with MathML's defaults `(`, `)`, and `,`. */
546
+ function fenced(s, node) {
547
+ const separators = [...(attribute(s, node, 'separators') ?? ',').replace(XML_SPACE, '')];
548
+ const items = [attribute(s, node, 'open') ?? '('];
549
+ for (const [index, child] of node.children.entries()) {
550
+ if (index > 0 && separators.length > 0) {
551
+ items.push(separators[Math.min(index - 1, separators.length - 1)]);
552
+ }
553
+ items.push(child);
554
+ }
555
+ items.push(attribute(s, node, 'close') ?? ')');
556
+ return items;
557
+ }
558
+ /** Prescripts (after `<mprescripts/>`) first, then the base, then the postscripts. */
559
+ function multiscripts(children) {
560
+ const [first, ...rest] = children;
561
+ const pre = [];
562
+ const post = [];
563
+ let target = post;
564
+ let position = 0;
565
+ for (const child of rest) {
566
+ if (typeof child !== 'string' && child.local === 'mprescripts') {
567
+ target = pre;
568
+ position = 0;
569
+ continue;
570
+ }
571
+ for (const item of script(position % 2 === 0 ? '_' : '^', child))
572
+ target.push(item);
573
+ position++;
574
+ }
575
+ return [...pre, ...base(first), ...post];
576
+ }
577
+ /** `x̄` for an accent over one character, as a combining mark; else `undefined`. */
578
+ function accented(over, under) {
579
+ if (over === undefined || under === undefined)
580
+ return;
581
+ if (measure(over) !== 1 || measure(under) !== 1)
582
+ return;
583
+ const accent = singleText(over);
584
+ const mark = ACCENTS.get(accent) ?? (COMBINING_MARK.test(accent) ? accent : undefined);
585
+ return mark === undefined ? undefined : [`${singleText(under)}${mark}`];
586
+ }
587
+ /** The items one element renders to, in order. */
588
+ function expand(s, node) {
589
+ const children = node.children;
590
+ const [first, second, third] = children;
591
+ switch (node.kind) {
592
+ case 'root':
593
+ case 'row':
594
+ return children;
595
+ case 'block':
596
+ return node.inList ? [SOFT, ...children, SOFT] : [PARA, ...children, PARA];
597
+ case 'inline':
598
+ return [SOFT, ...children, SOFT];
599
+ case 'formula':
600
+ return [SOFT, EDGE_OPEN, ...children, EDGE_CLOSE, SOFT];
601
+ case 'display':
602
+ return [LINE, EDGE_OPEN, ...children, EDGE_CLOSE, LINE];
603
+ case 'label':
604
+ return [SOFT, ...children, ' ', SOFT];
605
+ case 'first':
606
+ return base(firstElement(children));
607
+ case 'tex':
608
+ return [SOFT, EDGE_OPEN, node.text, EDGE_CLOSE, SOFT];
609
+ case 'token':
610
+ return [trimCollapse(node.text)];
611
+ case 'mtext':
612
+ return [SOFT, collapse(node.text), SOFT];
613
+ case 'drop':
614
+ return [SOFT];
615
+ case 'empty':
616
+ return [];
617
+ case 'br':
618
+ return [LINE];
619
+ case 'etal':
620
+ return [SOFT, ...(children.length > 0 ? children : [' et al.']), SOFT];
621
+ case 'mspace':
622
+ return hasWidth(s, node) ? [SOFT, ' ', SOFT] : [];
623
+ case 'sub':
624
+ return bound(script('_', asGroup(children)));
625
+ case 'sup':
626
+ return bound(script('^', asGroup(children)));
627
+ case 'msub':
628
+ return [...base(first), ...script('_', second)];
629
+ case 'msup':
630
+ return [...base(first), ...script('^', second)];
631
+ case 'msubsup':
632
+ return [...base(first), ...script('_', second), ...script('^', third)];
633
+ case 'mover':
634
+ return accented(second, first) ?? [...base(first), ...script('^', second)];
635
+ case 'mmultiscripts':
636
+ return multiscripts(children);
637
+ case 'mfrac':
638
+ return [...operand(first), '/', ...operand(second)];
639
+ case 'msqrt':
640
+ return ['√', ...operand(asGroup(children))];
641
+ case 'mroot':
642
+ return [
643
+ '√',
644
+ ...(second === undefined ? [] : ['[', TRIM_OPEN, second, TRIM_CLOSE, ']']),
645
+ ...operand(first),
646
+ ];
647
+ case 'mfenced':
648
+ return fenced(s, node);
649
+ case 'mtable':
650
+ return children.flatMap((row, index) => (index === 0 ? [row] : ['; ', row]));
651
+ case 'mtr':
652
+ return children.flatMap((cell, index) => index === 0 ? [SOFT, cell, SOFT] : [' ', SOFT, cell, SOFT]);
653
+ }
654
+ }
655
+ /** A letter or digit ending the text before a run or starting the text after it (each tested on two code units). */
656
+ const WORD_END = /[\p{L}\p{N}]$/u;
657
+ const WORD_START = /^[\p{L}\p{N}]/u;
658
+ /**
659
+ * The text of one whitespace run between the rendered text `before` and `after`
660
+ * it (`''` at either end of the string): kept as written with no marker in it,
661
+ * else resolved.
662
+ */
663
+ function resolveRun(run, before, after) {
664
+ let marked = false;
665
+ let firstOpen = -1;
666
+ let lastClose = -1;
667
+ let firstEdgeOpen = -1;
668
+ let lastEdgeClose = -1;
669
+ let para = false;
670
+ let line = false;
671
+ let bind = false;
672
+ for (const [index, item] of run.entries()) {
673
+ if (typeof item === 'string')
674
+ continue;
675
+ marked = true;
676
+ if (item === TRIM_OPEN || item === EDGE_OPEN) {
677
+ if (firstOpen === -1)
678
+ firstOpen = index;
679
+ if (item === EDGE_OPEN && firstEdgeOpen === -1)
680
+ firstEdgeOpen = index;
681
+ }
682
+ else if (item === TRIM_CLOSE || item === EDGE_CLOSE) {
683
+ lastClose = index;
684
+ if (item === EDGE_CLOSE)
685
+ lastEdgeClose = index;
686
+ }
687
+ else if (item === PARA)
688
+ para = true;
689
+ else if (item === LINE)
690
+ line = true;
691
+ else if (item === BIND)
692
+ bind = true;
693
+ }
694
+ if (!marked)
695
+ return run.join('');
696
+ if (before === '' || after === '')
697
+ return '';
698
+ if (para)
699
+ return '\n\n';
700
+ if (line)
701
+ return '\n';
702
+ const spaceWhere = (keep) => run.some((item, index) => typeof item === 'string' && keep(index));
703
+ if (bind && run.some((item) => typeof item === 'string' && LINE_BREAK.test(item)))
704
+ return '';
705
+ if (spaceWhere((index) => !(firstOpen !== -1 && index > firstOpen) && index > lastClose)) {
706
+ return ' ';
707
+ }
708
+ const closedOnWord = lastEdgeClose !== -1 &&
709
+ WORD_START.test(after.slice(0, 2)) &&
710
+ spaceWhere((index) => index < lastEdgeClose);
711
+ const openedOnWord = firstEdgeOpen !== -1 &&
712
+ WORD_END.test(before.slice(-2)) &&
713
+ spaceWhere((index) => index > firstEdgeOpen);
714
+ return closedOnWord || openedOnWord ? ' ' : '';
715
+ }
716
+ /** Rendered pieces joined, each whitespace run resolved once; runs at either end of marked text are dropped. */
717
+ function joinPieces(pieces) {
718
+ const out = [];
719
+ let run = [];
720
+ let before = '';
721
+ for (const piece of pieces) {
722
+ if (typeof piece === 'number') {
723
+ run.push(piece);
724
+ continue;
725
+ }
726
+ let start = 0;
727
+ let end = piece.length;
728
+ while (start < end && isXmlSpace(piece.charCodeAt(start)))
729
+ start++;
730
+ if (start === end) {
731
+ if (end > 0)
732
+ run.push(piece);
733
+ continue;
734
+ }
735
+ while (isXmlSpace(piece.charCodeAt(end - 1)))
736
+ end--;
737
+ if (start > 0)
738
+ run.push(piece.slice(0, start));
739
+ const text = piece.slice(start, end);
740
+ out.push(resolveRun(run, before, text), text);
741
+ before = text;
742
+ run = end < piece.length ? [piece.slice(end)] : [];
743
+ }
744
+ out.push(resolveRun(run, before, ''));
745
+ return out.join('');
746
+ }
747
+ function render(s, root) {
748
+ const pieces = [];
749
+ const work = [root];
750
+ while (work.length > 0) {
751
+ const item = work.pop();
752
+ if (typeof item !== 'object') {
753
+ pieces.push(item);
754
+ continue;
755
+ }
756
+ const items = expand(s, item);
757
+ for (let k = items.length - 1; k >= 0; k--)
758
+ work.push(items[k]);
759
+ }
760
+ return joinPieces(pieces);
761
+ }
762
+ // ─── Entry point ────────────────────────────────────────────────────────────
763
+ /**
764
+ * `text` with recognized publisher markup converted to text and entities
765
+ * decoded. A string with no recognized markup and no entity comes back
766
+ * unchanged, byte for byte.
767
+ */
768
+ export function markupToText(text) {
769
+ if (!text.includes('<'))
770
+ return decodeEntities(text);
771
+ const tokens = scan(text);
772
+ return hasMarkup(tokens) ? render(text, buildTree(text, tokens)) : decodeEntities(text);
773
+ }
774
+ /**
775
+ * True when `text` holds markup `markupToText()` would convert: a recognized tag
776
+ * that pairs or stands alone, a comment, a processing instruction, or a CDATA
777
+ * section. Converted text answers true when its one entity decode produced
778
+ * markup: a CDS abstract stored as `&lt;p&gt;…&lt;/p&gt;` converts to `<p>…</p>`.
779
+ */
780
+ export function containsMarkup(text) {
781
+ return text.includes('<') && hasMarkup(scan(text));
782
+ }
783
+ //# sourceMappingURL=markup-to-text.js.map