FIX: Generate valid heading ids (#14840)

FIX: Generate valid heading ids (#14840)

diff --git a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js
index 2a64ccc..0f2a487 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js
@@ -701,6 +701,16 @@ eviltrout</p>
     );
   });
 
+  test("Heading anchors are valid", function (assert) {
+    assert.cooked(
+      "# One\n\n# 1\n\n# $$",
+      '<h1><a name="one-1" class="anchor" href="#one-1"></a>One</h1>\n' +
+        '<h1><a name="h-1-2" class="anchor" href="#h-1-2"></a>1</h1>\n' +
+        '<h1><a name="h-3" class="anchor" href="#h-3"></a>$$</h1>',
+      "It will bold the heading"
+    );
+  });
+
   test("bold and italics", function (assert) {
     assert.cooked(
       'a "**hello**"',
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/anchor.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/anchor.js
index 4ac1920..78027d6 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/anchor.js
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/anchor.js
@@ -39,7 +39,11 @@ export function setup(helper) {
           .replace(/^-+/, "")
           .replace(/-+$/, "");
 
-        slug = `${slug || "heading"}-${++headingId}`;
+        if (slug.match(/^[^a-z]/)) {
+          slug = `h-${slug}`;
+        }
+
+        slug = `${slug || "h"}-${++headingId}`;
 
         linkOpen.attrSet("name", slug);
         linkOpen.attrSet("class", "anchor");

GitHub sha: 7fb693c8f63609a22029f56e5c08e05bdcfeabdc

This commit appears in #14840 which was approved by CvX. It was merged by nbianca.