mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-03 08:49:51 +02:00
feat: support reference-style linking in documents (#1048)
feat: support reference-style linking in documents
This commit is contained in:
parent
7488de266f
commit
bbf355fdc7
7 changed files with 180 additions and 14 deletions
|
@ -52,14 +52,31 @@ module.exports = function(fileString) {
|
|||
if (fencedBlock) return line;
|
||||
|
||||
let modifiedLine = line;
|
||||
const mdLinks = [];
|
||||
const mdRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
|
||||
let match = mdRegex.exec(content);
|
||||
while (match !== null) {
|
||||
mdLinks.push(match[1]);
|
||||
match = mdRegex.exec(content);
|
||||
const inlineLinks = [];
|
||||
const refLinks = [];
|
||||
|
||||
/* Replace inline-style links e.g:
|
||||
This is [Document 1](doc1.md) -> we replace this doc1.md with correct link
|
||||
*/
|
||||
const inlineRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
|
||||
let inlineMatch = inlineRegex.exec(content);
|
||||
while (inlineMatch !== null) {
|
||||
inlineLinks.push(inlineMatch[1]);
|
||||
inlineMatch = inlineRegex.exec(content);
|
||||
}
|
||||
mdLinks.forEach(mdLink => {
|
||||
|
||||
/* Replace reference-style links e.g:
|
||||
This is [Document 1][doc1].
|
||||
[doc1]: doc1.md -> we replace this doc1.md with correct link
|
||||
*/
|
||||
const refRegex = /(?:\]:)(?:\s)?(?:\.\/|\.\.\/)?([^'")\]\s>]+\.md)/g;
|
||||
let refMatch = refRegex.exec(content);
|
||||
while (refMatch !== null) {
|
||||
refLinks.push(refMatch[1]);
|
||||
refMatch = refRegex.exec(content);
|
||||
}
|
||||
|
||||
[...refLinks, ...inlineLinks].forEach(mdLink => {
|
||||
const targetSource = `${sourceDir}/${mdLink}`;
|
||||
const {permalink} = sourceToMetadata[targetSource] || {};
|
||||
if (permalink) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue