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 0d42988e1de..3d6d500ec97 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 @@ -670,7 +670,7 @@ eviltrout

assert.cooked( "# #category-hashtag", - '

#category-hashtag

', + '

#category-hashtag

', "it works within ATX-style headers" ); @@ -696,7 +696,7 @@ eviltrout

test("Heading", function (assert) { assert.cooked( "**Bold**\n----------", - '

Bold

', + '

Bold

', "It will bold the heading" ); }); @@ -939,7 +939,7 @@ eviltrout

assert.cooked( "## a\nb\n```\nc\n```", - '

a

\n

b

\n
c\n
', + '

a

\n

b

\n
c\n
', "it handles headings with code blocks after them." ); }); 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 03941e6a0f8..4ac19201e62 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/anchor.js +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/anchor.js @@ -1,5 +1,3 @@ -const SPECIAL_CHARACTERS_REGEX = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~’]/g; - export function setup(helper) { if (helper.getOptions().previewing) { return; @@ -7,7 +5,11 @@ export function setup(helper) { helper.registerPlugin((md) => { md.core.ruler.push("anchor", (state) => { - for (let idx = 0, lvl = 0; idx < state.tokens.length; idx++) { + for ( + let idx = 0, lvl = 0, headingId = 0; + idx < state.tokens.length; + idx++ + ) { if ( state.tokens[idx].type === "blockquote_open" || (state.tokens[idx].type === "bbcode_open" && @@ -37,15 +39,7 @@ export function setup(helper) { .replace(/^-+/, "") .replace(/-+$/, ""); - if (slug.length === 0) { - slug = state.tokens[idx + 1].content - .replace(/\s+/g, "-") - .replace(SPECIAL_CHARACTERS_REGEX, "") - .replace(/\-\-+/g, "-") - .replace(/^-+/, "") - .replace(/-+$/, ""); - slug = encodeURI(slug).replace(/%/g, "").substr(0, 24); - } + slug = `${slug || "heading"}-${++headingId}`; linkOpen.attrSet("name", slug); linkOpen.attrSet("class", "anchor"); diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rb index c5b25e09a93..52fed2284ea 100644 --- a/plugins/poll/spec/lib/pretty_text_spec.rb +++ b/plugins/poll/spec/lib/pretty_text_spec.rb @@ -189,8 +189,8 @@ describe PrettyText do HTML - expect(cooked).to include("

\nPre-heading

") - expect(cooked).to include("

\nPost-heading

") + expect(cooked).to include("

\nPre-heading

") + expect(cooked).to include("

\nPost-heading

") end it "does not break when there are headings before/after a poll without a title" do @@ -211,7 +211,7 @@ describe PrettyText do
HTML - expect(cooked).to include("

\nPre-heading

") - expect(cooked).to include("

\nPost-heading

") + expect(cooked).to include("

\nPre-heading

") + expect(cooked).to include("

\nPost-heading

") end end diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 6cd371a64b9..3baaa24ba73 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -1909,7 +1909,7 @@ HTML html = <<~HTML

- + Hello world

HTML