Skip to content

Commit f945da4

Browse files
committed
πŸ›βœ… support headings in list items
CommonMark specifically [allows headings in list items](https://spec.commonmark.org/0.31.2/#example-300).
1 parent 71ccb41 commit f945da4

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

β€Žindex.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ function extractSections(markdown) {
4747
// First remove code blocks.
4848
markdown = markdown.replace(/^```[\S\s]+?^```$/mg, '');
4949

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

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

5658
var uniq = {};

β€Žtest/hash-links.md

+16
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ There is an anchor named [Tomato](#tomato).
2323
## Header with special char ✨
2424

2525
Test [header with image](#header-with-special-char-)
26+
27+
1. ## Header in an ordered list
28+
29+
I'm in [the ordered list](#header-in-an-ordered-list)
30+
31+
1. ### Indented header in an ordered list
32+
33+
I'm in [the indented ordered list](#indented-header-in-an-ordered-list)
34+
35+
- ## Header in an unordered list
36+
37+
I'm in [the unordered list](#header-in-an-unordered-list)
38+
39+
- ### Indented header in an unordered list
40+
41+
I'm in [the indented unordered list](#indented-header-in-an-unordered-list)

β€Žtest/markdown-link-check.test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ describe('markdown-link-check', function () {
370370
done();
371371
});
372372
});
373-
it('check hash links', function (done) {
373+
374+
it('should validate hash links', function (done) {
374375
markdownLinkCheck(fs.readFileSync(path.join(dirname, 'hash-links.md')).toString(), {}, function (err, result) {
375376
expect(err).to.be(null);
376377
expect(result).to.eql([
@@ -379,6 +380,10 @@ describe('markdown-link-check', function () {
379380
{ link: '#potato', statusCode: 404, err: null, status: 'dead' },
380381
{ link: '#tomato', statusCode: 404, err: null, status: 'dead' },
381382
{ link: '#header-with-special-char-', statusCode: 404, err: null, status: 'dead' },
383+
{ link: '#header-in-an-ordered-list', statusCode: 200, err: null, status: 'alive' },
384+
{ link: '#indented-header-in-an-ordered-list', statusCode: 200, err: null, status: 'alive' },
385+
{ link: '#header-in-an-unordered-list', statusCode: 200, err: null, status: 'alive' },
386+
{ link: '#indented-header-in-an-unordered-list', statusCode: 200, err: null, status: 'alive' },
382387
]);
383388
done();
384389
});

0 commit comments

Comments
Β (0)