Skip to content

Commit

Permalink
Add support for '~' paths to new modulePaths configuration file option.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Oct 9, 2023
1 parent 0359c5a commit a657164
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ of the rules within.
- When glob expressions match files *not* under the current directory,
configuration for the current directory is applied to the closest common
parent directory.
- Paths beginning with `~` are resolved relative to the user's home directory
(typically `$HOME` on UNIX and `%USERPROFILE%` on Windows)
- There are two kinds of configuration file (both detailed below):
- Configuration files like `.markdownlint-cli2.*` allow complete control of
`markdownlint-cli2` behavior and are also used by `vscode-markdownlint`.
Expand Down
10 changes: 7 additions & 3 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ const negateGlob = (glob) => `!${glob}`;
// Return a posix path (even on Windows)
const posixPath = (p) => p.split(pathDefault.sep).join(pathPosix.sep);

// Expands a path with a tilde to an absolute path
const expandTildePath = (id) => (
markdownlintRuleHelpers.expandTildePath(id, require("node:os"))
);

// Resolves module paths relative to the specified directory
const resolveModulePaths = (dir, modulePaths) => (
modulePaths.map((path) => pathDefault.resolve(dir, path))
modulePaths.map((path) => pathDefault.resolve(dir, expandTildePath(path)))
);

