Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function init(el) {

const container = el.dataset.tocContainer ?
findOne(el.dataset.tocContainer) | document.body : document.body;
const selectors = el.dataset.toc.split(',').map(s => s.trim());
// const selectors = el.dataset.toc.split(',').map(s => s.trim());
const selectors = [el.dataset.toc];
const tocItems = [];
const offset = el.dataset.tocOffset ? parseInt(el.dataset.tocOffset, 10) : 1;
let i = 1;
Expand All @@ -29,8 +30,8 @@ function init(el) {
const index = item.id || `toc-${i++}`;
const text = item.dataset.tocTitle ?
item.dataset.tocTitle.trim() : item.textContent.trim();
const sanitizedClassName = selector.replace(/((:+[\w-\d]*)|[^A-z0-9-\s])/g, ' ').replace(/\s{2,}/g, ' ').trim();
const className = `toc-${sanitizedClassName}`;
const name = item.tagName.toLowerCase();
const className = `toc-${name}`;

// Set it if none
if (item.id !== index) {
Expand Down
8 changes: 5 additions & 3 deletions test/toc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ test('links', assert => {
assert.equal(container.querySelectorAll('a').length, 5, 'should have links');
assert.equal(container.querySelectorAll('a')[3].textContent, container.querySelector('h3').dataset.tocTitle, 'should use data-toc-title if available');
assert.equal(container.querySelectorAll('a')[0].textContent, container.querySelector('h1').textContent, 'should use text otherwise');
assert.ok(container.querySelectorAll('li')[0].classList.contains('page-title'), 'should have "page-title" class');
assert.ok(container.querySelectorAll('li')[3].classList.contains('toc-last'), 'should have "toc-last" class');
assert.ok(container.querySelectorAll('li')[4].classList.contains('toc-last-heading'), 'should have "toc-last-heading" class');
assert.ok(container.querySelectorAll('li')[0].classList.contains('toc-h1'), 'should have "toc-h1" class');
assert.ok(container.querySelectorAll('li')[3].classList.contains('toc-h3'), 'should have "toc-h3" class');
// assert.ok(container.querySelectorAll('li')[0].classList.contains('page-title'), 'should have "page-title" class');
// assert.ok(container.querySelectorAll('li')[3].classList.contains('toc-last'), 'should have "toc-last" class');
// assert.ok(container.querySelectorAll('li')[4].classList.contains('toc-last-heading'), 'should have "toc-last-heading" class');
assert.end();
});

Expand Down