Jared Gardner teaches & writes about American literature, film, comics, & higher education. All opinions are his own & not those of his employer, the State of Ohio, or his dogs.
let regex = new RegExp(/\[\[([0-9]+)\]\](?!:)/, 'g') ;
document.querySelectorAll('article p, article li, article figcaption, article .kg-callout-text').forEach(element => {
var textchunk = "";
for (var i = 0; i < element.childNodes.length; i++) {
var curNode = element.childNodes[i];
if (curNode.nodeName === "#text") {
textchunk=curNode.nodeValue
const matches = curNode.nodeValue.matchAll(regex);
for (const match of matches) {
let firstpiece = textchunk.slice(0,match.index)
let lastpiece = textchunk.slice(match.index + match[0].length)
let linkpiece = `
${match[1]}
`
let newpiece = firstpiece + linkpiece + lastpiece
let tmp = document.createRange().createContextualFragment(newpiece)
curNode.replaceWith(tmp)
}
}
}
}
)
let regex2 = new RegExp(/\[\[([0-9]+)\]\]:/, 'g')
for (let node of document.querySelectorAll('article p')) {
textchunk=node.innerHTML
const matches = textchunk.matchAll(regex2);
for (const match of matches) {
let newpiece = `