Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't replace static assets linking in fenced code blocks #864

Merged
merged 6 commits into from
Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/server/__tests__/__fixtures__/doc1.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ id: doc1
title: Document 1
---

Docusaurus is the best :)
Docusaurus is the best :)

![image1](assets/image1.png)

```js
console.log("Docusaurus");
```

![image2](assets/image2.jpg)
![image3](assets/image3.gif)

13 changes: 12 additions & 1 deletion lib/server/__tests__/__fixtures__/doc2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ title: Document 2
## Repeating Docs

- [doc1](doc1.md)
- [doc2](./doc2.md)
- [doc2](./doc2.md)

## Do not replace this
```md
![image1](assets/image1.png)
```

```js
const doc1 = foo();
console.log("[image2](assets/image2.jpg)");
const testStr = `![image3](assets/image3.gif)`;
```
70 changes: 68 additions & 2 deletions lib/server/__tests__/__snapshots__/docs.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

exports[`mdToHtmlify transform nothing 1`] = `
"
Docusaurus is the best :)"
Docusaurus is the best :)

![image1](assets/image1.png)

\`\`\`js
console.log(\\"Docusaurus\\");
\`\`\`

![image2](assets/image2.jpg)
![image3](assets/image3.gif)

"
`;

exports[`mdToHtmlify transform to correct link 1`] = `
Expand All @@ -19,5 +30,60 @@ exports[`mdToHtmlify transform to correct link 1`] = `
## Repeating Docs

- [doc1](/docs/en/next/doc1)
- [doc2](/docs/en/next/doc2)"
- [doc2](/docs/en/next/doc2)

## Do not replace this
\`\`\`md
![image1](assets/image1.png)
\`\`\`

\`\`\`js
const doc1 = foo();
console.log(\\"[image2](assets/image2.jpg)\\");
const testStr = \`![image3](assets/image3.gif)\`;
\`\`\`"
`;

exports[`replaceAssetsLink does not transform document without valid assets link 1`] = `
"
### Existing Docs

- [doc1](doc1.md)
- [doc2](./doc2.md)

### Non-existing Docs

- [hahaha](hahaha.md)

## Repeating Docs

- [doc1](doc1.md)
- [doc2](./doc2.md)

## Do not replace this
\`\`\`md
![image1](assets/image1.png)
\`\`\`

\`\`\`js
const doc1 = foo();
console.log(\\"[image2](assets/image2.jpg)\\");
const testStr = \`![image3](assets/image3.gif)\`;
\`\`\`"
`;

exports[`replaceAssetsLink transform document with valid assets link 1`] = `
"
Docusaurus is the best :)

![image1](/docs/assets/image1.png)

\`\`\`js
console.log(\\"Docusaurus\\");
\`\`\`

![image2](/docs/assets/image2.jpg)
![image3](/docs/assets/image3.gif)

"
`;
25 changes: 23 additions & 2 deletions lib/server/__tests__/docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ const doc2 = fs.readFileSync(
'utf8'
);

const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;

describe('mdToHtmlify', () => {
const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;
const mdToHtml = metadataUtils.mdToHtml(Metadata, '/');

test('transform nothing', () => {
Expand Down Expand Up @@ -129,6 +130,26 @@ describe('getFile', () => {
});
});

describe('replaceAssetsLink', () => {
test('transform document with valid assets link', () => {
const content1 = docs.replaceAssetsLink(rawContent1);
expect(content1).toMatchSnapshot();
expect(content1).toContain('![image1](/docs/assets/image1.png)');
expect(content1).toContain('![image2](/docs/assets/image2.jpg)');
expect(content1).toContain('![image3](/docs/assets/image3.gif)');
expect(content1).not.toEqual(rawContent1);
});

test('does not transform document without valid assets link', () => {
const content2 = docs.replaceAssetsLink(rawContent2);
expect(content2).toMatchSnapshot();
expect(content2).not.toContain('![image1](/docs/assets/image1.png)');
expect(content2).not.toContain('![image2](/docs/assets/image2.jpg)');
expect(content2).not.toContain('![image3](/docs/assets/image3.gif)');
expect(content2).toEqual(rawContent2);
});
});

afterAll(() => {
process.chdir(originalCwd);
});
18 changes: 13 additions & 5 deletions lib/server/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,25 @@ function mdToHtmlify(oldContent, mdToHtml, metadata) {
return content;
}

function replaceAssetsLink(oldContent) {
return oldContent.replace(
/(^`{3}(?:[\S]+)\n[\s\S]*?)?(?:\]\(assets\/)([\s\S]*?\n`{3})?/gm,
(match, fenceBlockStart, fenceBlockEnd) =>
fenceBlockStart && fenceBlockEnd
? match
: match.replace(/\]\(assets\//g, `](${siteConfig.baseUrl}docs/assets/`)
);
}

function getComponent(rawContent, mdToHtml, metadata) {
// generate table of contents
let content = insertTOC(rawContent);

// 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 (
Expand All @@ -97,4 +104,5 @@ module.exports = {
getComponent,
getFile,
mdToHtmlify,
replaceAssetsLink,
};