2021-01-19 20:41:04 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import fs from "node:fs/promises";
|
|
|
|
import path from "node:path";
|
|
|
|
import yaml from "js-yaml";
|
|
|
|
import { __dirname, importWithTypeJson } from "../test/esm-helpers.mjs";
|
|
|
|
const configSchema = await importWithTypeJson(import.meta, "../schema/markdownlint-config-schema.json");
|
2021-01-19 20:41:04 -08:00
|
|
|
|
2025-09-18 21:21:12 -07:00
|
|
|
/** @type {Object<string, any>} */
|
2021-01-19 20:41:04 -08:00
|
|
|
const configExample = {};
|
|
|
|
for (const rule in configSchema.properties) {
|
2022-12-19 21:51:18 -08:00
|
|
|
if (/^(?:MD\d{3}|default|extends)$/.test(rule)) {
|
2021-01-19 20:41:04 -08:00
|
|
|
const properties = configSchema.properties[rule];
|
|
|
|
configExample[rule + "-description"] = properties.description;
|
|
|
|
configExample[rule] = properties.default;
|
2025-09-18 21:21:12 -07:00
|
|
|
const subproperties = Object.fromEntries(
|
|
|
|
Object.entries(
|
|
|
|
properties.oneOf?.at(-1).properties || []
|
|
|
|
).filter(([ key ]) => key !== "severity")
|
|
|
|
);
|
|
|
|
if (Object.keys(subproperties).length > 0) {
|
|
|
|
/** @type {Object<string, any>} */
|
2021-01-19 20:41:04 -08:00
|
|
|
const ruleExample = {};
|
|
|
|
// eslint-disable-next-line guard-for-in
|
2025-09-13 15:35:07 -07:00
|
|
|
for (const property in subproperties) {
|
|
|
|
const ruleProperties = subproperties[property];
|
2021-01-19 20:41:04 -08:00
|
|
|
ruleExample[property + "-sub-description"] = ruleProperties.description;
|
|
|
|
ruleExample[property] = ruleProperties.default;
|
|
|
|
}
|
|
|
|
configExample[rule] = ruleExample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-09-18 21:21:12 -07:00
|
|
|
/**
|
|
|
|
* Transforms comments to use the specified prefix.
|
|
|
|
*
|
|
|
|
* @param {string} input Markdown input.
|
|
|
|
* @param {string} commentPrefix Comment prefix.
|
|
|
|
* @returns {string} Transformed input.
|
|
|
|
*/
|
2023-02-21 00:04:05 +01:00
|
|
|
const transformComments = (input, commentPrefix) => (
|
|
|
|
commentPrefix +
|
|
|
|
" Example markdownlint configuration with all properties set to their default value\n" +
|
|
|
|
input
|
|
|
|
.replace(/^(\s*)[^-\s]+-sub-description"?: "?([^"\n]+)"?,?$/gm, "$1" + commentPrefix + " $2")
|
|
|
|
.replace(/^(\s*)[^-\s]+-description"?: "?([^"\n]+)"?,?$/gm, "\n$1" + commentPrefix + " $2")
|
|
|
|
);
|
|
|
|
|
|
|
|
const configStringJson = JSON.stringify(configExample, null, 2);
|
2024-11-28 20:36:44 -08:00
|
|
|
await fs.writeFile(
|
|
|
|
path.join(__dirname(import.meta), ".markdownlint.jsonc"),
|
2023-02-21 00:04:05 +01:00
|
|
|
transformComments(configStringJson, "//"),
|
2021-01-21 19:50:57 -08:00
|
|
|
"utf8"
|
|
|
|
);
|
|
|
|
|
2024-02-17 15:33:27 -08:00
|
|
|
const configStringYaml = yaml.dump(
|
2023-02-21 00:04:05 +01:00
|
|
|
configExample,
|
|
|
|
{
|
2024-02-17 15:33:27 -08:00
|
|
|
"forceQuotes": true,
|
|
|
|
"lineWidth": -1,
|
|
|
|
"quotingType": "\""
|
2023-02-21 00:04:05 +01:00
|
|
|
}
|
|
|
|
);
|
2024-11-28 20:36:44 -08:00
|
|
|
await fs.writeFile(
|
|
|
|
path.join(__dirname(import.meta), ".markdownlint.yaml"),
|
2023-02-21 00:04:05 +01:00
|
|
|
transformComments(configStringYaml, "#"),
|
2021-01-19 20:41:04 -08:00
|
|
|
"utf8"
|
|
|
|
);
|