Skip to content

Commit 6c4992e

Browse files
Cerallinnevilm-lt
authored andcommitted
feat(toc_obj): Support unnumbered headings (hexojs#269)
* feat(toc_obj): Support unnumbered headings * Update readme * Rollback TOC * Fix typo * Fix lint
1 parent ed99cf2 commit 6c4992e

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ wordWrap('Once upon a time', {width: 1})
544544

545545
### tocObj(str, [options])
546546

547-
Generate a table of contents in JSON format based on the given html string.
547+
Generate a table of contents in JSON format based on the given html string. Headings with attribute `data-toc-unnumbered="true"` will be marked as unnumbered.
548548

549549
Option | Description | Default
550550
--- | --- | ---
@@ -601,6 +601,13 @@ tocObj(html, { max_depth: 2 });
601601
{ text: 'Title 2.1', id: 'title_2_1', level: 2 },
602602
]
603603
*/
604+
605+
tocObj('<h1 id="reference" data-toc-unnumbered="true">Reference</h1>')
606+
/*
607+
[
608+
{ text: 'Reference', id: 'reference', level: 1, unnumbered: true }
609+
]
610+
*/
604611
```
605612

606613
### truncate(str, [options])

lib/toc_obj.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const getId = ({ attribs = {}, parent }) => {
1313
return attribs.id || (!parent ? '' : getId(parent));
1414
};
1515

16+
/**
17+
* Identify a heading that to be unnumbered or not.
18+
*/
19+
const isUnnumbered = ({ attribs = {} }) => {
20+
return attribs['data-toc-unnumbered'] === 'true';
21+
};
22+
1623
function tocObj(str, options = {}) {
1724
const { min_depth, max_depth } = Object.assign({
1825
min_depth: 1,
@@ -31,6 +38,7 @@ function tocObj(str, options = {}) {
3138
const el = headings[i];
3239
const level = +el.name[1];
3340
const id = getId(el);
41+
const unnumbered = isUnnumbered(el);
3442
let text = '';
3543
for (const element of el.children) {
3644
const elText = DomUtils.getText(element);
@@ -43,7 +51,9 @@ function tocObj(str, options = {}) {
4351
}
4452
if (!text) text = escapeHTML(DomUtils.getText(el));
4553

46-
result.push({ text, id, level });
54+
const res = { text, id, level };
55+
if (unnumbered) res.unnumbered = true;
56+
result.push(res);
4757
}
4858

4959
return result;

test/toc_obj.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,19 @@ describe('tocObj', () => {
165165

166166
result.forEach(str => str[0].text.should.eql('foobarbaz'));
167167
});
168+
169+
it('unnumbered headings', () => {
170+
const input = [
171+
'<h1 id="title_1" data-toc-unnumbered="true">Title 1</h1>',
172+
'<h2 data-toc-unnumbered="false" id="title_2">Title 2</h2>'
173+
].join('');
174+
175+
const expected = [
176+
{ text: 'Title 1', id: 'title_1', level: 1, unnumbered: true },
177+
{ text: 'Title 2', id: 'title_2', level: 2 }
178+
];
179+
180+
tocObj(input).should.eql(expected);
181+
});
168182
});
169183
});

0 commit comments

Comments
 (0)