diff --git a/lib/util/__fixtures__/release-notes.txt b/lib/util/__fixtures__/release-notes.txt new file mode 100644 index 0000000000000000000000000000000000000000..a75a96dcc679d176eb010860aad35a2e779fbe7c --- /dev/null +++ b/lib/util/__fixtures__/release-notes.txt @@ -0,0 +1,15 @@ +#### Our Gold Sponsors + +<table> +</table> + +#### Our Silver Sponsors + +<table> +</table> + +#### What's Changed +* pnpm rebuild accepts --store-dir by @​user in https://github.com/foo/foo/pull/1 + +#### New Contributors +* @​user made their first contribution in https://github.com/foo/foo/pull/2 diff --git a/lib/util/markdown.spec.ts b/lib/util/markdown.spec.ts index 0aac8a997bea619db1db3c5e5d549ecc65f11375..f608ddce11438f87e974791eda1120d4bb468a1f 100644 --- a/lib/util/markdown.spec.ts +++ b/lib/util/markdown.spec.ts @@ -1,4 +1,5 @@ -import { linkify } from './markdown'; +import { Fixtures } from '../../test/fixtures'; +import { linkify, sanitizeMarkdown } from './markdown'; describe('util/markdown', () => { describe('.linkify', () => { @@ -29,5 +30,25 @@ describe('util/markdown', () => { it('works', async () => { expect(await linkify(before, { repository: 'some/repo' })).toEqual(after); }); + + it('sanitizeMarkdown check massaged release notes', () => { + const input = + '#### Our Gold Sponsors\n' + + '\n' + + '<table>\n' + + '</table>\n' + + '#### Our Silver Sponsors\n' + + '\n' + + '<table>\n' + + '</table>\n' + + "#### What's Changed\n" + + '* pnpm rebuild accepts --store-dir by @user in https://github.com/foo/foo/pull/1\n' + + '\n' + + '#### New Contributors\n' + + '* @user made their first contribution in https://github.com/foo/foo/pull/2\n'; + + const expected = Fixtures.get('release-notes.txt'); + expect(sanitizeMarkdown(input)).toEqual(expected); + }); }); }); diff --git a/lib/util/markdown.ts b/lib/util/markdown.ts index 8f869c1afe3481cc4d51c08314acc640119bd888..0af35f07f36790dc55930bbad447dc89f67ddfb9 100644 --- a/lib/util/markdown.ts +++ b/lib/util/markdown.ts @@ -18,6 +18,10 @@ export function sanitizeMarkdown(markdown: string): string { const backTickRe = regEx(/`([^/]*?)`/g); res = res.replace(backTickRe, '`$1`'); res = res.replace(regEx(/`#​(\d+)`/g), '`#$1`'); + res = res.replace( + regEx(/(?<before>[^\n]\n)(?<title>#.*)/g), + '$<before>\n$<title>' + ); return res; }