Skip to content

Commit 7017f3f

Browse files
authored
Fix: Support mapping links to full URLs from hint Github repo
Fix #933 Close #934
1 parent bd0b812 commit 7017f3f

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

helpers/update-site/documentation.js

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,22 +524,56 @@ const getFilesInfo = (files) => {
524524
};
525525

526526
/** 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;
528530
/**
529531
* This regex should match the following examples:
530532
* * [link label]: ./somewhere.md → \s
531533
* * [link label]:./somewhere.md → :
532534
* * [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
533538
*
534539
* The \.? allow us to also match "../"
535540
*/
536-
const mdLinkRegex = /(\s|:|\()\.?\.\/(\S*?)\.md/gi;
541+
const mdLinkRegex = /(]:\s*|]\()(\.?\.\/|https:\/\/github.com\/webhintio\/hint\/blob\/HEAD\/(packages\/)?)(\S*?)\.md/gi;
537542
const isIndex = filePath.toLowerCase().endsWith('index.md');
538543
let transformed = content;
539544
let val;
540545

541546
while ((val = mdLinkRegex.exec(content)) !== null) {
542547
/**
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+
*
543577
* Pages in the live site are `index.html` inside a folder with the name of the original page. E.g.:
544578
*
545579
* * `/user-guide/concepts/connectors.md` → `/user-guide/concepts/connectors/
@@ -564,14 +598,31 @@ const updateMarkdownlinks = (content, filePath) => {
564598
val[0].replace(/(index|readme)\.md/i, '') :
565599
val[0].replace('.md', '/');
566600

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+
}
567618
/**
568619
* If val[0] contains './docs/ then, it is part of a multi-hint hint, so
569620
* we need to ignore it.
570621
* Later in the the `link-filter.js`, the link will be replaced for the final one.
571622
*/
572623
if (!val[0].includes('./docs/')) {
573624
/**
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 "]("
575626
* and it's part of the matched value so we need to add it at the beginning as well. E.g.:
576627
*
577628
* * `[events](./events.md)` will match `(./events.md` that needs to be transformed into `(../events/`.
@@ -595,7 +646,7 @@ const updateLinks = (files) => {
595646
return;
596647
}
597648

598-
file.content = updateMarkdownlinks(file.content, file.orig || file.dest);
649+
file.content = updateMarkdownlinks(file);
599650
});
600651
};
601652

0 commit comments

Comments
 (0)