Skip to content

Commit

Permalink
Add "noBanner" configuration option to suppress the display of name/v…
Browse files Browse the repository at this point in the history
…ersion information at the top of stdout (fixes #291, closes #291).
  • Loading branch information
DavidAnson committed Mar 31, 2024
1 parent ec27669 commit 5fcfe57
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 29 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ of the rules within.
- Search [`markdown-it-plugins` on npm][markdown-it-plugins]
- `modulePaths`: `Array` of `String`s providing additional paths to use when
resolving module references (e.g., alternate locations for `node_modules`)
- `noBanner`: `Boolean` value to disable the display of the banner message and
version numbers on `stdout`
- This top-level setting is valid **only** in the directory from which
`markdownlint-cli2` is run
- `noInlineConfig`: `Boolean` value to disable the support of
[HTML comments][html-comment] within Markdown content
- For example: `<!-- markdownlint-disable some-rule -->`
Expand Down
44 changes: 26 additions & 18 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const packageName = "markdownlint-cli2";
const packageVersion = "0.12.1";
const libraryName = "markdownlint";
const libraryVersion = markdownlintLibrary.getVersion();
const bannerMessage = `${packageName} v${packageVersion} (${libraryName} v${libraryVersion})`;
const dotOnlySubstitute = "*.{md,markdown}";
const utf8 = "utf8";

Expand Down Expand Up @@ -241,7 +242,10 @@ const processArgv = (argv) => {
};

// Show help if missing arguments
const showHelp = (logMessage) => {
const showHelp = (logMessage, showBanner) => {
if (showBanner) {
logMessage(bannerMessage);
}
logMessage(`https://github.com/DavidAnson/markdownlint-cli2
Syntax: markdownlint-cli2 glob0 [glob1] [...] [globN] [--config file] [--fix] [--help]
Expand Down Expand Up @@ -903,10 +907,6 @@ const main = async (params) => {
(directory && pathDefault.resolve(directory)) ||
process.cwd();
const baseDir = posixPath(baseDirSystem);
// Output banner
logMessage(
`${packageName} v${packageVersion} (${libraryName} v${libraryVersion})`
);
// Merge and process args/argv
let fixDefault = false;
// eslint-disable-next-line unicorn/no-useless-undefined
Expand All @@ -933,22 +933,24 @@ const main = async (params) => {
return true;
});
if (shouldShowHelp) {
return showHelp(logMessage);
return showHelp(logMessage, true);
}
// Read argv configuration file (if relevant and present)
let optionsArgv = null;
let relativeDir = null;
if (configPath) {
const resolvedConfigPath =
posixPath(pathDefault.resolve(baseDirSystem, configPath));
optionsArgv =
await readOptionsOrConfig(resolvedConfigPath, fs, noRequire);
relativeDir = pathPosix.dirname(resolvedConfigPath);
}
// Process arguments and get base options
const globPatterns = processArgv(argvFiltered);
const { baseMarkdownlintOptions, dirToDirInfo } =
await getBaseOptions(
let globPatterns = null;
let baseOptions = null;
try {
if (configPath) {
const resolvedConfigPath =
posixPath(pathDefault.resolve(baseDirSystem, configPath));
optionsArgv =
await readOptionsOrConfig(resolvedConfigPath, fs, noRequire);
relativeDir = pathPosix.dirname(resolvedConfigPath);
}
// Process arguments and get base options
globPatterns = processArgv(argvFiltered);
baseOptions = await getBaseOptions(
fs,
baseDir,
relativeDir,
Expand All @@ -958,13 +960,19 @@ const main = async (params) => {
noGlobs,
noRequire
);
} finally {
if (!baseOptions?.baseMarkdownlintOptions.noBanner) {
logMessage(bannerMessage);
}
}
if (
((globPatterns.length === 0) && !nonFileContents) ||
(configPath === null)
) {
return showHelp(logMessage);
return showHelp(logMessage, false);
}
// Include any file overrides or non-file content
const { baseMarkdownlintOptions, dirToDirInfo } = baseOptions;
const resolvedFileContents = {};
for (const file in fileContents) {
const resolvedFile = posixPath(pathDefault.resolve(baseDirSystem, file));
Expand Down
5 changes: 5 additions & 0 deletions schema/markdownlint-cli2-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
"minLength": 1
}
},
"noBanner": {
"description": "Whether to disable the display of the banner message and version numbers on stdout (only valid at the root) : https://github.com/DavidAnson/markdownlint-cli2/blob/v0.12.1/README.md#markdownlint-cli2jsonc",
"type": "boolean",
"default": false
},
"noInlineConfig": {
"description": "Whether to disable support of HTML comments within Markdown content : https://github.com/DavidAnson/markdownlint-cli2/blob/v0.12.1/README.md#markdownlint-cli2jsonc",
"type": "boolean",
Expand Down
3 changes: 3 additions & 0 deletions test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"./modules"
],

// Disable banner message on stdout (only valid at root)
"noBanner": true,

// Disable inline config comments
"noInlineConfig": true,

Expand Down
24 changes: 21 additions & 3 deletions test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const fs = require("node:fs/promises");
const path = require("node:path");
const Ajv = require("ajv");
const test = require("ava").default;
const jsoncParser = require("jsonc-parser");
const { "main": markdownlintCli2 } = require("../markdownlint-cli2.js");
const jsoncParse = require("../parsers/jsonc-parse.js");
const yamlParse = require("../parsers/yaml-parse.js");
const FsMock = require("./fs-mock");

const schemaIdVersionRe = /^.*v(?<version>\d+\.\d+\.\d+).*$/u;
Expand Down Expand Up @@ -98,7 +99,7 @@ test("validateMarkdownlintConfigSchema", async (t) => {
);
return Promise.all(files.map(async (file) => {
const content = await fs.readFile(file, "utf8");
const json = jsoncParser.parse(content);
const json = jsoncParse(content);
const instanceResult = validateConfigSchema(json);
t.truthy(
instanceResult,
Expand Down Expand Up @@ -145,7 +146,7 @@ test("validateMarkdownlintCli2ConfigSchema", async (t) => {
);
return Promise.all(files.map(async (file) => {
const content = await fs.readFile(file, "utf8");
const json = jsoncParser.parse(content);
const json = jsoncParse(content);
const instanceResult = validateConfigSchema(json);
t.truthy(
instanceResult,
Expand All @@ -154,6 +155,23 @@ test("validateMarkdownlintCli2ConfigSchema", async (t) => {
}));
});

test("validateExampleObjectsMatch", async (t) => {
t.plan(1);
const jsonExample = jsoncParse(
await fs.readFile(
"./test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc",
"utf8"
)
);
const yamlExample = yamlParse(
await fs.readFile(
"./test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml",
"utf8"
)
);
t.deepEqual(jsonExample, yamlExample);
});

test("absolute path to directory glob", async (t) => {
t.plan(1);
const argv = [ path.resolve("./test/no-config") ];
Expand Down
3 changes: 3 additions & 0 deletions test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ markdownItPlugins:
modulePaths:
- "./modules"

# Disable banner message on stdout (only valid at root)
noBanner: true

# Disable inline config comments
noInlineConfig: true

Expand Down
6 changes: 2 additions & 4 deletions test/snapshots/markdownlint-cli2-test-exec.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1184,8 +1184,7 @@ Generated by [AVA](https://avajs.dev).
stderr: `title-case.md:1:1 titlecase-rule Titlecase rule [Title Case: 'Expected # Heading, found # heading']␊
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
`,
stdout: '',
}

## markdownlint-cli2-jsonc-invalid (exec)
Expand Down Expand Up @@ -1242,8 +1241,7 @@ Generated by [AVA](https://avajs.dev).
stderr: `title-case.md:1:1 titlecase-rule Titlecase rule [Title Case: 'Expected # Heading, found # heading']␊
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
`,
stdout: '',
}

## markdownlint-cli2-yaml-invalid (exec)
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-exec.js.snap
Binary file not shown.
6 changes: 2 additions & 4 deletions test/snapshots/markdownlint-cli2-test-main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1184,8 +1184,7 @@ Generated by [AVA](https://avajs.dev).
stderr: `title-case.md:1:1 titlecase-rule Titlecase rule [Title Case: 'Expected # Heading, found # heading']␊
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
`,
stdout: '',
}

## markdownlint-cli2-jsonc-invalid (main)
Expand Down Expand Up @@ -1242,8 +1241,7 @@ Generated by [AVA](https://avajs.dev).
stderr: `title-case.md:1:1 titlecase-rule Titlecase rule [Title Case: 'Expected # Heading, found # heading']␊
viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊
`,
stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊
`,
stdout: '',
}

## markdownlint-cli2-yaml-invalid (main)
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-main.js.snap
Binary file not shown.

0 comments on commit 5fcfe57

Please sign in to comment.