@@ -102,6 +102,33 @@ function getBikeshedLinkTexts(elem) {
102
102
return texts . map ( ( x ) => x . trim ( ) . replaceAll ( / \s + / g, ' ' ) ) ;
103
103
}
104
104
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
+
105
132
function convert ( infile , outfile ) {
106
133
const source = readFileSync ( infile , 'utf-8' ) ;
107
134
const dom = new JSDOM ( source ) ;
@@ -218,8 +245,8 @@ function convert(infile, outfile) {
218
245
const value = span . getAttribute ( name ) ;
219
246
switch ( name ) {
220
247
case 'data-x' :
221
- case 'data-lt' :
222
248
break ;
249
+ case 'data-lt' :
223
250
case 'id' :
224
251
// Copy over.
225
252
a . setAttribute ( name , value ) ;
@@ -234,7 +261,7 @@ function convert(infile, outfile) {
234
261
}
235
262
span . replaceWith ( a ) ;
236
263
237
- // TODO: ensure that Bikeshed will identify the same < dfn> as Wattsi.
264
+ ensureLink ( a , dfn ) ;
238
265
}
239
266
240
267
// Wrap <i data-x="..."> with <a>. Wattsi handling is here:
@@ -256,7 +283,7 @@ function convert(infile, outfile) {
256
283
i . parentNode . insertBefore ( a , i ) ;
257
284
a . appendChild ( i ) ;
258
285
259
- // TODO: ensure that Bikeshed will identify the same < dfn> as Wattsi.
286
+ ensureLink ( a , dfn ) ;
260
287
}
261
288
262
289
for ( const code of document . querySelectorAll ( 'pre > code' ) ) {
@@ -327,7 +354,7 @@ function convert(infile, outfile) {
327
354
code . replaceWith ( a ) ;
328
355
a . appendChild ( code ) ;
329
356
330
- // TODO: ensure that Bikeshed will identify the same < dfn> as Wattsi.
357
+ ensureLink ( a , dfn ) ;
331
358
}
332
359
333
360
// Rewrite data-lt to lt.
0 commit comments