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

Add external link redirect mechanism #2586

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

frankharkins
Copy link
Member

@frankharkins frankharkins commented Jan 21, 2025

Fixes #2579 by adding a mechanism to redirect links without needing to modify the artifact. This is less work for maintainers, and less risky as it means we don't need to edit the artifact as much.

A future PR could have the link checker push to external-redirects.ts when it detects a 301, but this should be fine for now.

Copy link
Collaborator

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Thanks! This is a good idea.

scripts/config/external-redirects.ts Outdated Show resolved Hide resolved
Comment on lines 48 to 50
for (const redirect of externalRedirects) {
markdown = markdown.replaceAll(redirect.old, redirect.new);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This makes more sense in processHtml.ts, such as how we implement this:

export function replaceViewcodeLinksWithGitHub(

Reasons:
a) It'd be good to have a unit test for this new functionality
b) It's less risky using Cheerio because we can be confident we're only changing the <a> and not anything unexpected
c) abstraction level: this function sphinxHtmlToMarkdown is meant to be very high level and having this replace breaks that abstraction.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with Eric that maybe this is better to implement in processHtml.ts 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok done, the catch with <a> is that it'll only replace the href, not the link text. Is that the intended behaviour?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd be good to replace the text as well if it's a match. You can use .text() to do that

export function updateModuleHeadings($: CheerioAPI, $main: Cheerio<any>): void {
$main
.find("h1,h2")
.toArray()
.forEach((el) => {
const $el = $(el);
const $a = $($el.find("a"));
const signature = $a.text();
$a.remove();
let title = $el.text();
title = title.replace("()", "");
let replacement = `<${el.tagName}>${title}</${el.tagName}>`;
if (signature.trim().length > 0) {
replacement += `<p><code>${signature}</code></p>`;
}
$el.replaceWith(replacement);
});
}

scripts/js/lib/api/htmlToMd.ts Outdated Show resolved Hide resolved
scripts/js/lib/api/htmlToMd.ts Outdated Show resolved Hide resolved
};
const doc = CheerioDoc.load(
`<a href="https://ibm.com/old">https://ibm.com/old</a>
<a href="https://basename.example-website.com/old#anchor?and=query"></a>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please add a URL that is not redirected? Ideally it looks very similar to an old URL, such as https://ibm.com/old-but-not-redirected. We want to only change exact matches.

Comment on lines +222 to +223
const newText = $a.text().replaceAll(href, replacement);
$a.text(newText)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should require an exact match, not partial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Extend link checker to handle redirects
3 participants