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

🐛✅ support headings in list items #363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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