Skip to content

Commit

Permalink
πŸ›βœ… support headings in list items
Browse files Browse the repository at this point in the history
CommonMark specifically [allows headings in list items](https://spec.commonmark.org/0.31.2/#example-300).
  • Loading branch information
dudeofawesome committed Oct 15, 2024
1 parent 71ccb41 commit 10363f5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ function extractSections(markdown) {
// First remove code blocks.
markdown = markdown.replace(/^```[\S\s]+?^```$/mg, '');

const sectionTitles = markdown.match(/^#+ .*$/gm) || [];
const sectionTitles = Array.from(
markdown.matchAll(/^(?:\s*(?:[-*+]|\d+[.)])\s+)?(#+ .*)$/gm) ?? []
);

const sections = sectionTitles.map(section =>
section.replace(/^\W+/, '').replace(/\W+$/, '').replace(/[^\w\s-]+/g, '').replace(/\s+/g, '-').toLowerCase()
section[1].replace(/^\W+/, '').replace(/\W+$/, '').replace(/[^\w\s-]+/g, '').replace(/\s+/g, '-').toLowerCase()
);

var uniq = {};
Expand Down
16 changes: 16 additions & 0 deletions test/hash-links.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ There is an anchor named [Tomato](#tomato).
## Header with special char ✨

Test [header with image](#header-with-special-char-)

1. # Header in an ordered list

I'm in [the ordered list](#header-in-an-ordered-list)

1. ## Indented header in an ordered list

I'm in [the indented ordered list](#indented-header-in-an-ordered-list)

- # Header in an unordered list

I'm in [the unordered list](#header-in-an-unordered-list)

- ## Indented header in an unordered list

I'm in [the indented unordered list](#indented-header-in-an-unordered-list)
8 changes: 7 additions & 1 deletion test/markdown-link-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ describe('markdown-link-check', function () {
done();
});
});
it('check hash links', function (done) {

it('should validate hash links', function (done) {
markdownLinkCheck(fs.readFileSync(path.join(dirname, 'hash-links.md')).toString(), {}, function (err, result) {
expect(err).to.be(null);
expect(result).to.eql([
Expand All @@ -379,7 +380,12 @@ describe('markdown-link-check', function () {
{ link: '#potato', statusCode: 404, err: null, status: 'dead' },
{ link: '#tomato', statusCode: 404, err: null, status: 'dead' },
{ link: '#header-with-special-char-', statusCode: 404, err: null, status: 'dead' },
{ link: '#header-in-an-ordered-list', statusCode: 200, err: null, status: 'alive' },
{ link: '#indented-header-in-an-ordered-list', statusCode: 200, err: null, status: 'alive' },
{ link: '#header-in-an-unordered-list', statusCode: 200, err: null, status: 'alive' },
{ link: '#indented-header-in-an-unordered-list', statusCode: 200, err: null, status: 'alive' },
]);

done();
});
});
Expand Down

0 comments on commit 10363f5

Please sign in to comment.