// Read a JSON(C) or YAML file and return the object
Expand All @@ -79,8 +84,7 @@ const importOrRequireResolve = async (dirs, id, noRequire) => {
if (noRequire) {
return null;
}
const expandId =
markdownlintRuleHelpers.expandTildePath(id, require("node:os"));
const expandId = expandTildePath(id);
const errors = [];
try {
return resolveAndRequire(dynamicRequire, expandId, dirs);
Expand Down
28 changes: 22 additions & 6 deletions test/snapshots/markdownlint-cli2-test-exec.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -6955,17 +6955,25 @@ Generated by [AVA](https://avajs.dev).
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `viewme.md 3 any-blockquote␊
stderr: `hr.md 3 sample-rule-commonjs␊
viewme.md 3 any-blockquote␊
viewme.md 3 MD009/no-trailing-spaces␊
viewme.md 5 MD012/no-multiple-blanks␊
viewme.md 6 MD025/single-title/single-h1␊
viewme.md 12 MD019/no-multiple-space-atx␊
viewme.md 14 MD047/single-trailing-newline␊
cjs: hr.md 3 sample-rule-commonjs␊
cjs: viewme.md 3 any-blockquote␊
cjs: viewme.md 3 MD009/no-trailing-spaces␊
cjs: viewme.md 5 MD012/no-multiple-blanks␊
cjs: viewme.md 6 MD025/single-title/single-h1␊
cjs: viewme.md 12 MD019/no-multiple-space-atx␊
cjs: viewme.md 14 MD047/single-trailing-newline␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: *.md␊
Linting: 2 file(s)␊
Summary: 6 error(s)␊
Linting: 3 file(s)␊
Summary: 7 error(s)␊
`,
}

Expand All @@ -6979,17 +6987,25 @@ Generated by [AVA](https://avajs.dev).
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `viewme.md 3 any-blockquote␊
stderr: `hr.md 3 sample-rule-commonjs␊
viewme.md 3 any-blockquote␊
viewme.md 3 MD009/no-trailing-spaces␊
viewme.md 5 MD012/no-multiple-blanks␊
viewme.md 6 MD025/single-title/single-h1␊
viewme.md 12 MD019/no-multiple-space-atx␊
viewme.md 14 MD047/single-trailing-newline␊
cjs: hr.md 3 sample-rule-commonjs␊
cjs: viewme.md 3 any-blockquote␊
cjs: viewme.md 3 MD009/no-trailing-spaces␊
cjs: viewme.md 5 MD012/no-multiple-blanks␊
cjs: viewme.md 6 MD025/single-title/single-h1␊
cjs: viewme.md 12 MD019/no-multiple-space-atx␊
cjs: viewme.md 14 MD047/single-trailing-newline␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: *.md␊
Linting: 2 file(s)␊
Summary: 6 error(s)␊
Linting: 3 file(s)␊
Summary: 7 error(s)␊
`,
}

Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-exec.js.snap
Binary file not shown.
28 changes: 22 additions & 6 deletions test/snapshots/markdownlint-cli2-test-main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -5030,17 +5030,25 @@ Generated by [AVA](https://avajs.dev).
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `viewme.md 3 any-blockquote␊
stderr: `hr.md 3 sample-rule-commonjs␊
viewme.md 3 any-blockquote␊
viewme.md 3 MD009/no-trailing-spaces␊
viewme.md 5 MD012/no-multiple-blanks␊
viewme.md 6 MD025/single-title/single-h1␊
viewme.md 12 MD019/no-multiple-space-atx␊
viewme.md 14 MD047/single-trailing-newline␊
cjs: hr.md 3 sample-rule-commonjs␊
cjs: viewme.md 3 any-blockquote␊
cjs: viewme.md 3 MD009/no-trailing-spaces␊
cjs: viewme.md 5 MD012/no-multiple-blanks␊
cjs: viewme.md 6 MD025/single-title/single-h1␊
cjs: viewme.md 12 MD019/no-multiple-space-atx␊
cjs: viewme.md 14 MD047/single-trailing-newline␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: *.md␊
Linting: 2 file(s)␊
Summary: 6 error(s)␊
Linting: 3 file(s)␊
Summary: 7 error(s)␊
`,
}

Expand All @@ -5054,17 +5062,25 @@ Generated by [AVA](https://avajs.dev).
formatterJson: '',
formatterJunit: '',
formatterSarif: '',
stderr: `viewme.md 3 any-blockquote␊
stderr: `hr.md 3 sample-rule-commonjs␊
viewme.md 3 any-blockquote␊
viewme.md 3 MD009/no-trailing-spaces␊
viewme.md 5 MD012/no-multiple-blanks␊
viewme.md 6 MD025/single-title/single-h1␊
viewme.md 12 MD019/no-multiple-space-atx␊
viewme.md 14 MD047/single-trailing-newline␊
cjs: hr.md 3 sample-rule-commonjs␊
cjs: viewme.md 3 any-blockquote␊
cjs: viewme.md 3 MD009/no-trailing-spaces␊
cjs: viewme.md 5 MD012/no-multiple-blanks␊
cjs: viewme.md 6 MD025/single-title/single-h1␊
cjs: viewme.md 12 MD019/no-multiple-space-atx␊
cjs: viewme.md 14 MD047/single-trailing-newline␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
Finding: *.md␊
Linting: 2 file(s)␊
Summary: 6 error(s)␊
Linting: 3 file(s)␊
Summary: 7 error(s)␊
`,
}

Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-main.js.snap
Binary file not shown.
16 changes: 12 additions & 4 deletions test/tilde-paths-commonjs/.markdownlint-cli2.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ const { homedir } = require("os");
const { relative, resolve } = require("path");

const makeTildePath = (script) => {
const dir = resolve(__dirname, "scripts", script);
const dir = resolve(__dirname, script);
return `~/${relative(homedir(), dir)}`;
};

module.exports = {
"customRules": [
makeTildePath("any-blockquote.cjs")
makeTildePath("scripts/any-blockquote.cjs"),
"markdownlint-rule-sample-commonjs"
],
"markdownItPlugins": [
[ makeTildePath("custom-markdown-it-plugin.cjs") ]
[ makeTildePath("scripts/custom-markdown-it-plugin.cjs") ],
[ "custom-markdown-it-plugin" ]
],
"outputFormatters": [
[ makeTildePath("custom-output-formatter.cjs") ]
[ makeTildePath("scripts/custom-output-formatter.cjs") ],
[ "output-formatter-sample-commonjs" ]
],
"modulePaths": [
makeTildePath("../customRules"),
makeTildePath("../markdownItPlugins/module"),
makeTildePath("../outputFormatters-module")
]
};
3 changes: 3 additions & 0 deletions test/tilde-paths-commonjs/hr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hr

---
15 changes: 11 additions & 4 deletions test/tilde-paths-module/.markdownlint-cli2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ import { fileURLToPath } from "url";
const makeTildePath = (script) => {
const dir = resolve(
dirname(fileURLToPath(import.meta.url)),
"scripts",
script
);
return `~/${relative(homedir(), dir)}`;
};

const options = {
"customRules": [
makeTildePath("any-blockquote.mjs")
makeTildePath("scripts/any-blockquote.mjs"),
"markdownlint-rule-sample-commonjs"
],
"markdownItPlugins": [
[ makeTildePath("custom-markdown-it-plugin.mjs") ]
[ makeTildePath("scripts/custom-markdown-it-plugin.mjs") ],
[ "custom-markdown-it-plugin" ]
],
"outputFormatters": [
[ makeTildePath("custom-output-formatter.mjs") ]
[ makeTildePath("scripts/custom-output-formatter.mjs") ],
[ "output-formatter-sample-commonjs" ]
],
"modulePaths": [
makeTildePath("../customRules"),
makeTildePath("../markdownItPlugins/module"),
makeTildePath("../outputFormatters-module")
]
};

Expand Down
3 changes: 3 additions & 0 deletions test/tilde-paths-module/hr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# hr

---

0 comments on commit a657164

Please sign in to comment.