diff --git a/README.md b/README.md index 940ed302..a6d09aa4 100644 --- a/README.md +++ b/README.md @@ -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: `` diff --git a/markdownlint-cli2.js b/markdownlint-cli2.js index ded56451..129a035f 100755 --- a/markdownlint-cli2.js +++ b/markdownlint-cli2.js @@ -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"; @@ -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] @@ -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 @@ -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, @@ -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)); diff --git a/schema/markdownlint-cli2-config-schema.json b/schema/markdownlint-cli2-config-schema.json index def6ad50..bb807b8e 100644 --- a/schema/markdownlint-cli2-config-schema.json +++ b/schema/markdownlint-cli2-config-schema.json @@ -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", diff --git a/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc b/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc index 87cd4992..babf3e7e 100644 --- a/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc +++ b/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc @@ -39,6 +39,9 @@ "./modules" ], + // Disable banner message on stdout (only valid at root) + "noBanner": true, + // Disable inline config comments "noInlineConfig": true, diff --git a/test/markdownlint-cli2-test.js b/test/markdownlint-cli2-test.js index 516bb7de..39708993 100644 --- a/test/markdownlint-cli2-test.js +++ b/test/markdownlint-cli2-test.js @@ -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(?\d+\.\d+\.\d+).*$/u; @@ -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, @@ -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, @@ -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") ]; diff --git a/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml b/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml index eca7edd6..7f53dda7 100644 --- a/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml +++ b/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml @@ -34,6 +34,9 @@ markdownItPlugins: modulePaths: - "./modules" +# Disable banner message on stdout (only valid at root) +noBanner: true + # Disable inline config comments noInlineConfig: true diff --git a/test/snapshots/markdownlint-cli2-test-exec.js.md b/test/snapshots/markdownlint-cli2-test-exec.js.md index d7bc3566..bd72c57b 100644 --- a/test/snapshots/markdownlint-cli2-test-exec.js.md +++ b/test/snapshots/markdownlint-cli2-test-exec.js.md @@ -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) @@ -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) diff --git a/test/snapshots/markdownlint-cli2-test-exec.js.snap b/test/snapshots/markdownlint-cli2-test-exec.js.snap index 23949622..106c5e9b 100644 Binary files a/test/snapshots/markdownlint-cli2-test-exec.js.snap and b/test/snapshots/markdownlint-cli2-test-exec.js.snap differ diff --git a/test/snapshots/markdownlint-cli2-test-main.js.md b/test/snapshots/markdownlint-cli2-test-main.js.md index 5ab2ff73..5e41059a 100644 --- a/test/snapshots/markdownlint-cli2-test-main.js.md +++ b/test/snapshots/markdownlint-cli2-test-main.js.md @@ -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) @@ -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) diff --git a/test/snapshots/markdownlint-cli2-test-main.js.snap b/test/snapshots/markdownlint-cli2-test-main.js.snap index fd8077e7..c8ca827a 100644 Binary files a/test/snapshots/markdownlint-cli2-test-main.js.snap and b/test/snapshots/markdownlint-cli2-test-main.js.snap differ