Files
docmd-io--docmd/packages/ui/templates/toc.ejs
T
wehub-resource-sync 6db8fca185
docmd CI verification / verify (push) Failing after 0s
chore: import upstream snapshot with attribution
2026-07-13 12:31:55 +08:00

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(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&nbsp;/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>
<% } } %>