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
def header(text, header_level)
friendly_text = text.parameterize
friendly_text = text.gsub(/<[^>]*>/,"").parameterize
@@headers_history[header_level] = text.parameterize
if header_level > 1

View file

@ -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: []
})
@ -27,4 +28,4 @@ def toc_data(page_content)
end
end
headers
end
end

View file

@ -7,7 +7,7 @@ class UniqueHeadCounter < Middleman::Renderers::MiddlemanRedcarpetHTML
@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

View file

@ -3,7 +3,7 @@
;(function () {
'use strict';
var htmlPattern = /<[^>]*>/g;
var htmlPattern = /<[^>]*>/g;
var loaded = false;
var debounce = function(func, waitTime) {
@ -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
@ -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;
}
}
};

View file

@ -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>