Skip to content

Commit

Permalink
Add support for absolute "file://" URL reference in MJS file, add tes…
Browse files Browse the repository at this point in the history
…t case for same.
  • Loading branch information
DavidAnson committed Feb 13, 2024
1 parent cf0df1e commit 0301dd9
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 9 deletions.
8 changes: 5 additions & 3 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ const importOrRequireResolve = async (dirOrDirs, id, noRequire) => {
errors.push(error);
}
try {
const fileUrlString =
pathToFileURL(pathDefault.resolve(dirs[0], expandId)).toString();
const isURL = !pathDefault.isAbsolute(expandId) && URL.canParse(expandId);
const urlString = (
isURL ? new URL(expandId) : pathToFileURL(pathDefault.resolve(dirs[0], expandId))
).toString();
// eslint-disable-next-line no-inline-comments
const module = await import(/* webpackIgnore: true */ fileUrlString);
const module = await import(/* webpackIgnore: true */ urlString);
return module.default;
} catch (error) {
errors.push(error);
Expand Down
15 changes: 15 additions & 0 deletions test/customRules/dir4/.markdownlint-cli2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-check

import { sep as sepDefault } from "node:path";
import { sep as sepPosix } from "node:path/posix";

const options = {
"customRules": [
(new URL(
"../node_modules/markdownlint-rule-sample-module/sample-rule.mjs",
import.meta.url
)).toString()
]
};

export default options;
3 changes: 3 additions & 0 deletions test/customRules/dir4/hr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hr

---
5 changes: 3 additions & 2 deletions test/snapshots/markdownlint-cli2-test-exec.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,7 @@ Generated by [AVA](https://avajs.dev).
dir3/hr.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
viewme.md:1 first-line Rule that reports an error for the first line␊
viewme.md:3 any-blockquote Rule that reports an error for any blockquote [Blockquote spans 1 line(s).] [Context: "> Tagli"]␊
viewme.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
Expand All @@ -2849,8 +2850,8 @@ Generated by [AVA](https://avajs.dev).
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 8 file(s)␊
Summary: 46 error(s)␊
Linting: 9 file(s)␊
Summary: 47 error(s)␊
`,
}

Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-exec.js.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion test/snapshots/markdownlint-cli2-test-fs.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2245,7 +2245,7 @@ Generated by [AVA](https://avajs.dev).
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 8 file(s)␊
Linting: 9 file(s)␊
Summary: 23 error(s)␊
`,
}
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-fs.js.snap
Binary file not shown.
7 changes: 4 additions & 3 deletions test/snapshots/markdownlint-cli2-test-main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,7 @@ Generated by [AVA](https://avajs.dev).
dir3/hr.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
dir3/hr.md:3 sample-rule-commonjs Sample rule (commonjs) [Sample error for hr]␊
dir3/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
dir4/hr.md:3 sample-rule-module Sample rule (module) [Sample error for hr]␊
viewme.md:1 first-line Rule that reports an error for the first line␊
viewme.md:3 any-blockquote Rule that reports an error for any blockquote [Blockquote spans 1 line(s).] [Context: "> Tagli"]␊
viewme.md:3 every-n-lines Rule that reports an error every N lines [Line number 3]␊
Expand All @@ -2849,8 +2850,8 @@ Generated by [AVA](https://avajs.dev).
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 8 file(s)␊
Summary: 46 error(s)␊
Linting: 9 file(s)␊
Summary: 47 error(s)␊
`,
}

Expand Down Expand Up @@ -5224,7 +5225,7 @@ Generated by [AVA](https://avajs.dev).
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: **/*.md␊
Linting: 8 file(s)␊
Linting: 9 file(s)␊
Summary: 23 error(s)␊
`,
}
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-main.js.snap
Binary file not shown.

0 comments on commit 0301dd9

Please sign in to comment.