diff --git a/lib/redmine/wiki_formatting.rb b/lib/redmine/wiki_formatting.rb
index 11ec6803a..8c18d547f 100644
--- a/lib/redmine/wiki_formatting.rb
+++ b/lib/redmine/wiki_formatting.rb
@@ -65,10 +65,22 @@ module Redmine
# Patch to add 'table of content' support to RedCloth
def textile_p_withtoc(tag, atts, cite, content)
- if tag =~ /^h(\d)$/
- @toc << [$1.to_i, content]
+ # removes wiki links from the item
+ toc_item = content.gsub(/(\[\[|\]\])/, '')
+ # removes styles
+ # eg. %{color:red}Triggers% => Triggers
+ toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
+
+ # replaces non word caracters by dashes
+ anchor = toc_item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
+
+ unless anchor.blank?
+ if tag =~ /^h(\d)$/
+ @toc << [$1.to_i, anchor, toc_item]
+ end
+ atts << " id=\"#{anchor}\""
+ content = content + "¶"
end
- content = "" + content
textile_p(tag, atts, cite, content)
end
@@ -82,13 +94,9 @@ module Redmine
div_class << ' right' if $1 == '>'
div_class << ' left' if $1 == '<'
out = "
"
- @toc.each_with_index do |heading, index|
- # remove wiki links from the item
- toc_item = heading.last.gsub(/(\[\[|\]\])/, '')
- # remove styles
- # eg. %{color:red}Triggers% => Triggers
- toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
- out << "- #{toc_item}
\n"
+ @toc.each do |heading|
+ level, anchor, toc_item = heading
+ out << "- #{toc_item}
\n"
end
out << '
'
out
diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css
index 9ae4d1595..c7e9284d2 100644
--- a/public/stylesheets/application.css
+++ b/public/stylesheets/application.css
@@ -487,6 +487,10 @@ div.wiki ul.toc a {
}
div.wiki ul.toc a:hover { color: #c61a1a; text-decoration: underline;}
+a.wiki-anchor { display: none; margin-left: 6px; text-decoration: none; }
+a.wiki-anchor:hover { color: #aaa !important; text-decoration: none; }
+h1:hover a.wiki-anchor, h2:hover a.wiki-anchor, h3:hover a.wiki-anchor { display: inline; color: #ddd; }
+
/***** My page layout *****/
.block-receiver {
border:1px dashed #c0c0c0;
diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb
index eea4a83f8..71ef572b0 100644
--- a/test/unit/helpers/application_helper_test.rb
+++ b/test/unit/helpers/application_helper_test.rb
@@ -215,10 +215,10 @@ h1. Another title
RAW
expected = ''
assert textilizable(raw).gsub("\n", "").include?(expected)