Fix ToC issues, fix #995

This commit is contained in:
Robert Lord 2018-07-05 16:28:05 -07:00
parent e621be095a
commit b2119b0313
5 changed files with 13 additions and 9 deletions

View file

@ -8,7 +8,7 @@ class NestingUniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
end end
def header(text, header_level) def header(text, header_level)
friendly_text = text.parameterize friendly_text = text.gsub(/<[^>]*>/,"").parameterize
@@headers_history[header_level] = text.parameterize @@headers_history[header_level] = text.parameterize
if header_level > 1 if header_level > 1

View file

@ -9,6 +9,7 @@ def toc_data(page_content)
headers.push({ headers.push({
id: header.attribute('id').to_s, id: header.attribute('id').to_s,
content: header.children, content: header.children,
title: header.children.to_s.gsub(/<[^>]*>/, ''),
level: header.name[1].to_i, level: header.name[1].to_i,
children: [] children: []
}) })

View file

@ -7,7 +7,7 @@ class UniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
@head_count = {} @head_count = {}
end end
def header(text, header_level) def header(text, header_level)
friendly_text = text.gsub(/<[^<]+>/,"").parameterize friendly_text = text.gsub(/<[^>]*>/,"").parameterize
if friendly_text.strip.length == 0 if friendly_text.strip.length == 0
# Looks like parameterize removed the whole thing! It removes many unicode # Looks like parameterize removed the whole thing! It removes many unicode
# characters like Chinese and Russian. To get a unique URL, let's just # characters like Chinese and Russian. To get a unique URL, let's just

View file

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

View file

@ -72,12 +72,12 @@ under the License.
<ul id="toc" class="toc-list-h1"> <ul id="toc" class="toc-list-h1">
<% toc_data(page_content).each do |h1| %> <% toc_data(page_content).each do |h1| %>
<li> <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 %> <% if h1[:children].length > 0 %>
<ul class="toc-list-h2"> <ul class="toc-list-h2">
<% h1[:children].each do |h2| %> <% h1[:children].each do |h2| %>
<li> <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> </li>
<% end %> <% end %>
</ul> </ul>