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