Skip to content

Commit

Permalink
Replace reference links in documents (fixes facebook#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Oct 18, 2018
1 parent 8cf9afe commit c26e0dd
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions v1/lib/server/docs.js
Expand Up @@ -48,16 +48,24 @@ function getFile(metadata) {
function mdToHtmlify(oldContent, mdToHtml, metadata) {
let content = oldContent;
const mdLinks = [];
const mdReferences = [];

// find any links to markdown files
const regex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
let match = regex.exec(content);
while (match !== null) {
mdLinks.push(match[1]);
match = regex.exec(content);
const linkRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
let linkMatch = linkRegex.exec(content);
while (linkMatch !== null) {
mdLinks.push(linkMatch[1]);
linkMatch = linkRegex.exec(content);
}
// find any reference links to markdown files
const refRegex = /(?:\]\:)(?:\s)?(?:\.\/|\.\.\/)?([^'")\]\s>]+\.md)/g;
let refMatch = refRegex.exec(content);
while (refMatch !== null) {
mdReferences.push(refMatch[1]);
refMatch = refRegex.exec(content);
}

// replace to their website html links
// replace markdown links to their website html links
new Set(mdLinks).forEach(mdLink => {
let htmlLink = mdToHtml[mdLink];
if (htmlLink) {
Expand All @@ -75,6 +83,25 @@ function mdToHtmlify(oldContent, mdToHtml, metadata) {
);
}
});

// replace markdown refernces to their website html links
new Set(mdReferences).forEach(refLink => {
let htmlLink = mdToHtml[refLink];
if (htmlLink) {
htmlLink = getPath(htmlLink, siteConfig.cleanUrl);
htmlLink = htmlLink.replace('/en/', `/${metadata.language}/`);
htmlLink = htmlLink.replace(
'/VERSION/',
metadata.version && metadata.version !== env.versioning.latestVersion
? `/${metadata.version}/`
: '/',
);
content = content.replace(
new RegExp(`\\]\\:(?:\\s)?(\\./|\\.\\./)?${refLink}`, 'g'),
`]: ${htmlLink}`,
);
}
});
return content;
}

Expand Down

0 comments on commit c26e0dd

Please sign in to comment.