-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
linkify.js
50 lines (48 loc) · 1.44 KB
/
linkify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Work in progress: tooling to linkify the HTML produced from
// the MessageFormat 2 markdown.
// this has been tested on the tr35-messageformat.html file
// but not implemented in LDML45
function linkify() {
const terms = findTerms();
const missing = new Set();
const links = document.querySelectorAll("em");
links.forEach((item) => {
const target = generateId(item.textContent);
if (terms.has(target)) {
const el = item.lastElementChild ?? item;
el.innerHTML = `<a href="#${target}">${item.textContent}</a>`;
} else {
missing.add(target);
}
});
// report missing terms
// (leave out sort if you want it in file order)
Array.from(missing).sort().forEach((item)=> {
console.log(item);
});
}
function findTerms() {
const terms = new Set();
document.querySelectorAll("dfn").forEach((item) => {
// console.log(index + ": " + item.textContent);
const term = generateId(item.textContent);
// guard against duplicates
if (terms.has(term)) {
console.log("Duplicate term: " + term);
}
terms.add(term);
item.setAttribute("id", term);
});
return terms;
}
function generateId(term) {
const id = term.toLowerCase().replaceAll(" ", "-");
if (id.endsWith("rategies")) {
// found in the bidi isolation strategies
return id.slice(0, -3) + "y";
} else if (id.endsWith("s") && id !== "status") {
// regular English plurals
return id.slice(0, -1);
}
return id;
}