Don't replace static assets linking in fenced code blocks ()

* Don't replace any relative links to static assets in a fenced code block

* revert unintended docs change

* improve regex

* remove test file :sob

* stronger regex test for replace assets

* super simplify solution
This commit is contained in:
Endilie Yacop Sucipto 2018-07-21 20:54:45 +07:00 committed by GitHub
parent 53993f1ba6
commit d42ecb943f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 11 deletions
lib/server

View file

@ -69,6 +69,19 @@ function mdToHtmlify(oldContent, mdToHtml, metadata) {
return content;
}
function replaceAssetsLink(oldContent) {
let fencedBlock = false;
const lines = oldContent.split('\n').map(line => {
if (line.trim().startsWith('```')) {
fencedBlock = !fencedBlock;
}
return fencedBlock
? line
: line.replace(/\]\(assets\//g, `](${siteConfig.baseUrl}docs/assets/`);
});
return lines.join('\n');
}
function getComponent(rawContent, mdToHtml, metadata) {
// generate table of contents
let content = insertTOC(rawContent);
@ -76,11 +89,8 @@ function getComponent(rawContent, mdToHtml, metadata) {
// replace any links to markdown files to their website html links
content = mdToHtmlify(content, mdToHtml, metadata);
// replace any relative links to static assets to absolute links
content = content.replace(
/\]\(assets\//g,
`](${siteConfig.baseUrl}docs/assets/`
);
// replace any relative links to static assets (not in fenced code blocks) to absolute links
content = replaceAssetsLink(content);
const DocsLayout = require('../core/DocsLayout.js');
return (
@ -97,4 +107,5 @@ module.exports = {
getComponent,
getFile,
mdToHtmlify,
replaceAssetsLink,
};