Skip to content

Commit

Permalink
Fix ToC issues, fix #995
Browse files Browse the repository at this point in the history
  • Loading branch information
lord committed Jul 5, 2018
1 parent e621be0 commit b2119b0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/nesting_unique_head.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize
end

def header(text, header_level)
friendly_text = text.parameterize
friendly_text = text.gsub(/<[^>]*>/,"").parameterize
@@headers_history[header_level] = text.parameterize

if header_level > 1
Expand Down
3 changes: 2 additions & 1 deletion lib/toc_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def toc_data(page_content)
headers.push({
id: header.attribute('id').to_s,
content: header.children,
title: header.children.to_s.gsub(/<[^>]*>/, ''),
level: header.name[1].to_i,
children: []
})
Expand All @@ -27,4 +28,4 @@ def toc_data(page_content)
end
end
headers
end
end
2 changes: 1 addition & 1 deletion lib/unique_head.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize
@head_count = {}
end
def header(text, header_level)
friendly_text = text.gsub(/<[^<]+>/,"").parameterize
friendly_text = text.gsub(/<[^>]*>/,"").parameterize
if friendly_text.strip.length == 0
# Looks like parameterize removed the whole thing! It removes many unicode
# characters like Chinese and Russian. To get a unique URL, let's just
Expand Down
11 changes: 7 additions & 4 deletions source/javascripts/app/_toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;(function () {
'use strict';

var htmlPattern = /<[^>]*>/g;
var htmlPattern = /<[^>]*>/g;
var loaded = false;

var debounce = function(func, waitTime) {
Expand Down Expand Up @@ -67,7 +67,6 @@
}

var $best = $toc.find("[href='" + best + "']").first();
var joinedTitle = $best.data("title") + " – " + originalTitle;
if (!$best.hasClass("active")) {
// .active is applied to the ToC link we're currently on, and its parent <ul>s selected by tocListSelector
// .active-expanded is applied to the ToC links that are parents of this one
Expand All @@ -81,8 +80,12 @@
if (window.history.replaceState) {
window.history.replaceState(null, "", best);
}
// TODO remove classnames
document.title = joinedTitle.replace(htmlPattern, '');
var thisTitle = $best.data("title")
if (thisTitle !== undefined && thisTitle.length > 0) {
document.title = thisTitle + " – " + originalTitle;
} else {
document.title = originalTitle;
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions source/layouts/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ under the License.
<ul id="toc" class="toc-list-h1">
<% toc_data(page_content).each do |h1| %>
<li>
<a href="#<%= h1[:id] %>" class="toc-h1 toc-link" data-title="<%= h1[:content].to_s.parameterize %>"><%= h1[:content] %></a>
<a href="#<%= h1[:id] %>" class="toc-h1 toc-link" data-title="<%= h1[:title] %>"><%= h1[:content] %></a>
<% if h1[:children].length > 0 %>
<ul class="toc-list-h2">
<% h1[:children].each do |h2| %>
<li>
<a href="#<%= h2[:id] %>" class="toc-h2 toc-link" data-title="<%= h2[:content].to_s.parameterize %>"><%= h2[:content] %></a>
<a href="#<%= h2[:id] %>" class="toc-h2 toc-link" data-title="<%= h2[:title] %>"><%= h2[:content] %></a>
</li>
<% end %>
</ul>
Expand Down

0 comments on commit b2119b0

Please sign in to comment.