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(github): Better massageMarkdown implementation #11133

Merged
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion lib/platform/github/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7665,7 +7665,10 @@ Array [
]
`;

exports[`platform/github/index massageMarkdown(input) returns updated pr body 1`] = `"https://github.com/foo/bar/issues/5 plus also [a link](https://github.com/foo/bar/issues/5)"`;
exports[`platform/github/index massageMarkdown(input) returns updated pr body 1`] = `
"[https://github.com/foo/bar/issues/5](https://github.com/foo/bar/issues/5) plus also [a link](https://github.com/foo/bar/issues/5)
viceice marked this conversation as resolved.
Show resolved Hide resolved
"
`;

exports[`platform/github/index mergePr(prNo) - autodetection should give up 1`] = `
Array [
Expand Down
7 changes: 2 additions & 5 deletions lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
PrList,
} from './types';
import { UserDetails, getUserDetails, getUserEmail } from './user';
import { massageMarkdownLinks } from './massage-markdown-links';

const githubApi = new githubHttp.GithubHttp();

Expand Down Expand Up @@ -1572,11 +1573,7 @@ export function massageMarkdown(input: string): string {
if (config.isGhe) {
return smartTruncate(input, 60000);
}
const massagedInput = input
// to be safe, replace all github.com links with renovatebot redirector
.replace(/href="https?:\/\/github.com\//g, 'href="https://github.com/')
.replace(/]\(https:\/\/github\.com\//g, '](https://github.com/')
.replace(/]: https:\/\/github\.com\//g, ']: https://github.com/');
const massagedInput = massageMarkdownLinks(input);
return smartTruncate(massagedInput, 60000);
}

Expand Down
35 changes: 35 additions & 0 deletions lib/platform/github/massage-markdown-links.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getName } from '../../../test/util';
import { massageMarkdownLinks } from './massage-markdown-links';

describe(getName(), () => {
const table = [
['github.com', '[github.com](github.com)'],
['www.github.com', '[www.github.com](www.github.com)'],
['api.github.com', 'api.github.com'],
['github.com', 'github.com'],
['foobartogit.luolix.top', 'foobartogit.luolix.top'],
['[github.com](github.com)', '[github.com](github.com)'],
['[github.com](www.github.com)', '[github.com](www.github.com)'],
[
'[github.com](https://github.com/foo/bar)',
'[github.com](https://github.com/foo/bar)',
],
[
'https://github.com/foo/bar',
'[https://github.com/foo/bar](https://github.com/foo/bar)',
],
[
'[foobar](https://github.com/foo/bar)',
'[foobar](https://github.com/foo/bar)',
],
[
'[https://github.com/foo/bar](https://github.com/foo/bar)',
'[https://github.com/foo/bar](https://github.com/foo/bar)',
],
];

test.each(table)('%s -> %s', (input, output) => {
const res = massageMarkdownLinks(input).trimRight();
expect(res).toEqual(output);
});
});
48 changes: 48 additions & 0 deletions lib/platform/github/massage-markdown-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import findAndReplace from 'mdast-util-find-and-replace';
import remark from 'remark';
import type { Plugin, Transformer } from 'unified';
// eslint-disable-next-line import/no-unresolved
Copy link
Collaborator Author

@zharinov zharinov Aug 6, 2021

Choose a reason for hiding this comment

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

@types/unist exists in NPM while unist doesn't 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

It's a type only package 🤔

import type { Node } from 'unist';
import visit from 'unist-util-visit';

const urlRegex =
/(?:https?:)?(?:\/\/)?(?:www\.)?(?<!api\.)(?:to)?github\.com(?:[/?#][^\s")'.,]*)?/i;

function massageLink(input: string): string {
return urlRegex.test(input)
? input.replace(/(?:to)?github\.com/, 'github.com')
: input;
}

function linkifyText(url: string): Node | boolean {
const newUrl = massageLink(url);
if (newUrl !== url) {
const content = { type: 'text', value: url };
return {
type: 'link',
url: newUrl,
title: null,
children: [content],
} as Node;
}
return false;
}

function transformer(tree: Node): void {
findAndReplace(tree, urlRegex, linkifyText, {
ignore: ['link', 'linkReference'],
});
visit(tree, 'link', (node: any) => {
if (node?.url) {
// eslint-disable-next-line no-param-reassign
node.url = massageLink(node.url);
}
});
}

const githubExtra: Plugin<any> = (): Transformer => transformer;
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just didn't want to write this lambda in .use(...) expression


export function massageMarkdownLinks(content: string): string {
const output = remark().use(githubExtra).processSync(content);
return output.toString();
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@
"luxon": "2.0.1",
"markdown-it": "12.1.0",
"markdown-table": "2.0.0",
"mdast-util-find-and-replace": "1.0.0",
"mdast-util-to-string": "1.0.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same versions as for remark-github

Copy link
Member

Choose a reason for hiding this comment

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

remark-github seems to use ^1.0.0 which not points to some higher version, check lockfile changes below

"minimatch": "3.0.4",
"moo": "0.5.1",
"node-html-parser": "3.3.6",
Expand All @@ -187,6 +189,7 @@
"simple-git": "2.41.2",
"slugify": "1.6.0",
"traverse": "0.6.6",
"unist-util-visit": "2.0.0",
"upath": "2.0.1",
"url-join": "4.0.1",
"validate-npm-package-name": "3.0.0",
Expand Down
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6501,6 +6501,15 @@ matcher@^3.0.0:
dependencies:
escape-string-regexp "^4.0.0"

mdast-util-find-and-replace@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/mdast-util-find-and-replace/-/mdast-util-find-and-replace-1.0.0.tgz#ce33ca0e720ce6ba16cf49dea441cd5f277270a1"
integrity sha512-3ArHEP8jGEtgcAyM1kUDhsuZwEVfabBjAQz9ohRWKHkheaBFdOElA0uzHKYVpuh6flnDTkg7cqWBs0pHSfPLqQ==
dependencies:
escape-string-regexp "^4.0.0"
unist-util-is "^4.0.0"
unist-util-visit-parents "^3.0.0"

mdast-util-find-and-replace@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/mdast-util-find-and-replace/-/mdast-util-find-and-replace-1.1.1.tgz#b7db1e873f96f66588c321f1363069abf607d1b5"
Expand Down Expand Up @@ -6533,6 +6542,11 @@ mdast-util-to-markdown@^0.6.0:
repeat-string "^1.0.0"
zwitch "^1.0.0"

mdast-util-to-string@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/mdast-util-to-string/-/mdast-util-to-string-1.0.0.tgz#4f74267b39353df5f49727cd9c3d108b1196845f"
integrity sha1-T3Qmezk1PfX0lyfNnD0QixGWhF8=

mdast-util-to-string@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/mdast-util-to-string/-/mdast-util-to-string-1.1.0.tgz#27055500103f51637bd07d01da01eb1967a43527"
Expand Down Expand Up @@ -9449,6 +9463,15 @@ unist-util-visit-parents@^3.0.0:
"@types/unist" "^2.0.0"
unist-util-is "^4.0.0"

unist-util-visit@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-2.0.0.tgz#1fdae5ea88251651bfe49b7e84390d664fc227c5"
integrity sha512-kiTpWKsF54u/78L/UU/i7lxrnqGiEWBgqCpaIZBYP0gwUC+Akq0Ajm4U8JiNIoQNfAioBdsyarnOcTEAb9mLeQ==
dependencies:
"@types/unist" "^2.0.0"
unist-util-is "^4.0.0"
unist-util-visit-parents "^3.0.0"

unist-util-visit@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-2.0.3.tgz#c3703893146df47203bb8a9795af47d7b971208c"
Expand Down