mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-14 01:27:35 +02:00
* fix: #1114 static files in `blog/assets` is not working * lint fix
This commit is contained in:
parent
db6017903b
commit
a1e7af7e0e
6 changed files with 114 additions and 23 deletions
|
@ -15,3 +15,51 @@ We are very happy to introduce [Docusaurus](https://github.com/facebook/Docusaur
|
|||
"title": "Docusaurus",
|
||||
}
|
||||
`;
|
||||
|
||||
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
|
||||

|
||||
\`\`\`
|
||||
|
||||
\`\`\`js
|
||||
const doc1 = foo();
|
||||
console.log(\\"[image2](assets/image2.jpg)\\");
|
||||
const testStr = \`\`;
|
||||
\`\`\`"
|
||||
`;
|
||||
|
||||
exports[`replaceAssetsLink transform document with valid assets link 1`] = `
|
||||
"
|
||||
Docusaurus is the best :)
|
||||
|
||||

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

|
||||

|
||||
|
||||
Don't replace the one below
|
||||
\`\`\`md
|
||||
|
||||

|
||||
\`\`\`"
|
||||
`;
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const blog = require('../blog');
|
||||
const metadataUtils = require('../metadataUtils');
|
||||
const {replaceAssetsLink} = require('../utils.js');
|
||||
|
||||
const testFile = path.join(
|
||||
__dirname,
|
||||
|
@ -66,3 +68,38 @@ describe('urlToSource', () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('replaceAssetsLink', () => {
|
||||
test('transform document with valid assets link', () => {
|
||||
const doc1 = fs.readFileSync(
|
||||
path.join(__dirname, '__fixtures__', 'doc1.md'),
|
||||
'utf8',
|
||||
);
|
||||
const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
|
||||
const content1 = replaceAssetsLink(rawContent1, 'blog');
|
||||
expect(content1).toMatchSnapshot();
|
||||
expect(content1).toContain('');
|
||||
expect(content1).toContain('');
|
||||
expect(content1).toContain('');
|
||||
expect(content1).toContain('');
|
||||
expect(content1).not.toContain('');
|
||||
expect(content1).not.toContain('');
|
||||
expect(content1).not.toContain('');
|
||||
expect(content1).not.toContain('');
|
||||
expect(content1).not.toEqual(rawContent1);
|
||||
});
|
||||
|
||||
test('does not transform document without valid assets link', () => {
|
||||
const doc2 = fs.readFileSync(
|
||||
path.join(__dirname, '__fixtures__', 'doc2.md'),
|
||||
'utf8',
|
||||
);
|
||||
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;
|
||||
const content2 = replaceAssetsLink(rawContent2, 'blog');
|
||||
expect(content2).toMatchSnapshot();
|
||||
expect(content2).not.toContain('');
|
||||
expect(content2).not.toContain('');
|
||||
expect(content2).not.toContain('');
|
||||
expect(content2).toEqual(rawContent2);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -14,6 +14,7 @@ const path = require('path');
|
|||
const fs = require('fs-extra');
|
||||
const docs = require('../docs');
|
||||
const metadataUtils = require('../metadataUtils');
|
||||
const {replaceAssetsLink} = require('../utils.js');
|
||||
|
||||
jest.mock('../env', () => ({
|
||||
translation: {
|
||||
|
@ -186,7 +187,7 @@ describe('getFile', () => {
|
|||
|
||||
describe('replaceAssetsLink', () => {
|
||||
test('transform document with valid assets link', () => {
|
||||
const content1 = docs.replaceAssetsLink(rawContent1);
|
||||
const content1 = replaceAssetsLink(rawContent1, 'docs');
|
||||
expect(content1).toMatchSnapshot();
|
||||
expect(content1).toContain('');
|
||||
expect(content1).toContain('');
|
||||
|
@ -200,7 +201,7 @@ describe('replaceAssetsLink', () => {
|
|||
});
|
||||
|
||||
test('does not transform document without valid assets link', () => {
|
||||
const content2 = docs.replaceAssetsLink(rawContent2);
|
||||
const content2 = replaceAssetsLink(rawContent2, 'docs');
|
||||
expect(content2).toMatchSnapshot();
|
||||
expect(content2).not.toContain('');
|
||||
expect(content2).not.toContain('');
|
||||
|
|
|
@ -7,8 +7,9 @@
|
|||
const React = require('react');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
|
||||
const metadataUtils = require('./metadataUtils');
|
||||
const {replaceAssetsLink} = require('./utils.js');
|
||||
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
|
||||
|
||||
function urlToSource(url) {
|
||||
if (!url || typeof url !== 'string') {
|
||||
|
@ -56,7 +57,10 @@ function getMetadata(file) {
|
|||
fs.readFileSync(file, {encoding: 'utf8'}),
|
||||
);
|
||||
const metadata = Object.assign(
|
||||
{path: fileToUrl(file), content: result.rawContent},
|
||||
{
|
||||
path: fileToUrl(file),
|
||||
content: replaceAssetsLink(result.rawContent, 'blog'),
|
||||
},
|
||||
result.metadata,
|
||||
);
|
||||
metadata.id = metadata.title;
|
||||
|
|
|
@ -16,6 +16,7 @@ const env = require('./env.js');
|
|||
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
|
||||
const readMetadata = require('./readMetadata.js');
|
||||
const {insertTOC} = require('../core/toc.js');
|
||||
const {replaceAssetsLink} = require('./utils.js');
|
||||
const {getPath} = require('../core/utils.js');
|
||||
|
||||
const docsPart = `${siteConfig.docsUrl ? `${siteConfig.docsUrl}/` : ''}`;
|
||||
|
@ -102,22 +103,6 @@ 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}${docsPart}assets/`,
|
||||
);
|
||||
});
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function getMarkup(rawContent, mdToHtml, metadata) {
|
||||
// generate table of contents
|
||||
let content = insertTOC(rawContent);
|
||||
|
@ -126,7 +111,7 @@ function getMarkup(rawContent, mdToHtml, metadata) {
|
|||
content = mdToHtmlify(content, mdToHtml, metadata);
|
||||
|
||||
// replace any relative links to static assets (not in fenced code blocks) to absolute links
|
||||
content = replaceAssetsLink(content);
|
||||
content = replaceAssetsLink(content, 'docs');
|
||||
|
||||
const DocsLayout = require('../core/DocsLayout.js');
|
||||
return renderToStaticMarkupWithDoctype(
|
||||
|
@ -164,5 +149,4 @@ module.exports = {
|
|||
getFilePath,
|
||||
getRedirectMarkup,
|
||||
mdToHtmlify,
|
||||
replaceAssetsLink,
|
||||
};
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const cssnano = require('cssnano');
|
||||
const autoprefixer = require('autoprefixer');
|
||||
const postcss = require('postcss');
|
||||
const path = require('path');
|
||||
const escapeStringRegexp = require('escape-string-regexp');
|
||||
const siteConfig = require('../../website/siteConfig.js');
|
||||
|
||||
function getSubDir(file, refDir) {
|
||||
const subDir = path.dirname(path.relative(refDir, file)).replace(/\\/g, '/');
|
||||
|
@ -66,10 +66,27 @@ function autoPrefixCss(cssContent) {
|
|||
.then(result => result.css);
|
||||
}
|
||||
|
||||
function replaceAssetsLink(oldContent, location) {
|
||||
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}${location}/assets/`,
|
||||
);
|
||||
});
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSubDir,
|
||||
getLanguage,
|
||||
isSeparateCss,
|
||||
minifyCss,
|
||||
autoPrefixCss,
|
||||
replaceAssetsLink,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue