scholarly-parse 0.1.1 → 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 +4 -0
- package/README.md +4 -4
- 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 +116 -114
- package/dist/formats/html/blocks.js.map +1 -1
- package/dist/formats/html/context.d.ts +17 -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 +30 -28
- 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 +207 -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 +11 -13
- 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 +120 -32
- package/dist/formats/jats/blocks.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.js +10 -1
- 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 +6 -4
- package/dist/formats/jats/references.js.map +1 -1
- package/dist/formats/latexml/blocks.d.ts.map +1 -1
- package/dist/formats/latexml/blocks.js +42 -31
- package/dist/formats/latexml/blocks.js.map +1 -1
- package/dist/formats/latexml/context.d.ts +4 -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 +19 -11
- package/dist/formats/latexml/parse.js.map +1 -1
- 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 +32 -1
- 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 +15 -5
- package/dist/formats/pdf/load.d.ts.map +1 -1
- package/dist/formats/pdf/load.js +17 -5
- package/dist/formats/pdf/load.js.map +1 -1
- package/dist/formats/pdf/parse.d.ts +5 -0
- package/dist/formats/pdf/parse.d.ts.map +1 -1
- package/dist/formats/pdf/parse.js +9 -10
- 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 +36 -6
- package/dist/formats/pdf/references.js.map +1 -1
- package/dist/formats/pdf/structure.d.ts +3 -1
- package/dist/formats/pdf/structure.d.ts.map +1 -1
- package/dist/formats/pdf/structure.js +89 -33
- package/dist/formats/pdf/structure.js.map +1 -1
- package/dist/formats/tei/inline.d.ts +4 -1
- package/dist/formats/tei/inline.d.ts.map +1 -1
- package/dist/formats/tei/inline.js +6 -3
- 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 +16 -8
- 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/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/model/document.d.ts +5 -0
- package/dist/model/document.d.ts.map +1 -1
- package/dist/model/doi.d.ts +21 -3
- package/dist/model/doi.d.ts.map +1 -1
- package/dist/model/doi.js +45 -3
- package/dist/model/doi.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/parse.d.ts +2 -0
- package/dist/parse.d.ts.map +1 -1
- 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 +67 -6
- package/dist/render/inline.d.ts.map +1 -1
- package/dist/render/inline.js +243 -9
- package/dist/render/inline.js.map +1 -1
- package/dist/render/markdown.d.ts +14 -9
- package/dist/render/markdown.d.ts.map +1 -1
- package/dist/render/markdown.js +23 -14
- package/dist/render/markdown.js.map +1 -1
- package/dist/render/sections.d.ts +9 -5
- package/dist/render/sections.d.ts.map +1 -1
- package/dist/render/sections.js +20 -15
- package/dist/render/sections.js.map +1 -1
- package/dist/render/text.d.ts +5 -0
- package/dist/render/text.d.ts.map +1 -1
- package/dist/render/text.js +65 -87
- 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 +1 -6
- package/dist/xml/ordered.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project. Each entry links to its full per-version file in [changelog/](changelog/).
|
|
4
4
|
|
|
5
|
+
## [0.2.0](changelog/0.2.x/0.2.0.md) — 2026-10-06 · 🛡️ Security
|
|
6
|
+
|
|
7
|
+
Escaping that holds across element boundaries and HTML nesting bounded before the DOM is built, plus toSections entries for footnotes and references, PDF footnotes, a PDF abort signal, and parsing fixes in every format.
|
|
8
|
+
|
|
5
9
|
## [0.1.1](changelog/0.1.x/0.1.1.md) — 2026-09-26 · 🛡️ Security
|
|
6
10
|
|
|
7
11
|
Hardening against hostile input: PDF reading budgets, one table budget per document, capped Markdown nesting, and linear-time text patterns, plus Grobid TEI outline fixes.
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
<div align="center">
|
|
7
7
|
|
|
8
|
-
[](./CHANGELOG.md) [](./LICENSE) [](https://www.npmjs.com/package/scholarly-parse) [](https://www.typescriptlang.org/) [](https://nodejs.org/)
|
|
9
9
|
|
|
10
10
|
</div>
|
|
11
11
|
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
## What it does
|
|
15
15
|
|
|
16
|
-
`scholarly-parse` reads a paper in any of five formats — JATS XML from PubMed Central, Europe PMC, and publisher feeds; TEI from Grobid; LaTeXML HTML from arXiv; publisher article pages; and PDF — into one `ScholarlyDocument`, and renders it as Markdown.
|
|
16
|
+
`scholarly-parse` reads a paper in any of five formats — JATS XML from PubMed Central, Europe PMC, and publisher feeds; TEI from Grobid; LaTeXML HTML from arXiv; publisher article pages; and PDF — into one `ScholarlyDocument`, and renders it as Markdown.
|
|
17
17
|
|
|
18
18
|
## Install
|
|
19
19
|
|
|
@@ -54,7 +54,7 @@ if (!result.ok && result.error.reason === 'blocked') {
|
|
|
54
54
|
}
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
`toSections` flattens the document into sections with their heading paths and sizes, for serving a long paper a piece at a time. Figures and tables
|
|
57
|
+
`toSections` flattens the document into sections with their heading paths and sizes, for serving a long paper a piece at a time. Figures and tables outside any section, the footnotes, and the references come last, one entry each, and each entry's `id` renders it alone through `toMarkdown(document, { sections: [id] })`:
|
|
58
58
|
|
|
59
59
|
```ts
|
|
60
60
|
import { toSections } from 'scholarly-parse';
|
|
@@ -77,7 +77,7 @@ for (const { path, chars, markdown } of toSections(document)) {
|
|
|
77
77
|
|
|
78
78
|
Every parser returns `{ ok: true, document }` or `{ ok: false, error: { reason, message } }`, where `reason` is `malformed`, `wrong-format`, `empty`, `blocked`, or `too-large`. Each takes a `maxInputBytes` budget; `parseLatexml` and `parseHtml` take the page's `baseUrl` to resolve links, and `parsePdf` reading budgets counted across the document: `maxPages` (default 300), `maxTextItems` (500,000), `maxTextChars` (4,000,000), and `maxOperators` (10,000,000). A PDF that reaches one parses as far as it was read, with a `truncated-input` warning. `parseJats` and `parseTei` are synchronous; the rest are async.
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
`parsePdf` and `parse` also take a `signal`: once it is aborted (`AbortSignal.timeout(ms)`, a request's cancellation), reading stops at the next page boundary and keeps what was read, warned `truncated-input`. pdf.js reads a page without yielding, so for a hard time or memory bound on untrusted PDFs, run `parsePdf` in a worker or process you can terminate.
|
|
81
81
|
|
|
82
82
|
## Formats
|
|
83
83
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: "Escaping that holds across element boundaries and HTML nesting bounded before the DOM is built, plus toSections entries for footnotes and references, PDF footnotes, a PDF abort signal, and parsing fixes in every format."
|
|
3
|
+
breaking: false
|
|
4
|
+
security: true
|
|
5
|
+
agent-notes: |
|
|
6
|
+
`FlatSection.kind` gains `'footnotes'` and `'references'`: `toSections` ends with those entries after `floats` whenever the document has any, so an exhaustive `switch` on `kind` needs both cases.
|
|
7
|
+
Abstract-part section IDs from TEI, LaTeXML, publisher HTML, and PDF are now `abstract-<n>-<m>` (was `abstract-<n>`, `abstract`, or `s<n>`); a stored part ID needs remapping. `abstract-<digits>`, `floats`, `footnotes`, and `references` are never section IDs.
|
|
8
|
+
`parsePdf` now fills `document.footnotes`, so PDF Markdown can gain a `## Footnotes` section.
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# 0.2.0 — 2026-10-06
|
|
12
|
+
|
|
13
|
+
## Added
|
|
14
|
+
|
|
15
|
+
- **`PdfOptions.signal` and `AutoParseOptions.signal`**: an aborted `AbortSignal` stops a PDF read at the next page boundary, keeping what was read with a `truncated-input` warning ([#17](https://github.com/cyanheads/scholarly-parse/issues/17)). With a signal, each page waits one `setTimeout` turn so a timeout can fire.
|
|
16
|
+
- **`toSections` ends with `footnotes` and `references` entries** after `floats`, each holding what `toMarkdown` writes for it, and `toMarkdown({ sections })` renders them ([#27](https://github.com/cyanheads/scholarly-parse/issues/27)). `FlatSection.kind` gains both.
|
|
17
|
+
- **PDF footnotes**: labelled notes set below a page's text before the reference list become `document.footnotes`; first-page affiliations and licences stay front matter ([#43](https://github.com/cyanheads/scholarly-parse/issues/43)).
|
|
18
|
+
- **`detect`** reads bytes by their UTF-8 or UTF-16 byte-order mark, skips processing instructions before the root, names an `<article class="ltx_document">` fragment LaTeXML, and names JATS inside a PMC OAI-PMH record, which `parseJats` reads with flavor `pmc` ([#31](https://github.com/cyanheads/scholarly-parse/issues/31), [#45](https://github.com/cyanheads/scholarly-parse/issues/45)).
|
|
19
|
+
|
|
20
|
+
## Changed
|
|
21
|
+
|
|
22
|
+
- **Section IDs**: `issueId` never gives a section a `toSections` ID (`abstract-<n>`, `floats`, `footnotes`, `references`), and every reader numbers abstract parts `abstract-<n>-<m>`, so each `toSections` ID renders one entry ([#24](https://github.com/cyanheads/scholarly-parse/issues/24)).
|
|
23
|
+
- **Publisher HTML lead**: on a titled page with no abstract, article prose between the title and the first heading opens the body, byline still dropped ([#32](https://github.com/cyanheads/scholarly-parse/issues/32)).
|
|
24
|
+
- **Publisher HTML landing pages** parse as front matter and an abstract with a `no-body` warning that names a landing page; the title heading falls back to the one matching `citation_title`, and `Metadata`/`BibTeX` sections are furniture ([#30](https://github.com/cyanheads/scholarly-parse/issues/30)).
|
|
25
|
+
- **JATS footnotes** write every block of a note into its text (lists, formulas, code, captions; a table's rows are warned `unhandled-element`), definitions keep their paragraphs, and block elements read as inline text are spaced ([#35](https://github.com/cyanheads/scholarly-parse/issues/35)).
|
|
26
|
+
|
|
27
|
+
## Fixed
|
|
28
|
+
|
|
29
|
+
- **DOIs** in JATS and TEI article, related-work, and reference fields lose resolver and `doi:` prefixes, through one shared `normalizeDoi` ([#38](https://github.com/cyanheads/scholarly-parse/issues/38)).
|
|
30
|
+
- **`toText`** strips links whose label holds brackets, and `link()` escapes a label's unpaired brackets, leaving inline math alone ([#36](https://github.com/cyanheads/scholarly-parse/issues/36)).
|
|
31
|
+
- **MathML** `mmultiscripts` keep prescripts and postscripts (`{}_6^{14}C`) ([#40](https://github.com/cyanheads/scholarly-parse/issues/40)), and LaTeXML and publisher HTML convert MathML that carries no TeX instead of dropping or flattening it ([#41](https://github.com/cyanheads/scholarly-parse/issues/41)).
|
|
32
|
+
- **Publisher HTML metadata**: `<a rel="license">` and `<area rel="license">` are read as the license ([#29](https://github.com/cyanheads/scholarly-parse/issues/29)), and an `<h1>` fallback title is escaped once ([#33](https://github.com/cyanheads/scholarly-parse/issues/33)).
|
|
33
|
+
- **PDF**: runs drawn under a heading no longer join its line ([#2](https://github.com/cyanheads/scholarly-parse/issues/2)); a figure's panel labels are not headings ([#3](https://github.com/cyanheads/scholarly-parse/issues/3)); bold back-matter labels after the last reference end the list ([#5](https://github.com/cyanheads/scholarly-parse/issues/5)); one- and two-entry numbered bibliographies and `68.[No title]` entries split ([#42](https://github.com/cyanheads/scholarly-parse/issues/42)); a quoted or colon-less first-page DOI is read ([#23](https://github.com/cyanheads/scholarly-parse/issues/23)).
|
|
34
|
+
|
|
35
|
+
## Security
|
|
36
|
+
|
|
37
|
+
- **Inline escaping holds across element boundaries**: `escapeInline` closes each fragment's end and every reader runs `joinInlineSeams` over finished inline Markdown, so text split across elements cannot form raw HTML, an image, or a non-web link; citation markers are escaped ([#44](https://github.com/cyanheads/scholarly-parse/issues/44)).
|
|
38
|
+
- **HTML and LaTeXML nesting is bounded before the DOM is built**: tags of elements past 256 levels are dropped with their text kept and a `truncated-input` warning, and walkers read each element's subtree facts once, so deep or chained markup parses in linear time ([#7](https://github.com/cyanheads/scholarly-parse/issues/7)). Repeated self-closed `<math/>` and `<svg/>` are bounded the same way.
|
|
39
|
+
- **Linear-time lookups**: HTML and LaTeXML affiliations, HTML in-page link targets, JATS trailing `<notes>` ([#10](https://github.com/cyanheads/scholarly-parse/issues/10)), and a LaTeXML paragraph holding many footnotes.
|
|
40
|
+
|
|
41
|
+
## Dependencies
|
|
42
|
+
|
|
43
|
+
- `source-map-js` 1.2.1 → 1.2.2 (transitive dev dependency through vitest; [GHSA-68fv-2mgg-jv7q](https://github.com/advisories/GHSA-68fv-2mgg-jv7q))
|
package/dist/detect.d.ts
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { SourceFormat } from './model/document.js';
|
|
6
6
|
/**
|
|
7
|
-
* The format of `input`, or undefined when it is none this package reads.
|
|
8
|
-
*
|
|
7
|
+
* The format of `input`, or undefined when it is none this package reads. Bytes are read
|
|
8
|
+
* by their byte-order mark, else as Latin-1. A challenge or access-denied page is `html`;
|
|
9
|
+
* the HTML parser reports it as `blocked`.
|
|
9
10
|
*/
|
|
10
11
|
export declare function detect(input: string | Uint8Array): SourceFormat | undefined;
|
|
11
12
|
//# sourceMappingURL=detect.d.ts.map
|
package/dist/detect.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../src/detect.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../src/detect.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAMxD;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,YAAY,GAAG,SAAS,CAwB3E"}
|
package/dist/detect.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { bomEncoding } from './model/input.js';
|
|
2
|
+
/** How many characters of the input the sniffers read: headers and root elements sit at the start. */
|
|
3
|
+
const SNIFF_CHARS = 64 * 1024;
|
|
3
4
|
/**
|
|
4
|
-
* The format of `input`, or undefined when it is none this package reads.
|
|
5
|
-
*
|
|
5
|
+
* The format of `input`, or undefined when it is none this package reads. Bytes are read
|
|
6
|
+
* by their byte-order mark, else as Latin-1. A challenge or access-denied page is `html`;
|
|
7
|
+
* the HTML parser reports it as `blocked`.
|
|
6
8
|
*/
|
|
7
9
|
export function detect(input) {
|
|
8
|
-
const head = typeof input === 'string'
|
|
9
|
-
? input.slice(0, SNIFF_BYTES)
|
|
10
|
-
: latin1(input.subarray(0, SNIFF_BYTES));
|
|
10
|
+
const head = typeof input === 'string' ? input.slice(0, SNIFF_CHARS) : opening(input);
|
|
11
11
|
if (/^[\s\S]{0,1024}%PDF-/.test(head))
|
|
12
12
|
return 'pdf';
|
|
13
13
|
const text = head.replace(/^/, '').replace(/^\s+/, '');
|
|
@@ -16,9 +16,18 @@ export function detect(input) {
|
|
|
16
16
|
// TEI before HTML: OpenAlex serves Grobid's TEI wrapped in an HTML page.
|
|
17
17
|
if (/<TEI[\s>]|xmlns=["']http:\/\/www\.tei-c\.org\/ns\/1\.0["']/i.test(text))
|
|
18
18
|
return 'tei';
|
|
19
|
-
|
|
19
|
+
const root = afterProlog(text);
|
|
20
|
+
// LaTeXML before JATS: an arXiv fragment is rooted at `<article class="ltx_document">`.
|
|
21
|
+
if (/^<article\s[^<>]*\bclass\s*=\s*["'][^"'<>]*\bltx_document\b/i.test(root))
|
|
22
|
+
return 'latexml';
|
|
23
|
+
if (/^(?:<!DOCTYPE\s+(?:article|pmc-articleset)\b|<article[\s>]|<pmc-articleset[\s>])/i.test(root)) {
|
|
24
|
+
return 'jats';
|
|
25
|
+
}
|
|
26
|
+
// PMC's OAI-PMH record carries the article in `<metadata>`; an error or `oai_dc` record does not.
|
|
27
|
+
if (/^<OAI-PMH[\s>]/i.test(root) && /<metadata[^<>]*>\s*<article[\s>]/i.test(root)) {
|
|
20
28
|
return 'jats';
|
|
21
29
|
}
|
|
30
|
+
// After the JATS roots, since a JATS article's text can name the class.
|
|
22
31
|
// Each `<meta` is read up to the next `<`: a run of unclosed ones is scanned once, not once per tag.
|
|
23
32
|
if (/\bltx_document\b|<meta[^<>]+content=["']LaTeXML/i.test(text))
|
|
24
33
|
return 'latexml';
|
|
@@ -26,26 +35,39 @@ export function detect(input) {
|
|
|
26
35
|
return 'html';
|
|
27
36
|
return;
|
|
28
37
|
}
|
|
38
|
+
/** Markup the prolog may hold before the root, by how it opens and closes. */
|
|
39
|
+
const PROLOG_MARKUP = [
|
|
40
|
+
['<!--', '-->'],
|
|
41
|
+
['<?', '?>'],
|
|
42
|
+
];
|
|
29
43
|
/**
|
|
30
|
-
* The text from the root element on, past
|
|
31
|
-
* whitespace before the root. Scanned forward once: a regex over a run of
|
|
32
|
-
* backtracks exponentially when the root after them does not match.
|
|
44
|
+
* The text from the root element on, past the XML declaration, processing instructions,
|
|
45
|
+
* comments, and whitespace before the root. Scanned forward once: a regex over a run of
|
|
46
|
+
* comments backtracks exponentially when the root after them does not match.
|
|
33
47
|
*/
|
|
34
48
|
function afterProlog(text) {
|
|
35
|
-
let at =
|
|
49
|
+
let at = 0;
|
|
36
50
|
for (;;) {
|
|
37
51
|
while (/\s/.test(text.charAt(at)))
|
|
38
52
|
at++;
|
|
39
|
-
|
|
53
|
+
const markup = PROLOG_MARKUP.find(([open]) => text.startsWith(open, at));
|
|
54
|
+
if (!markup)
|
|
40
55
|
return text.slice(at);
|
|
41
|
-
const
|
|
56
|
+
const [open, close] = markup;
|
|
57
|
+
const end = text.indexOf(close, at + open.length);
|
|
42
58
|
if (end === -1)
|
|
43
59
|
return '';
|
|
44
|
-
at = end +
|
|
60
|
+
at = end + close.length;
|
|
45
61
|
}
|
|
46
62
|
}
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
/**
|
|
64
|
+
* The first {@link SNIFF_CHARS} characters of `bytes`, decoded by their byte-order mark as
|
|
65
|
+
* `decodeText` does — UTF-16 takes twice the bytes — else as Latin-1, which reads ASCII
|
|
66
|
+
* markup whatever the real encoding.
|
|
67
|
+
*/
|
|
68
|
+
function opening(bytes) {
|
|
69
|
+
const encoding = bomEncoding(bytes);
|
|
70
|
+
const width = encoding === 'utf-16le' || encoding === 'utf-16be' ? 2 : 1;
|
|
71
|
+
return new TextDecoder(encoding ?? 'latin1').decode(bytes.subarray(0, SNIFF_CHARS * width));
|
|
50
72
|
}
|
|
51
73
|
//# sourceMappingURL=detect.js.map
|
package/dist/detect.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detect.js","sourceRoot":"","sources":["../src/detect.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"detect.js","sourceRoot":"","sources":["../src/detect.ts"],"names":[],"mappings":"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"}
|
|
@@ -16,7 +16,7 @@ export type FloatKind = 'figure' | 'table' | 'supplement';
|
|
|
16
16
|
* `tw`, `supplementary`) together with the image, table, or file link they wrap, so a
|
|
17
17
|
* class alone never swallows a section.
|
|
18
18
|
*/
|
|
19
|
-
export declare function floatKind(element: Element): FloatKind | undefined;
|
|
19
|
+
export declare function floatKind(element: Element, ctx: HtmlContext): FloatKind | undefined;
|
|
20
20
|
/** Blocks for a node list, in document order. */
|
|
21
21
|
export declare function flowBlocks(nodes: Node[], ctx: HtmlContext): Block[];
|
|
22
22
|
/** Link-list words publishers put in captions and reference entries, not part of the text. */
|
|
@@ -24,6 +24,7 @@ export declare const LINK_LABEL: RegExp;
|
|
|
24
24
|
/**
|
|
25
25
|
* True for an element that holds only links and link-list words ("Download: PNG TIFF",
|
|
26
26
|
* "Full size image", "[DOI] [PubMed]"): navigation around a caption, not caption text.
|
|
27
|
+
* Text outside its links that runs past the subtree text cap is not a link list.
|
|
27
28
|
*/
|
|
28
|
-
export declare function isLinkList(element: Element): boolean;
|
|
29
|
+
export declare function isLinkList(element: Element, ctx: HtmlContext): boolean;
|
|
29
30
|
//# sourceMappingURL=blocks.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blocks.d.ts","sourceRoot":"","sources":["../../../src/formats/html/blocks.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"blocks.d.ts","sourceRoot":"","sources":["../../../src/formats/html/blocks.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAaH,OAAO,KAAK,EACV,KAAK,EAKN,MAAM,yBAAyB,CAAC;AAKjC,OAAO,EAAE,KAAK,WAAW,EAA2B,MAAM,cAAc,CAAC;AAezE,mGAAmG;AACnG,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,OAAO,GAAG,YAAY,CAAC;AAY1D;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,WAAW,GAAG,SAAS,GAAG,SAAS,CAuBnF;AA0DD,iDAAiD;AACjD,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,WAAW,GAAG,KAAK,EAAE,CAKnE;AAqJD,8FAA8F;AAC9F,eAAO,MAAM,UAAU,QACid,CAAC;AAEze;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,WAAW,GAAG,OAAO,CAStE"}
|
|
@@ -7,40 +7,15 @@
|
|
|
7
7
|
* @module src/formats/html/blocks
|
|
8
8
|
*/
|
|
9
9
|
import { childElements, childNodes, isElement, resolveUrl, TEXT_NODE, tagOf, textOfElement, } from '../../html/dom.js';
|
|
10
|
+
import { domMathTex } from '../../html/math.js';
|
|
10
11
|
import { readHtmlTable } from '../../html/tables.js';
|
|
11
12
|
import { append } from '../../model/extremes.js';
|
|
12
13
|
import { tableName, truncatedGridMessage } from '../../model/table-grid.js';
|
|
13
14
|
import { escapeInline } from '../../render/escape.js';
|
|
14
|
-
import {
|
|
15
|
+
import { collapseInline, emphasis } from '../../render/inline.js';
|
|
15
16
|
import { isFurniture, nameTokens } from './context.js';
|
|
16
|
-
import { inlineMarkdown, inlineText, isMathScript,
|
|
17
|
-
|
|
18
|
-
'address',
|
|
19
|
-
'article',
|
|
20
|
-
'blockquote',
|
|
21
|
-
'center',
|
|
22
|
-
'details',
|
|
23
|
-
'div',
|
|
24
|
-
'dl',
|
|
25
|
-
'figure',
|
|
26
|
-
'h1',
|
|
27
|
-
'h2',
|
|
28
|
-
'h3',
|
|
29
|
-
'h4',
|
|
30
|
-
'h5',
|
|
31
|
-
'h6',
|
|
32
|
-
'header',
|
|
33
|
-
'hr',
|
|
34
|
-
'main',
|
|
35
|
-
'ol',
|
|
36
|
-
'p',
|
|
37
|
-
'pre',
|
|
38
|
-
'section',
|
|
39
|
-
'summary',
|
|
40
|
-
'table',
|
|
41
|
-
'ul',
|
|
42
|
-
]);
|
|
43
|
-
const BLOCK_SELECTOR = [...BLOCK_TAGS].join(',');
|
|
17
|
+
import { inlineMarkdown, inlineText, isMathScript, scriptTex, warnImageFormula } from './inline.js';
|
|
18
|
+
import { BELOW, BLOCK_TAGS, below, ownText, plainText, shortText, textFacts, textLead, textLength, } from './subtree.js';
|
|
44
19
|
/** Tags a figure, table, or supplement container can have; class names on anything else don't make one. */
|
|
45
20
|
const CONTAINER_TAGS = new Set(['div', 'figure', 'section', 'li', 'details']);
|
|
46
21
|
const SUPPLEMENT_TOKENS = new Set([
|
|
@@ -55,7 +30,7 @@ const SUPPLEMENT_TOKENS = new Set([
|
|
|
55
30
|
* `tw`, `supplementary`) together with the image, table, or file link they wrap, so a
|
|
56
31
|
* class alone never swallows a section.
|
|
57
32
|
*/
|
|
58
|
-
export function floatKind(element) {
|
|
33
|
+
export function floatKind(element, ctx) {
|
|
59
34
|
const tag = tagOf(element);
|
|
60
35
|
if (tag === 'table')
|
|
61
36
|
return 'table';
|
|
@@ -68,31 +43,24 @@ export function floatKind(element) {
|
|
|
68
43
|
const supplementLike = tokens.some((t) => SUPPLEMENT_TOKENS.has(t));
|
|
69
44
|
if (!figureLike && !tableLike && !supplementLike)
|
|
70
45
|
return;
|
|
46
|
+
const inside = below(element, ctx);
|
|
71
47
|
// A class names a wrapper; one that holds sections is a layout region, not a float.
|
|
72
|
-
if (tag !== 'figure' &&
|
|
48
|
+
if (tag !== 'figure' && inside.kinds & BELOW.sectioning)
|
|
73
49
|
return;
|
|
74
50
|
if (supplementLike)
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
: undefined;
|
|
78
|
-
const tables = element.querySelectorAll('table');
|
|
79
|
-
if (tables.length > 1)
|
|
51
|
+
return inside.kinds & BELOW.link && inside.tables === 0 ? 'supplement' : undefined;
|
|
52
|
+
if (inside.tables > 1)
|
|
80
53
|
return;
|
|
81
|
-
if (tables
|
|
54
|
+
if (inside.tables === 1)
|
|
82
55
|
return 'table';
|
|
83
|
-
const hasImage =
|
|
56
|
+
const hasImage = (inside.kinds & BELOW.image) !== 0;
|
|
84
57
|
if (!hasImage && !tableLike)
|
|
85
58
|
return;
|
|
86
|
-
if (!hasImage && !/^\s*Table\b/i.test(
|
|
59
|
+
if (!hasImage && !/^\s*Table\b/i.test(textLead(element, ctx, 40)))
|
|
87
60
|
return;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
/** The first caption-like text in a container, to tell a table rendered as an image from a figure. */
|
|
93
|
-
function captionLead(element) {
|
|
94
|
-
const caption = Array.from(element.querySelectorAll('figcaption, caption, h1, h2, h3, h4, h5, h6, [class*="caption"], [class*="title"], [class*="label"]')).find((candidate) => textOfElement(candidate) && !insideFurniture(candidate, element));
|
|
95
|
-
return textOfElement(caption ?? element).slice(0, 40);
|
|
61
|
+
// The first caption-like text tells a table rendered as an image from a figure.
|
|
62
|
+
const caption = textLead(inside.firstCaption ?? element, ctx, 40);
|
|
63
|
+
return /^\s*(?:Supplementary\s+)?Table\b/i.test(caption) || !hasImage ? 'table' : 'figure';
|
|
96
64
|
}
|
|
97
65
|
/** True when `element` or an ancestor below `container` is furniture. */
|
|
98
66
|
function insideFurniture(element, container) {
|
|
@@ -103,15 +71,15 @@ function insideFurniture(element, container) {
|
|
|
103
71
|
return false;
|
|
104
72
|
}
|
|
105
73
|
function flushRun(flow) {
|
|
106
|
-
const text =
|
|
74
|
+
const text = collapseInline(flow.run);
|
|
107
75
|
if (text)
|
|
108
76
|
flow.blocks.push({ text, type: 'paragraph' });
|
|
109
77
|
flow.run = '';
|
|
110
78
|
}
|
|
111
|
-
function isBlockElement(element) {
|
|
79
|
+
function isBlockElement(element, ctx) {
|
|
112
80
|
return (BLOCK_TAGS.has(tagOf(element)) ||
|
|
113
|
-
floatKind(element) !== undefined ||
|
|
114
|
-
displayFormula(element) !== undefined);
|
|
81
|
+
floatKind(element, ctx) !== undefined ||
|
|
82
|
+
displayFormula(element, ctx) !== undefined);
|
|
115
83
|
}
|
|
116
84
|
function walk(nodes, flow, ctx) {
|
|
117
85
|
for (const node of nodes) {
|
|
@@ -135,11 +103,11 @@ function walk(nodes, flow, ctx) {
|
|
|
135
103
|
}
|
|
136
104
|
if (isFurniture(node))
|
|
137
105
|
continue;
|
|
138
|
-
if (isBlockElement(node)) {
|
|
106
|
+
if (isBlockElement(node, ctx)) {
|
|
139
107
|
flushRun(flow);
|
|
140
108
|
append(flow.blocks, renderBlock(node, ctx));
|
|
141
109
|
}
|
|
142
|
-
else if (tagOf(node) !== 'math' && node.
|
|
110
|
+
else if (tagOf(node) !== 'math' && below(node, ctx).kinds & BELOW.block) {
|
|
143
111
|
walk(childNodes(node), flow, ctx);
|
|
144
112
|
}
|
|
145
113
|
else {
|
|
@@ -155,7 +123,7 @@ export function flowBlocks(nodes, ctx) {
|
|
|
155
123
|
return flow.blocks;
|
|
156
124
|
}
|
|
157
125
|
function renderBlock(element, ctx) {
|
|
158
|
-
const formula = displayFormula(element);
|
|
126
|
+
const formula = displayFormula(element, ctx);
|
|
159
127
|
if (formula?.href) {
|
|
160
128
|
const href = resolveUrl(formula.href, ctx.baseUrl);
|
|
161
129
|
warnImageFormula(ctx, href, formula.id);
|
|
@@ -163,7 +131,7 @@ function renderBlock(element, ctx) {
|
|
|
163
131
|
}
|
|
164
132
|
if (formula)
|
|
165
133
|
return [formula];
|
|
166
|
-
const kind = floatKind(element);
|
|
134
|
+
const kind = floatKind(element, ctx);
|
|
167
135
|
if (kind === 'table')
|
|
168
136
|
return [tableBlock(element, ctx)];
|
|
169
137
|
if (kind === 'figure')
|
|
@@ -200,56 +168,68 @@ const EQUATION_NUMBER = /^\(?\s*([A-Z]?\d+(?:\.\d+)*[a-z]?)\s*\)?$/;
|
|
|
200
168
|
* often its number in a separate cell. One holding only an image of the formula (PLOS
|
|
201
169
|
* sets each as a `<span class="equation">` around it) yields that image's address.
|
|
202
170
|
*/
|
|
203
|
-
function displayFormula(element) {
|
|
171
|
+
function displayFormula(element, ctx) {
|
|
204
172
|
const tag = tagOf(element);
|
|
205
173
|
if (tag === 'math' && element.getAttribute('display') === 'block')
|
|
206
|
-
return formulaFrom(element, undefined);
|
|
174
|
+
return formulaFrom(element, undefined, false, ctx);
|
|
207
175
|
const tokens = nameTokens(element.getAttribute('class'));
|
|
208
176
|
if (tokens.includes('inline') || !tokens.some((t) => t === 'equation' || t === 'formula'))
|
|
209
177
|
return;
|
|
210
|
-
const
|
|
211
|
-
const
|
|
212
|
-
const
|
|
178
|
+
const inside = below(element, ctx);
|
|
179
|
+
const number = inside.firstNumbered && shortText(inside.firstNumbered, ctx);
|
|
180
|
+
const label = EQUATION_NUMBER.exec(number ?? '')?.[1];
|
|
181
|
+
const image = equationImage(element, inside, ctx);
|
|
213
182
|
if (image) {
|
|
214
183
|
const id = element.getAttribute('id') ?? undefined;
|
|
215
184
|
return { type: 'formula', href: image, ...(id && { id }), ...(label && { label }) };
|
|
216
185
|
}
|
|
217
186
|
if (!CONTAINER_TAGS.has(tag))
|
|
218
187
|
return;
|
|
219
|
-
if (
|
|
188
|
+
if (inside.kinds & (BELOW.table | BELOW.plainImage | BELOW.figure))
|
|
220
189
|
return;
|
|
221
|
-
return formulaFrom(element, label,
|
|
190
|
+
return formulaFrom(element, label, true, ctx);
|
|
222
191
|
}
|
|
223
192
|
/** The one image an equation container holds when it has no math, source, or text besides its number. */
|
|
224
|
-
function equationImage(element,
|
|
225
|
-
if (
|
|
193
|
+
function equationImage(element, inside, ctx) {
|
|
194
|
+
if (inside.kinds & (BELOW.math | BELOW.script | BELOW.table | BELOW.figure))
|
|
226
195
|
return;
|
|
227
|
-
|
|
228
|
-
if (images.length !== 1 || ownText(element, numberEl).trim())
|
|
196
|
+
if (inside.images !== 1 || ownText(element, ctx).besidesNumber.visible > 0)
|
|
229
197
|
return;
|
|
230
|
-
return
|
|
198
|
+
return inside.firstImage?.getAttribute('src') ?? undefined;
|
|
231
199
|
}
|
|
232
|
-
|
|
200
|
+
/** Delimiters that wrap a display formula's TeX written as text: `$$…$$`, `\[…\]`, `\(…\)`. */
|
|
201
|
+
const TEX_DELIMITERS = new Map([
|
|
202
|
+
['$$', '$$'],
|
|
203
|
+
['\\[', '\\]'],
|
|
204
|
+
['\\(', '\\)'],
|
|
205
|
+
]);
|
|
206
|
+
/**
|
|
207
|
+
* A formula from an equation container (or a display `<math>`): TeX from its MathML, else
|
|
208
|
+
* its math source script, else its own text when delimiters wrap it whole; else its text.
|
|
209
|
+
* `besidesNumber` leaves the container's equation number out of its own text. The
|
|
210
|
+
* subtree is read in full only for a formula it returns.
|
|
211
|
+
*/
|
|
212
|
+
function formulaFrom(element, label, besidesNumber, ctx) {
|
|
233
213
|
const id = element.getAttribute('id') ?? undefined;
|
|
234
|
-
const
|
|
235
|
-
const
|
|
236
|
-
|
|
214
|
+
const inside = below(element, ctx);
|
|
215
|
+
const math = tagOf(element) === 'math' ? element : inside.firstMath;
|
|
216
|
+
const script = inside.firstMathScript;
|
|
217
|
+
let tex = math ? cachedTex(math, domMathTex, ctx) : '';
|
|
237
218
|
if (!tex && script)
|
|
238
|
-
tex =
|
|
219
|
+
tex = cachedTex(script, scriptTex, ctx);
|
|
239
220
|
if (!tex) {
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
221
|
+
const own = ownText(element, ctx);
|
|
222
|
+
const { head, tail, visible } = besidesNumber ? own.besidesNumber : own.all;
|
|
223
|
+
// Delimiters around at least one character other than whitespace: the TeX between them.
|
|
224
|
+
if (visible === 5 && TEX_DELIMITERS.get(head) === tail) {
|
|
225
|
+
const exclude = besidesNumber ? inside.firstNumbered : undefined;
|
|
226
|
+
tex = sourceText(element, exclude).trim().slice(2, -2).trim();
|
|
227
|
+
}
|
|
247
228
|
}
|
|
248
|
-
const
|
|
229
|
+
const source = math ?? element;
|
|
230
|
+
const text = tex || textLength(source, ctx) === 0
|
|
249
231
|
? ''
|
|
250
|
-
:
|
|
251
|
-
.replace(EQUATION_NUMBER, '')
|
|
252
|
-
.trim();
|
|
232
|
+
: plainText(source, ctx).replace(EQUATION_NUMBER, '').trim();
|
|
253
233
|
if (!tex && !text)
|
|
254
234
|
return;
|
|
255
235
|
return {
|
|
@@ -259,13 +239,22 @@ function formulaFrom(element, label, exclude) {
|
|
|
259
239
|
...(tex ? { tex } : { text }),
|
|
260
240
|
};
|
|
261
241
|
}
|
|
242
|
+
/** The TeX `read` gives for a `<math>` or math script, read once per element. */
|
|
243
|
+
function cachedTex(element, read, ctx) {
|
|
244
|
+
let tex = ctx.subtrees.tex.get(element);
|
|
245
|
+
if (tex === undefined) {
|
|
246
|
+
tex = read(element);
|
|
247
|
+
ctx.subtrees.tex.set(element, tex);
|
|
248
|
+
}
|
|
249
|
+
return tex;
|
|
250
|
+
}
|
|
262
251
|
/** Text of an element without furniture and without one descendant (the equation number). */
|
|
263
|
-
function
|
|
252
|
+
function sourceText(element, exclude) {
|
|
264
253
|
return Array.from(element.childNodes)
|
|
265
254
|
.map((node) => {
|
|
266
255
|
if (!isElement(node))
|
|
267
256
|
return node.textContent ?? '';
|
|
268
|
-
return node === exclude || isFurniture(node) ? '' :
|
|
257
|
+
return node === exclude || isFurniture(node) ? '' : sourceText(node, exclude);
|
|
269
258
|
})
|
|
270
259
|
.join('');
|
|
271
260
|
}
|
|
@@ -279,26 +268,17 @@ export const LINK_LABEL = /^(?:\[?\s*(?:doi|pubmed(?: abstract| central)?|pmc(?:
|
|
|
279
268
|
/**
|
|
280
269
|
* True for an element that holds only links and link-list words ("Download: PNG TIFF",
|
|
281
270
|
* "Full size image", "[DOI] [PubMed]"): navigation around a caption, not caption text.
|
|
271
|
+
* Text outside its links that runs past the subtree text cap is not a link list.
|
|
282
272
|
*/
|
|
283
|
-
export function isLinkList(element) {
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
if (!text)
|
|
273
|
+
export function isLinkList(element, ctx) {
|
|
274
|
+
const text = shortText(element, ctx);
|
|
275
|
+
if (text === '')
|
|
287
276
|
return true;
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
.
|
|
293
|
-
.replace(/\b(?:download|view|export)s?\s*:?/gi, '');
|
|
294
|
-
return /^[\s[\]()|,;:.·•/-]*$/.test(outside);
|
|
295
|
-
}
|
|
296
|
-
function outsideLinks(node) {
|
|
297
|
-
if (!isElement(node))
|
|
298
|
-
return node.textContent ?? '';
|
|
299
|
-
if (tagOf(node) === 'a' || tagOf(node) === 'button')
|
|
300
|
-
return '';
|
|
301
|
-
return Array.from(node.childNodes).map(outsideLinks).join('');
|
|
277
|
+
const { links, outside } = textFacts(element, ctx);
|
|
278
|
+
if (!links)
|
|
279
|
+
return text !== undefined && LINK_LABEL.test(text);
|
|
280
|
+
return (outside !== undefined &&
|
|
281
|
+
/^[\s[\]()|,;:.·•/-]*$/.test(outside.replace(/\b(?:download|view|export)s?\s*:?/gi, '')));
|
|
302
282
|
}
|
|
303
283
|
/** Class tokens marking a container's caption text, and those marking its notes. */
|
|
304
284
|
const CAPTION_TOKENS = new Set([
|
|
@@ -335,6 +315,10 @@ function roleOf(element) {
|
|
|
335
315
|
*/
|
|
336
316
|
function captionPieces(container, content, ctx) {
|
|
337
317
|
const pieces = { caption: [], notes: [] };
|
|
318
|
+
// The elements between the container and its content, so each is known at a glance.
|
|
319
|
+
const aroundContent = new Set();
|
|
320
|
+
for (let el = content?.parentElement; el && el !== container; el = el.parentElement)
|
|
321
|
+
aroundContent.add(el);
|
|
338
322
|
let passed = false;
|
|
339
323
|
let run = [];
|
|
340
324
|
const push = (text, role) => {
|
|
@@ -342,7 +326,7 @@ function captionPieces(container, content, ctx) {
|
|
|
342
326
|
pieces[role ?? (passed ? 'notes' : 'caption')].push(text);
|
|
343
327
|
};
|
|
344
328
|
const flushInline = (role) => {
|
|
345
|
-
push(inlineMarkdown(run, ctx)
|
|
329
|
+
push(collapseInline(inlineMarkdown(run, ctx)), role);
|
|
346
330
|
run = [];
|
|
347
331
|
};
|
|
348
332
|
const visit = (element, inherited) => {
|
|
@@ -357,19 +341,19 @@ function captionPieces(container, content, ctx) {
|
|
|
357
341
|
continue;
|
|
358
342
|
}
|
|
359
343
|
const role = roleOf(node) ?? inherited;
|
|
360
|
-
if (
|
|
344
|
+
if (aroundContent.has(node)) {
|
|
361
345
|
flushInline(inherited);
|
|
362
346
|
visit(node, role);
|
|
363
347
|
continue;
|
|
364
348
|
}
|
|
365
349
|
if (isFurniture(node) || tagOf(node) === 'img' || tagOf(node) === 'picture')
|
|
366
350
|
continue;
|
|
367
|
-
|
|
351
|
+
const holdsBlock = (below(node, ctx).kinds & BELOW.block) !== 0;
|
|
352
|
+
if (isBlockElement(node, ctx) || holdsBlock) {
|
|
368
353
|
flushInline(inherited);
|
|
369
|
-
if (isLinkList(node))
|
|
354
|
+
if (isLinkList(node, ctx))
|
|
370
355
|
continue;
|
|
371
|
-
if (
|
|
372
|
-
!/^(p|figcaption|caption|h[1-6])$/.test(tagOf(node))) {
|
|
356
|
+
if (holdsBlock && !/^(p|figcaption|caption|h[1-6])$/.test(tagOf(node))) {
|
|
373
357
|
visit(node, role);
|
|
374
358
|
continue;
|
|
375
359
|
}
|
|
@@ -498,12 +482,30 @@ function tableBlock(container, ctx) {
|
|
|
498
482
|
};
|
|
499
483
|
}
|
|
500
484
|
// ─── Lists ──────────────────────────────────────────────────────────────────
|
|
485
|
+
/**
|
|
486
|
+
* A list: one item per `<li>`. Content between items (a list whose `<li>` tags were
|
|
487
|
+
* left out of markup nested too deep) is an item of its own, so its text is kept.
|
|
488
|
+
*/
|
|
501
489
|
function listBlock(element, ctx) {
|
|
502
|
-
const items =
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
490
|
+
const items = [];
|
|
491
|
+
let loose = [];
|
|
492
|
+
const flushLoose = () => {
|
|
493
|
+
if (loose.length > 0)
|
|
494
|
+
items.push(flowBlocks(loose, ctx));
|
|
495
|
+
loose = [];
|
|
496
|
+
};
|
|
497
|
+
for (const node of childNodes(element)) {
|
|
498
|
+
if (!isElement(node) || tagOf(node) !== 'li') {
|
|
499
|
+
loose.push(node);
|
|
500
|
+
}
|
|
501
|
+
else if (!isFurniture(node)) {
|
|
502
|
+
flushLoose();
|
|
503
|
+
items.push(flowBlocks(childNodes(node), ctx));
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
flushLoose();
|
|
507
|
+
const kept = items.filter((blocks) => blocks.length > 0);
|
|
508
|
+
return kept.length > 0 ? [{ type: 'list', items: kept, ordered: tagOf(element) === 'ol' }] : [];
|
|
507
509
|
}
|
|
508
510
|
/** A description list: `**term** — definition` per item. */
|
|
509
511
|
function descriptionList(element, ctx) {
|