Skip to content

Commit

Permalink
refactor: replace linkify-markdown (#9954)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
viceice and rarkins authored May 19, 2021
1 parent a52b39c commit d8f4b86
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 361 deletions.
15 changes: 15 additions & 0 deletions lib/util/__snapshots__/markdown.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`util/markdown .linkify works 1`] = `
"Some references:
- Commit: [\`f808317\`](https://github.com/some/repo/commit/f8083175fe890cbf14f41d0a06e7aa35d4989587)
- Commit (fork): [foo@\`f808317\`](https://github.com/foo/repo/commit/f8083175fe890cbf14f41d0a06e7aa35d4989587)
- Commit (repo): [remarkjs/remark@\`e1aa9f6\`](https://github.com/remarkjs/remark/commit/e1aa9f6c02de18b9459b7d269712bcb50183ce89)
- Issue or PR (\`#\`): [#1](https://github.com/some/repo/issues/1)
- Issue or PR (\`GH-\`): [GH-1](https://github.com/some/repo/issues/1)
- Issue or PR (fork): [foo#1](https://github.com/foo/repo/issues/1)
- Issue or PR (project): [remarkjs/remark#1](https://github.com/remarkjs/remark/issues/1)
- Mention: [@wooorm](https://github.com/wooorm)
"
`;
21 changes: 21 additions & 0 deletions lib/util/markdown.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getName } from '../../test/util';
import { linkify } from './markdown';

describe(getName(), () => {
describe('.linkify', () => {
const md = `Some references:
* Commit: f8083175fe890cbf14f41d0a06e7aa35d4989587
* Commit (fork): foo@f8083175fe890cbf14f41d0a06e7aa35d4989587
* Commit (repo): remarkjs/remark@e1aa9f6c02de18b9459b7d269712bcb50183ce89
* Issue or PR (\`#\`): #1
* Issue or PR (\`GH-\`): GH-1
* Issue or PR (fork): foo#1
* Issue or PR (project): remarkjs/remark#1
* Mention: @wooorm
`;
it('works', async () => {
expect(await linkify(md, { repository: 'some/repo' })).toMatchSnapshot();
});
});
});
21 changes: 21 additions & 0 deletions lib/util/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import remark from 'remark';
import github from 'remark-github';

