From 7fb693c8f63609a22029f56e5c08e05bdcfeabdc Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Mon, 8 Nov 2021 20:44:46 +0200 Subject: [PATCH] FIX: Generate valid heading ids (#14840) --- .../discourse/tests/unit/lib/pretty-text-test.js | 10 ++++++++++ .../pretty-text/engines/discourse-markdown/anchor.js | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) 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 2a64ccc6159..0f2a4877a84 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

); }); + test("Heading anchors are valid", function (assert) { + assert.cooked( + "# One\n\n# 1\n\n# $$", + '

One

\n' + + '

1

\n' + + '

$$

', + "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 4ac19201e62..78027d6f2f1 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");