Skip to content

Commit 4fc3a60

Browse files
committed
Merge pull request #64 from jakedouglas/non_english_anchors
Support non-English characters in anchor names
2 parents 5ad9fd1 + 0c5e1c1 commit 4fc3a60

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/html/pipeline/toc_filter.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ def call
1111
headers = Hash.new(0)
1212
doc.css('h1, h2, h3, h4, h5, h6').each do |node|
1313
name = node.text.downcase
14-
name.gsub!(/[^\w\- ]/, '') # remove punctuation
14+
name.gsub!(/[^\p{Word}\- ]/u, '') # remove punctuation
1515
name.gsub!(' ', '-') # replace spaces with dash
16-
name = EscapeUtils.escape_uri(name) # escape extended UTF-8 chars
1716

1817
uniq = (headers[name] > 0) ? "-#{headers[name]}" : ''
1918
headers[name] += 1
@@ -25,4 +24,4 @@ def call
2524
end
2625
end
2726
end
28-
end
27+
end

test/html/pipeline/toc_filter_test.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# encoding: utf-8
12
require "test_helper"
23

34
class HTML::Pipeline::TableOfContentsFilterTest < Test::Unit::TestCase
@@ -42,6 +43,16 @@ def test_all_header_tags_are_found_when_adding_anchors
4243
doc = TocFilter.call(orig)
4344
assert_equal 6, doc.search('a').size
4445
end
45-
end
4646

47+
def test_anchors_with_utf8_characters
48+
orig = %(<h1>日本語</h1>
49+
<h1>Русский</h1)
50+
51+
rendered_h1s = TocFilter.call(orig).search('h1').map(&:to_s)
4752

53+
assert_equal "<h1>\n<a name=\"%E6%97%A5%E6%9C%AC%E8%AA%9E\" class=\"anchor\" href=\"#%E6%97%A5%E6%9C%AC%E8%AA%9E\"><span class=\"octicon octicon-link\"></span></a>日本語</h1>",
54+
rendered_h1s[0]
55+
assert_equal "<h1>\n<a name=\"%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9\" class=\"anchor\" href=\"#%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9\"><span class=\"octicon octicon-link\"></span></a>Русский</h1>",
56+
rendered_h1s[1]
57+
end
58+
end

0 commit comments

Comments
 (0)