// Generic replacements/link-breakers
export function sanitizeMarkdown(markdown: string): string {
let res = markdown;
Expand All @@ -16,3 +19,21 @@ export function sanitizeMarkdown(markdown: string): string {
res = res.replace(/`#&#8203;(\d+)`/g, '`#$1`');
return res;
}

/**
*
* @param content content to process
* @param options github options
* @returns linkified content
*/
export async function linkify(
content: string,
options: github.RemarkGithubOptions
): Promise<string> {
// https://github.com/syntax-tree/mdast-util-to-markdown#optionsbullet
const output = await remark()
.use({ settings: { bullet: '-' } })
.use(github, { mentionStrong: false, ...options })
.process(content);
return output.toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ Closes ADAPT-207
See merge request itentialopensource/adapter-utils!177
* * *
***
",
"url": "https://gitlab.com/itentialopensource/adapter-utils/blob/master/CHANGELOG.md#4330-05-15-2020",
}
Expand Down Expand Up @@ -789,7 +789,7 @@ Object {
([#4730](https://github.com/facebook/jest/pull/4730))
- \`[jest-runtime]\` Use realpath to match transformers
([#5000](https://github.com/facebook/jest/pull/5000))
- \`[expect]\` [**BREAKING**] Replace identity equality with Object.is in toBe
- \`[expect]\` \\\\[**BREAKING**] Replace identity equality with Object.is in toBe
matcher ([#4917](https://github.com/facebook/jest/pull/4917))
##### Features
Expand All @@ -810,18 +810,18 @@ Object {
([#4866](https://github.com/facebook/jest/pull/4866))
- \`[eslint-plugin-jest]\` Add \`prefer-to-have-length\` lint rule.
([#4771](https://github.com/facebook/jest/pull/4771))
- \`[jest-environment-jsdom]\` [**BREAKING**] Upgrade to JSDOM@11
- \`[jest-environment-jsdom]\` \\\\[**BREAKING**] Upgrade to JSDOM@11
([#4770](https://github.com/facebook/jest/pull/4770))
- \`[jest-environment-*]\` [**BREAKING**] Add Async Test Environment APIs, dispose
- \`[jest-environment-*]\` \\\\[**BREAKING**] Add Async Test Environment APIs, dispose
is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))
- \`[jest-cli]\` Add an option to clear the cache
([#4430](https://github.com/facebook/jest/pull/4430))
- \`[babel-plugin-jest-hoist]\` Improve error message, that the second argument of
\`jest.mock\` must be an inline function
([#4593](https://github.com/facebook/jest/pull/4593))
- \`[jest-snapshot]\` [**BREAKING**] Concatenate name of test and snapshot
- \`[jest-snapshot]\` \\\\[**BREAKING**] Concatenate name of test and snapshot
([#4460](https://github.com/facebook/jest/pull/4460))
- \`[jest-cli]\` [**BREAKING**] Fail if no tests are found
- \`[jest-cli]\` \\\\[**BREAKING**] Fail if no tests are found
([#3672](https://github.com/facebook/jest/pull/3672))
- \`[jest-diff]\` Highlight only last of odd length leading spaces
([#4558](https://github.com/facebook/jest/pull/4558))
Expand Down Expand Up @@ -855,7 +855,7 @@ Object {
([#4614](https://github.com/facebook/jest/pull/4614))
- \`[jest-runtime]\` Support sourcemaps in transformers
([#3458](https://github.com/facebook/jest/pull/3458))
- \`[jest-snapshot]\` [**BREAKING**] Add a serializer for \`jest.fn\` to allow a
- \`[jest-snapshot]\` \\\\[**BREAKING**] Add a serializer for \`jest.fn\` to allow a
snapshot of a jest mock ([#4668](https://github.com/facebook/jest/pull/4668))
- \`[jest-worker]\` Initial version of parallel worker abstraction, say hello!
([#4497](https://github.com/facebook/jest/pull/4497))
Expand All @@ -877,7 +877,7 @@ Object {
([#5093](https://github.com/facebook/jest/pull/5093))
- \`[jest-util]\` \`jest-util\` should not depend on \`jest-mock\`
([#4992](https://github.com/facebook/jest/pull/4992))
- \`[*]\` [**BREAKING**] Drop support for Node.js version 4
- \`[*]\` \\\\[**BREAKING**] Drop support for Node.js version 4
([#4769](https://github.com/facebook/jest/pull/4769))
- \`[docs]\` Wrap code comments at 80 characters
([#4781](https://github.com/facebook/jest/pull/4781))
Expand Down
10 changes: 5 additions & 5 deletions lib/workers/pr/changelog/release-notes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as URL from 'url';
import is from '@sindresorhus/is';
import { linkify } from 'linkify-markdown';
import { DateTime } from 'luxon';
import MarkdownIt from 'markdown-it';
import { PLATFORM_TYPE_GITLAB } from '../../../constants/platforms';
import { logger } from '../../../logger';
import * as memCache from '../../../util/cache/memory';
import * as packageCache from '../../../util/cache/package';
import * as hostRules from '../../../util/host-rules';
import { linkify } from '../../../util/markdown';
import * as github from './github';
import * as gitlab from './gitlab';
import type { ChangeLogFile, ChangeLogNotes, ChangeLogResult } from './types';
Expand Down Expand Up @@ -104,7 +104,7 @@ export async function getReleaseNotes(
const releaseList = await getCachedReleaseList(apiBaseUrl, repository);
logger.trace({ releaseList }, 'Release list from getReleaseList');
let releaseNotes: ChangeLogNotes | null = null;
releaseList.forEach((release) => {
for (const release of releaseList) {
if (
release.tag === version ||
release.tag === `v${version}` ||
Expand All @@ -120,7 +120,7 @@ export async function getReleaseNotes(
if (releaseNotes.body.length) {
try {
if (baseUrl !== 'https://gitlab.com/') {
releaseNotes.body = linkify(releaseNotes.body, {
releaseNotes.body = await linkify(releaseNotes.body, {
repository: `${baseUrl}${repository}`,
});
}
Expand All @@ -131,7 +131,7 @@ export async function getReleaseNotes(
releaseNotes = null;
}
}
});
}
logger.trace({ releaseNotes });
return releaseNotes;
}
Expand Down Expand Up @@ -257,7 +257,7 @@ export async function getReleaseNotesMd(
body = massageBody(body, baseUrl);
if (body?.length) {
try {
body = linkify(body, {
body = await linkify(body, {
repository: `${baseUrl}${repository}`,
});
} catch (err) /* istanbul ignore next */ {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
"json-dup-key-validator": "1.0.3",
"json-stringify-pretty-compact": "2.0.0",
"json5": "2.2.0",
"linkify-markdown": "1.0.0",
"luxon": "1.26.0",
"markdown-it": "12.0.6",
"markdown-table": "2.0.0",
Expand All @@ -174,6 +173,8 @@
"parse-link-header": "1.0.1",
"redis": "3.1.2",
"registry-auth-token": "4.2.1",
"remark": "13.0.0",
"remark-github": "10.1.0",
"semver": "7.3.5",
"semver-stable": "3.0.0",
"semver-utils": "1.1.4",
Expand Down Expand Up @@ -262,7 +263,8 @@
"ts-jest": "26.5.6",
"ts-node": "9.1.1",
"type-fest": "1.1.1",
"typescript": "4.2.4"
"typescript": "4.2.4",
"unified": "9.2.1"
},
"resolutions": {
"underscore": "^1.12.1",
Expand Down
Loading

0 comments on commit d8f4b86

Please sign in to comment.