Skip to content

Commit

Permalink
Merge pull request #10 from Symbitic/master
Browse files Browse the repository at this point in the history
fix: allow double hashtags, close #9
  • Loading branch information
Scrum authored Feb 10, 2023
2 parents 4159cb5 + 397426f commit 5397ab3
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 5397ab3

Please sign in to comment.