diff --git a/scripts/md2html/md2html.js b/scripts/md2html/md2html.js index 147e33f62a..07b5f4ad1b 100644 --- a/scripts/md2html/md2html.js +++ b/scripts/md2html/md2html.js @@ -47,7 +47,42 @@ 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; + // 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) + && !text.includes('RFC') + ) { + localBiblio[reference] = { + title: text, + href: url + }; + + // Do not show url text and closing html tag. + tokens[idx + 1].content = ''; + tokens[idx + 2].hidden = true; + + return '[[' + reference + ']]'; + } + } + + return self.renderToken(tokens, idx, options); +}; + +function preface(title, options, localBublio) { const respec = { specStatus: "base", editors: maintainers, @@ -55,16 +90,17 @@ 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", noTOC: false, lint: false, additionalCopyrightHolders: "the Linux Foundation", - includePermalinks: true + includePermalinks: true, + localBiblio }; let preface = `${md.utils.escapeHtml(title)}`; @@ -312,7 +348,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);