Actualizar templates/manual.js
This commit is contained in:
+30
-23
@@ -26,11 +26,11 @@
|
|||||||
|
|
||||||
function afterFonts() {
|
function afterFonts() {
|
||||||
try {
|
try {
|
||||||
buildTOC();
|
|
||||||
renderAnnotations();
|
renderAnnotations();
|
||||||
setupChapterString();
|
setupChapterString();
|
||||||
ensureAnchors();
|
ensureAnchors();
|
||||||
ensureImagePaths();
|
ensureImagePaths();
|
||||||
|
buildTOC();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[manual.js] Error preparando el manual:', err);
|
console.error('[manual.js] Error preparando el manual:', err);
|
||||||
}
|
}
|
||||||
@@ -39,6 +39,14 @@
|
|||||||
/* ---------------------------------------------------------
|
/* ---------------------------------------------------------
|
||||||
1. Índice automático
|
1. Índice automático
|
||||||
--------------------------------------------------------- */
|
--------------------------------------------------------- */
|
||||||
|
function slugify(text) {
|
||||||
|
return (text || '')
|
||||||
|
.toLowerCase()
|
||||||
|
.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
|
||||||
|
.replace(/[^a-z0-9]+/g, '-')
|
||||||
|
.replace(/(^-|-$)/g, '');
|
||||||
|
}
|
||||||
|
|
||||||
function buildTOC() {
|
function buildTOC() {
|
||||||
const out = document.querySelector('[data-toc-output]');
|
const out = document.querySelector('[data-toc-output]');
|
||||||
if (!out) return;
|
if (!out) return;
|
||||||
@@ -59,14 +67,14 @@
|
|||||||
|
|
||||||
const li = document.createElement('li');
|
const li = document.createElement('li');
|
||||||
li.className = 'toc-h1';
|
li.className = 'toc-h1';
|
||||||
li.appendChild(tocRow(romanize(chapterIndex) + '. ' + title.textContent.trim()));
|
li.appendChild(tocRow(romanize(chapterIndex) + '. ' + title.textContent.trim(), title.id));
|
||||||
frag.appendChild(li);
|
frag.appendChild(li);
|
||||||
|
|
||||||
const subsections = chapter.querySelectorAll('h2');
|
const subsections = chapter.querySelectorAll('h2');
|
||||||
subsections.forEach((sub) => {
|
subsections.forEach((sub) => {
|
||||||
const row = document.createElement('li');
|
const row = document.createElement('li');
|
||||||
row.className = 'toc-h2';
|
row.className = 'toc-h2';
|
||||||
row.appendChild(tocRow(' ' + (sub.dataset.tocLabel || sub.textContent.trim())));
|
row.appendChild(tocRow((sub.dataset.tocLabel || sub.textContent.trim()), sub.id));
|
||||||
frag.appendChild(row);
|
frag.appendChild(row);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -74,7 +82,7 @@
|
|||||||
subsub.forEach((sub) => {
|
subsub.forEach((sub) => {
|
||||||
const row = document.createElement('li');
|
const row = document.createElement('li');
|
||||||
row.className = 'toc-h3';
|
row.className = 'toc-h3';
|
||||||
row.appendChild(tocRow(' ' + (sub.dataset.tocLabel || sub.textContent.trim())));
|
row.appendChild(tocRow((sub.dataset.tocLabel || sub.textContent.trim()), sub.id));
|
||||||
frag.appendChild(row);
|
frag.appendChild(row);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -83,21 +91,25 @@
|
|||||||
out.appendChild(frag);
|
out.appendChild(frag);
|
||||||
}
|
}
|
||||||
|
|
||||||
function tocRow(label) {
|
function tocRow(label, anchorId) {
|
||||||
const span = document.createElement('span');
|
const row = document.createElement('span');
|
||||||
span.className = 'toc-row';
|
row.className = 'toc-row';
|
||||||
const text = document.createElement('span');
|
|
||||||
text.className = 'toc-text';
|
const link = document.createElement('a');
|
||||||
text.textContent = label;
|
link.className = 'toc-text';
|
||||||
|
link.textContent = label;
|
||||||
|
if (anchorId) {
|
||||||
|
link.href = '#' + anchorId;
|
||||||
|
link.setAttribute('aria-label', 'Ir a ' + label.trim());
|
||||||
|
}
|
||||||
|
row.appendChild(link);
|
||||||
|
|
||||||
const dots = document.createElement('span');
|
const dots = document.createElement('span');
|
||||||
dots.className = 'toc-dots';
|
dots.className = 'toc-dots';
|
||||||
const num = document.createElement('span');
|
dots.setAttribute('aria-hidden', 'true');
|
||||||
num.className = 'toc-page-num';
|
row.appendChild(dots);
|
||||||
num.textContent = '—';
|
|
||||||
span.appendChild(text);
|
return row;
|
||||||
span.appendChild(dots);
|
|
||||||
span.appendChild(num);
|
|
||||||
return span;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function romanize(num) {
|
function romanize(num) {
|
||||||
@@ -170,12 +182,7 @@
|
|||||||
const headings = document.querySelectorAll('[data-chapter] h1, [data-chapter] h2, [data-chapter] h3, [data-chapter] h4');
|
const headings = document.querySelectorAll('[data-chapter] h1, [data-chapter] h2, [data-chapter] h3, [data-chapter] h4');
|
||||||
headings.forEach((h) => {
|
headings.forEach((h) => {
|
||||||
if (!h.id) {
|
if (!h.id) {
|
||||||
const slug = (h.textContent || '')
|
h.id = slugify(h.textContent) || 'h-' + Math.random().toString(36).slice(2, 8);
|
||||||
.toLowerCase()
|
|
||||||
.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
|
|
||||||
.replace(/[^a-z0-9]+/g, '-')
|
|
||||||
.replace(/(^-|-$)/g, '');
|
|
||||||
h.id = slug || 'h-' + Math.random().toString(36).slice(2, 8);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user