mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 09:07:29 +02:00
fix: do not hardcode siteConfig path (#1150)
* Fix broken build Do not hardcode path in require to siteConfig * Just require instead of loadConfig * Actually use loadConfig I think the test may be wrong too. * refactor & nits * jest mock to fix test * fix test
This commit is contained in:
parent
71824a3541
commit
c78a8b4262
5 changed files with 19 additions and 11 deletions
|
@ -10,6 +10,10 @@ const blog = require('../blog');
|
||||||
const metadataUtils = require('../metadataUtils');
|
const metadataUtils = require('../metadataUtils');
|
||||||
const {replaceAssetsLink} = require('../utils.js');
|
const {replaceAssetsLink} = require('../utils.js');
|
||||||
|
|
||||||
|
jest.mock(`${process.cwd()}/siteConfig.js`, () => ({baseUrl: '/'}), {
|
||||||
|
virtual: true,
|
||||||
|
});
|
||||||
|
|
||||||
const testFile = path.join(
|
const testFile = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
'__fixtures__',
|
'__fixtures__',
|
||||||
|
@ -76,7 +80,7 @@ describe('replaceAssetsLink', () => {
|
||||||
'utf8',
|
'utf8',
|
||||||
);
|
);
|
||||||
const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
|
const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
|
||||||
const content1 = replaceAssetsLink(rawContent1, 'blog');
|
const content1 = replaceAssetsLink(rawContent1, '/blog');
|
||||||
expect(content1).toMatchSnapshot();
|
expect(content1).toMatchSnapshot();
|
||||||
expect(content1).toContain('');
|
expect(content1).toContain('');
|
||||||
expect(content1).toContain('');
|
expect(content1).toContain('');
|
||||||
|
@ -95,7 +99,7 @@ describe('replaceAssetsLink', () => {
|
||||||
'utf8',
|
'utf8',
|
||||||
);
|
);
|
||||||
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;
|
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;
|
||||||
const content2 = replaceAssetsLink(rawContent2, 'blog');
|
const content2 = replaceAssetsLink(rawContent2, '/blog');
|
||||||
expect(content2).toMatchSnapshot();
|
expect(content2).toMatchSnapshot();
|
||||||
expect(content2).not.toContain('');
|
expect(content2).not.toContain('');
|
||||||
expect(content2).not.toContain('');
|
expect(content2).not.toContain('');
|
||||||
|
|
|
@ -10,6 +10,7 @@ const originalCwd = process.cwd();
|
||||||
if (!/website$/.test(originalCwd)) {
|
if (!/website$/.test(originalCwd)) {
|
||||||
process.chdir(process.cwd() + '/website');
|
process.chdir(process.cwd() + '/website');
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const docs = require('../docs');
|
const docs = require('../docs');
|
||||||
|
@ -187,7 +188,7 @@ describe('getFile', () => {
|
||||||
|
|
||||||
describe('replaceAssetsLink', () => {
|
describe('replaceAssetsLink', () => {
|
||||||
test('transform document with valid assets link', () => {
|
test('transform document with valid assets link', () => {
|
||||||
const content1 = replaceAssetsLink(rawContent1, 'docs');
|
const content1 = replaceAssetsLink(rawContent1, '/docs');
|
||||||
expect(content1).toMatchSnapshot();
|
expect(content1).toMatchSnapshot();
|
||||||
expect(content1).toContain('');
|
expect(content1).toContain('');
|
||||||
expect(content1).toContain('');
|
expect(content1).toContain('');
|
||||||
|
@ -201,7 +202,7 @@ describe('replaceAssetsLink', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('does not transform document without valid assets link', () => {
|
test('does not transform document without valid assets link', () => {
|
||||||
const content2 = replaceAssetsLink(rawContent2, 'docs');
|
const content2 = replaceAssetsLink(rawContent2, '/docs');
|
||||||
expect(content2).toMatchSnapshot();
|
expect(content2).toMatchSnapshot();
|
||||||
expect(content2).not.toContain('');
|
expect(content2).not.toContain('');
|
||||||
expect(content2).not.toContain('');
|
expect(content2).not.toContain('');
|
||||||
|
|
|
@ -4,12 +4,16 @@
|
||||||
* This source code is licensed under the MIT license found in the
|
* This source code is licensed under the MIT license found in the
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
const CWD = process.cwd();
|
||||||
const React = require('react');
|
const React = require('react');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const metadataUtils = require('./metadataUtils');
|
const metadataUtils = require('./metadataUtils');
|
||||||
const {replaceAssetsLink} = require('./utils.js');
|
const {replaceAssetsLink} = require('./utils.js');
|
||||||
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
|
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
|
||||||
|
const loadConfig = require('./config');
|
||||||
|
|
||||||
|
const siteConfig = loadConfig(`${CWD}/siteConfig.js`);
|
||||||
|
|
||||||
function urlToSource(url) {
|
function urlToSource(url) {
|
||||||
if (!url || typeof url !== 'string') {
|
if (!url || typeof url !== 'string') {
|
||||||
|
@ -59,7 +63,10 @@ function getMetadata(file) {
|
||||||
const metadata = Object.assign(
|
const metadata = Object.assign(
|
||||||
{
|
{
|
||||||
path: fileToUrl(file),
|
path: fileToUrl(file),
|
||||||
content: replaceAssetsLink(result.rawContent, 'blog'),
|
content: replaceAssetsLink(
|
||||||
|
result.rawContent,
|
||||||
|
`${siteConfig.baseUrl}blog`,
|
||||||
|
),
|
||||||
},
|
},
|
||||||
result.metadata,
|
result.metadata,
|
||||||
);
|
);
|
||||||
|
|
|
@ -111,7 +111,7 @@ function getMarkup(rawContent, mdToHtml, metadata) {
|
||||||
content = mdToHtmlify(content, mdToHtml, metadata);
|
content = mdToHtmlify(content, mdToHtml, metadata);
|
||||||
|
|
||||||
// replace any relative links to static assets (not in fenced code blocks) to absolute links
|
// replace any relative links to static assets (not in fenced code blocks) to absolute links
|
||||||
content = replaceAssetsLink(content, 'docs');
|
content = replaceAssetsLink(content, `${siteConfig.baseUrl}docs`);
|
||||||
|
|
||||||
const DocsLayout = require('../core/DocsLayout.js');
|
const DocsLayout = require('../core/DocsLayout.js');
|
||||||
return renderToStaticMarkupWithDoctype(
|
return renderToStaticMarkupWithDoctype(
|
||||||
|
|
|
@ -9,7 +9,6 @@ const autoprefixer = require('autoprefixer');
|
||||||
const postcss = require('postcss');
|
const postcss = require('postcss');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const escapeStringRegexp = require('escape-string-regexp');
|
const escapeStringRegexp = require('escape-string-regexp');
|
||||||
const siteConfig = require('../../website/siteConfig.js');
|
|
||||||
|
|
||||||
function getSubDir(file, refDir) {
|
function getSubDir(file, refDir) {
|
||||||
const subDir = path.dirname(path.relative(refDir, file)).replace(/\\/g, '/');
|
const subDir = path.dirname(path.relative(refDir, file)).replace(/\\/g, '/');
|
||||||
|
@ -74,10 +73,7 @@ function replaceAssetsLink(oldContent, location) {
|
||||||
}
|
}
|
||||||
return fencedBlock
|
return fencedBlock
|
||||||
? line
|
? line
|
||||||
: line.replace(
|
: line.replace(/\]\(assets\//g, `](${location}/assets/`);
|
||||||
/\]\(assets\//g,
|
|
||||||
`](${siteConfig.baseUrl}${location}/assets/`,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue