Skip to content

Commit 465fcb3

Browse files
authored
Merge pull request #3956 from ralfhandl/preserve-rfc-section-links
Preserve rfc section links
2 parents 3e8b64a + 8b13bab commit 465fcb3

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

scripts/md2html/md2html.js

+37-19
Original file line numberDiff line numberDiff line change
@@ -266,34 +266,51 @@ for (let l in lines) {
266266
if (!inCodeBlock) {
267267

268268
// minor fixups to get RFC links to work properly
269-
if (line.indexOf('RFC [')>=0) {
270-
line = line.replace('RFC [','[RFC');
271-
}
269+
line = line.replace('RFC [','[RFC');
272270
line = line.replace('[Authorization header as defined in ','Authorization header as defined in [');
271+
line = line.replace('[JSON Pointer]','JSON Pointer [RFC6901]'); // only in 2.0.md
272+
273+
//TODO: more "hidden" RFC references in older specs, for example
274+
// [media type range](https://tools.ietf.org/html/rfc7231#appendix-D)
275+
// [ABNF](https://tools.ietf.org/html/rfc5234)
276+
277+
//TODO: unconventional references to RFCs in 3.0.4 and 3.1.1, for example
278+
// [RFC3986 §5.1.2 – 5.1.4](https://tools.ietf.org/html/rfc3986#section-5.1.2)
279+
// RFC6570 [mentions](https://www.rfc-editor.org/rfc/rfc6570.html#section-2.4.2)
280+
// [are not](https://datatracker.ietf.org/doc/html/rfc3986#appendix-A)
281+
// [special behavior](https://www.rfc-editor.org/rfc/rfc1866#section-8.2.1)
282+
// [RFC6570 considers to be _undefined_](https://datatracker.ietf.org/doc/html/rfc6570#section-2.3)
273283

274284
if (line.indexOf('[RFC')>=0) {
275-
line = line.replace(/\[RFC ?([0-9]{1,5})\]/g,function(match,group1){
276-
console.warn('Fixing RFC reference',match,group1);
285+
// also detect [RFC4648 §3.2] etc. in 3.0.4.md and 3.1.1.md
286+
line = line.replace(/\[RFC ?([0-9]{1,5})( §[0-9 .-]+)?\]/g,function(match,group1){
287+
// console.warn('Fixing RFC reference',match,group1);
277288
return '[[RFC'+group1+']]';
278289
});
279290
}
280291

281-
line = line.replace('http://tools.ietf.org','https://tools.ietf.org');
282-
if (line.indexOf('xml2rfc.ietf.org')>0) {
283-
line = line.replace('https://xml2rfc.ietf.org/public/rfc/html/rfc','https://tools.ietf.org/html/rfc');
284-
line = line.replace('.html','');
285-
}
286-
line = line.replace(/\]\]\(https:\/\/tools.ietf.org\/html\/rfc[0-9]{1,5}\/?(\#.*?)?\)/g,function(match,group1){
287-
//return (group1 ? group1 : '')+']]';
288-
return ']]';
292+
//TODO: non-link mentions of RFCs in 3.0.4 and 3.1.1, for example
293+
// RFC3986's definition of [reserved](https://datatracker.ietf.org/doc/html/rfc3986#section-2.2)
294+
295+
// harmonize RFC URLs
296+
line = line.replace('http://www.ietf.org/rfc/rfc2119.txt','https://tools.ietf.org/html/rfc2119'); // only in 2.0.md
297+
line = line.replace(/https:\/\/www.rfc-editor.org\/rfc\/rfc([0-9]{1,5})(\.html)?/g,'https://tools.ietf.org/html/rfc$1');
298+
line = line.replaceAll('https://datatracker.ietf.org/doc/html/rfc','https://tools.ietf.org/html/rfc');
299+
line = line.replaceAll('http://tools.ietf.org','https://tools.ietf.org');
300+
301+
// handle url fragments in RFC links and construct section titles links as well as RFC links
302+
line = line.replace(/\]\]\(https:\/\/tools.ietf.org\/html\/rfc([0-9]{1,5})\/?(\#[^)]*)?\)/g, function(match, rfcNumber, fragment) {
303+
if (fragment) {
304+
// Extract section title from the fragment
305+
let sectionTitle = fragment.replace('#', '').replace(/-/g, ' ');
306+
sectionTitle = sectionTitle.charAt(0).toUpperCase() + sectionTitle.slice(1); // Capitalize the first letter
307+
return `]] [${sectionTitle}](https://datatracker.ietf.org/doc/html/rfc${rfcNumber}${fragment})`;
308+
} else {
309+
return ']]';
310+
}
289311
});
290312
}
291313

292-
// minor fixup to get bibliography link to work
293-
//if (line.indexOf('[ABNF]')>=0) {
294-
// line = line.replace('[ABNF]','[Augmented Backus-Naur Form]');
295-
//}
296-
297314
if (!inCodeBlock && line.indexOf('](../') >= 0) {
298315
const regExp = /\((\.\.[^)]+)\)/g;
299316
line = line.replace(regExp,function(match,group1){
@@ -306,9 +323,10 @@ for (let l in lines) {
306323
let heading = 0;
307324
while (line[heading] === '#') heading++;
308325
let delta = heading-prevHeading;
326+
if (delta>1) console.warn(delta,line);
309327
if (delta>0) delta = 1;
310328
//if (delta<0) delta = -1;
311-
if (Math.abs(delta)>1) console.warn(delta,line);
329+
// if (Math.abs(delta)>1) console.warn(delta,line);
312330
let prefix = '';
313331
let newSection = '<section>';
314332
if (line.includes('## Version ')) {

0 commit comments

Comments
 (0)