Skip to content

Commit c0daa40

Browse files
authored
refactor(core/data-cite): simplify linking (#2836)
1 parent a207f06 commit c0daa40

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

src/core/dfn-map.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @ts-check
22
import { CaseInsensitiveMap } from "./utils.js";
3+
4+
/** @type {CaseInsensitiveMap<Set<HTMLElement>>} */
35
export const definitionMap = new CaseInsensitiveMap();
46

57
/**

src/core/link-to-dfn.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,30 @@ export async function run(conf) {
5656
/** @type {HTMLAnchorElement[]} */
5757
const badLinks = [];
5858

59-
const localLinkSelector =
60-
"a[data-cite=''], a:not([href]):not([data-cite]):not(.logo):not(.externalDFN)";
61-
document.querySelectorAll(localLinkSelector).forEach((
62-
/** @type {HTMLAnchorElement} */ anchor
63-
) => {
59+
/** @type {NodeListOf<HTMLAnchorElement>} */
60+
const localAnchors = document.querySelectorAll(
61+
"a[data-cite=''], a:not([href]):not([data-cite]):not(.logo):not(.externalDFN)"
62+
);
63+
for (const anchor of localAnchors) {
6464
const linkTargets = getLinkTargets(anchor);
65-
const foundDfn = linkTargets.some(target => {
66-
return findLinkTarget(target, anchor, titleToDfns);
67-
});
68-
if (!foundDfn && linkTargets.length !== 0) {
65+
const linkTarget = linkTargets.find(
66+
target =>
67+
titleToDfns.has(target.title) &&
68+
titleToDfns.get(target.title).has(target.for)
69+
);
70+
if (linkTarget) {
71+
const foundLocalMatch = processAnchor(anchor, linkTarget, titleToDfns);
72+
if (!foundLocalMatch) {
73+
possibleExternalLinks.push(anchor);
74+
}
75+
} else {
6976
if (anchor.dataset.cite === "") {
7077
badLinks.push(anchor);
7178
} else {
7279
possibleExternalLinks.push(anchor);
7380
}
7481
}
75-
});
82+
}
7683

7784
showLinkingError(badLinks);
7885

@@ -86,6 +93,7 @@ export async function run(conf) {
8693
}
8794

8895
function mapTitleToDfns() {
96+
/** @type {CaseInsensitiveMap<Map<string, HTMLElement>>} */
8997
const titleToDfns = new CaseInsensitiveMap();
9098
for (const key of definitionMap.keys()) {
9199
const { result, duplicates } = collectDfns(key);
@@ -128,43 +136,36 @@ function collectDfns(title) {
128136
}
129137

130138
/**
131-
* @param {import("./utils.js").LinkTarget} target
132139
* @param {HTMLAnchorElement} anchor
133-
* @param {CaseInsensitiveMap} titleToDfns
140+
* @param {import("./utils.js").LinkTarget} target
141+
* @param {ReturnType<typeof mapTitleToDfns>} titleToDfns
134142
*/
135-
function findLinkTarget(target, anchor, titleToDfns) {
143+
function processAnchor(anchor, target, titleToDfns) {
144+
let noLocalMatch = false;
136145
const { linkFor } = anchor.dataset;
137-
if (
138-
!titleToDfns.has(target.title) ||
139-
!titleToDfns.get(target.title).get(target.for)
140-
) {
141-
return false;
142-
}
143146
const dfn = titleToDfns.get(target.title).get(target.for);
144147
if (dfn.dataset.cite) {
145148
anchor.dataset.cite = dfn.dataset.cite;
146149
} else if (linkFor && !titleToDfns.get(linkFor)) {
147-
possibleExternalLinks.push(anchor);
150+
noLocalMatch = true;
148151
} else if (dfn.classList.contains("externalDFN")) {
149152
// data-lt[0] serves as unique id for the dfn which this element references
150153
const lt = dfn.dataset.lt ? dfn.dataset.lt.split("|") : [];
151154
anchor.dataset.lt = lt[0] || dfn.textContent;
152-
possibleExternalLinks.push(anchor);
155+
noLocalMatch = true;
156+
} else if (anchor.dataset.idl !== "partial") {
157+
anchor.href = `#${dfn.id}`;
158+
anchor.classList.add("internalDFN");
153159
} else {
154-
if (anchor.dataset.idl === "partial") {
155-
possibleExternalLinks.push(anchor);
156-
} else {
157-
anchor.href = `#${dfn.id}`;
158-
anchor.classList.add("internalDFN");
159-
}
160+
noLocalMatch = true;
160161
}
161162
if (!anchor.hasAttribute("data-link-type")) {
162163
anchor.dataset.linkType = "idl" in dfn.dataset ? "idl" : "dfn";
163164
}
164165
if (isCode(dfn)) {
165166
wrapAsCode(anchor, dfn);
166167
}
167-
return true;
168+
return !noLocalMatch;
168169
}
169170

170171
/**

src/core/utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,13 @@ function* walkTree(walker) {
903903
}
904904
}
905905

906+
/**
907+
* @template ValueType
908+
* @extends {Map<string, ValueType>}
909+
*/
906910
export class CaseInsensitiveMap extends Map {
907911
/**
908-
* @param {Array<[String, HTMLElement]>} [entries]
912+
* @param {Array<[string, ValueType]>} [entries]
909913
*/
910914
constructor(entries = []) {
911915
super();
@@ -916,7 +920,7 @@ export class CaseInsensitiveMap extends Map {
916920
}
917921
/**
918922
* @param {String} key
919-
* @param {*} value
923+
* @param {ValueType} value
920924
*/
921925
set(key, value) {
922926
super.set(key.toLowerCase(), value);

0 commit comments

Comments
 (0)