From 4de1df3a6c054c44eddaf38e9bc2e2071b9c4e8f Mon Sep 17 00:00:00 2001
From: Maron <98313426+MaronHatoum@users.noreply.github.com>
Date: Wed, 31 Aug 2022 10:39:35 +0300
Subject: [PATCH] fix: fails to massage pnpm release notes (#17466)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/util/__fixtures__/release-notes.txt | 15 +++++++++++++++
 lib/util/markdown.spec.ts               | 23 ++++++++++++++++++++++-
 lib/util/markdown.ts                    |  4 ++++
 3 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 lib/util/__fixtures__/release-notes.txt

diff --git a/lib/util/__fixtures__/release-notes.txt b/lib/util/__fixtures__/release-notes.txt
new file mode 100644
index 0000000000..a75a96dcc6
--- /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 @&#8203;user in https://github.com/foo/foo/pull/1
+
+#### New Contributors
+* @&#8203;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 0aac8a997b..f608ddce11 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 8f869c1afe..0af35f07f3 100644
--- a/lib/util/markdown.ts
+++ b/lib/util/markdown.ts
@@ -18,6 +18,10 @@ export function sanitizeMarkdown(markdown: string): string {
   const backTickRe = regEx(/&#x60;([^/]*?)&#x60;/g);
   res = res.replace(backTickRe, '`$1`');
   res = res.replace(regEx(/`#&#8203;(\d+)`/g), '`#$1`');
+  res = res.replace(
+    regEx(/(?<before>[^\n]\n)(?<title>#.*)/g),
+    '$<before>\n$<title>'
+  );
   return res;
 }
 
-- 
GitLab