Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Support mapping links to full URLs from hint Github repo #934

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions helpers/update-site/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,21 @@ const getFilesInfo = (files) => {
};

/** Updates the markdown links to other parts of the documentation, i.e.: start with "./" */
const updateMarkdownlinks = (content, filePath) => {
const updateMarkdownlinks = (file) => {
const content = file.content;
const filePath = file.orig || file.dest;
/**
* This regex should match the following examples:
* * [link label]: ./somewhere.md → \s
* * [link label]:./somewhere.md → :
* * [link](./somewhere.md) → \(
* * [link](https://github.com/webhintio/hint/blob/HEAD/path-to-file/file.md)
* * [link]: https://github.com/webhintio/hint/blob/HEAD/path-to-file/file.md
* * [link]:https://github.com/webhintio/hint/blob/HEAD/path-to-file/file.md
*
* The \.? allow us to also match "../"
*/
const mdLinkRegex = /(\s|:|\()\.?\.\/(\S*?)\.md/gi;
const mdLinkRegex = /(]:\s*|]\()(\.?\.\/|https:\/\/github.com\/webhintio\/hint\/blob\/HEAD\/(packages\/)?)(\S*?)\.md/gi;
const isIndex = filePath.toLowerCase().endsWith('index.md');
let transformed = content;
let val;
Expand Down Expand Up @@ -564,14 +569,25 @@ const updateMarkdownlinks = (content, filePath) => {
val[0].replace(/(index|readme)\.md/i, '') :
val[0].replace('.md', '/');

/*
* An github url is found for a md file.
* In this case we need to replace it with the a valid
* url in the website.
*/
if (val[2].startsWith('https')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit weird to say the link is for github and then check just for https.

Suggested change
if (val[2].startsWith('https')) {
if (val[2].startsWith('https://github.com/')) {

replacement =
`${val[1]}/docs/${file.frontMatter.section}${file.frontMatter.tocTitle ? `/${file.frontMatter.tocTitle.replace(' ', '-')}` : ''}/${val[4].replace('/docs/', '/')}/`;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could document what the different val[x] are?

transformed = transformed.replace(val[0], replacement);
}
/**
* If val[0] contains './docs/ then, it is part of a multi-hint hint, so
* we need to ignore it.
* Later in the the `link-filter.js`, the link will be replaced for the final one.
*/
if (!val[0].includes('./docs/')) {
/**
* `val[1] is the first capturing group. It could be "\s", ":", or "("
* `val[1] is the first capturing group. It could be "]:\s", "]:", or "]("
* and it's part of the matched value so we need to add it at the beginning as well. E.g.:
*
* * `[events](./events.md)` will match `(./events.md` that needs to be transformed into `(../events/`.
Expand All @@ -595,7 +611,7 @@ const updateLinks = (files) => {
return;
}

file.content = updateMarkdownlinks(file.content, file.orig || file.dest);
file.content = updateMarkdownlinks(file);
});
};

Expand Down