scholarly-parse 0.1.0 → 0.2.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.
- package/CHANGELOG.md +8 -0
- package/README.md +6 -4
- package/changelog/0.1.x/0.1.1.md +41 -0
- package/changelog/0.2.x/0.2.0.md +43 -0
- package/dist/detect.d.ts +3 -2
- package/dist/detect.d.ts.map +1 -1
- package/dist/detect.js +40 -18
- package/dist/detect.js.map +1 -1
- package/dist/formats/html/blocks.d.ts +3 -2
- package/dist/formats/html/blocks.d.ts.map +1 -1
- package/dist/formats/html/blocks.js +128 -123
- package/dist/formats/html/blocks.js.map +1 -1
- package/dist/formats/html/context.d.ts +20 -0
- package/dist/formats/html/context.d.ts.map +1 -1
- package/dist/formats/html/context.js +4 -0
- package/dist/formats/html/context.js.map +1 -1
- package/dist/formats/html/inline.d.ts +7 -6
- package/dist/formats/html/inline.d.ts.map +1 -1
- package/dist/formats/html/inline.js +98 -38
- package/dist/formats/html/inline.js.map +1 -1
- package/dist/formats/html/metadata.d.ts +10 -5
- package/dist/formats/html/metadata.d.ts.map +1 -1
- package/dist/formats/html/metadata.js +41 -16
- package/dist/formats/html/metadata.js.map +1 -1
- package/dist/formats/html/parse.d.ts.map +1 -1
- package/dist/formats/html/parse.js +209 -59
- package/dist/formats/html/parse.js.map +1 -1
- package/dist/formats/html/references.d.ts.map +1 -1
- package/dist/formats/html/references.js +31 -17
- package/dist/formats/html/references.js.map +1 -1
- package/dist/formats/html/subtree.d.ts +112 -0
- package/dist/formats/html/subtree.d.ts.map +1 -0
- package/dist/formats/html/subtree.js +316 -0
- package/dist/formats/html/subtree.js.map +1 -0
- package/dist/formats/jats/blocks.d.ts +4 -1
- package/dist/formats/jats/blocks.d.ts.map +1 -1
- package/dist/formats/jats/blocks.js +129 -38
- package/dist/formats/jats/blocks.js.map +1 -1
- package/dist/formats/jats/context.d.ts +4 -1
- package/dist/formats/jats/context.d.ts.map +1 -1
- package/dist/formats/jats/fragment.d.ts.map +1 -1
- package/dist/formats/jats/fragment.js +2 -0
- package/dist/formats/jats/fragment.js.map +1 -1
- package/dist/formats/jats/front.d.ts.map +1 -1
- package/dist/formats/jats/front.js +12 -5
- package/dist/formats/jats/front.js.map +1 -1
- package/dist/formats/jats/inline.d.ts.map +1 -1
- package/dist/formats/jats/inline.js +23 -4
- package/dist/formats/jats/inline.js.map +1 -1
- package/dist/formats/jats/parse.d.ts.map +1 -1
- package/dist/formats/jats/parse.js +16 -4
- package/dist/formats/jats/parse.js.map +1 -1
- package/dist/formats/jats/references.d.ts.map +1 -1
- package/dist/formats/jats/references.js +95 -24
- package/dist/formats/jats/references.js.map +1 -1
- package/dist/formats/jats/tables.d.ts.map +1 -1
- package/dist/formats/jats/tables.js +14 -9
- package/dist/formats/jats/tables.js.map +1 -1
- package/dist/formats/latexml/blocks.d.ts.map +1 -1
- package/dist/formats/latexml/blocks.js +58 -40
- package/dist/formats/latexml/blocks.js.map +1 -1
- package/dist/formats/latexml/context.d.ts +7 -1
- package/dist/formats/latexml/context.d.ts.map +1 -1
- package/dist/formats/latexml/context.js.map +1 -1
- package/dist/formats/latexml/inline.d.ts +4 -3
- package/dist/formats/latexml/inline.d.ts.map +1 -1
- package/dist/formats/latexml/inline.js +10 -11
- package/dist/formats/latexml/inline.js.map +1 -1
- package/dist/formats/latexml/parse.d.ts.map +1 -1
- package/dist/formats/latexml/parse.js +30 -17
- package/dist/formats/latexml/parse.js.map +1 -1
- package/dist/formats/pdf/blocks.d.ts +2 -1
- package/dist/formats/pdf/blocks.d.ts.map +1 -1
- package/dist/formats/pdf/blocks.js +43 -16
- package/dist/formats/pdf/blocks.js.map +1 -1
- package/dist/formats/pdf/context.d.ts +15 -0
- package/dist/formats/pdf/context.d.ts.map +1 -0
- package/dist/formats/pdf/context.js +2 -0
- package/dist/formats/pdf/context.js.map +1 -0
- package/dist/formats/pdf/inline.d.ts +16 -0
- package/dist/formats/pdf/inline.d.ts.map +1 -1
- package/dist/formats/pdf/inline.js +45 -3
- package/dist/formats/pdf/inline.js.map +1 -1
- package/dist/formats/pdf/layout.d.ts.map +1 -1
- package/dist/formats/pdf/layout.js +39 -10
- package/dist/formats/pdf/layout.js.map +1 -1
- package/dist/formats/pdf/lines.d.ts +7 -1
- package/dist/formats/pdf/lines.d.ts.map +1 -1
- package/dist/formats/pdf/lines.js +17 -1
- package/dist/formats/pdf/lines.js.map +1 -1
- package/dist/formats/pdf/load.d.ts +40 -3
- package/dist/formats/pdf/load.d.ts.map +1 -1
- package/dist/formats/pdf/load.js +132 -46
- package/dist/formats/pdf/load.js.map +1 -1
- package/dist/formats/pdf/parse.d.ts +21 -1
- package/dist/formats/pdf/parse.d.ts.map +1 -1
- package/dist/formats/pdf/parse.js +60 -16
- package/dist/formats/pdf/parse.js.map +1 -1
- package/dist/formats/pdf/references.d.ts.map +1 -1
- package/dist/formats/pdf/references.js +38 -10
- package/dist/formats/pdf/references.js.map +1 -1
- package/dist/formats/pdf/structure.d.ts +4 -6
- package/dist/formats/pdf/structure.d.ts.map +1 -1
- package/dist/formats/pdf/structure.js +91 -35
- package/dist/formats/pdf/structure.js.map +1 -1
- package/dist/formats/tei/inline.d.ts +7 -1
- package/dist/formats/tei/inline.d.ts.map +1 -1
- package/dist/formats/tei/inline.js +20 -11
- package/dist/formats/tei/inline.js.map +1 -1
- package/dist/formats/tei/parse.d.ts.map +1 -1
- package/dist/formats/tei/parse.js +198 -50
- package/dist/formats/tei/parse.js.map +1 -1
- package/dist/html/dom.d.ts +34 -7
- package/dist/html/dom.d.ts.map +1 -1
- package/dist/html/dom.js +459 -1
- package/dist/html/dom.js.map +1 -1
- package/dist/html/interstitial.d.ts.map +1 -1
- package/dist/html/interstitial.js +47 -6
- package/dist/html/interstitial.js.map +1 -1
- package/dist/html/math.d.ts +7 -0
- package/dist/html/math.d.ts.map +1 -0
- package/dist/html/math.js +34 -0
- package/dist/html/math.js.map +1 -0
- package/dist/html/tables.d.ts +13 -8
- package/dist/html/tables.d.ts.map +1 -1
- package/dist/html/tables.js +7 -6
- package/dist/html/tables.js.map +1 -1
- package/dist/model/document.d.ts +5 -0
- package/dist/model/document.d.ts.map +1 -1
- package/dist/model/doi.d.ts +40 -0
- package/dist/model/doi.d.ts.map +1 -0
- package/dist/model/doi.js +98 -0
- package/dist/model/doi.js.map +1 -0
- package/dist/model/extremes.d.ts +7 -3
- package/dist/model/extremes.d.ts.map +1 -1
- package/dist/model/extremes.js +10 -3
- package/dist/model/extremes.js.map +1 -1
- package/dist/model/input.d.ts +2 -0
- package/dist/model/input.d.ts.map +1 -1
- package/dist/model/input.js +2 -1
- package/dist/model/input.js.map +1 -1
- package/dist/model/limits.d.ts +14 -0
- package/dist/model/limits.d.ts.map +1 -0
- package/dist/model/limits.js +14 -0
- package/dist/model/limits.js.map +1 -0
- package/dist/model/section-ids.d.ts +23 -2
- package/dist/model/section-ids.d.ts.map +1 -1
- package/dist/model/section-ids.js +38 -13
- package/dist/model/section-ids.js.map +1 -1
- package/dist/model/table-grid.d.ts +44 -7
- package/dist/model/table-grid.d.ts.map +1 -1
- package/dist/model/table-grid.js +59 -11
- package/dist/model/table-grid.js.map +1 -1
- package/dist/model/trailing.d.ts +15 -0
- package/dist/model/trailing.d.ts.map +1 -0
- package/dist/model/trailing.js +22 -0
- package/dist/model/trailing.js.map +1 -0
- package/dist/parse.d.ts +8 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +5 -2
- package/dist/parse.js.map +1 -1
- package/dist/render/escape.d.ts +5 -0
- package/dist/render/escape.d.ts.map +1 -1
- package/dist/render/escape.js +9 -2
- package/dist/render/escape.js.map +1 -1
- package/dist/render/inline.d.ts +68 -7
- package/dist/render/inline.d.ts.map +1 -1
- package/dist/render/inline.js +249 -13
- package/dist/render/inline.js.map +1 -1
- package/dist/render/markdown.d.ts +41 -7
- package/dist/render/markdown.d.ts.map +1 -1
- package/dist/render/markdown.js +222 -44
- package/dist/render/markdown.js.map +1 -1
- package/dist/render/sections.d.ts +11 -2
- package/dist/render/sections.d.ts.map +1 -1
- package/dist/render/sections.js +25 -6
- package/dist/render/sections.js.map +1 -1
- package/dist/render/text.d.ts +17 -4
- package/dist/render/text.d.ts.map +1 -1
- package/dist/render/text.js +235 -49
- package/dist/render/text.js.map +1 -1
- package/dist/xml/mathml.d.ts +16 -12
- package/dist/xml/mathml.d.ts.map +1 -1
- package/dist/xml/mathml.js +110 -40
- package/dist/xml/mathml.js.map +1 -1
- package/dist/xml/ordered.d.ts +0 -6
- package/dist/xml/ordered.d.ts.map +1 -1
- package/dist/xml/ordered.js +37 -8
- package/dist/xml/ordered.js.map +1 -1
- package/package.json +1 -1
package/dist/render/text.js
CHANGED
|
@@ -1,49 +1,231 @@
|
|
|
1
|
+
import { autolinkAt, codeSpanAt, codeSpanCloser, ESCAPABLE, pairBrackets, unescapedDollar, } from './inline.js';
|
|
1
2
|
import { toMarkdown } from './markdown.js';
|
|
2
|
-
/**
|
|
3
|
-
export function
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
/** Render a document as plain text. Options are the same as for Markdown. */
|
|
4
|
+
export function toText(document, options = {}) {
|
|
5
|
+
const lines = [];
|
|
6
|
+
let block;
|
|
7
|
+
// A list marker from a dropped opening fence, for the block's first line.
|
|
8
|
+
let marker = '';
|
|
9
|
+
for (const line of toMarkdown(document, options).split('\n')) {
|
|
10
|
+
if (block) {
|
|
11
|
+
const { kept, rest } = insideBlock(line, block.parts);
|
|
12
|
+
if (rest === block.fence) {
|
|
13
|
+
block = undefined;
|
|
14
|
+
if (marker)
|
|
15
|
+
lines.push(marker.trimEnd());
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
lines.push((marker || kept) + rest);
|
|
19
|
+
}
|
|
20
|
+
marker = '';
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const { kept, parts, rest } = containerPrefix(line);
|
|
24
|
+
const fence = CODE_FENCE.exec(rest)?.[1] ?? (rest === '$$' ? rest : undefined);
|
|
25
|
+
if (fence) {
|
|
26
|
+
block = { fence, parts };
|
|
27
|
+
marker = /\S/.test(kept) ? kept : '';
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
lines.push(TABLE_RULE.test(rest) ? '' : kept + stripInline(rest.replace(HEADING, '')));
|
|
31
|
+
}
|
|
32
|
+
return lines.join('\n').replace(/\n{3,}/g, '\n\n');
|
|
12
33
|
}
|
|
34
|
+
/** A quote marker, a run of indentation, or a list marker (`- `, `12. `), where a line's prefix goes on. */
|
|
35
|
+
const PREFIX_PART = /(>) ?|( +)|(?:-|\d{1,9}\.) /y;
|
|
36
|
+
/** A code block's opening fence after its prefix, with its language; the closing one is the same backtick run. */
|
|
37
|
+
const CODE_FENCE = /^(`{3,})[\w+#.-]{0,32}$/;
|
|
38
|
+
/** A table's rule row. */
|
|
39
|
+
const TABLE_RULE = /^\|\s*-{3}/;
|
|
40
|
+
const HEADING = /^#{1,6}\s+/;
|
|
13
41
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* reads it.
|
|
42
|
+
* A line's container prefix — the quotes, list items, and indentation it sits in — and
|
|
43
|
+
* the rest of the line. Plain text keeps the indentation and list markers and drops the
|
|
44
|
+
* quote markers, at every depth.
|
|
18
45
|
*/
|
|
19
|
-
function
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
46
|
+
function containerPrefix(line) {
|
|
47
|
+
const parts = [];
|
|
48
|
+
const part = new RegExp(PREFIX_PART);
|
|
49
|
+
let kept = '';
|
|
50
|
+
let at = 0;
|
|
51
|
+
for (let match = part.exec(line); match; match = part.exec(line)) {
|
|
52
|
+
at = part.lastIndex;
|
|
53
|
+
if (match[1]) {
|
|
54
|
+
parts.push('quote');
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
parts.push(match[0].length);
|
|
58
|
+
kept += match[0];
|
|
59
|
+
}
|
|
30
60
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
61
|
+
return { kept, parts, rest: line.slice(at) };
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A line inside a code or display-math block, read against the container prefix of its
|
|
65
|
+
* opening fence: the quote markers of those containers come off, their indentation is
|
|
66
|
+
* kept, and the rest is the block's text as written. A list item's marker stands as
|
|
67
|
+
* indentation on every line after its first.
|
|
68
|
+
*/
|
|
69
|
+
function insideBlock(line, parts) {
|
|
70
|
+
let kept = '';
|
|
71
|
+
let at = 0;
|
|
72
|
+
for (const part of parts) {
|
|
73
|
+
if (part === 'quote') {
|
|
74
|
+
if (line.charAt(at) !== '>')
|
|
75
|
+
break;
|
|
76
|
+
at += line.charAt(at + 1) === ' ' ? 2 : 1;
|
|
35
77
|
continue;
|
|
36
|
-
|
|
37
|
-
|
|
78
|
+
}
|
|
79
|
+
let width = 0;
|
|
80
|
+
while (width < part && line.charAt(at + width) === ' ')
|
|
81
|
+
width++;
|
|
82
|
+
kept += line.slice(at, at + width);
|
|
83
|
+
at += width;
|
|
84
|
+
if (width < part)
|
|
85
|
+
break;
|
|
38
86
|
}
|
|
39
|
-
return
|
|
87
|
+
return { kept, rest: line.slice(at) };
|
|
40
88
|
}
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
89
|
+
/** Where inline markup can start, or a link's text end. */
|
|
90
|
+
const SPECIAL = /[\\`$<![\]*~]/g;
|
|
91
|
+
/**
|
|
92
|
+
* Inline Markdown this package emits → plain text, read left to right in one pass. Code
|
|
93
|
+
* span content comes out as written, and inline math with its dollars; a link and an
|
|
94
|
+
* autolink leave their text; a backslash escape leaves its character; emphasis,
|
|
95
|
+
* strong, and strikethrough markers are removed in pairs, and one with no partner stays.
|
|
96
|
+
*
|
|
97
|
+
* A link's text is read in place, its brackets paired beforehand by `pairBrackets` with
|
|
98
|
+
* inline math read as a span, as here and as `link` balances them, and its emphasis pairs
|
|
99
|
+
* only within it. Nothing read inside a link's text reaches past the
|
|
100
|
+
* text's end, so links nested any depth cost no more than their length.
|
|
101
|
+
*/
|
|
102
|
+
export function stripInline(markdown) {
|
|
103
|
+
const out = [];
|
|
104
|
+
/** Every delimiter run, to write back once paired. */
|
|
105
|
+
const runs = [];
|
|
106
|
+
/** The runs of the text around the read position, the innermost link's last. */
|
|
107
|
+
const scopes = [[]];
|
|
108
|
+
/** The links whose text is being read, innermost last. */
|
|
109
|
+
const links = [];
|
|
110
|
+
const codeSpanEnd = codeSpanCloser(markdown);
|
|
111
|
+
const { pairs } = pairBrackets(markdown, { skipMath: true });
|
|
112
|
+
let mathCloses = true;
|
|
113
|
+
let at = 0;
|
|
114
|
+
const special = new RegExp(SPECIAL);
|
|
115
|
+
for (let found = special.exec(markdown); found; found = special.exec(markdown)) {
|
|
116
|
+
const start = found.index;
|
|
117
|
+
if (start > at)
|
|
118
|
+
out.push(markdown.slice(at, start));
|
|
119
|
+
const limit = links.at(-1)?.close ?? markdown.length;
|
|
120
|
+
const char = found[0];
|
|
121
|
+
let end = start + 1;
|
|
122
|
+
if (char === '\\') {
|
|
123
|
+
const escaped = markdown.charAt(start + 1);
|
|
124
|
+
if (ESCAPABLE.test(escaped))
|
|
125
|
+
end = start + 2;
|
|
126
|
+
out.push(end > start + 1 ? escaped : char);
|
|
127
|
+
}
|
|
128
|
+
else if (char === '`') {
|
|
129
|
+
const span = codeSpanAt(markdown, start, codeSpanEnd);
|
|
130
|
+
if (span.content === undefined || span.end > limit) {
|
|
131
|
+
while (markdown.charAt(end) === '`')
|
|
132
|
+
end++;
|
|
133
|
+
out.push(markdown.slice(start, end));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
out.push(unpad(span.content));
|
|
137
|
+
end = span.end;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else if (char === '$') {
|
|
141
|
+
const close = mathCloses ? unescapedDollar(markdown, start + 1) : -1;
|
|
142
|
+
if (close === -1)
|
|
143
|
+
mathCloses = false;
|
|
144
|
+
if (close > start + 1 && close < limit)
|
|
145
|
+
end = close + 1;
|
|
146
|
+
out.push(markdown.slice(start, end));
|
|
147
|
+
}
|
|
148
|
+
else if (char === '<') {
|
|
149
|
+
const autolink = autolinkAt(markdown, start);
|
|
150
|
+
if (autolink && autolink.end <= limit) {
|
|
151
|
+
end = autolink.end;
|
|
152
|
+
out.push(autolink.target);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
out.push(char);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else if (char === '*' || char === '~') {
|
|
159
|
+
while (markdown.charAt(end) === char)
|
|
160
|
+
end++;
|
|
161
|
+
const run = {
|
|
162
|
+
canClose: start > 0 && !/\s/.test(markdown.charAt(start - 1)),
|
|
163
|
+
canOpen: end < markdown.length && !/\s/.test(markdown.charAt(end)),
|
|
164
|
+
char,
|
|
165
|
+
left: end - start,
|
|
166
|
+
piece: out.length,
|
|
167
|
+
};
|
|
168
|
+
runs.push(run);
|
|
169
|
+
scopes.at(-1)?.push(run);
|
|
170
|
+
out.push(markdown.slice(start, end));
|
|
171
|
+
}
|
|
172
|
+
else if (char === ']') {
|
|
173
|
+
const link = links.at(-1);
|
|
174
|
+
if (link?.close === start) {
|
|
175
|
+
// The link's text ends: its emphasis is paired, and its destination dropped.
|
|
176
|
+
links.pop();
|
|
177
|
+
pairRuns(scopes.pop() ?? []);
|
|
178
|
+
end = link.end;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
out.push(char);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
// `[` opens a link, and `!` before one is dropped with it.
|
|
186
|
+
const open = char === '!' ? start + 1 : start;
|
|
187
|
+
const link = linkAt(markdown, open, pairs, limit);
|
|
188
|
+
if (link) {
|
|
189
|
+
links.push(link);
|
|
190
|
+
scopes.push([]);
|
|
191
|
+
end = open + 1;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
out.push(char);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
at = end;
|
|
198
|
+
special.lastIndex = end;
|
|
199
|
+
}
|
|
200
|
+
out.push(markdown.slice(at));
|
|
201
|
+
for (const scope of scopes)
|
|
202
|
+
pairRuns(scope);
|
|
203
|
+
for (const run of runs)
|
|
204
|
+
out[run.piece] = run.char.repeat(run.left);
|
|
205
|
+
return out.join('');
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Pair the runs of each character: a run that can close takes from the nearest open run
|
|
209
|
+
* of its character, two at a time at most (`**` strong, `*` emphasis, `~~` strikethrough),
|
|
210
|
+
* and whatever is left of a run that can open waits for a closer. Each run is pushed and
|
|
211
|
+
* popped at most once.
|
|
212
|
+
*/
|
|
213
|
+
function pairRuns(runs) {
|
|
214
|
+
const open = { '*': [], '~': [] };
|
|
215
|
+
for (const run of runs) {
|
|
216
|
+
const stack = open[run.char];
|
|
217
|
+
if (run.canClose) {
|
|
218
|
+
for (let opener = stack.at(-1); opener && run.left > 0; opener = stack.at(-1)) {
|
|
219
|
+
const paired = Math.min(opener.left, run.left, 2);
|
|
220
|
+
opener.left -= paired;
|
|
221
|
+
run.left -= paired;
|
|
222
|
+
if (opener.left === 0)
|
|
223
|
+
stack.pop();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (run.left > 0 && run.canOpen)
|
|
227
|
+
stack.push(run);
|
|
228
|
+
}
|
|
47
229
|
}
|
|
48
230
|
/** A code span's content without the space padding both of its sides. */
|
|
49
231
|
function unpad(content) {
|
|
@@ -51,16 +233,20 @@ function unpad(content) {
|
|
|
51
233
|
? content.slice(1, -1)
|
|
52
234
|
: content;
|
|
53
235
|
}
|
|
54
|
-
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
236
|
+
/**
|
|
237
|
+
* A link `[text](destination)` whose `[` is at `start` and which ends by `limit`: its
|
|
238
|
+
* text runs to the `]` `pairs` pairs with that `[`, and its destination, which holds no
|
|
239
|
+
* parentheses (a written URL has them percent-encoded), to the next parenthesis.
|
|
240
|
+
*/
|
|
241
|
+
function linkAt(markdown, start, pairs, limit) {
|
|
242
|
+
const close = pairs.get(start);
|
|
243
|
+
if (close === undefined || markdown.charAt(close + 1) !== '(')
|
|
244
|
+
return;
|
|
245
|
+
const parenthesis = /[()]/g;
|
|
246
|
+
parenthesis.lastIndex = close + 2;
|
|
247
|
+
const end = parenthesis.exec(markdown);
|
|
248
|
+
if (end?.[0] !== ')' || end.index >= limit)
|
|
249
|
+
return;
|
|
250
|
+
return { close, end: end.index + 1 };
|
|
65
251
|
}
|
|
66
252
|
//# sourceMappingURL=text.js.map
|
package/dist/render/text.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../src/render/text.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../src/render/text.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,UAAU,EACV,UAAU,EACV,cAAc,EACd,SAAS,EACT,YAAY,EACZ,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAwB,UAAU,EAAE,MAAM,eAAe,CAAC;AAEjE,6EAA6E;AAC7E,MAAM,UAAU,MAAM,CAAC,QAA2B,EAAE,OAAO,GAAoB,EAAE;IAC/E,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,KAAgC,CAAC;IACrC,0EAA0E;IAC1E,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,MAAM,IAAI,IAAI,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,CAA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;IACvC,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,GAAG,CAAC,KAAK,IAAI,KAAK;QAAE,OAAO;IACnD,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC;AACvC,CAAC"}
|
package/dist/xml/mathml.d.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview MathML (on the ordered XML tree) to TeX notation, and cleanup of TeX
|
|
3
|
-
* that publishers deposit.
|
|
4
|
-
*
|
|
5
|
-
* Most publishers deposit formulas as MathML only, and flattening MathML to its text
|
|
6
|
-
* turns `x²` into `x2` and a fraction into two numbers side by side. The conversion
|
|
7
|
-
* prefers TeX the document already carries (`@alttext`, a TeX `<annotation>`), then
|
|
8
|
-
* linearizes presentation MathML. The output is TeX a person or model can read, not a
|
|
9
|
-
* guaranteed-compilable document.
|
|
10
|
-
* @module src/xml/mathml
|
|
11
|
-
*/
|
|
12
1
|
import { type XmlNode } from './ordered.js';
|
|
2
|
+
/** How the converter reads a MathML tree whose nodes are `N`. */
|
|
3
|
+
export interface MathNodes<N> {
|
|
4
|
+
/** An element's attribute, by its source name. */
|
|
5
|
+
attr(node: N, name: string): string | undefined;
|
|
6
|
+
/** An element's element and text children, in order; empty for a text node. */
|
|
7
|
+
children(node: N): readonly N[];
|
|
8
|
+
/** True for a text node. */
|
|
9
|
+
isText(node: N): boolean;
|
|
10
|
+
/** An element's name without a namespace prefix (`mml:mi` → `mi`); undefined for text. */
|
|
11
|
+
name(node: N): string | undefined;
|
|
12
|
+
/** A text node's text. */
|
|
13
|
+
text(node: N): string;
|
|
14
|
+
}
|
|
13
15
|
/**
|
|
14
16
|
* TeX for a `<math>` element: its own TeX when it carries some, else a linearization
|
|
15
|
-
* of the presentation markup. Empty when neither yields anything.
|
|
17
|
+
* of the presentation markup. Empty when neither yields anything. Read from the ordered
|
|
18
|
+
* XML tree unless `nodes` says how to read another.
|
|
16
19
|
*/
|
|
17
20
|
export declare function mathmlToTex(math: XmlNode): string;
|
|
21
|
+
export declare function mathmlToTex<N>(math: N, nodes: MathNodes<N>): string;
|
|
18
22
|
/**
|
|
19
23
|
* A publisher's TeX reduced to the expression: the `\documentclass…\begin{document}`
|
|
20
24
|
* wrapper some deposit around every formula removed, and one layer of math
|
package/dist/xml/mathml.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mathml.d.ts","sourceRoot":"","sources":["../../src/xml/mathml.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mathml.d.ts","sourceRoot":"","sources":["../../src/xml/mathml.ts"],"names":[],"mappings":"AAcA,OAAO,EAAuD,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AAEjG,iEAAiE;AACjE,MAAM,WAAW,SAAS,CAAC,CAAC;IAC1B,kDAAkD;IAClD,IAAI,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAChD,+EAA+E;IAC/E,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,SAAS,CAAC,EAAE,CAAC;IAChC,4BAA4B;IAC5B,MAAM,CAAC,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC;IACzB,0FAA0F;IAC1F,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,GAAG,SAAS,CAAC;IAClC,0BAA0B;IAC1B,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC;CACvB;AAwED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC;AACnD,wBAAgB,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;AA8LrE;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAS5C"}
|
package/dist/xml/mathml.js
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview MathML
|
|
3
|
-
* that publishers deposit.
|
|
2
|
+
* @fileoverview MathML to TeX notation, and cleanup of TeX that publishers deposit.
|
|
4
3
|
*
|
|
5
4
|
* Most publishers deposit formulas as MathML only, and flattening MathML to its text
|
|
6
5
|
* turns `x²` into `x2` and a fraction into two numbers side by side. The conversion
|
|
7
6
|
* prefers TeX the document already carries (`@alttext`, a TeX `<annotation>`), then
|
|
8
7
|
* linearizes presentation MathML. The output is TeX a person or model can read, not a
|
|
9
8
|
* guaranteed-compilable document.
|
|
9
|
+
*
|
|
10
|
+
* The converter reads its tree through a {@link MathNodes} accessor: the ordered XML tree
|
|
11
|
+
* by default, or a DOM (`src/html/math.ts`), so one linearization serves every format.
|
|
10
12
|
* @module src/xml/mathml
|
|
11
13
|
*/
|
|
14
|
+
import { MAX_XML_DEPTH } from '../model/limits.js';
|
|
12
15
|
import { attrOf, childrenOf, isTextNode, localNameOf, textOf } from './ordered.js';
|
|
16
|
+
/** The ordered XML tree every XML format parses into. */
|
|
17
|
+
const ORDERED_NODES = {
|
|
18
|
+
attr: attrOf,
|
|
19
|
+
children: childrenOf,
|
|
20
|
+
isText: isTextNode,
|
|
21
|
+
name: localNameOf,
|
|
22
|
+
text: textOf,
|
|
23
|
+
};
|
|
13
24
|
/** Annotation encodings that carry TeX. */
|
|
14
25
|
const TEX_ENCODINGS = new Set(['application/x-tex', 'tex', 'latex', 'application/x-latex']);
|
|
15
26
|
/**
|
|
@@ -59,70 +70,83 @@ function looksLikeTex(s) {
|
|
|
59
70
|
function group(s) {
|
|
60
71
|
return [...s].length === 1 ? s : `{${s}}`;
|
|
61
72
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
* of the presentation markup. Empty when neither yields anything.
|
|
65
|
-
*/
|
|
66
|
-
export function mathmlToTex(math) {
|
|
67
|
-
const alttext = attrOf(math, 'alttext')?.trim();
|
|
73
|
+
export function mathmlToTex(math, nodes = ORDERED_NODES) {
|
|
74
|
+
const alttext = nodes.attr(math, 'alttext')?.trim();
|
|
68
75
|
if (alttext && looksLikeTex(alttext))
|
|
69
76
|
return cleanTex(alttext);
|
|
70
|
-
const annotation = findTexAnnotation(math);
|
|
77
|
+
const annotation = findTexAnnotation(math, nodes, 1);
|
|
71
78
|
if (annotation)
|
|
72
79
|
return cleanTex(annotation);
|
|
73
|
-
return linearize(
|
|
80
|
+
return linearize(nodes.children(math), nodes, 1).replace(/\s+/g, ' ').trim();
|
|
74
81
|
}
|
|
75
|
-
function findTexAnnotation(node) {
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
function findTexAnnotation(node, nodes, depth) {
|
|
83
|
+
if (depth >= MAX_XML_DEPTH)
|
|
84
|
+
return;
|
|
85
|
+
for (const child of nodes.children(node)) {
|
|
86
|
+
const name = nodes.name(child);
|
|
78
87
|
if (name === 'annotation' &&
|
|
79
|
-
TEX_ENCODINGS.has((
|
|
80
|
-
const text =
|
|
88
|
+
TEX_ENCODINGS.has((nodes.attr(child, 'encoding') ?? '').toLowerCase())) {
|
|
89
|
+
const text = nodes
|
|
90
|
+
.children(child)
|
|
91
|
+
.map((part) => nodes.text(part))
|
|
92
|
+
.join('')
|
|
93
|
+
.trim();
|
|
81
94
|
if (text)
|
|
82
95
|
return text;
|
|
83
96
|
}
|
|
84
97
|
if (name === 'semantics' || name === 'mrow') {
|
|
85
|
-
const nested = findTexAnnotation(child);
|
|
98
|
+
const nested = findTexAnnotation(child, nodes, depth + 1);
|
|
86
99
|
if (nested)
|
|
87
100
|
return nested;
|
|
88
101
|
}
|
|
89
102
|
}
|
|
90
103
|
return;
|
|
91
104
|
}
|
|
92
|
-
function linearize(nodes) {
|
|
93
|
-
return
|
|
105
|
+
function linearize(list, nodes, depth) {
|
|
106
|
+
return list.map((node) => linearizeNode(node, nodes, depth)).join('');
|
|
94
107
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
108
|
+
/**
|
|
109
|
+
* One node as TeX, construct by construct. An element `MAX_XML_DEPTH` levels below the
|
|
110
|
+
* `<math>` is written as its text: the XML parser never builds a tree that deep, but a
|
|
111
|
+
* DOM may hold one, and the recursion must not outrun the stack.
|
|
112
|
+
*/
|
|
113
|
+
function linearizeNode(node, nodes, depth) {
|
|
114
|
+
if (nodes.isText(node))
|
|
115
|
+
return nodes.text(node).trim();
|
|
116
|
+
if (depth >= MAX_XML_DEPTH)
|
|
117
|
+
return deepText(node, nodes);
|
|
118
|
+
const kids = nodes
|
|
119
|
+
.children(node)
|
|
120
|
+
.filter((child) => !nodes.isText(child) || nodes.text(child).trim());
|
|
121
|
+
const inner = depth + 1;
|
|
99
122
|
const arg = (i) => {
|
|
100
123
|
const child = kids[i];
|
|
101
|
-
return child ? linearizeNode(child) : '';
|
|
124
|
+
return child ? linearizeNode(child, nodes, inner) : '';
|
|
102
125
|
};
|
|
103
|
-
|
|
126
|
+
const each = () => kids.map((kid) => linearizeNode(kid, nodes, inner));
|
|
127
|
+
switch (nodes.name(node)) {
|
|
104
128
|
case 'mi': {
|
|
105
|
-
const text =
|
|
106
|
-
const variant = VARIANTS.get(
|
|
129
|
+
const text = each().join('');
|
|
130
|
+
const variant = VARIANTS.get(nodes.attr(node, 'mathvariant') ?? '');
|
|
107
131
|
if (variant && text)
|
|
108
132
|
return `${variant}{${text}}`;
|
|
109
133
|
return [...text].length > 1 && /^[A-Za-z]+$/.test(text) ? `\\mathrm{${text}}` : text;
|
|
110
134
|
}
|
|
111
135
|
case 'mn': {
|
|
112
|
-
const text =
|
|
113
|
-
const variant = VARIANTS.get(
|
|
136
|
+
const text = each().join('');
|
|
137
|
+
const variant = VARIANTS.get(nodes.attr(node, 'mathvariant') ?? '');
|
|
114
138
|
return variant && variant !== '\\mathrm' && text ? `${variant}{${text}}` : text;
|
|
115
139
|
}
|
|
116
140
|
case 'mo': {
|
|
117
|
-
const text =
|
|
141
|
+
const text = each().join('');
|
|
118
142
|
return OPERATORS.get(text) ?? text;
|
|
119
143
|
}
|
|
120
144
|
case 'mtext': {
|
|
121
|
-
const text =
|
|
145
|
+
const text = each().join('');
|
|
122
146
|
return text ? `\\text{${text}}` : '';
|
|
123
147
|
}
|
|
124
148
|
case 'ms':
|
|
125
|
-
return `"${
|
|
149
|
+
return `"${each().join('')}"`;
|
|
126
150
|
case 'mspace':
|
|
127
151
|
return ' ';
|
|
128
152
|
case 'msup':
|
|
@@ -134,7 +158,7 @@ function linearizeNode(node) {
|
|
|
134
158
|
case 'mfrac':
|
|
135
159
|
return `\\frac{${arg(0)}}{${arg(1)}}`;
|
|
136
160
|
case 'msqrt':
|
|
137
|
-
return `\\sqrt{${
|
|
161
|
+
return `\\sqrt{${each().join('')}}`;
|
|
138
162
|
case 'mroot':
|
|
139
163
|
return `\\sqrt[${arg(1)}]{${arg(0)}}`;
|
|
140
164
|
case 'mover': {
|
|
@@ -147,16 +171,16 @@ function linearizeNode(node) {
|
|
|
147
171
|
case 'munderover':
|
|
148
172
|
return `${arg(0)}_${group(arg(1))}^${group(arg(2))}`;
|
|
149
173
|
case 'mfenced': {
|
|
150
|
-
const open =
|
|
151
|
-
const close =
|
|
152
|
-
const separator =
|
|
153
|
-
return `${open}${
|
|
174
|
+
const open = nodes.attr(node, 'open') ?? '(';
|
|
175
|
+
const close = nodes.attr(node, 'close') ?? ')';
|
|
176
|
+
const separator = nodes.attr(node, 'separators')?.trim().charAt(0) ?? ',';
|
|
177
|
+
return `${open}${each().join(separator)}${close}`;
|
|
154
178
|
}
|
|
155
179
|
case 'mtable':
|
|
156
|
-
return `\\begin{matrix}${
|
|
180
|
+
return `\\begin{matrix}${each().join(' \\\\ ')}\\end{matrix}`;
|
|
157
181
|
case 'mtr':
|
|
158
182
|
case 'mlabeledtr':
|
|
159
|
-
return
|
|
183
|
+
return each().join(' & ');
|
|
160
184
|
case 'mphantom':
|
|
161
185
|
case 'annotation':
|
|
162
186
|
case 'annotation-xml':
|
|
@@ -167,12 +191,58 @@ function linearizeNode(node) {
|
|
|
167
191
|
case 'semantics':
|
|
168
192
|
return arg(0);
|
|
169
193
|
case 'mmultiscripts':
|
|
170
|
-
return
|
|
194
|
+
return multiscripts(kids, nodes, inner);
|
|
171
195
|
default:
|
|
172
196
|
// mrow, mstyle, mpadded, menclose, merror, mtd, math, and anything unknown:
|
|
173
197
|
// the children in order.
|
|
174
|
-
return
|
|
198
|
+
return each().join('');
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* `<mmultiscripts>`: a base, subscript–superscript pairs after it, and past
|
|
203
|
+
* `<mprescripts/>` pairs before it, each pair written `_sub^sup` as `msubsup` is. A
|
|
204
|
+
* prescript pair follows an empty group `{}`, since siblings join with nothing between
|
|
205
|
+
* and a bare `^{14}` would attach to the token before; the first postscript pair attaches
|
|
206
|
+
* to the base and each later one follows `{}`, so tensor indices stay staggered
|
|
207
|
+
* (`R_i{}^j{}_k`). A script written empty (`<none/>`, or the empty `<mrow/>` MathML Core
|
|
208
|
+
* and LaTeXML put in its place) is left out, and a lone last script is a subscript.
|
|
209
|
+
*/
|
|
210
|
+
function multiscripts(kids, nodes, depth) {
|
|
211
|
+
const [base, ...scripts] = kids;
|
|
212
|
+
const split = scripts.findIndex((kid) => nodes.name(kid) === 'mprescripts');
|
|
213
|
+
const post = split === -1 ? scripts : scripts.slice(0, split);
|
|
214
|
+
const pre = split === -1 ? [] : scripts.slice(split + 1);
|
|
215
|
+
const [first = '', ...later] = scriptPairs(post, nodes, depth);
|
|
216
|
+
const before = scriptPairs(pre, nodes, depth).map((pair) => `{}${pair}`);
|
|
217
|
+
const after = later.map((pair) => `{}${pair}`);
|
|
218
|
+
const core = base ? linearizeNode(base, nodes, depth) : '';
|
|
219
|
+
return `${before.join('')}${core}${first}${after.join('')}`;
|
|
220
|
+
}
|
|
221
|
+
/** Each subscript–superscript pair that writes anything, as `_sub^sup`. */
|
|
222
|
+
function scriptPairs(scripts, nodes, depth) {
|
|
223
|
+
const pairs = [];
|
|
224
|
+
for (let i = 0; i < scripts.length; i += 2) {
|
|
225
|
+
const [sub, sup] = [scripts[i], scripts[i + 1]].map((script) => script === undefined ? '' : linearizeNode(script, nodes, depth));
|
|
226
|
+
const pair = `${sub ? `_${group(sub)}` : ''}${sup ? `^${group(sup)}` : ''}`;
|
|
227
|
+
if (pair)
|
|
228
|
+
pairs.push(pair);
|
|
229
|
+
}
|
|
230
|
+
return pairs;
|
|
231
|
+
}
|
|
232
|
+
/** The text under `node`, each text node trimmed, read without recursion. */
|
|
233
|
+
function deepText(node, nodes) {
|
|
234
|
+
let out = '';
|
|
235
|
+
const pending = [node];
|
|
236
|
+
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
|
237
|
+
if (nodes.isText(next)) {
|
|
238
|
+
out += nodes.text(next).trim();
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const children = nodes.children(next);
|
|
242
|
+
for (let i = children.length - 1; i >= 0; i--)
|
|
243
|
+
pending.push(children[i]);
|
|
175
244
|
}
|
|
245
|
+
return out;
|
|
176
246
|
}
|
|
177
247
|
/** The body of a LaTeX document deposited as `<tex-math>`, closing marker optional. */
|
|
178
248
|
const TEX_DOCUMENT_BODY = /\\begin\{document\}([\s\S]*?)(?:\\end\{document\}|$)/;
|