Skip to content

Commit

Permalink
fix(tocobj): parse permalink if no text
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Feb 23, 2020
1 parent 6f796aa commit 38a0e5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/toc_obj.js
Expand Up @@ -34,13 +34,14 @@ function tocObj(str, options = {}) {
let text = '';
for (const element of el.children) {
const elText = DomUtils.getText(element);
// Skip permalink symbol
// Skip permalink symbol wrapped in <a>
// permalink is a single non-word character, word = [a-Z0-9]
// permalink may be wrapped in whitespace(s)
if (element.name !== 'a' || !nonWord.test(elText)) {
text += escapeHTML(elText);
}
}
if (!text) text = escapeHTML(DomUtils.getText(el));

result.push({ text, id, level });
}
Expand Down
14 changes: 14 additions & 0 deletions test/toc_obj.spec.js
Expand Up @@ -113,6 +113,20 @@ describe('tocObj', () => {
result[0].text.should.eql('foo');
});

it('<a> element - single permalink', () => {
const input = '<h1><a>#</a></h1>';
const result = tocObj(input);

result[0].text.should.eql('#');
});

it('<a> element - non-permalink', () => {
const input = '<h1><a>a</a> one</h1>';
const result = tocObj(input);

result[0].text.should.eql('a one');
});

it('non-permalink <a> element + text', () => {
const input = [
'<h1><a>foo</a>bar</h1>',
Expand Down

0 comments on commit 38a0e5f

Please sign in to comment.