43 lines
2.0 KiB
Plaintext
43 lines
2.0 KiB
Plaintext
<%# ---------------------------------------------------------------
|
|
# docmd : the zero-config documentation engine.
|
|
# @website https://docmd.io
|
|
# [docmd-source] - Please do not remove this header.
|
|
# ---------------------------------------------------------------
|
|
%>
|
|
|
|
<%
|
|
function decodeHtmlEntities(html) {
|
|
return html.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'").replace(/ /g, ' ');
|
|
}
|
|
const shouldShowToc = typeof isActivePage !== 'undefined' ? isActivePage :
|
|
(typeof navigationHtml !== 'undefined' && navigationHtml && navigationHtml.includes('class="active"'));
|
|
|
|
if (shouldShowToc && !frontmatter?.toc || frontmatter?.toc !== 'false') {
|
|
let tocHeadings = [];
|
|
if (headings && headings.length > 0) {
|
|
tocHeadings = headings.filter(h => h.level >= 1 && h.level <= 4);
|
|
} else if (content) {
|
|
const headingRegex = /<h([1-4])[^>]*?(?:id="([^"]*)")?[^>]*?>([\s\S]*?)<\/h\1>/g;
|
|
let match;
|
|
let contentStr = content.toString();
|
|
while ((match = headingRegex.exec(contentStr)) !== null) {
|
|
const level = parseInt(match[1], 10);
|
|
let id = match[2];
|
|
const text = decodeHtmlEntities(match[3].replace(/<\/?\w+[^>]*>/g, '')); // Stripped tags
|
|
if (!id) id = text.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]/g, '').replace(/--+/g, '-').replace(/^-+|-+$/g, '');
|
|
tocHeadings.push({ id, level, text });
|
|
}
|
|
}
|
|
if (tocHeadings.length > 1) {
|
|
%>
|
|
<div class="toc-container">
|
|
<h2 class="toc-title"><%= typeof t === 'function' ? t('onThisPage') : 'On This Page' %><span class="mobile-view toc-menu-button float-right"><%- renderIcon("chevron-down") %></span></h2>
|
|
<ul class="toc-list">
|
|
<% tocHeadings.forEach(heading => { %>
|
|
<li class="toc-item toc-level-<%= heading.level %>">
|
|
<a href="#<%= heading.id %>" class="toc-link"><%= decodeHtmlEntities(heading.text.replace(/<[^>]*>?/gm, '')) %></a>
|
|
</li>
|
|
<% }); %>
|
|
</ul>
|
|
</div>
|
|
<% } } %> |