|
| 1 | +/** |
| 2 | + * @fileoverview Rule to prevent bare URLs in Markdown. |
| 3 | + * @author xbinaryx |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Type Definitions |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +/** @typedef {import("mdast").Node} Node */ |
| 11 | +/** @typedef {import("mdast").Paragraph} ParagraphNode */ |
| 12 | +/** @typedef {import("mdast").Heading} HeadingNode */ |
| 13 | +/** @typedef {import("mdast").TableCell} TableCellNode */ |
| 14 | +/** @typedef {import("mdast").Link} LinkNode */ |
| 15 | +/** |
| 16 | + * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} |
| 17 | + * NoBareUrlsRuleDefinition |
| 18 | + */ |
| 19 | + |
| 20 | +//----------------------------------------------------------------------------- |
| 21 | +// Helpers |
| 22 | +//----------------------------------------------------------------------------- |
| 23 | + |
| 24 | +const htmlTagNamePattern = /^<([^!>][^/\s>]*)/u; |
| 25 | + |
| 26 | +/** |
| 27 | + * Parses an HTML tag to extract its name and closing status |
| 28 | + * @param {string} tagText The HTML tag text to parse |
| 29 | + * @returns {{ name: string; isClosing: boolean; } | null} Object containing tag name and closing status, or null if not a valid tag |
| 30 | + */ |
| 31 | +function parseHtmlTag(tagText) { |
| 32 | + const match = tagText.match(htmlTagNamePattern); |
| 33 | + if (match) { |
| 34 | + const tagName = match[1].toLowerCase(); |
| 35 | + const isClosing = tagName.startsWith("/"); |
| 36 | + |
| 37 | + return { |
| 38 | + name: isClosing ? tagName.slice(1) : tagName, |
| 39 | + isClosing, |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + return null; |
| 44 | +} |
| 45 | + |
| 46 | +//----------------------------------------------------------------------------- |
| 47 | +// Rule Definition |
| 48 | +//----------------------------------------------------------------------------- |
| 49 | + |
| 50 | +/** @type {NoBareUrlsRuleDefinition} */ |
| 51 | +export default { |
| 52 | + meta: { |
| 53 | + type: "problem", |
| 54 | + |
| 55 | + docs: { |
| 56 | + description: "Disallow bare URLs", |
| 57 | + url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-bare-urls.md", |
| 58 | + }, |
| 59 | + |
| 60 | + fixable: "code", |
| 61 | + |
| 62 | + messages: { |
| 63 | + bareUrl: |
| 64 | + "Unexpected bare URL. Use autolink (<URL>) or link ([text](URL)) instead.", |
| 65 | + }, |
| 66 | + }, |
| 67 | + |
| 68 | + create(context) { |
| 69 | + const { sourceCode } = context; |
| 70 | + /** @type {Array<LinkNode>} */ |
| 71 | + const bareUrls = []; |
| 72 | + |
| 73 | + /** |
| 74 | + * Finds bare URLs in markdown nodes while handling HTML tags. |
| 75 | + * When an HTML tag is found, it looks for its closing tag and skips all nodes |
| 76 | + * between them to prevent checking for bare URLs inside HTML content. |
| 77 | + * @param {ParagraphNode|HeadingNode|TableCellNode} node The node to process |
| 78 | + * @returns {void} |
| 79 | + */ |
| 80 | + function findBareUrls(node) { |
| 81 | + /** |
| 82 | + * Recursively traverses the AST to find bare URLs, skipping over HTML blocks. |
| 83 | + * @param {Node} currentNode The current AST node being traversed. |
| 84 | + * @returns {void} |
| 85 | + */ |
| 86 | + function traverse(currentNode) { |
| 87 | + if ( |
| 88 | + "children" in currentNode && |
| 89 | + Array.isArray(currentNode.children) |
| 90 | + ) { |
| 91 | + for (let i = 0; i < currentNode.children.length; i++) { |
| 92 | + const child = currentNode.children[i]; |
| 93 | + |
| 94 | + if (child.type === "html") { |
| 95 | + const tagInfo = parseHtmlTag( |
| 96 | + sourceCode.getText(child), |
| 97 | + ); |
| 98 | + |
| 99 | + if (tagInfo && !tagInfo.isClosing) { |
| 100 | + for ( |
| 101 | + let j = i + 1; |
| 102 | + j < currentNode.children.length; |
| 103 | + j++ |
| 104 | + ) { |
| 105 | + const nextChild = currentNode.children[j]; |
| 106 | + if (nextChild.type === "html") { |
| 107 | + const closingTagInfo = parseHtmlTag( |
| 108 | + sourceCode.getText(nextChild), |
| 109 | + ); |
| 110 | + if ( |
| 111 | + closingTagInfo?.name === |
| 112 | + tagInfo.name && |
| 113 | + closingTagInfo?.isClosing |
| 114 | + ) { |
| 115 | + i = j; |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + continue; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (child.type === "link") { |
| 125 | + const text = sourceCode.getText(child); |
| 126 | + const { url } = child; |
| 127 | + |
| 128 | + if ( |
| 129 | + text === url || |
| 130 | + url === `http://${text}` || |
| 131 | + url === `mailto:${text}` |
| 132 | + ) { |
| 133 | + bareUrls.push(child); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + traverse(child); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + traverse(node); |
| 143 | + } |
| 144 | + |
| 145 | + return { |
| 146 | + "root:exit"() { |
| 147 | + for (const bareUrl of bareUrls) { |
| 148 | + context.report({ |
| 149 | + node: bareUrl, |
| 150 | + messageId: "bareUrl", |
| 151 | + fix(fixer) { |
| 152 | + const text = sourceCode.getText(bareUrl); |
| 153 | + return fixer.replaceText(bareUrl, `<${text}>`); |
| 154 | + }, |
| 155 | + }); |
| 156 | + } |
| 157 | + }, |
| 158 | + |
| 159 | + paragraph(node) { |
| 160 | + findBareUrls(node); |
| 161 | + }, |
| 162 | + |
| 163 | + heading(node) { |
| 164 | + findBareUrls(node); |
| 165 | + }, |
| 166 | + |
| 167 | + tableCell(node) { |
| 168 | + findBareUrls(node); |
| 169 | + }, |
| 170 | + }; |
| 171 | + }, |
| 172 | +}; |
0 commit comments