ecmarkup
Version:
Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.
51 lines (50 loc) • 1.87 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
class Toc {
constructor(spec) {
this.spec = spec;
}
/** @internal */
build(maxDepth = Infinity) {
if (this.spec.subclauses.length === 0) {
return;
}
const html = Toc.build(this.spec, { maxDepth });
const tocContainer = this.spec.doc.createElement('div');
tocContainer.setAttribute('id', 'toc');
tocContainer.innerHTML = '<h2>Contents</h2>' + html;
const intro = this.spec.doc.querySelector('emu-intro, emu-clause, emu-annex');
if (intro && intro.parentNode) {
intro.parentNode.insertBefore(tocContainer, intro);
}
const bodyClass = this.spec.doc.body.getAttribute('class') || '';
this.spec.doc.body.setAttribute('class', bodyClass + ' oldtoc');
}
static build(level, { maxDepth = Infinity, expandy = false } = {}) {
if (maxDepth <= 0) {
return '';
}
let html = '<ol class="toc">';
level.subclauses.forEach(sub => {
html += '<li>';
if (expandy) {
if (sub.subclauses.length > 0) {
html += '<span class="item-toggle">+</span>';
}
else {
html += '<span class="item-toggle-none"></span>';
}
}
html += `<a href="#${sub.id}" title="${sub.title}">${sub.getSecnumHTML()}${shorten(sub.titleHTML)}</a>`;
if (sub.subclauses.length > 0)
html += Toc.build(sub, { maxDepth: maxDepth - 1, expandy });
html += '</li>';
});
html += '</ol>';
return html;
}
}
exports.default = Toc;
function shorten(title) {
return title.replace('Static Semantics:', 'SS:').replace('Runtime Semantics:', 'RS:');
}
;