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: only lint the list items #7

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 23 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ module.exports = rule(
function processor(tree, file) {
const links = new Map();

visit(tree, 'link', node => {
const url = node.url.startsWith('#') ? node.url : normalizeUrl(node.url, {
removeDirectoryIndex: true,
stripHash: true,
stripProtocol: true,
// removeQueryParameters: [/\.*/i]
});
visit(tree, 'list', list => {
for (const listItem of list.children) {
const [paragraph] = listItem.children
if (!paragraph || paragraph.type !== 'paragraph' || paragraph.children.length === 0) {
continue
}

if (links.has(url)) {
links.get(url).push(node)
} else {
links.set(url, [node])
const [node] = paragraph.children
if (node.type === 'text') {
continue
}

const url = node.url.startsWith('#') ? node.url : normalizeUrl(node.url, {
removeDirectoryIndex: true,
stripHash: true,
stripProtocol: true,
// removeQueryParameters: [/\.*/i]
})

if (links.has(url)) {
links.get(url).push(node)
} else {
links.set(url, [node])
}
}
})

Expand Down
15 changes: 12 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const nok = `![img](./logo)
const ok = `- [puper-link-1](http://link-1)
- [puper-link-1](http://link-2)`;

const okInDesc = `- [puper-link-1](http://link-1) - [puper-link-1](http://link-1)
- [puper-link-2](http://link-2) - Forked [puper-link-1](http://link-1)`;

test('remark-lint-double-link valid', t => {
t.deepEqual(
processor.processSync(ok).messages.map(String),
Expand All @@ -24,15 +27,21 @@ test('remark-lint-double-link valid', t => {
);
});

test('remark-lint-double-link valid for descriptions', t => {
t.deepEqual(
processor.processSync(okInDesc).messages.map(String),
[],
'should work on valid fixtures ignoring descriptions'
);
});

test('remark-lint-double-link invalid', t => {
t.deepEqual(
processor.processSync(nok).messages.map(String),
[
'2:1-2:30: http://link-1',
'3:3-3:32: http://link-1',
'6:3-6:44: #appimage-discovery',
'7:3-7:44: #appimage-discovery',
],
'should work on valid fixtures'
'should work on invalid fixtures'
);
});