Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toc_obj): Support unnumbered headings #269

Merged
merged 5 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ wordWrap('Once upon a time', {width: 1})

### tocObj(str, [options])

Generate a table of contents in JSON format based on the given html string.
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.

Option | Description | Default
--- | --- | ---
Expand Down Expand Up @@ -601,6 +601,13 @@ tocObj(html, { max_depth: 2 });
{ text: 'Title 2.1', id: 'title_2_1', level: 2 },
]
*/

tocObj('<h1 id="reference" data-toc-unnumbered="true">Reference</h1>')
/*
[
{ text: 'Reference', id: 'reference', level: 1, unnumbered: true }
]
*/
```

### truncate(str, [options])
Expand Down
12 changes: 11 additions & 1 deletion lib/toc_obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const getId = ({ attribs = {}, parent }) => {
return attribs.id || (!parent ? '' : getId(parent));
};

/**
* Identify a heading that to be unnumbered or not.
*/
const isUnnumbered = ({ attribs = {} }) => {
return ("true" === attribs['data-toc-unnumbered']);
};

function tocObj(str, options = {}) {
const { min_depth, max_depth } = Object.assign({
min_depth: 1,
Expand All @@ -31,6 +38,7 @@ function tocObj(str, options = {}) {
const el = headings[i];
const level = +el.name[1];
const id = getId(el);
const unnumbered = isUnnumbered(el);
let text = '';
for (const element of el.children) {
const elText = DomUtils.getText(element);
Expand All @@ -43,7 +51,9 @@ function tocObj(str, options = {}) {
}
if (!text) text = escapeHTML(DomUtils.getText(el));

result.push({ text, id, level });
const res = { text, id, level };
if (unnumbered) res.unnumbered = true;
result.push(res);
}

return result;
Expand Down
14 changes: 14 additions & 0 deletions test/toc_obj.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,19 @@ describe('tocObj', () => {

result.forEach(str => str[0].text.should.eql('foobarbaz'));
});

it('unnumbered headings', () => {
const input = [
'<h1 id="title_1" data-toc-unnumbered="true">Title 1</h1>',
'<h2 data-toc-unnumbered="false" id="title_2">Title 2</h2>'
].join('');

const expected = [
{ text: 'Title 1', id: 'title_1', level: 1, unnumbered: true },
{ text: 'Title 2', id: 'title_2', level: 2 }
];

tocObj(input).should.eql(expected);
});
});
});