Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/tools/linkify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Work in progress: tooling to linkify the HTML produced from
// the MessageFormat v2 markdown.
// this has been tested on the tr35-messageformat.html file
// but not implemented in LDML45

const terms = new Array();

function findTerms() {
document.querySelectorAll('dfn').forEach(generateAnchor);
console.log(terms.length);
}

function generateAnchor(item, index) {
// debugging: print the list of terms
// console.log(index + ": " + item.textContent);
let t = generateId(item.textContent);
terms.push(t);
item.setAttribute('id', t);
}

function linkify() {
const links = document.querySelectorAll('em');
links.forEach(checkLink);
// console.log(terms.length);
}

function checkLink(item) {
var t = generateId(item.textContent);
if (terms.includes(t)) {
link(item, t);
} else {
// check that these are all just italicized strings that aren't terms
console.log(t);
}
}

function link(item, target) {
var a = document.createElement('a');
a.appendChild(document.createTextNode(item.textContent));
a.href = '#' + target;

// 'dfn' (or maybe other markup) appears inside the 'em'
var el = item.lastElementChild;
if (el) {
el.innerHTML = '';
el.appendChild(a);
} else {
item.innerHTML = '';
item.appendChild(a);
}
}

function generateId(term) {
var retval = '';
if (term.endsWith('s') && term !== 'status') {
retval = term.substring(0, term.length - 1);
} else {
retval = term;
}
retval = retval.replaceAll(/ /g, '-');
return retval.toLowerCase();
}