Skip to content

A platform for community discussion. Free, open, simple.

A11Y: add aria-label to automatically generated heading anchor links in posts (#36232)

Kris27854ac8e7f0
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index c18873d0c1d..ce2fb81ea0f 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4138,6 +4138,7 @@ en:
post:
accessible_heading: "post by %{username} %{date}"
+ heading_anchor: "Heading link"
confirm_delete: "Are you sure you want to delete this post?"
quote_reply: "Quote"
quote_reply_shortcut: "Quote (or press q)"
diff --git a/db/migrate/20251125202538_rebake_posts_with_heading_anchors.rb b/db/migrate/20251125202538_rebake_posts_with_heading_anchors.rb
new file mode 100644
index 00000000000..0c6bf0fc944
--- /dev/null
+++ b/db/migrate/20251125202538_rebake_posts_with_heading_anchors.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+class RebakePostsWithHeadingAnchors < ActiveRecord::Migration[8.0]
+ def up
+ execute <<~SQL
+ UPDATE posts SET baked_version = NULL
+ WHERE cooked LIKE '%class="anchor"%'
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/frontend/discourse-markdown-it/src/features/anchor.js b/frontend/discourse-markdown-it/src/features/anchor.js
index d9333da723f..c0be3bbc1e8 100644
--- a/frontend/discourse-markdown-it/src/features/anchor.js
+++ b/frontend/discourse-markdown-it/src/features/anchor.js
@@ -1,8 +1,12 @@
+import { i18n } from "discourse-i18n";
+
export function setup(helper) {
if (helper.getOptions().previewing) {
return;
}
+ helper.allowList(["a[aria-label]"]);
+
helper.registerPlugin((md) => {
md.core.ruler.push("anchor", (state) => {
const postId = helper.getOptions().postId;
@@ -54,6 +58,7 @@ export function setup(helper) {
linkOpen.attrSet("name", slug);
linkOpen.attrSet("class", "anchor");
linkOpen.attrSet("href", "#" + slug);
+ linkOpen.attrSet("aria-label", i18n("post.heading_anchor"));
state.tokens[idx + 1].children.unshift(linkClose);
state.tokens[idx + 1].children.unshift(linkOpen);
diff --git a/frontend/discourse/tests/unit/lib/pretty-text-test.js b/frontend/discourse/tests/unit/lib/pretty-text-test.js
index 3170c66672a..4a4d19852c8 100644
--- a/frontend/discourse/tests/unit/lib/pretty-text-test.js
+++ b/frontend/discourse/tests/unit/lib/pretty-text-test.js
@@ -663,7 +663,7 @@ eviltrout</p>
test("Heading", function (assert) {
assert.cooked(
"**Bold**\n----------",
- '<h2><a name="bold-1" class="anchor" href="#bold-1"></a><strong>Bold</strong></h2>',
+ '<h2><a name="bold-1" class="anchor" href="#bold-1" aria-label="Heading link"></a><strong>Bold</strong></h2>',
"will bold the heading"
);
});
@@ -671,9 +671,9 @@ 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>',
+ '<h1><a name="one-1" class="anchor" href="#one-1" aria-label="Heading link"></a>One</h1>\n' +
+ '<h1><a name="h-1-2" class="anchor" href="#h-1-2" aria-label="Heading link"></a>1</h1>\n' +
+ '<h1><a name="h-3" class="anchor" href="#h-3" aria-label="Heading link"></a>$$</h1>',
"will bold the heading"
);
});
@@ -682,8 +682,8 @@ eviltrout</p>
assert.cookedOptions(
"# 1\n\n# one",
{ postId: 1234 },
- '<h1><a name="p-1234-h-1-1" class="anchor" href="#p-1234-h-1-1"></a>1</h1>\n' +
- '<h1><a name="p-1234-one-2" class="anchor" href="#p-1234-one-2"></a>one</h1>'
+ '<h1><a name="p-1234-h-1-1" class="anchor" href="#p-1234-h-1-1" aria-label="Heading link"></a>1</h1>\n' +
+ '<h1><a name="p-1234-one-2" class="anchor" href="#p-1234-one-2" aria-label="Heading link"></a>one</h1>'
);
});
@@ -925,7 +925,7 @@ eviltrout</p>
assert.cooked(
"## a\nb\n```\nc\n```",
- '<h2><a name="a-1" class="anchor" href="#a-1"></a>a</h2>\n<p>b</p>\n<pre><code class="lang-auto">c\n</code></pre>',
+ '<h2><a name="a-1" class="anchor" href="#a-1" aria-label="Heading link"></a>a</h2>\n<p>b</p>\n<pre><code class="lang-auto">c\n</code></pre>',
"handles headings with code blocks after them"
);
});
diff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rb
index 08b610bd161..9b6d1298cee 100644
--- a/plugins/chat/spec/models/chat/message_spec.rb
+++ b/plugins/chat/spec/models/chat/message_spec.rb
@@ -264,12 +264,12 @@ describe Chat::Message do
MD
expect(cooked).to match_html <<~HTML
- <h1><a name="h1-1" class="anchor" href="#h1-1"></a>h1</h1>
- <h2><a name="h2-2" class="anchor" href="#h2-2"></a>h2</h2>
- <h3><a name="h3-3" class="anchor" href="#h3-3"></a>h3</h3>
- <h4><a name="h4-4" class="anchor" href="#h4-4"></a>h4</h4>
- <h5><a name="h5-5" class="anchor" href="#h5-5"></a>h5</h5>
- <h6><a name="h6-6" class="anchor" href="#h6-6"></a>h6</h6>
+ <h1><a name="h1-1" class="anchor" href="#h1-1" aria-label="Heading link"></a>h1</h1>
+ <h2><a name="h2-2" class="anchor" href="#h2-2" aria-label="Heading link"></a>h2</h2>
+ <h3><a name="h3-3" class="anchor" href="#h3-3" aria-label="Heading link"></a>h3</h3>
+ <h4><a name="h4-4" class="anchor" href="#h4-4" aria-label="Heading link"></a>h4</h4>
+ <h5><a name="h5-5" class="anchor" href="#h5-5" aria-label="Heading link"></a>h5</h5>
+ <h6><a name="h6-6" class="anchor" href="#h6-6" aria-label="Heading link"></a>h6</h6>
HTML
end
diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rb
index 1661da50742..3019a5b5d82 100644
--- a/plugins/poll/spec/lib/pretty_text_spec.rb
+++ b/plugins/poll/spec/lib/pretty_text_spec.rb
@@ -229,10 +229,10 @@ RSpec.describe PrettyText do
HTML
expect(cooked).to include(
- '<h1><a name="pre-heading-1" class="anchor" href="#pre-heading-1"></a>Pre-heading</h1>',
+ '<h1><a name="pre-heading-1" class="anchor" href="#pre-heading-1" aria-label="Heading link"></a>Pre-heading</h1>',
)
expect(cooked).to include(
- '<h1><a name="post-heading-2" class="anchor" href="#post-heading-2"></a>Post-heading</h1>',
+ '<h1><a name="post-heading-2" class="anchor" href="#post-heading-2" aria-label="Heading link"></a>Post-heading</h1>',
)
end
@@ -254,11 +254,11 @@ RSpec.describe PrettyText do
expect(cooked).to include('<div class="poll" data-poll-name="poll" data-poll-status="open">')
expect(cooked).to include(
- '<h1><a name="pre-heading-1" class="anchor" href="#pre-heading-1"></a>Pre-heading</h1>',
+ '<h1><a name="pre-heading-1" class="anchor" href="#pre-heading-1" aria-label="Heading link"></a>Pre-heading</h1>',
)
expect(cooked).to include(
- '<h1><a name="post-heading-2" class="anchor" href="#post-heading-2"></a>Post-heading</h1>',
+ '<h1><a name="post-heading-2" class="anchor" href="#post-heading-2" aria-label="Heading link"></a>Post-heading</h1>',
)
end
end
diff --git a/spec/integrity/common_mark_spec.rb b/spec/integrity/common_mark_spec.rb
index 3116cf265e2..cb23147358a 100644
--- a/spec/integrity/common_mark_spec.rb
+++ b/spec/integrity/common_mark_spec.rb
@@ -32,7 +32,7 @@ RSpec.describe "CommonMark" do
cooked.strip!
cooked.gsub!(" class=\"lang-auto\"", "")
cooked.gsub!(%r{<span class="hashtag-raw">(.*)</span>}, "\\1")
- cooked.gsub!(%r{<a name="(.*)" class="anchor" href="#\1*"></a>}, "")
+ cooked.gsub!(%r{<a name="(.*)" class="anchor" href="#\1*"( aria-label="[^"]*")?></a>}, "")
# we support data-attributes which is not in the spec
cooked.gsub!(" data-code-startline=\"3\"", "")
cooked.gsub!(%r{ data-code-wrap="[^"]+"}, "")
diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb
index 65dd67b67a9..092a02ee4ba 100644
--- a/spec/lib/pretty_text_spec.rb
+++ b/spec/lib/pretty_text_spec.rb
@@ -2758,7 +2758,7 @@ HTML
html = <<~HTML
<h1>
- <a name="hello-world-1" class="anchor" href="#hello-world-1"></a>
+ <a name="hello-world-1" class="anchor" href="#hello-world-1" aria-label="Heading link"></a>
Hello world
</h1>
HTML
diff --git a/spec/system/topic_page_spec.rb b/spec/system/topic_page_spec.rb
index b61db7619bc..f07e2dcfb39 100644
--- a/spec/system/topic_page_spec.rb
+++ b/spec/system/topic_page_spec.rb
@@ -6,7 +6,7 @@ describe "Topic page", type: :system do
before { Fabricate(:post, topic: topic, cooked: <<~HTML) }
<h2 dir="ltr" id="toc-h2-testing" data-d-toc="toc-h2-testing" class="d-toc-post-heading">
- <a name="toc-h2-testing" class="anchor" href="#toc-h2-testing">x</a>
+ <a name="toc-h2-testing" class="anchor" href="#toc-h2-testing" aria-label="Heading link">x</a>
Testing
</h2>
<p id="test-last-cooked-paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor.</p>