mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Improve MD034/no-bare-urls range reporting (fixes #181).
This commit is contained in:
parent
dba6d4994b
commit
684416a902
4 changed files with 138 additions and 11 deletions
26
lib/md034.js
26
lib/md034.js
|
|
@ -2,27 +2,31 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorContext, bareUrlRe, filterTokens, rangeFromRegExp } =
|
const { addErrorContext, bareUrlRe, filterTokens } = require("../helpers");
|
||||||
require("../helpers");
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD034", "no-bare-urls" ],
|
"names": [ "MD034", "no-bare-urls" ],
|
||||||
"description": "Bare URL used",
|
"description": "Bare URL used",
|
||||||
"tags": [ "links", "url" ],
|
"tags": [ "links", "url" ],
|
||||||
"function": function MD034(params, onError) {
|
"function": function MD034(params, onError) {
|
||||||
filterTokens(params, "inline", function forToken(token) {
|
filterTokens(params, "inline", (token) => {
|
||||||
let inLink = false;
|
let inLink = false;
|
||||||
token.children.forEach(function forChild(child) {
|
token.children.forEach((child) => {
|
||||||
|
const { content, line, lineNumber, type } = child;
|
||||||
let match = null;
|
let match = null;
|
||||||
if (child.type === "link_open") {
|
if (type === "link_open") {
|
||||||
inLink = true;
|
inLink = true;
|
||||||
} else if (child.type === "link_close") {
|
} else if (type === "link_close") {
|
||||||
inLink = false;
|
inLink = false;
|
||||||
} else if ((child.type === "text") &&
|
} else if ((type === "text") && !inLink &&
|
||||||
!inLink &&
|
(match = bareUrlRe.exec(content))) {
|
||||||
(match = bareUrlRe.exec(child.content))) {
|
const [ bareUrl ] = match;
|
||||||
addErrorContext(onError, child.lineNumber, match[0], null,
|
const index = line.indexOf(content);
|
||||||
null, rangeFromRegExp(child.line, bareUrlRe));
|
const range = (index === -1) ? null : [
|
||||||
|
line.indexOf(content) + match.index + 1,
|
||||||
|
bareUrl.length
|
||||||
|
];
|
||||||
|
addErrorContext(onError, lineNumber, bareUrl, null, null, range);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
5
test/detailed-results-links.json
Normal file
5
test/detailed-results-links.json
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD013": false,
|
||||||
|
"MD046": false
|
||||||
|
}
|
||||||
26
test/detailed-results-links.md
Normal file
26
test/detailed-results-links.md
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
# Detailed Link Results
|
||||||
|
|
||||||
|
Text https://example.com/ text
|
||||||
|
|
||||||
|
Text <https://example.com/brackets> text https://example.com/bare text
|
||||||
|
|
||||||
|
Text https://example.com/bare text <https://example.com/brackets> text
|
||||||
|
|
||||||
|
Text `code https://example.com/code code` text https://example.com/ text
|
||||||
|
|
||||||
|
> Text <https://example.com/brackets> text https://example.com/bare text
|
||||||
|
|
||||||
|
Text https://example.com/dir
|
||||||
|
text https://example.com/file.txt
|
||||||
|
text <https://example.com/dir/dir>
|
||||||
|
text https://example.com/dir/dir/file?query=param
|
||||||
|
|
||||||
|
```text
|
||||||
|
Code https://example.com/code?type=fence code
|
||||||
|
```
|
||||||
|
|
||||||
|
Code https://example.com/code?type=indent code
|
||||||
|
|
||||||
|
Text <https://example.com/same> more text https://example.com/same still more text <https://example.com/same> done
|
||||||
|
|
||||||
|
Text <https://example.com/same> more \* text https://example.com/same more \[ text <https://example.com/same> done
|
||||||
92
test/detailed-results-links.results.json
Normal file
92
test/detailed-results-links.results.json
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"lineNumber": 3,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/",
|
||||||
|
"errorRange": [ 6, 20 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 5,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/bare",
|
||||||
|
"errorRange": [ 42, 24 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 7,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/bare",
|
||||||
|
"errorRange": [ 6, 24 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 9,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/",
|
||||||
|
"errorRange": [ 48, 20 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 11,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/bare",
|
||||||
|
"errorRange": [ 44, 24 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 13,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/dir",
|
||||||
|
"errorRange": [ 6, 23 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 14,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/file.txt",
|
||||||
|
"errorRange": [ 6, 28 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 16,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/dir/dir/fi...",
|
||||||
|
"errorRange": [ 6, 44 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 24,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/same",
|
||||||
|
"errorRange": [ 43, 24 ]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lineNumber": 26,
|
||||||
|
"ruleNames": [ "MD034", "no-bare-urls" ],
|
||||||
|
"ruleDescription": "Bare URL used",
|
||||||
|
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md034",
|
||||||
|
"errorDetail": null,
|
||||||
|
"errorContext": "https://example.com/same",
|
||||||
|
"errorRange": null
|
||||||
|
}
|
||||||
|
]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue