Skip to content

Commit

Permalink
fix: allow double hashtags, close #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Symbitic committed Feb 7, 2023
1 parent 4159cb5 commit 397426f
Show file tree
Hide file tree
Showing 6 changed files with 15,358 additions and 9,839 deletions.
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

0 comments on commit 397426f

Please sign in to comment.