Skip to content

Commit

Permalink
Refactor mdToHtml out
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Jul 12, 2018
1 parent a7a214f commit 816b11c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 48 deletions.
27 changes: 3 additions & 24 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,7 @@ async function execute() {

const buildDir = join(CWD, 'build', siteConfig.projectName);

// mdToHtml is a map from a markdown file name to its html link, used to
// change relative markdown links that work on GitHub into actual site links
const mdToHtml = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
if (metadata.language !== 'en' || metadata.original_id) {
return;
}
let htmlLink =
siteConfig.baseUrl + metadata.permalink.replace('/next/', '/');
if (htmlLink.includes('/docs/en/')) {
htmlLink = htmlLink.replace('/docs/en/', '/docs/en/VERSION/');
} else {
htmlLink = htmlLink.replace('/docs/', '/docs/VERSION/');
}
mdToHtml[metadata.source] = htmlLink;
});
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl);

const DocsLayout = require('../core/DocsLayout.js');
const Redirect = require('../core/Redirect.js');
Expand Down Expand Up @@ -171,14 +155,9 @@ async function execute() {
? `/${metadata.version}/`
: '/'
);
// replace relative links without "./"
rawContent = rawContent.replace(
new RegExp(`\\]\\(${key}`, 'g'),
`](${link}`
);
// replace relative links with "./"
// replace relative links with & without "./"
rawContent = rawContent.replace(
new RegExp(`\\]\\(\\./${key}`, 'g'),
new RegExp(`\\]\\((${key}|\\./${key})`, 'g'),
`](${link}`
);
});
Expand Down
21 changes: 21 additions & 0 deletions lib/server/metadataUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ function extractMetadata(content) {
return {metadata, rawContent: both.content};
}

// mdToHtml is a map from a markdown file name to its html link, used to
// change relative markdown links that work on GitHub into actual site links
function mdToHtml(Metadata, baseUrl) {
const result = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
if (metadata.language !== 'en' || metadata.original_id) {
return;
}
let htmlLink = baseUrl + metadata.permalink.replace('/next/', '/');
if (htmlLink.includes('/docs/en/')) {
htmlLink = htmlLink.replace('/docs/en/', '/docs/en/VERSION/');
} else {
htmlLink = htmlLink.replace('/docs/', '/docs/VERSION/');
}
result[metadata.source] = htmlLink;
});
return result;
}

module.exports = {
extractMetadata,
mdToHtml,
};
27 changes: 3 additions & 24 deletions lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,7 @@ function execute(port, options) {
links[metadata.permalink] = id;
});

// mdToHtml is a map from a markdown file name to its html link, used to
// change relative markdown links that work on GitHub into actual site links
const mdToHtml = {};
Object.keys(Metadata).forEach(id => {
const metadata = Metadata[id];
if (metadata.language !== 'en' || metadata.original_id) {
return;
}
let htmlLink =
siteConfig.baseUrl + metadata.permalink.replace('/next/', '/');
if (htmlLink.includes('/docs/en/')) {
htmlLink = htmlLink.replace('/docs/en/', '/docs/en/VERSION/');
} else {
htmlLink = htmlLink.replace('/docs/', '/docs/VERSION/');
}
mdToHtml[metadata.source] = htmlLink;
});
const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl);

const metadata = Metadata[links[url]];
if (!metadata) {
Expand Down Expand Up @@ -259,14 +243,9 @@ function execute(port, options) {
? `/${metadata.version}/`
: '/'
);
// replace relative links without "./"
rawContent = rawContent.replace(
new RegExp(`\\]\\(${key}`, 'g'),
`](${link}`
);
// replace relative links with "./"
// replace relative links with & without "./"
rawContent = rawContent.replace(
new RegExp(`\\]\\(\\./${key}`, 'g'),
new RegExp(`\\]\\((${key}|\\./${key})`, 'g'),
`](${link}`
);
});
Expand Down

0 comments on commit 816b11c

Please sign in to comment.