Skip to content

Commit 5f90dd1

Browse files
committed
Extract alternate ids for headings
fixes #1824
1 parent ace6012 commit 5f90dd1

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

schemas/browserlib/extract-headings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"href": { "$ref": "../common.json#/$defs/url" },
1313
"title": { "type": "string" },
1414
"level": { "type": "integer" },
15-
"number": { "$ref": "../common.json#/$defs/headingNumber" }
15+
"number": { "$ref": "../common.json#/$defs/headingNumber" },
16+
"alternateIds": { "type": "array", "items": { "$ref": "../common.json#/$defs/id"} }
1617
}
1718
}
1819
}

src/browserlib/extract-headings.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ export default function (spec, idToHeading) {
4141
href,
4242
title: n.textContent.trim()
4343
};
44-
4544
const res = {
4645
id: heading.id,
4746
href: heading.href,
4847
level: parseInt(headingEl.tagName.slice(1), 10),
4948
title: heading.title
5049
};
50+
if (heading.alternateIds?.length) {
51+
res.alternateIds = heading.alternateIds;
52+
}
5153
if (heading.number) {
5254
res.number = heading.number;
5355
}

src/browserlib/map-ids-to-headings.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,21 @@ export default function () {
8686
let href = nodeid;
8787

8888
if (parentSection) {
89+
const alternateIds = [];
8990
let id;
9091

9192
const heading = parentSection.heading;
9293
if (heading.id) {
9394
id = heading.id;
9495
href = getAbsoluteUrl(heading, { singlePage });
96+
alternateIds.push(id);
9597
}
9698
else {
9799
const anchor = heading.querySelector('a[name]');
98100
if (anchor) {
99101
id = anchor.getAttribute('name');
100102
href = getAbsoluteUrl(anchor, { singlePage, attribute: 'name' });
103+
alternateIds.push(id);
101104
}
102105
}
103106

@@ -116,6 +119,7 @@ export default function () {
116119
}
117120
mapping.href = href;
118121
mapping.title = trimmedText.replace(reNumber, '');
122+
mapping.alternateIds = alternateIds.filter(id => id !== mapping.id);
119123
mappingTable[nodeid] = mapping;
120124

121125
if (number) {

test/extract-headings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ const testHeadings = [
5858
html: "<section id><h1 id=title>Heading in a section with empty id</h1>",
5959
res: [{id: "title", "href": "about:blank#title", title: "Heading in a section with empty id", level: 1}]
6060
},
61+
{
62+
title: "documents alternate ids when they exist",
63+
html: "<section id=title-0><h1 id=title>Heading in a section with its own id</h1>",
64+
res: [{id: "title-0", "href": "about:blank#title-0", title: "Heading in a section with its own id", level: 1, alternateIds: ["title"]}]
65+
},
6166
];
6267

6368
describe("Test headings extraction", function () {

0 commit comments

Comments
 (0)