Skip to content

External Link Checker: Open in new tab #3517

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

Merged
Merged
Changes from all commits
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
43 changes: 25 additions & 18 deletions tests/external-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ async function updateGithubIssue(urlResults) {

try {
const lines = body.split(/\r?\n/); // support both \n and \r\n newline types
const firstContentLine = lines.findIndex(line => line.startsWith(`|`)) + 2;
const firstContentLine = lines.indexOf(`<table>`) + 2;
lines.splice(0, firstContentLine); // delete first lines which only hold general data
for (const line of lines) {
const [, url, lastResults] = line.match(/^\| (.*) <td nowrap>(.*)<\/td>$/);
const [, lastResults, url] = line.match(/<tr><td nowrap>(.*?)<\/td><td><a href="(.*?)"/);

linkData[url] = lastResults.split(`&nbsp;`).map(item => {
if (item === `:heavy_check_mark:`) {
Expand Down Expand Up @@ -358,34 +358,41 @@ async function updateGithubIssue(urlResults) {
return linkData;
}

/**
* @param {LinkStatus} status The status to get the linked emoji for.
* @returns {string} An emoji, wrapped in a link to the failed job if applicable.
*/
function getStatusEmojiLink(status) {
if (!status.failed) {
return `:heavy_check_mark:`;
}

const message = status.message.replaceAll(`\n`, ` `).replaceAll(`"`, `&quot;`);
const emoji = getFailedEmoji(status.message);
return `<a href="${status.jobUrl}" title="${message}">${emoji}</a>`;
}

/**
* @param {LinkData} linkData The new link data from which to create the issue body.
* @returns {string} The new issue body (in Markdown and HTML) from the given link data.
*/
function getBodyFromLinkData(linkData) {
const scriptName = import.meta.url.split(`/`).slice(-2).join(`/`);
const rows = Object.entries(linkData).map(([url, statuses]) => {
const statusIcons = statuses.map(status => getStatusEmojiLink(status)).join(`&nbsp;`);
const link = `<a href="${url}" target="_blank">${url}</a>`;
return `<tr><td nowrap>${statusIcons}</td><td>${link}</td></tr>`;
});
const lines = [
`*Auto-generated content by \`${scriptName}\`.*`,
``,
`**Last updated:** ${new Date().toISOString()}`,
``,
`| URL <th nowrap>today … 6 days ago</th>`,
`|--------------------------------------|`,
...Object.entries(linkData).map(([url, statuses]) => {
const statusIcons = statuses.map(status => {
if (!status.failed) {
return `:heavy_check_mark:`;
}

const message = status.message.replaceAll(`\n`, ` `).replaceAll(`"`, `&quot;`);
const emoji = getFailedEmoji(status.message);
return `<a href="${status.jobUrl}" title="${message}">${emoji}</a>`;
}).join(`&nbsp;`);

return `| ${url} <td nowrap>${statusIcons}</td>`;
}),
`<table>`,
`<tr><th nowrap>today … 6 days ago</th><th>URL</th></tr>`,
...rows,
`</table>`,
];

return lines.join(`\n`);
}

Expand Down