@@ -524,22 +524,56 @@ const getFilesInfo = (files) => {
524
524
} ;
525
525
526
526
/** Updates the markdown links to other parts of the documentation, i.e.: start with "./" */
527
- const updateMarkdownlinks = ( content , filePath ) => {
527
+ const updateMarkdownlinks = ( file ) => {
528
+ const content = file . content ;
529
+ const filePath = file . orig || file . dest ;
528
530
/**
529
531
* This regex should match the following examples:
530
532
* * [link label]: ./somewhere.md → \s
531
533
* * [link label]:./somewhere.md → :
532
534
* * [link](./somewhere.md) → \(
535
+ * * [link](https://github.com/webhintio/hint/blob/HEAD/path-to-file/file.md)
536
+ * * [link]: https://github.com/webhintio/hint/blob/HEAD/path-to-file/file.md
537
+ * * [link]:https://github.com/webhintio/hint/blob/HEAD/path-to-file/file.md
533
538
*
534
539
* The \.? allow us to also match "../"
535
540
*/
536
- const mdLinkRegex = / ( \s | : | \( ) \. ? \. \/ ( \S * ?) \. m d / gi;
541
+ const mdLinkRegex = / ( ] : \s * | ] \( ) ( \. ? \. \/ | h t t p s : \/ \/ g i t h u b . c o m \/ w e b h i n t i o \/ h i n t \/ b l o b \/ H E A D \/ ( p a c k a g e s \/ ) ? ) ( \S * ?) \. m d / gi;
537
542
const isIndex = filePath . toLowerCase ( ) . endsWith ( 'index.md' ) ;
538
543
let transformed = content ;
539
544
let val ;
540
545
541
546
while ( ( val = mdLinkRegex . exec ( content ) ) !== null ) {
542
547
/**
548
+ * Values:
549
+ *
550
+ * val[0]: Full string matched. E.g:
551
+ * * - `](./getting-started/architecture.md`
552
+ * * - `]: ../../user-guide/index.md`
553
+ * * - `](https://github.com/webhintio/hint/blob/HEAD/packages/hint/docs/user-guide/troubleshoot/summary.md`
554
+ *
555
+ * val[1]: First group matched (]:\s*|]\(). E.g:
556
+ * * - `](`
557
+ * * - `]: `
558
+ * * - `](`
559
+ *
560
+ * val[2]: Second group matched
561
+ * (\.?\.\/|https:\/\/github.com\/webhintio\/hint\/blob\/HEAD\/(packages\/)?)
562
+ * E.g:
563
+ * * - `./`
564
+ * * - `../`
565
+ * * - `https://github.com/webhintio/hint/blob/HEAD/packages/`
566
+ *
567
+ * val[3]: Third group matched (packages\/). E.g:
568
+ * * - `undefined`
569
+ * * - `undefined`
570
+ * * - `packages/`
571
+ *
572
+ * val[4]: fourth group matched (\S*?). E.g:
573
+ * * - getting-started/architecture
574
+ * * - ../user-guide/index
575
+ * * - hint/docs/user-guide/troubleshoot/summary
576
+ *
543
577
* Pages in the live site are `index.html` inside a folder with the name of the original page. E.g.:
544
578
*
545
579
* * `/user-guide/concepts/connectors.md` → `/user-guide/concepts/connectors/
@@ -564,14 +598,31 @@ const updateMarkdownlinks = (content, filePath) => {
564
598
val [ 0 ] . replace ( / ( i n d e x | r e a d m e ) \. m d / i, '' ) :
565
599
val [ 0 ] . replace ( '.md' , '/' ) ;
566
600
601
+ /*
602
+ * An github url is found for a md file.
603
+ * In this case we need to replace it with the a valid
604
+ * url in the website.
605
+ */
606
+ if ( val [ 2 ] . startsWith ( 'https://github.com/' ) ) {
607
+ /*
608
+ * Documentation in the package hint has to be parsed in a diferent way
609
+ * because it already has the folder structure in place.
610
+ */
611
+ if ( val [ 4 ] . startsWith ( 'hint/docs/user-guide' ) ) {
612
+ replacement = `${ val [ 1 ] } ${ val [ 4 ] . substr ( 4 ) } ` ;
613
+ } else {
614
+ replacement =
615
+ `${ val [ 1 ] } /docs/${ file . frontMatter . section } ${ file . frontMatter . tocTitle ? `/${ file . frontMatter . tocTitle . replace ( ' ' , '-' ) } ` : '' } /${ val [ 4 ] . replace ( '/docs/' , '/' ) } /` ;
616
+ }
617
+ }
567
618
/**
568
619
* If val[0] contains './docs/ then, it is part of a multi-hint hint, so
569
620
* we need to ignore it.
570
621
* Later in the the `link-filter.js`, the link will be replaced for the final one.
571
622
*/
572
623
if ( ! val [ 0 ] . includes ( './docs/' ) ) {
573
624
/**
574
- * `val[1] is the first capturing group. It could be "\s", ":", or "("
625
+ * `val[1] is the first capturing group. It could be "]: \s", "] :", or "] ("
575
626
* and it's part of the matched value so we need to add it at the beginning as well. E.g.:
576
627
*
577
628
* * `[events](./events.md)` will match `(./events.md` that needs to be transformed into `(../events/`.
@@ -595,7 +646,7 @@ const updateLinks = (files) => {
595
646
return ;
596
647
}
597
648
598
- file . content = updateMarkdownlinks ( file . content , file . orig || file . dest ) ;
649
+ file . content = updateMarkdownlinks ( file ) ;
599
650
} ) ;
600
651
} ;
601
652
0 commit comments