From ad634656141d47f4e9f2a8134ca80f5971253e99 Mon Sep 17 00:00:00 2001 From: Bellangelo Date: Tue, 7 May 2024 00:21:57 +0300 Subject: [PATCH 1/4] Create localBiblio for Respec --- scripts/md2html/md2html.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/scripts/md2html/md2html.js b/scripts/md2html/md2html.js index 147e33f62a..3f792bd7ff 100644 --- a/scripts/md2html/md2html.js +++ b/scripts/md2html/md2html.js @@ -48,6 +48,39 @@ const md = require('markdown-it')({ }); function preface(title,options) { +const localBiblio = {}; +const baseURL = 'https://github.com/OAI/OpenAPI-Specification/'; + +md.renderer.rules.link_open = function(tokens, idx, options, env, self) { + if ( + idx > 0 + && tokens[idx + 1] && tokens[idx + 1].type === 'text' + && tokens[idx + 2] && tokens[idx + 2].type === 'link_close' + ) { + const text = tokens[idx + 1].content; + const url = tokens[idx].attrs[tokens[idx].attrIndex('href')][1]; + + if ( + url.startsWith('http') + && !url.startsWith(baseURL) + && !url.startsWith('https://tools.ietf.org') + ) { + localBiblio[text] = { + title: text, + href: url + }; + + // Do not show url text and closing html tag. + tokens[idx + 1].content = ''; + tokens[idx + 2].hidden = true; + + return '[[' + text + ']]'; + } + } + + return self.renderToken(tokens, idx, options); +}; + const respec = { specStatus: "base", editors: maintainers, @@ -55,9 +88,9 @@ function preface(title,options) { publishDate: options.publishDate, subtitle: 'Version '+options.subtitle, processVersion: 2017, - edDraftURI: "https://github.com/OAI/OpenAPI-Specification/", + edDraftURI: baseURL, github: { - repoURL: "https://github.com/OAI/OpenAPI-Specification/", + repoURL: baseURL, branch: "master" }, shortName: "OAS", From 988c47b49f4066142297bd27ff4621e7ec8d35f4 Mon Sep 17 00:00:00 2001 From: Bellangelo Date: Tue, 7 May 2024 00:22:28 +0300 Subject: [PATCH 2/4] Pass localBiblio to Respec config --- scripts/md2html/md2html.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/md2html/md2html.js b/scripts/md2html/md2html.js index 3f792bd7ff..8d07062b9e 100644 --- a/scripts/md2html/md2html.js +++ b/scripts/md2html/md2html.js @@ -47,7 +47,6 @@ const md = require('markdown-it')({ } }); -function preface(title,options) { const localBiblio = {}; const baseURL = 'https://github.com/OAI/OpenAPI-Specification/'; @@ -81,6 +80,7 @@ md.renderer.rules.link_open = function(tokens, idx, options, env, self) { return self.renderToken(tokens, idx, options); }; +function preface(title, options, localBublio) { const respec = { specStatus: "base", editors: maintainers, @@ -97,7 +97,8 @@ md.renderer.rules.link_open = function(tokens, idx, options, env, self) { noTOC: false, lint: false, additionalCopyrightHolders: "the Linux Foundation", - includePermalinks: true + includePermalinks: true, + localBiblio }; let preface = `${md.utils.escapeHtml(title)}`; @@ -345,7 +346,7 @@ for (let l in lines) { lines[l] = (linkTarget ? linkTarget : '') + line; } -s = preface(`OpenAPI Specification v${argv.subtitle} | Introduction, Definitions, & More`,argv)+'\n\n'+lines.join('\n'); +s = preface(`OpenAPI Specification v${argv.subtitle} | Introduction, Definitions, & More`, argv, localBiblio)+'\n\n'+lines.join('\n'); let out = md.render(s); out = out.replace(/\[([RGB])\]/g,function(match,group1){ console.warn('Fixing',match,group1); From 2aa7a93c72dbca57bef337243acc375faee82835 Mon Sep 17 00:00:00 2001 From: Bellangelo Date: Tue, 7 May 2024 00:23:33 +0300 Subject: [PATCH 3/4] Explain code with variables --- scripts/md2html/md2html.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/md2html/md2html.js b/scripts/md2html/md2html.js index 8d07062b9e..c4888a117e 100644 --- a/scripts/md2html/md2html.js +++ b/scripts/md2html/md2html.js @@ -49,6 +49,7 @@ const md = require('markdown-it')({ const localBiblio = {}; const baseURL = 'https://github.com/OAI/OpenAPI-Specification/'; +const formativeRFC = 'https://tools.ietf.org'; md.renderer.rules.link_open = function(tokens, idx, options, env, self) { if ( @@ -62,7 +63,7 @@ md.renderer.rules.link_open = function(tokens, idx, options, env, self) { if ( url.startsWith('http') && !url.startsWith(baseURL) - && !url.startsWith('https://tools.ietf.org') + && !url.startsWith(formativeRFC) ) { localBiblio[text] = { title: text, From 84f9d8245e62b9a06530f997711bfc7d8ab55506 Mon Sep 17 00:00:00 2001 From: Bellangelo Date: Tue, 7 May 2024 00:40:27 +0300 Subject: [PATCH 4/4] Improve handling of links with whitespaces and RFCs --- scripts/md2html/md2html.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/md2html/md2html.js b/scripts/md2html/md2html.js index c4888a117e..07b5f4ad1b 100644 --- a/scripts/md2html/md2html.js +++ b/scripts/md2html/md2html.js @@ -49,7 +49,6 @@ const md = require('markdown-it')({ const localBiblio = {}; const baseURL = 'https://github.com/OAI/OpenAPI-Specification/'; -const formativeRFC = 'https://tools.ietf.org'; md.renderer.rules.link_open = function(tokens, idx, options, env, self) { if ( @@ -58,14 +57,16 @@ md.renderer.rules.link_open = function(tokens, idx, options, env, self) { && tokens[idx + 2] && tokens[idx + 2].type === 'link_close' ) { const text = tokens[idx + 1].content; + // Reference must not contain any whitespace. + const reference = text.replace(/\s+/g, '-'); const url = tokens[idx].attrs[tokens[idx].attrIndex('href')][1]; if ( url.startsWith('http') && !url.startsWith(baseURL) - && !url.startsWith(formativeRFC) + && !text.includes('RFC') ) { - localBiblio[text] = { + localBiblio[reference] = { title: text, href: url }; @@ -74,7 +75,7 @@ md.renderer.rules.link_open = function(tokens, idx, options, env, self) { tokens[idx + 1].content = ''; tokens[idx + 2].hidden = true; - return '[[' + text + ']]'; + return '[[' + reference + ']]'; } }