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

fix: do not hardcode siteConfig path #1150

Merged
merged 6 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions v1/lib/server/__tests__/blog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const blog = require('../blog');
const metadataUtils = require('../metadataUtils');
const {replaceAssetsLink} = require('../utils.js');

jest.mock(`${process.cwd()}/siteConfig.js`, () => ({baseUrl: '/'}), {
virtual: true,
});

const testFile = path.join(
__dirname,
'__fixtures__',
Expand Down Expand Up @@ -76,7 +80,7 @@ describe('replaceAssetsLink', () => {
'utf8',
);
const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
const content1 = replaceAssetsLink(rawContent1, 'blog');
const content1 = replaceAssetsLink(rawContent1, '/blog');
expect(content1).toMatchSnapshot();
expect(content1).toContain('![image1](/blog/assets/image1.png)');
expect(content1).toContain('![image2](/blog/assets/image2.jpg)');
Expand All @@ -95,7 +99,7 @@ describe('replaceAssetsLink', () => {
'utf8',
);
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;
const content2 = replaceAssetsLink(rawContent2, 'blog');
const content2 = replaceAssetsLink(rawContent2, '/blog');
expect(content2).toMatchSnapshot();
expect(content2).not.toContain('![image1](/blog/assets/image1.png)');
expect(content2).not.toContain('![image2](/blog/assets/image2.jpg)');
Expand Down
5 changes: 3 additions & 2 deletions v1/lib/server/__tests__/docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const originalCwd = process.cwd();
if (!/website$/.test(originalCwd)) {
process.chdir(process.cwd() + '/website');
}

const path = require('path');
const fs = require('fs-extra');
const docs = require('../docs');
Expand Down Expand Up @@ -187,7 +188,7 @@ describe('getFile', () => {

describe('replaceAssetsLink', () => {
test('transform document with valid assets link', () => {
const content1 = replaceAssetsLink(rawContent1, 'docs');
const content1 = replaceAssetsLink(rawContent1, '/docs');
expect(content1).toMatchSnapshot();
expect(content1).toContain('![image1](/docs/assets/image1.png)');
expect(content1).toContain('![image2](/docs/assets/image2.jpg)');
Expand All @@ -201,7 +202,7 @@ describe('replaceAssetsLink', () => {
});

test('does not transform document without valid assets link', () => {
const content2 = replaceAssetsLink(rawContent2, 'docs');
const content2 = replaceAssetsLink(rawContent2, '/docs');
expect(content2).toMatchSnapshot();
expect(content2).not.toContain('![image1](/docs/assets/image1.png)');
expect(content2).not.toContain('![image2](/docs/assets/image2.jpg)');
Expand Down
9 changes: 8 additions & 1 deletion v1/lib/server/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const CWD = process.cwd();
const React = require('react');
const path = require('path');
const fs = require('fs-extra');
const metadataUtils = require('./metadataUtils');
const {replaceAssetsLink} = require('./utils.js');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
const loadConfig = require('./config');

const siteConfig = loadConfig(`${CWD}/siteConfig.js`);

function urlToSource(url) {
if (!url || typeof url !== 'string') {
Expand Down Expand Up @@ -59,7 +63,10 @@ function getMetadata(file) {
const metadata = Object.assign(
{
path: fileToUrl(file),
content: replaceAssetsLink(result.rawContent, 'blog'),
content: replaceAssetsLink(
result.rawContent,
`${siteConfig.baseUrl}blog`,
),
},
result.metadata,
);
Expand Down
2 changes: 1 addition & 1 deletion v1/lib/server/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,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, 'docs');
content = replaceAssetsLink(content, `${siteConfig.baseUrl}docs`);

const DocsLayout = require('../core/DocsLayout.js');
return renderToStaticMarkupWithDoctype(
Expand Down
6 changes: 1 addition & 5 deletions v1/lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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, '/');
Expand Down Expand Up @@ -74,10 +73,7 @@ function replaceAssetsLink(oldContent, location) {
}
return fencedBlock
? line
: line.replace(
/\]\(assets\//g,
`](${siteConfig.baseUrl}${location}/assets/`,
);
: line.replace(/\]\(assets\//g, `](${location}/assets/`);
});
return lines.join('\n');
}
Expand Down