@@ -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 > ] + \. m d ) / 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 > ] + \. m d ) / 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 > ] + \. m d ) / 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