Skip to content

Commit 62968ae

Browse files
committed
add for v2 as well
1 parent 4de9b6d commit 62968ae

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

v1/lib/server/docs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ function mdToHtmlify(oldContent, mdToHtml, metadata) {
5050
const mdLinks = [];
5151
const mdReferences = [];
5252

53-
// find any links to markdown files
53+
// find any inline-style links to markdown files
5454
const linkRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
5555
let linkMatch = linkRegex.exec(content);
5656
while (linkMatch !== null) {
5757
mdLinks.push(linkMatch[1]);
5858
linkMatch = linkRegex.exec(content);
5959
}
60-
// find any reference links to markdown files
60+
// find any reference-style links to markdown files
6161
const refRegex = /(?:\]:)(?:\s)?(?:\.\/|\.\.\/)?([^'")\]\s>]+\.md)/g;
6262
let refMatch = refRegex.exec(content);
6363
while (refMatch !== null) {

v2/lib/webpack/loader/markdown.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,31 @@ module.exports = function(fileString) {
5252
if (fencedBlock) return line;
5353

5454
let modifiedLine = line;
55-
const mdLinks = [];
56-
const mdRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
57-
let match = mdRegex.exec(content);
58-
while (match !== null) {
59-
mdLinks.push(match[1]);
60-
match = mdRegex.exec(content);
55+
const inlineLinks = [];
56+
const refLinks = [];
57+
58+
/* Replace inline-style links e.g:
59+
This is [Document 1](doc1.md) -> we replace this doc1.md with correct link
60+
*/
61+
const inlineRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
62+
let inlineMatch = inlineRegex.exec(content);
63+
while (inlineMatch !== null) {
64+
inlineLinks.push(inlineMatch[1]);
65+
inlineMatch = inlineRegex.exec(content);
66+
}
67+
68+
/* Replace reference-style links e.g:
69+
This is [Document 1][doc1].
70+
[doc1]: doc1.md -> we replace this doc1.md with correct link
71+
*/
72+
const refRegex = /(?:\]:)(?:\s)?(?:\.\/|\.\.\/)?([^'")\]\s>]+\.md)/g;
73+
let refMatch = refRegex.exec(content);
74+
while (refMatch !== null) {
75+
refLinks.push(refMatch[1]);
76+
refMatch = refRegex.exec(content);
6177
}
62-
mdLinks.forEach(mdLink => {
78+
79+
[...refLinks, ...inlineLinks].forEach(mdLink => {
6380
const targetSource = `${sourceDir}/${mdLink}`;
6481
const {permalink} = sourceToMetadata[targetSource] || {};
6582
if (permalink) {

0 commit comments

Comments
 (0)