Skip to content

Commit 03bfae7

Browse files
committed
Make Bikeshed find the right <dfn> more often
1 parent 0e8e03e commit 03bfae7

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

wattsi2bikeshed.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ function getBikeshedLinkTexts(elem) {
102102
return texts.map((x) => x.trim().replaceAll(/\s+/g, ' '));
103103
}
104104

105+
// Add for and lt to ensure that Bikeshed will link the <a> to the right <dfn>.
106+
function ensureLink(a, dfn) {
107+
if (dfn.hasAttribute('for')) {
108+
a.setAttribute('for', dfn.getAttribute('for'));
109+
// TODO: don't add when it's already unambiguous.
110+
}
111+
112+
const dfnLts = getBikeshedLinkTexts(dfn);
113+
if (dfnLts.length === 0) {
114+
console.warn('No linking text for', dfn.outerHTML);
115+
return;
116+
}
117+
const aLts = getBikeshedLinkTexts(a);
118+
if (aLts.length !== 1) {
119+
console.warn('Zero or too many linking texts for', a.outerHTML);
120+
}
121+
if (!dfnLts.some((lt) => lt === aLts[0])) {
122+
// console.log('Fixing link from', a.outerHTML, 'to', dfn.outerHTML, 'with lt');
123+
// Note: data-lt is rewritten to lt later. It would also work to remove
124+
// any data-lt attribute here and just add lt.
125+
a.setAttribute('data-lt', dfnLts[0]);
126+
}
127+
128+
// TODO: check if Bikeshed would now find the right <dfn> and if not
129+
// add additional attributes to make it so.
130+
}
131+
105132
function convert(infile, outfile) {
106133
const source = readFileSync(infile, 'utf-8');
107134
const dom = new JSDOM(source);
@@ -218,8 +245,8 @@ function convert(infile, outfile) {
218245
const value = span.getAttribute(name);
219246
switch (name) {
220247
case 'data-x':
221-
case 'data-lt':
222248
break;
249+
case 'data-lt':
223250
case 'id':
224251
// Copy over.
225252
a.setAttribute(name, value);
@@ -234,7 +261,7 @@ function convert(infile, outfile) {
234261
}
235262
span.replaceWith(a);
236263

237-
// TODO: ensure that Bikeshed will identify the same <dfn> as Wattsi.
264+
ensureLink(a, dfn);
238265
}
239266

240267
// Wrap <i data-x="..."> with <a>. Wattsi handling is here:
@@ -256,7 +283,7 @@ function convert(infile, outfile) {
256283
i.parentNode.insertBefore(a, i);
257284
a.appendChild(i);
258285

259-
// TODO: ensure that Bikeshed will identify the same <dfn> as Wattsi.
286+
ensureLink(a, dfn);
260287
}
261288

262289
for (const code of document.querySelectorAll('pre > code')) {
@@ -327,7 +354,7 @@ function convert(infile, outfile) {
327354
code.replaceWith(a);
328355
a.appendChild(code);
329356

330-
// TODO: ensure that Bikeshed will identify the same <dfn> as Wattsi.
357+
ensureLink(a, dfn);
331358
}
332359

333360
// Rewrite data-lt to lt.

0 commit comments

Comments
 (0)