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

fix: allow double hashtags, close #9 #10

Merged
merged 1 commit into from
Feb 10, 2023
Merged
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
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import lintRule from "./index.js";
export default lintRule;
38 changes: 30 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
const normalizeUrl = require('normalize-url');
const rule = require('unified-lint-rule');
var visit = require('unist-util-visit')
/**
* @typedef {import('unist').Node} Node
* @typedef {import('vfile').VFile} VFile
*/
import normalizeUrl from 'normalize-url';
import { lintRule } from 'unified-lint-rule';
import { visit } from 'unist-util-visit';

module.exports = rule(
export default lintRule(
'remark-lint:double-link',
processor,
);

/**
* Check for repeated links.
*
* @param {Node} tree
* @param {VFile} file
* @returns {void}
*/
function processor(tree, file) {
const links = new Map();

visit(tree, 'link', node => {
/**
* @param {any} node
*/
function visitor(node) {
const hashCount = node.url.split("#").length - 1
if (hashCount > 1) {
return
}

const url = node.url.startsWith('#') ? node.url : normalizeUrl(node.url, {
removeDirectoryIndex: true,
removeDirectoryIndex: [/^index\.[a-z]+$/],
stripHash: true,
stripProtocol: true,
// removeQueryParameters: [/\.*/i]
Expand All @@ -23,11 +42,14 @@ function processor(tree, file) {
} else {
links.set(url, [node])
}
})
}

visit(tree, 'link', visitor)

links.forEach(nodes => {
if (nodes.length > 1) {
nodes.forEach(node => {
// @ts-ignore
nodes.forEach((node) => {
file.message(node.url, node)
})
}
Expand Down
Loading