mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-29 00:47:03 +02:00
Don't replace static assets linking in fenced code blocks (#864)
* 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:
parent
53993f1ba6
commit
d42ecb943f
5 changed files with 148 additions and 11 deletions
lib/server
|
@ -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,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue