Commit 98cb3b4a authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '26210-toc-links-v2' into 'master'

Fix issue with non-ASCII wiki TOC links

Closes #26210

See merge request gitlab-org/gitlab!17838
parents 228ab0f3 1e982b47
---
title: Fix issue with wiki TOC links being treated as external links
merge_request:
author: Oren Kanner
type: fixed
...@@ -56,7 +56,8 @@ module Banzai ...@@ -56,7 +56,8 @@ module Banzai
private private
def anchor_tag(href) def anchor_tag(href)
%Q{<a id="user-content-#{href}" class="anchor" href="##{href}" aria-hidden="true"></a>} escaped_href = CGI.escape(href) # account for non-ASCII characters
%Q{<a id="user-content-#{href}" class="anchor" href="##{escaped_href}" aria-hidden="true"></a>}
end end
def push_toc(children, root: false) def push_toc(children, root: false)
...@@ -80,7 +81,7 @@ module Banzai ...@@ -80,7 +81,7 @@ module Banzai
def initialize(node: nil, href: nil, previous_header: nil) def initialize(node: nil, href: nil, previous_header: nil)
@node = node @node = node
@href = href @href = CGI.escape(href) if href
@children = [] @children = []
@parent = find_parent(previous_header) @parent = find_parent(previous_header)
......
...@@ -82,7 +82,9 @@ describe Banzai::Filter::TableOfContentsFilter do ...@@ -82,7 +82,9 @@ describe Banzai::Filter::TableOfContentsFilter do
it 'supports Unicode' do it 'supports Unicode' do
doc = filter(header(1, '한글')) doc = filter(header(1, '한글'))
expect(doc.css('h1 a').first.attr('id')).to eq 'user-content-한글' expect(doc.css('h1 a').first.attr('id')).to eq 'user-content-한글'
expect(doc.css('h1 a').first.attr('href')).to eq '#한글' # check that we encode the href to avoid issues with the
# ExternalLinkFilter (see https://gitlab.com/gitlab-org/gitlab/issues/26210)
expect(doc.css('h1 a').first.attr('href')).to eq "##{CGI.escape('한글')}"
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment