2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/spec/integrity/common_mark_spec.rb
Kris 27854ac8e7
A11Y: add aria-label to automatically generated heading anchor links in posts (#36232)
Currently our automatically generated anchor links for headings in posts
are pretty awful to listen to in screenreaders, because screenreaders
are reading the `name` attribute.

Here's an example from a [Meta
post](https://meta.discourse.org/t/a-new-review-queue-layout-with-all-new-features/388194)...
this "A refreshed layout" heading reads like:

 "p-1888119-a-refreshed-layout-1" 

This PR adds `aria-label="heading link"` so now this would read like:

"Heading link, same page link, A Refreshed Layout, heading level 1"

I've added a migration so posts with headings will rebake with the
improved markup.

---------

Co-authored-by: Yuriy Kurant <yuriy@discourse.org>
2025-11-25 18:29:20 -05:00

74 lines
2.3 KiB
Ruby

# frozen_string_literal: true
RSpec.describe "CommonMark" do
it "passes spec" do
SiteSetting.traditional_markdown_linebreaks = true
SiteSetting.enable_markdown_typographer = false
SiteSetting.highlighted_languages = "ruby|aa"
html, state, md = nil
failed = 0
File
.readlines(Rails.root + "spec/fixtures/md/spec.txt")
.each do |line|
if line == "```````````````````````````````` example\n"
state = :example
next
end
if line == "````````````````````````````````\n"
md.gsub!("", "\t")
html ||= String.new
html.gsub!("", "\t")
html.strip!
# normalize brs
html.gsub!("<br />", "<br>")
html.gsub!("<hr />", "<hr>")
html.gsub!(%r{<img([^>]+) />}, "<img\\1>")
SiteSetting.enable_markdown_linkify = false
cooked = PrettyText.markdown(md, sanitize: false)
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*"( 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="[^"]+"}, "")
# we don't care about this
cooked.gsub!("<blockquote>\n</blockquote>", "<blockquote></blockquote>")
html.gsub!("<blockquote>\n</blockquote>", "<blockquote></blockquote>")
html.gsub!("language-ruby", "lang-ruby")
html.gsub!("language-aa", "lang-aa")
# strip out unsupported languages
html.gsub!(%r{ class="language-[;f].*"}, "")
unless cooked == html
failed += 1
puts "FAILED SPEC"
puts "Expected: "
puts html
puts "Got: "
puts cooked
puts "Markdown: "
puts md
puts
end
html, state, md = nil
next
end
if state == :example && line == ".\n"
state = :html
next
end
md = (md || String.new) << line if state == :example
html = (html || String.new) << line if state == :html
end
expect(failed).to eq(0)
end
end