@page2ai/core 0.1.12 → 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/README.md +16 -15
- package/dist/browser/index.js +3 -3
- package/dist/browser/index.js.map +1 -1
- package/dist/node/basic-renderer.d.ts +1 -0
- package/dist/node/basic-renderer.d.ts.map +1 -1
- package/dist/node/basic-renderer.js +264 -56
- package/dist/node/basic-renderer.js.map +1 -1
- package/dist/node/fetch-protected.d.ts +1 -0
- package/dist/node/fetch-protected.d.ts.map +1 -1
- package/dist/node/fetch-protected.js +18 -5
- package/dist/node/fetch-protected.js.map +1 -1
- package/dist/node/index.d.ts +1 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +209 -62
- package/dist/node/index.js.map +1 -1
- package/dist/shared/constants.d.ts +1 -1
- package/dist/shared/constants.d.ts.map +1 -1
- package/dist/shared/constants.js +1 -13
- package/dist/shared/constants.js.map +1 -1
- package/dist/shared/profiles.d.ts.map +1 -1
- package/dist/shared/profiles.js +0 -5
- package/dist/shared/profiles.js.map +1 -1
- package/dist/shared/types.d.ts +1 -11
- package/dist/shared/types.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
# @page2ai/core
|
|
2
2
|
|
|
3
3
|
> Webpage-to-Markdown extraction engine for LLM context (ChatGPT, Claude, Gemini).
|
|
4
|
-
>
|
|
5
|
-
>
|
|
4
|
+
> The engine of the [`@page2ai/mcp` server](https://github.com/igorsaevets/page2ai-mcp). The
|
|
5
|
+
> [Page2AI Chrome & Firefox extensions](https://github.com/igorsaevets/page2ai-extension) run their own
|
|
6
|
+
> pipeline in the browser (see below).
|
|
6
7
|
> Preserves code blocks with language hints, tables, and reading structure.
|
|
7
8
|
> Zero telemetry, MIT-licensed.
|
|
8
9
|
|
|
9
10
|
Works in Node.js (via linkedom) and in browsers.
|
|
10
11
|
|
|
11
12
|
- **Node.js**: `htmlToMarkdown(html, opts)` or `fetchAndConvert(url, opts)` — zero external API calls, uses linkedom for parsing
|
|
12
|
-
- **Browser**: exports `BrowserAdapter` and the `PageAdapter` interface; the full extract pipeline lives in the extension repo (
|
|
13
|
+
- **Browser**: exports `BrowserAdapter` and the `PageAdapter` interface; the full extract pipeline lives in the extension repo (a migration is planned)
|
|
13
14
|
- **Zero data collection**: no telemetry, no analytics, no third-party calls
|
|
14
15
|
- **MIT** licensed
|
|
15
16
|
|
|
@@ -49,8 +50,8 @@ Converts an HTML string to Markdown. Synchronous.
|
|
|
49
50
|
- `opts.baseUrl` (string) — URL used to resolve relative links (default: `'about:blank'`)
|
|
50
51
|
- `opts.profile` (string) — accepted, but **ignored by the Node build in this version**: `'dashboard'`, `'marketing'` and the rest produce the same output here. The profiles switch the browser extension's live-page pipeline (clicking tabs, waiting for lazy content), which the Node renderer does not run
|
|
51
52
|
- `opts.includeFrontmatter` (boolean) — emit YAML frontmatter block (default: `true`)
|
|
52
|
-
- `opts.includeImages` (boolean) — emit image links (default: `true`)
|
|
53
|
-
- `opts.includeHidden` (boolean) — also convert content under the HTML `hidden` attribute, which a browser does not show (default: `false`). Tab panels, `hidden="until-found"` sections, hidden
|
|
53
|
+
- `opts.includeImages` (boolean) — emit image links (default: `true`). Inside a table cell an image is written as its alt text either way, and an image with an empty alt or `aria-hidden="true"` adds nothing
|
|
54
|
+
- `opts.includeHidden` (boolean) — also convert content under the HTML `hidden` attribute, which a browser does not show (default: `false`). Tab panels, `hidden="until-found"` sections and React streaming segments are converted either way, and a hidden block that holds code contributes its code blocks and nothing else. Code, link names and tab labels never take text from a hidden element. A `<main>` under `hidden` is not taken as the page's main content while a visible one exists, and one inside a hidden tab panel or `until-found` section never is. The default is not a security boundary: those kept kinds are set by the page's own attributes, so a page can still put text a reader does not see into the output that way, and the converter reads no CSS (`display: none` is converted)
|
|
54
55
|
|
|
55
56
|
Returns `{ markdown, title, baseUrl, charCount, extractedAt }`. When nothing can be extracted the Markdown says so with `<!-- AI: page has no extractable body content -->` instead of coming back empty.
|
|
56
57
|
|
|
@@ -63,22 +64,23 @@ Fetches a URL and converts it to Markdown. Async.
|
|
|
63
64
|
- `opts.userAgent` (string) — custom user agent (default: `page2ai-core/0.1`)
|
|
64
65
|
- ...all `htmlToMarkdown` options
|
|
65
66
|
|
|
66
|
-
Uses Node 18+ built-in `fetch()`. Follows redirects; passes the final URL to `htmlToMarkdown` as `baseUrl` unless overridden.
|
|
67
|
+
Uses Node 18+ built-in `fetch()`. Follows redirects; passes the final URL to `htmlToMarkdown` as `baseUrl` unless overridden. It requests only the URL you pass: no `<url>.md` twin, no `llms.txt`, and the Accept header is the one Firefox sends when it opens a page (`text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`), so a docs platform that negotiates (Fumadocs, Mintlify, Vercel, Supabase, Stripe, the Claude docs) answers with the page a person sees, not its own Markdown copy, which can be older than the page. A URL whose path names a Markdown or text file (`.md`, `.markdown`, `.mdown`, `.mkd`, `.mdx`, `.txt`) is the page and is returned as is, whether the server sends it as `text/markdown`, `text/x-markdown`, `text/plain` (GitHub raw does) or `application/octet-stream`. Markdown sent for any other address is refused with a `FetchProtectionError`: the request asked for HTML, so that Markdown is the site's copy, not the page. `source` in the result says which way it came (`html-parse` or `markdown-response`), and `includeFrontmatter` applies on both. The body is decoded as UTF-8 whatever charset the server declares, so a page in another encoding comes out garbled.
|
|
67
68
|
|
|
68
69
|
The fetch refuses anything that is not public, on the URL and on every redirect hop, after DNS resolution: `localhost`, loopback `127.0.0.0/8` and `::1`, `0.0.0.0/8`, private `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`, link-local `169.254.0.0/16` (cloud metadata) and `fe80::/10`, CGNAT `100.64.0.0/10` (this includes Tailscale addresses), IPv6 unique-local `fc00::/7`, IPv4-mapped IPv6, the unspecified address `::`, and the hostnames `metadata` and `metadata.google.internal`. So a page on your own intranet or tailnet cannot be fetched this way: fetch the HTML yourself and pass it to `htmlToMarkdown`. `assertSafeUrlResolved(url)` runs the same check without fetching, and `findIpBlockReason(address)` applies its address rule; both are exported.
|
|
69
70
|
|
|
70
71
|
## What it does
|
|
71
72
|
|
|
72
73
|
- Parses HTML with [linkedom](https://github.com/WebReflection/linkedom) (fast, browser-DOM-fidelity, no layout engine)
|
|
73
|
-
-
|
|
74
|
-
-
|
|
74
|
+
- Places React streaming content first. A page streamed by React (Next.js App Router, Remix, Mintlify) sends a fallback such as "Loading..." where content will go and the content itself later, hidden at the end of the page; the page's own inline scripts (`$RC`, `$RR`, `$RS`) move it into place in a browser. The converter runs no scripts, so it makes those moves itself, in document order, and drops the fallback, only where a browser runs the call: in a classic script (not one with `src`, `nomodule` or a non-JavaScript `type`, not inside `<noscript>` or `<template>`), as a statement of its own, on nodes parsed before that script and on the markup React writes for them. What a script would do when run is not checked: call text in a script that redefines `$RC` still moves a segment. A segment whose boundary is gone is dropped, as React drops it. A hidden segment that no call places stays where it stands: inside the content root it is converted there, outside it is left out. React's external runtime (`<template data-rci>`, an opt-in that no page we sampled uses) is not replayed
|
|
75
|
+
- Picks a content root: `<main>` (else `[role="main"]`, else `<body>`), replaced by its largest `<article>` when that article holds at least half of the root's text or is three times larger than every other `<article>`. Text outside the root (beside `<main>`, or beside such an article) is not converted
|
|
76
|
+
- Emits: headings (`# `), paragraphs, lists (ordered/unordered, nested), links `[text](url)`, images ``, code blocks (with language tag when detectable), tables (`| a | b |`; a cell spanning columns or rows leaves empty cells, so every value stays under its header; a span ends with its row group, as browsers lay it out, and a table whose spans would cover more than 100,000 cells is written in the plain layout, cells in source order), blockquotes, definition lists (`**term**` then `: definition`), inline formatting (bold, italic, code). Text sitting directly in a `<div>`, `<section>` or `<span>` is kept as its own paragraph, so dashboards and admin pages built from bare `<div>`s convert too
|
|
75
77
|
- Extracts metadata into YAML frontmatter: `title`, `source`, `captured_at`, `language`, `description`, `canonical`, `og_title`, `og_description`, `author`, `published`, `extractor`, `extractor_version`
|
|
76
78
|
|
|
77
|
-
## What it does NOT do
|
|
79
|
+
## What it does NOT do
|
|
78
80
|
|
|
79
81
|
- No tab/dropdown expansion (browser-only interactive machinery)
|
|
80
82
|
- No lazy-load activation (Node has no runtime images)
|
|
81
|
-
- No `<details>`
|
|
83
|
+
- No `<details>` clicking, and none is needed: the body of a closed `<details>` is in the HTML and is converted with the rest of the page
|
|
82
84
|
- No computed-style-based visibility filtering (linkedom has no layout engine)
|
|
83
85
|
- No badge detection via font weight
|
|
84
86
|
- No shadow DOM traversal (linkedom has no shadow DOM support)
|
|
@@ -88,10 +90,9 @@ The fetch refuses anything that is not public, on the URL and on every redirect
|
|
|
88
90
|
|
|
89
91
|
## Roadmap
|
|
90
92
|
|
|
91
|
-
- **
|
|
92
|
-
- **
|
|
93
|
-
- **
|
|
94
|
-
- **v0.4** — official-markdown fallback (llms.txt, .md sibling URLs), MDX handling
|
|
93
|
+
- **Now** — Node basic renderer, browser adapter interface published; `fetchAndConvert` reads only the page (0.2.0, BL-49)
|
|
94
|
+
- **Next** — full port of `lib/core/html-to-md.ts` via PageAdapter, browser entry migrated from extension repo
|
|
95
|
+
- **Later** — headless-browser adapter (Playwright or Puppeteer) for React/Vue/SPA rendering, profile auto-detection in Node path, structured-data extraction. A client-rendered page that ships a nearly empty `<div id="root">` in its static HTML produces a graceful fallback error until then; the docs platforms above render on the server and convert today.
|
|
95
96
|
|
|
96
97
|
## Architecture
|
|
97
98
|
|
|
@@ -109,7 +110,7 @@ src/
|
|
|
109
110
|
│ ├── index.ts
|
|
110
111
|
│ ├── linkedom-adapter.ts
|
|
111
112
|
│ └── basic-renderer.ts
|
|
112
|
-
└── browser/ # BrowserAdapter (
|
|
113
|
+
└── browser/ # BrowserAdapter; extract() comes with the port
|
|
113
114
|
├── index.ts
|
|
114
115
|
└── browser-adapter.ts
|
|
115
116
|
```
|
package/dist/browser/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @page2ai/core browser entry point.
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
// still lives in the Page2AI extension repo (lib/core/*).
|
|
5
|
-
// the extension to import from this package.
|
|
3
|
+
// SCOPE: exports the BrowserAdapter only. The full extract() pipeline
|
|
4
|
+
// still lives in the Page2AI extension repo (lib/core/*). A later release may
|
|
5
|
+
// migrate the extension to import from this package.
|
|
6
6
|
export { BrowserAdapter } from './browser-adapter.js';
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/browser/index.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,EAAE;AACF,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/browser/index.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,EAAE;AACF,sEAAsE;AACtE,8EAA8E;AAC9E,qDAAqD;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basic-renderer.d.ts","sourceRoot":"","sources":["../../src/node/basic-renderer.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAkC7D,eAAO,MAAM,aAAa,GAAI,KAAK,QAAQ,KAAG,MACwC,CAAC;AAEvF,UAAU,UAAU;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,kBAAkB,EAAE,OAAO,CAAC;IAC5B,aAAa,EAAE,OAAO,CAAC;IAEvB,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB,MAAM,CAAC,EAAE,OAAO,CAAC;IAGjB,KAAK,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"basic-renderer.d.ts","sourceRoot":"","sources":["../../src/node/basic-renderer.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAkC7D,eAAO,MAAM,aAAa,GAAI,KAAK,QAAQ,KAAG,MACwC,CAAC;AAEvF,UAAU,UAAU;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,kBAAkB,EAAE,OAAO,CAAC;IAC5B,aAAa,EAAE,OAAO,CAAC;IAEvB,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB,MAAM,CAAC,EAAE,OAAO,CAAC;IAGjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAkBD,eAAO,MAAM,mBAAmB,GAC9B,KAAK,QAAQ,EACb,SAAS,WAAW,EACpB,MAAM,UAAU,KACf,MAiCF,CAAC"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
// Basic Markdown renderer for the
|
|
1
|
+
// Basic Markdown renderer for the Node build. Covers the essentials:
|
|
2
2
|
// headings, paragraphs, lists, links, images, code blocks, tables, blockquotes,
|
|
3
3
|
// inline formatting, YAML frontmatter, plus STATIC tab discovery emitting
|
|
4
4
|
// '## Tab: {label}' sections (Spark P20 fix — prevents Python+TypeScript panel
|
|
5
5
|
// concatenation on docs.anthropic.com, platform.openai.com, Mintlify, etc.).
|
|
6
6
|
// NOT a port of the full extension renderer.
|
|
7
7
|
//
|
|
8
|
-
//
|
|
9
|
-
// PageAdapter, once the browser renderer
|
|
8
|
+
// A later release may replace this with the full lib/core/html-to-md.ts
|
|
9
|
+
// pipeline via PageAdapter, once the browser renderer accepts an adapter.
|
|
10
10
|
import { CORE_VERSION } from '../shared/constants.js';
|
|
11
11
|
import { absUrl, chooseCodeFence, cleanInline, escapeMd, longestRun } from '../shared/utils.js';
|
|
12
12
|
// HEAD/TITLE: the root can be documentElement (see usableBody). The form widgets and embeds hold
|
|
@@ -40,7 +40,7 @@ const yq = (s) => '"' + String(s).replace(/"/g, '\\"') + '"';
|
|
|
40
40
|
export const documentTitle = (doc) => [...doc.querySelectorAll('title')].find((t) => !t.closest('svg'))?.textContent || '';
|
|
41
41
|
export const renderBasicMarkdown = (doc, adapter, opts) => {
|
|
42
42
|
const parts = [];
|
|
43
|
-
const root = pickContentRoot(doc);
|
|
43
|
+
const root = pickContentRoot(doc, opts);
|
|
44
44
|
const rawTitle = cleanInline(documentTitle(doc));
|
|
45
45
|
// The site suffix is dropped, not relocated: an earlier draft emitted it as a `site_name:`
|
|
46
46
|
// frontmatter field, which put the exact chrome string the cleanup had just removed back
|
|
@@ -52,7 +52,7 @@ export const renderBasicMarkdown = (doc, adapter, opts) => {
|
|
|
52
52
|
if (title)
|
|
53
53
|
parts.push(`# ${title}`, '');
|
|
54
54
|
if (root && root.children) {
|
|
55
|
-
const tabGroups = discoverTabGroups(root);
|
|
55
|
+
const tabGroups = discoverTabGroups(root, opts);
|
|
56
56
|
const capturedPanelEls = new WeakSet();
|
|
57
57
|
tabGroups.forEach((g) => g.panels.forEach((p) => capturedPanelEls.add(p.el)));
|
|
58
58
|
const lines = [];
|
|
@@ -255,11 +255,24 @@ const usableBody = (doc) => {
|
|
|
255
255
|
return body;
|
|
256
256
|
return doc.documentElement || body || null;
|
|
257
257
|
};
|
|
258
|
-
const pickContentRoot = (doc) => {
|
|
258
|
+
const pickContentRoot = (doc, opts) => {
|
|
259
259
|
// The semantic container first. <main> is unique per document by spec, so unlike <article>
|
|
260
|
-
// it cannot be a component.
|
|
261
|
-
|
|
262
|
-
|
|
260
|
+
// it cannot be a component. Unique among the SHOWN ones: a page may hold several <main> elements as long as all but
|
|
261
|
+
// one are hidden (HTML, "the main element"), and a hidden one became the root and printed its whole text (C7c, codex).
|
|
262
|
+
// So a <main> under no `hidden` at all comes first, one inside a React streaming segment only when there is none, and
|
|
263
|
+
// one under a hidden tab panel or `until-found` section never: those are shown as one part of the page, so as the
|
|
264
|
+
// root they printed only themselves and dropped the visible <main> (T124 panel: codex, grok, mimo, spark, agy).
|
|
265
|
+
const clearUp = (e, exempt) => {
|
|
266
|
+
for (let a = e; a; a = a.parentElement)
|
|
267
|
+
if (underHiddenAttr(a, opts) && !exempt(a))
|
|
268
|
+
return false;
|
|
269
|
+
return true;
|
|
270
|
+
};
|
|
271
|
+
const pick = (selector) => {
|
|
272
|
+
const all = [...doc.querySelectorAll(selector)];
|
|
273
|
+
return all.find((e) => clearUp(e, () => false)) || all.find((e) => clearUp(e, isReactSegment));
|
|
274
|
+
};
|
|
275
|
+
const semantic = pick('main') || pick('[role="main"]') || null;
|
|
263
276
|
const container = semantic || usableBody(doc);
|
|
264
277
|
if (!container)
|
|
265
278
|
return null;
|
|
@@ -269,7 +282,9 @@ const pickContentRoot = (doc) => {
|
|
|
269
282
|
// comments. On docs.astro.build the first <article> is `class="card sl-flex"` holding 155
|
|
270
283
|
// characters while <main> holds 1,362, so the tool emitted a heading, one question and one
|
|
271
284
|
// sentence for the whole page. Our own documentation site runs that framework.
|
|
285
|
+
// An <article> under a hidden ancestor is not a candidate: as the root it would print what the browser hides.
|
|
272
286
|
const articles = Array.from(container.querySelectorAll('article'))
|
|
287
|
+
.filter((a) => !hiddenUpTo(a, container, opts))
|
|
273
288
|
.map((a) => ({ el: a, n: contentTextLength(a) }))
|
|
274
289
|
.sort((x, y) => y.n - x.n);
|
|
275
290
|
if (articles.length) {
|
|
@@ -300,7 +315,7 @@ const pickContentRoot = (doc) => {
|
|
|
300
315
|
// Static tab discovery: aria-controls chains, role="tablist" siblings, and
|
|
301
316
|
// data-state attribute pairs. Emits '## Tab: {label}' sections. Does NOT
|
|
302
317
|
// click — for Node we treat each panel as ALREADY present in the DOM.
|
|
303
|
-
const discoverTabGroups = (root) => {
|
|
318
|
+
const discoverTabGroups = (root, opts) => {
|
|
304
319
|
const groups = [];
|
|
305
320
|
const seenContainers = new WeakSet();
|
|
306
321
|
// Path 1: [role="tablist"] with aria-controls chain
|
|
@@ -310,7 +325,8 @@ const discoverTabGroups = (root) => {
|
|
|
310
325
|
const tabs = [...tl.querySelectorAll('[role="tab"], button')];
|
|
311
326
|
const panels = [];
|
|
312
327
|
for (const tab of tabs) {
|
|
313
|
-
|
|
328
|
+
// A tab's label is its name, without the text of hidden elements (T124 panel: `One<span hidden>x</span>` read Onex).
|
|
329
|
+
const label = cleanInline(nameText(tab, opts));
|
|
314
330
|
if (!label)
|
|
315
331
|
continue;
|
|
316
332
|
const controls = tab.getAttribute('aria-controls');
|
|
@@ -321,7 +337,9 @@ const discoverTabGroups = (root) => {
|
|
|
321
337
|
if (!doc)
|
|
322
338
|
continue;
|
|
323
339
|
const panel = doc.getElementById(controls);
|
|
324
|
-
|
|
340
|
+
// A panel outside the content root is not part of it: a hidden block anywhere on the page was one aria-controls
|
|
341
|
+
// away from being printed (T124 panel: ocmimo).
|
|
342
|
+
if (!panel || !root.contains(panel))
|
|
325
343
|
continue;
|
|
326
344
|
panels.push({ label, el: panel });
|
|
327
345
|
}
|
|
@@ -348,7 +366,7 @@ const discoverTabGroups = (root) => {
|
|
|
348
366
|
const labelledText = labelledBy && doc
|
|
349
367
|
? labelledBy
|
|
350
368
|
.split(/\s+/)
|
|
351
|
-
.map((id) => doc.getElementById(id)
|
|
369
|
+
.map((id) => { const e = doc.getElementById(id); return e ? nameText(e, opts) : ''; })
|
|
352
370
|
.join(' ')
|
|
353
371
|
.replace(/\s+/g, ' ')
|
|
354
372
|
.trim()
|
|
@@ -409,8 +427,9 @@ const walkNodes = (nodes, out, opts, tabGroups, capturedPanelEls, dedupe) => {
|
|
|
409
427
|
if (node.nodeType !== 1 /* ELEMENT_NODE */)
|
|
410
428
|
continue;
|
|
411
429
|
const c = node;
|
|
412
|
-
// Skipped where they stand, without ending the run: renderNode would drop them anyway.
|
|
413
|
-
|
|
430
|
+
// Skipped where they stand, without ending the run: renderNode would drop them anyway. A hidden block that holds code
|
|
431
|
+
// goes on to renderNode, which prints only its code.
|
|
432
|
+
if (SKIP_TAGS.has(c.tagName) || hiddenKind(c, opts) === 'hidden')
|
|
414
433
|
continue;
|
|
415
434
|
const phrasing = PHRASING_TAGS.has(c.tagName) || (parentHasText && c.tagName.includes('-'));
|
|
416
435
|
if (phrasing && !capturedPanelEls.has(c) && !tabGroups.some((g) => g.container === c)) {
|
|
@@ -448,11 +467,21 @@ const renderNode = (el, out, opts, tabGroups, capturedPanelEls, dedupe) => {
|
|
|
448
467
|
const tag = el.tagName;
|
|
449
468
|
if (SKIP_TAGS.has(tag))
|
|
450
469
|
return;
|
|
451
|
-
if (isAriaHidden(el)
|
|
470
|
+
if (isAriaHidden(el))
|
|
471
|
+
return;
|
|
472
|
+
const hidden = hiddenKind(el, opts);
|
|
473
|
+
if (hidden === 'hidden')
|
|
452
474
|
return;
|
|
475
|
+
if (hidden === 'code') {
|
|
476
|
+
for (const pre of outerPres(el, opts))
|
|
477
|
+
renderCodeBlock(pre, out, opts);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
453
480
|
if (/^H[1-6]$/.test(tag)) {
|
|
454
481
|
const level = Number(tag.slice(1));
|
|
455
|
-
|
|
482
|
+
// A heading is one line, so a <br> in it, in its text or its code, is a space (T124 panel: mimo,
|
|
483
|
+
// `<h2>Run <code>a<br>b</code> now</h2>` put the rest of the heading into a paragraph under it).
|
|
484
|
+
const text = renderInline(el, opts).replace(/\s*\n\s*/g, ' ').trim();
|
|
456
485
|
if (text) {
|
|
457
486
|
if (dedupe.pendingTitle) {
|
|
458
487
|
// Match on textContent, not the rendered markdown: escaping and link syntax
|
|
@@ -467,7 +496,7 @@ const renderNode = (el, out, opts, tabGroups, capturedPanelEls, dedupe) => {
|
|
|
467
496
|
return;
|
|
468
497
|
}
|
|
469
498
|
if (tag === 'PRE') {
|
|
470
|
-
renderCodeBlock(el, out);
|
|
499
|
+
renderCodeBlock(el, out, opts);
|
|
471
500
|
return;
|
|
472
501
|
}
|
|
473
502
|
if (tag === 'P') {
|
|
@@ -609,7 +638,7 @@ const renderListItem = (li, marker, out, opts, tabGroups, capturedPanelEls, dedu
|
|
|
609
638
|
for (const node of li.childNodes) {
|
|
610
639
|
if (node.nodeType === 1 /* ELEMENT_NODE */) {
|
|
611
640
|
const c = node;
|
|
612
|
-
if (
|
|
641
|
+
if (hiddenKind(c, opts) === 'hidden')
|
|
613
642
|
continue;
|
|
614
643
|
if (c.tagName === 'P') {
|
|
615
644
|
flushInline();
|
|
@@ -635,28 +664,53 @@ const renderListItem = (li, marker, out, opts, tabGroups, capturedPanelEls, dedu
|
|
|
635
664
|
}
|
|
636
665
|
flushInline();
|
|
637
666
|
};
|
|
638
|
-
const renderInline = (el, opts, excludeEls) => renderInlineNodes([...el.childNodes], opts, excludeEls);
|
|
667
|
+
const renderInline = (el, opts, excludeEls) => renderInlineNodes(hiddenKind(el, opts) === 'code' ? outerPres(el, opts) : [...el.childNodes], opts, excludeEls);
|
|
639
668
|
const isAriaHidden = (el) => (el.getAttribute('aria-hidden') || '').toLowerCase() === 'true';
|
|
640
|
-
|
|
641
|
-
// it by default (includeHiddenMeaningfulText: false). It used to be converted (P2A-012: a grid's 30 closed details
|
|
642
|
-
// rows came out as table rows), and hidden text is a channel for instructions that the reader of the page never sees.
|
|
643
|
-
// Four kinds stay, each measured on the benchmark corpus: `hidden="until-found"` (a collapsed section the browser's
|
|
644
|
-
// find reveals); tab panels, which are all rendered as the extension clicks every tab (Starlight and Biome hide the
|
|
645
|
-
// inactive ones); a block holding code, which is a code tab the page did not mark up as one (docs.sentry.io keeps
|
|
646
|
-
// its yarn, pnpm, Loader and CDN variants in bare `<div hidden>`s: dropping them lost 5 unique code blocks) or a copy
|
|
647
|
-
// buffer repeating visible code; and React streaming segments: React SSR (Next.js, Mintlify) sends late Suspense
|
|
648
|
-
// content as `<div hidden id="S:0">`, or `<table hidden><tbody id="S:0">` in a table, and a script moves it into place.
|
|
649
|
-
const isHiddenEl = (el, opts) => {
|
|
669
|
+
const hiddenKind = (el, opts) => {
|
|
650
670
|
if (opts.includeHidden || !el.hasAttribute('hidden'))
|
|
651
|
-
return
|
|
671
|
+
return 'shown';
|
|
652
672
|
if ((el.getAttribute('hidden') || '').trim().toLowerCase() === 'until-found')
|
|
673
|
+
return 'shown';
|
|
674
|
+
// A tab panel is a block: all 27 on the corpus are <div>s, and `<a><span hidden role="tabpanel">` printed its text
|
|
675
|
+
// as the link's (T124 panel: mimo).
|
|
676
|
+
if ((el.getAttribute('role') === 'tabpanel' && !PHRASING_TAGS.has(el.tagName)) || el.tagName === 'PRE' || isReactSegment(el))
|
|
677
|
+
return 'shown';
|
|
678
|
+
return el.querySelector('pre') ? 'code' : 'hidden';
|
|
679
|
+
};
|
|
680
|
+
const isHiddenEl = (el, opts) => hiddenKind(el, opts) !== 'shown';
|
|
681
|
+
// Under the attribute at all, the kept kinds included. Code, a link's name, a tab's label and the page's <main> must not
|
|
682
|
+
// come from a hidden element of any kind (T124 panel: `<a><span hidden role="tabpanel">x</span></a>` was named x, and a
|
|
683
|
+
// <main> inside a hidden tab panel became the root and dropped the visible <main>).
|
|
684
|
+
const underHiddenAttr = (el, opts) => !opts.includeHidden && el.hasAttribute('hidden');
|
|
685
|
+
// React streaming segments as react-dom's server writes them (ReactFizzConfigDOM.js, startSegment*): `<div hidden id=…>`,
|
|
686
|
+
// in a table context `<table hidden id=…>` or a `<table hidden>` whose <tbody>, <tr> or <colgroup> carries the id. The id
|
|
687
|
+
// is the render's identifierPrefix, `S:` and the segment number in hex. A child's `S:` id counted on any hidden element
|
|
688
|
+
// (C14, grok), and so did any element and anything after `S:`: `<img hidden id="S:0">` printed its alt and `id="S:evil"`
|
|
689
|
+
// passed (T124 panel: codex, mimo). An HTML parser puts a <tbody> around `<table><tr>`, so a saved or re-serialised page
|
|
690
|
+
// has `<table hidden><tbody><tr id=…>` (grok).
|
|
691
|
+
const SEGMENT_ID = /S:[0-9a-f]+$/;
|
|
692
|
+
const isReactSegment = (el) => {
|
|
693
|
+
if (el.tagName === 'DIV')
|
|
694
|
+
return SEGMENT_ID.test(el.id);
|
|
695
|
+
if (el.tagName !== 'TABLE')
|
|
653
696
|
return false;
|
|
654
|
-
if (el.
|
|
655
|
-
return
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
return !(/^S:/.test(el.id) || [...el.children].some((c) => /^S:/.test(c.id)));
|
|
697
|
+
if (SEGMENT_ID.test(el.id))
|
|
698
|
+
return true;
|
|
699
|
+
return [...el.children].some((c) => (/^(?:TBODY|TR|COLGROUP)$/.test(c.tagName) && SEGMENT_ID.test(c.id)) ||
|
|
700
|
+
(c.tagName === 'TBODY' && !c.id && [...c.children].some((r) => r.tagName === 'TR' && SEGMENT_ID.test(r.id))));
|
|
659
701
|
};
|
|
702
|
+
// The <pre> elements of a block, outermost only, without those in a hidden list item, row, cell or other entry of a
|
|
703
|
+
// structure: such an entry is left out whole wherever it stands (T124 panel: codex, `<div hidden><ul><li hidden><pre>`
|
|
704
|
+
// printed the code that the same <li hidden> outside a hidden block drops).
|
|
705
|
+
const HIDDEN_ENTRY = /^(?:LI|TR|TD|TH|CAPTION|DT|DD|THEAD|TBODY|TFOOT)$/;
|
|
706
|
+
const outerPres = (el, opts) => [...el.querySelectorAll('pre')].filter((p) => {
|
|
707
|
+
if (p.parentElement?.closest('pre'))
|
|
708
|
+
return false;
|
|
709
|
+
for (let e = p.parentElement; e && e !== el; e = e.parentElement)
|
|
710
|
+
if (HIDDEN_ENTRY.test(e.tagName) && isHiddenEl(e, opts))
|
|
711
|
+
return false;
|
|
712
|
+
return true;
|
|
713
|
+
});
|
|
660
714
|
// A line of page TEXT that starts with Markdown block syntax becomes that block: `# x` a heading, `> x`
|
|
661
715
|
// a quote, `1. x` a list, `---` a rule, `<div` raw HTML, and an unclosed ``` fence swallows every line
|
|
662
716
|
// after it (T109 panel: `<div>```</div><p>AFTER</p>` put AFTER inside a code block). The renderer's own
|
|
@@ -702,9 +756,81 @@ const linkText = (a, opts, href, excludeEls, trim = true) => {
|
|
|
702
756
|
const inner = renderInline(a, href ? { ...opts, inLink: true } : opts, excludeEls).replace(/[ \t]*\n[ \t]*/g, ' ');
|
|
703
757
|
if (inner.trim())
|
|
704
758
|
return trim ? inner.trim() : inner;
|
|
705
|
-
const fallback = cleanInline(a
|
|
759
|
+
const fallback = cleanInline(linkName(a, opts));
|
|
706
760
|
return href ? escapeLinkText(fallback) : fallback;
|
|
707
761
|
};
|
|
762
|
+
// The name of a link whose content renders to nothing: its text, which keeps an icon's SVG <title>, as textContent did.
|
|
763
|
+
// textContent also returned the text of hidden elements (C7a, codex: `<a><span hidden>x</span></a>` printed x) and of a
|
|
764
|
+
// <style> or <script>, which name nothing. An image's alt is not used here: in a table cell the image already renders as
|
|
765
|
+
// its alt (C11a), and with includeImages: false an image link stays out, as the option says (the alt fallback wrote
|
|
766
|
+
// `[User avatar](profile)` before every author on the phpBB corpus page, T124 A/B).
|
|
767
|
+
const linkName = (a, opts) => nameText(a, opts);
|
|
768
|
+
// The text an element is named by, a link that renders empty or a tab: its text without what is not drawn, which is a
|
|
769
|
+
// hidden element of any kind, the fallback of a skipped element (<noscript>, <canvas>, <object>, <video>), and inside an
|
|
770
|
+
// SVG the parts SVG does not render (<defs>, <symbol>, <desc>, <metadata>). T124 panel: codex, grok, agy. An SVG's
|
|
771
|
+
// <title> and <text> stay: the <title> names an icon.
|
|
772
|
+
const SVG_UNDRAWN = /^(?:DEFS|SYMBOL|DESC|METADATA|STYLE|SCRIPT|CLIPPATH|MASK|PATTERN|MARKER|LINEARGRADIENT|RADIALGRADIENT|FILTER)$/;
|
|
773
|
+
const nameText = (el, opts) => {
|
|
774
|
+
let s = '';
|
|
775
|
+
const walk = (n, svg) => {
|
|
776
|
+
if (n.nodeType === 3 /* TEXT_NODE */) {
|
|
777
|
+
s += n.textContent || '';
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
if (n.nodeType !== 1 /* ELEMENT_NODE */)
|
|
781
|
+
return;
|
|
782
|
+
const e = n;
|
|
783
|
+
const tag = e.tagName.toUpperCase();
|
|
784
|
+
if (underHiddenAttr(e, opts) || (svg ? SVG_UNDRAWN.test(tag) : SKIP_TAGS.has(tag) && tag !== 'SVG'))
|
|
785
|
+
return;
|
|
786
|
+
for (const c of e.childNodes)
|
|
787
|
+
walk(c, svg || tag === 'SVG');
|
|
788
|
+
};
|
|
789
|
+
for (const c of el.childNodes)
|
|
790
|
+
walk(c, false);
|
|
791
|
+
return s;
|
|
792
|
+
};
|
|
793
|
+
// The text of code as a browser lays it out: a <br> is a line break, and hidden elements are left out. textContent
|
|
794
|
+
// skips the first and keeps the second: Docusaurus ends every line of a code block with a <br> and writes no newline
|
|
795
|
+
// character, so `cd my-website` and `npx docusaurus start` came out as one line (benchmark corpus, T124), `x<br>y` in
|
|
796
|
+
// inline code read `xy` (C5, codex), and `<code>a<span hidden>s</span>b</code>` printed the hidden s (C7b, codex).
|
|
797
|
+
// Outside a <pre> a newline in the source is only whitespace, so inline code marks a <br> with BR_MARK instead.
|
|
798
|
+
const BR_MARK = '\u0000';
|
|
799
|
+
const codeText = (el, opts, br = '\n') => {
|
|
800
|
+
let s = '';
|
|
801
|
+
const walk = (n) => {
|
|
802
|
+
if (n.nodeType === 3 /* TEXT_NODE */) {
|
|
803
|
+
s += n.textContent || '';
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
if (n.nodeType !== 1 /* ELEMENT_NODE */)
|
|
807
|
+
return;
|
|
808
|
+
const e = n;
|
|
809
|
+
// Hidden elements of every kind and skipped ones before the <br>: `<br hidden>` breaks no line, a <style> in code is
|
|
810
|
+
// not code, and a tab panel or an `S:` span inside code is no code tab (T124 panel: codex, grok, agy).
|
|
811
|
+
if (underHiddenAttr(e, opts) || SKIP_TAGS.has(e.tagName))
|
|
812
|
+
return;
|
|
813
|
+
if (e.tagName === 'BR') {
|
|
814
|
+
s += br;
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
for (const c of e.childNodes)
|
|
818
|
+
walk(c);
|
|
819
|
+
};
|
|
820
|
+
for (const c of el.childNodes)
|
|
821
|
+
walk(c);
|
|
822
|
+
return s;
|
|
823
|
+
};
|
|
824
|
+
// Lines of code as inline Markdown: one code span per line, the lines joined by a line break, which a table cell writes
|
|
825
|
+
// as <br> (C28: a <pre> in a cell folded into one line; C5). Blank lines at either end are dropped.
|
|
826
|
+
const codeLines = (raw) => {
|
|
827
|
+
const lines = raw.map(collapseWs);
|
|
828
|
+
while (lines.length > 1 && !lines[0].trim())
|
|
829
|
+
lines.shift();
|
|
830
|
+
while (lines.length > 1 && !lines[lines.length - 1].trim())
|
|
831
|
+
lines.pop();
|
|
832
|
+
return lines.map((l) => wrapInline(l, codeSpan)).join('\n');
|
|
833
|
+
};
|
|
708
834
|
// A code span needs a fence longer than any backtick run inside it, padded with a space when the text
|
|
709
835
|
// starts or ends with a backtick (CommonMark 6.1): `a`b` closed after "a", and ``x`` lost its backticks.
|
|
710
836
|
const codeSpan = (t) => {
|
|
@@ -795,12 +921,25 @@ const renderInlineNodes = (nodes, opts, excludeEls) => {
|
|
|
795
921
|
// aria-hidden is NOT skipped here, although the block walk skips it: docs.sentry.io puts the whole text
|
|
796
922
|
// of 15 <h2>s inside `<a aria-hidden="true">`, and skipping it deleted every one (T109 A/B). The
|
|
797
923
|
// attribute hides from screen readers, not from the screen. `hidden` hides from both (isHiddenEl).
|
|
798
|
-
|
|
924
|
+
const hidden = hiddenKind(c, opts);
|
|
925
|
+
if (SKIP_TAGS.has(tag) || hidden === 'hidden')
|
|
926
|
+
continue;
|
|
927
|
+
if (hidden === 'code') {
|
|
928
|
+
// Each <pre> is set on its own lines by the PRE branch below.
|
|
929
|
+
const code = renderInlineNodes(outerPres(c, opts), opts, excludeEls);
|
|
930
|
+
add(code, true, code.trim());
|
|
799
931
|
continue;
|
|
932
|
+
}
|
|
800
933
|
if (tag === 'BR') {
|
|
801
934
|
add('\n', false);
|
|
802
935
|
continue;
|
|
803
936
|
}
|
|
937
|
+
if (tag === 'PRE') {
|
|
938
|
+
// A <pre> in inline position, which is in a table cell or a link: its lines stay lines (codeLines).
|
|
939
|
+
const code = codeLines(codeText(c, opts).replace(/\r/g, '').split('\n'));
|
|
940
|
+
add(`\n${code}\n`, true, collapseWs(c.textContent || '').trim());
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
804
943
|
if (tag === 'TABLE') {
|
|
805
944
|
// In inline position a table is nested in a table cell (the block walk renders every other one): its rows
|
|
806
945
|
// become lines of the cell instead of rows of the outer table (P2A-013).
|
|
@@ -822,15 +961,23 @@ const renderInlineNodes = (nodes, opts, excludeEls) => {
|
|
|
822
961
|
continue;
|
|
823
962
|
}
|
|
824
963
|
if (tag === 'CODE') {
|
|
825
|
-
const
|
|
826
|
-
add(
|
|
964
|
+
const lines = codeText(c, opts, BR_MARK).split(BR_MARK);
|
|
965
|
+
add(codeLines(lines), true, collapseWs(lines.join(' ')).trim());
|
|
827
966
|
continue;
|
|
828
967
|
}
|
|
829
968
|
if (tag === 'IMG') {
|
|
830
969
|
// Same includeImages contract and src fallback as the block-level IMG branch: an image
|
|
831
970
|
// wrapped in a link, list item, table cell or text run arrives here instead.
|
|
832
|
-
if (!opts.includeImages)
|
|
971
|
+
if (!opts.includeImages) {
|
|
972
|
+
// A table cell shows no image but keeps its alt text, the text that stands for an image which is not shown (HTML,
|
|
973
|
+
// "the img element"): a severity icon's CRITICAL came out as an empty cell (C11b, T121 panel). An empty alt marks
|
|
974
|
+
// a decorative image and adds nothing, and so does aria-hidden="true" (T124 panel: agy, a bullet image wrote
|
|
975
|
+
// "bullet point" before every value).
|
|
976
|
+
const alt = opts.imageAlt && !isAriaHidden(c) ? cleanInline(c.getAttribute('alt') || '') : '';
|
|
977
|
+
if (alt)
|
|
978
|
+
add(` ${opts.inLink ? escapeLinkText(alt) : alt} `, true, alt);
|
|
833
979
|
continue;
|
|
980
|
+
}
|
|
834
981
|
const alt = cleanInline(c.getAttribute('alt') || '');
|
|
835
982
|
const src = absUrl(c.getAttribute('src') || c.getAttribute('data-src') || '', opts.baseUrl);
|
|
836
983
|
if (src)
|
|
@@ -927,9 +1074,16 @@ const normalizeLang = (raw, knownOnly) => {
|
|
|
927
1074
|
return l;
|
|
928
1075
|
return '';
|
|
929
1076
|
};
|
|
930
|
-
const renderCodeBlock = (pre, out) => {
|
|
931
|
-
|
|
932
|
-
|
|
1077
|
+
const renderCodeBlock = (pre, out, opts) => {
|
|
1078
|
+
// The first <code> not under a hidden element: a hidden one replaced the visible text (T124 panel: codex,
|
|
1079
|
+
// `<pre>VISIBLE<code hidden>SECRET</code>TAIL</pre>` printed SECRET where Chromium shows VISIBLETAIL).
|
|
1080
|
+
const codeEl = [...pre.querySelectorAll('code')].find((c) => {
|
|
1081
|
+
for (let e = c; e && e !== pre; e = e.parentElement)
|
|
1082
|
+
if (underHiddenAttr(e, opts))
|
|
1083
|
+
return false;
|
|
1084
|
+
return true;
|
|
1085
|
+
}) || pre;
|
|
1086
|
+
const text = codeText(codeEl, opts).replace(/\r/g, '').replace(/^\n+|\n+$/g, '');
|
|
933
1087
|
if (!text.trim())
|
|
934
1088
|
return;
|
|
935
1089
|
// The language label lives wherever the site generator put it, and that is rarely one place:
|
|
@@ -1007,31 +1161,85 @@ const ownCaption = (tbl, opts) => [...tbl.children].find((c) => c.tagName === 'C
|
|
|
1007
1161
|
// A cell as inline Markdown on one line, rendered like any other inline content: links keep their target and code its
|
|
1008
1162
|
// backticks (P2A-010: every row of a users list lost its link), whitespace collapses as a browser collapses it (a
|
|
1009
1163
|
// pretty-printed template's indentation used to become `<br> value<br>`), a block child is spaced from its neighbours
|
|
1010
|
-
// and a <br> stays a line break (`<br>`).
|
|
1011
|
-
|
|
1164
|
+
// and a <br> stays a line break (`<br>`). An image is written as its alt text: the image itself stays out of cells, as it
|
|
1165
|
+
// always has.
|
|
1166
|
+
const CELL = { includeImages: false, tight: true, imageAlt: true };
|
|
1167
|
+
const cellMd = (el, opts) => renderInline(el, { ...opts, ...CELL })
|
|
1012
1168
|
.replace(/ *\n */g, '\n')
|
|
1013
1169
|
.replace(/^\n+|\n+$/g, '')
|
|
1014
1170
|
.replace(/\|/g, '\\|')
|
|
1015
1171
|
.replace(/\n/g, '<br>')
|
|
1016
1172
|
.replace(/[ \t]+/g, ' ')
|
|
1017
1173
|
.trim();
|
|
1174
|
+
// The rows as a grid of slots, laid out as the HTML table model lays them: a cell spanning columns or rows leaves `null`
|
|
1175
|
+
// in the slots it covers, so the cells after it stay under their own headers (C6, codex: the cell after a colspan="2"
|
|
1176
|
+
// landed one column left). Spans are clamped as browsers clamp them (colspan 1..1000, rowspan 0..65534), and a span ends
|
|
1177
|
+
// with its row group, a <thead>, a <tbody>, a <tfoot> or a run of rows straight under <table>; rowspan="0" spans the
|
|
1178
|
+
// rest of the group. Chromium puts the <tbody> row after a <thead> rowspan="2" under the headers, where the grid pushed
|
|
1179
|
+
// it one column right (HTML: "A cell cannot cover slots that are from two or more row groups"; T124 panel: codex, grok,
|
|
1180
|
+
// mimo, agy; audits/T125-2026-10-16/scripts/browser_layout_check.py). A row under `hidden` takes no part: Chromium lays a
|
|
1181
|
+
// rowspan over the next shown row. Only the columns in which some cell starts are kept: `colspan="100"` on a details
|
|
1182
|
+
// row means "the full width", not a hundred columns. The extension pads a colspan with empty cells the same way (max 20,
|
|
1183
|
+
// no rowspan).
|
|
1184
|
+
// The page decides the spans, so the slots they cover are budgeted: past SPAN_SLOTS the table gets the plain layout,
|
|
1185
|
+
// cells in source order, which is what earlier versions printed for every table.
|
|
1186
|
+
const SPAN_SLOTS = 100000;
|
|
1187
|
+
const tableGrid = (rows, opts) => {
|
|
1188
|
+
const grid = rows.map(() => []);
|
|
1189
|
+
const starts = new Set();
|
|
1190
|
+
let covered = 0;
|
|
1191
|
+
const groupEnd = [];
|
|
1192
|
+
for (let r = rows.length - 1; r >= 0; r--) {
|
|
1193
|
+
groupEnd[r] = r + 1 < rows.length && rows[r + 1].parentElement === rows[r].parentElement ? groupEnd[r + 1] : r + 1;
|
|
1194
|
+
}
|
|
1195
|
+
for (let r = 0; r < rows.length; r++) {
|
|
1196
|
+
let col = 0;
|
|
1197
|
+
const left = groupEnd[r] - r;
|
|
1198
|
+
for (const cell of ownCells(rows[r], opts)) {
|
|
1199
|
+
while (grid[r][col] !== undefined)
|
|
1200
|
+
col++;
|
|
1201
|
+
const cs = Math.min(Math.max(parseInt(cell.getAttribute('colspan') || '', 10) || 1, 1), 1000);
|
|
1202
|
+
const rsRaw = parseInt(cell.getAttribute('rowspan') || '', 10);
|
|
1203
|
+
const rs = rsRaw === 0 ? left : Math.min(Math.max(rsRaw || 1, 1), 65534, left);
|
|
1204
|
+
covered += cs * rs - 1;
|
|
1205
|
+
if (covered > SPAN_SLOTS)
|
|
1206
|
+
return rows.map((tr) => ownCells(tr, opts));
|
|
1207
|
+
for (let dr = 0; dr < rs; dr++)
|
|
1208
|
+
for (let dc = 0; dc < cs; dc++)
|
|
1209
|
+
grid[r + dr][col + dc] = dr || dc ? null : cell;
|
|
1210
|
+
starts.add(col);
|
|
1211
|
+
col += cs;
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
const keep = [...starts].sort((a, b) => a - b);
|
|
1215
|
+
return grid.map((row) => keep.map((c) => row[c] || null));
|
|
1216
|
+
};
|
|
1018
1217
|
// A table nested in a cell, as lines of that cell: its caption, then one line per row, a header cell joined to the
|
|
1019
|
-
// data cell after it by ": " (the key/value tables of a details row read `k: v`) and other cells by " · ".
|
|
1218
|
+
// data cell after it by ": " (the key/value tables of a details row read `k: v`) and other cells by " · ". Under a header
|
|
1219
|
+
// row (two or more header cells and nothing else) an empty cell keeps its place, `a · · c`: dropping it moved every later
|
|
1220
|
+
// value under the header before it (C18, agy).
|
|
1020
1221
|
const flattenTable = (tbl, opts, excludeEls) => {
|
|
1021
1222
|
const one = (el) => renderInline(el, opts, excludeEls).replace(/\s+/g, ' ').trim();
|
|
1022
1223
|
const cap = ownCaption(tbl, opts);
|
|
1023
1224
|
const lines = cap && one(cap) ? [one(cap)] : [];
|
|
1024
|
-
|
|
1225
|
+
const grid = tableGrid(ownRows(tbl, opts), opts);
|
|
1226
|
+
const head = (grid[0] || []).filter((c) => !!c);
|
|
1227
|
+
const positional = head.length >= 2 && head.every((c) => c.tagName === 'TH');
|
|
1228
|
+
for (const row of grid) {
|
|
1229
|
+
const cells = row.map((c) => ({ t: c ? one(c) : '', th: c?.tagName === 'TH' }));
|
|
1230
|
+
while (cells.length && !cells[cells.length - 1].t)
|
|
1231
|
+
cells.pop();
|
|
1025
1232
|
let line = '';
|
|
1233
|
+
let started = false;
|
|
1026
1234
|
let afterTh = false;
|
|
1027
|
-
for (const
|
|
1028
|
-
|
|
1029
|
-
if (!t)
|
|
1235
|
+
for (const { t, th } of cells) {
|
|
1236
|
+
if (!t && !positional)
|
|
1030
1237
|
continue;
|
|
1031
|
-
if (
|
|
1032
|
-
line += afterTh &&
|
|
1238
|
+
if (started)
|
|
1239
|
+
line += afterTh && !th ? (line.endsWith(':') ? ' ' : ': ') : ' · ';
|
|
1033
1240
|
line += t;
|
|
1034
|
-
|
|
1241
|
+
started = true;
|
|
1242
|
+
afterTh = th;
|
|
1035
1243
|
}
|
|
1036
1244
|
if (line)
|
|
1037
1245
|
lines.push(line);
|
|
@@ -1044,10 +1252,10 @@ const renderTable = (tbl, out, opts) => {
|
|
|
1044
1252
|
return;
|
|
1045
1253
|
// <caption> text was never printed (grok, T109): it names the table.
|
|
1046
1254
|
const capEl = ownCaption(tbl, opts);
|
|
1047
|
-
const caption = capEl ? escapeBlockStart(renderInline(capEl, { ...opts,
|
|
1255
|
+
const caption = capEl ? escapeBlockStart(renderInline(capEl, { ...opts, ...CELL }).replace(/\s+/g, ' ').trim()) : '';
|
|
1048
1256
|
if (caption)
|
|
1049
1257
|
out.push('', caption);
|
|
1050
|
-
const data = rows.map((
|
|
1258
|
+
const data = tableGrid(rows, opts).map((row) => row.map((c) => (c ? cellMd(c, opts) : '')));
|
|
1051
1259
|
const width = Math.max(...data.map((r) => r.length));
|
|
1052
1260
|
const padded = data.map((r) => { while (r.length < width)
|
|
1053
1261
|
r.push(''); return r; });
|