From 3976dd5327be7a8951c769317764ce0c78145ccf Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Thu, 18 Oct 2018 16:41:25 -0700 Subject: [PATCH] Replace reference links in documents (fixes #257) --- v1/lib/server/docs.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/v1/lib/server/docs.js b/v1/lib/server/docs.js index dc650f3c1231a..03d23b931f519 100644 --- a/v1/lib/server/docs.js +++ b/v1/lib/server/docs.js @@ -48,16 +48,24 @@ function getFile(metadata) { function mdToHtmlify(oldContent, mdToHtml, metadata) { let content = oldContent; const mdLinks = []; + const mdReferences = []; // find any links to markdown files - const regex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g; - let match = regex.exec(content); - while (match !== null) { - mdLinks.push(match[1]); - match = regex.exec(content); + const linkRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g; + let linkMatch = linkRegex.exec(content); + while (linkMatch !== null) { + mdLinks.push(linkMatch[1]); + linkMatch = linkRegex.exec(content); + } + // find any reference links to markdown files + const refRegex = /(?:\]:)(?:\s)?(?:\.\/|\.\.\/)?([^'")\]\s>]+\.md)/g; + let refMatch = refRegex.exec(content); + while (refMatch !== null) { + mdReferences.push(refMatch[1]); + refMatch = refRegex.exec(content); } - // replace to their website html links + // replace markdown links to their website html links new Set(mdLinks).forEach(mdLink => { let htmlLink = mdToHtml[mdLink]; if (htmlLink) { @@ -75,6 +83,25 @@ function mdToHtmlify(oldContent, mdToHtml, metadata) { ); } }); + + // replace markdown refernces to their website html links + new Set(mdReferences).forEach(refLink => { + let htmlLink = mdToHtml[refLink]; + if (htmlLink) { + htmlLink = getPath(htmlLink, siteConfig.cleanUrl); + htmlLink = htmlLink.replace('/en/', `/${metadata.language}/`); + htmlLink = htmlLink.replace( + '/VERSION/', + metadata.version && metadata.version !== env.versioning.latestVersion + ? `/${metadata.version}/` + : '/', + ); + content = content.replace( + new RegExp(`\\]:(?:\\s)?(\\./|\\.\\./)?${refLink}`, 'g'), + `]: ${htmlLink}`, + ); + } + }); return content; }