Skip to content

Commit 26db6db

Browse files
Trotttargos
authored andcommitted
tools: implement markdown formatting
Markdown formatter is now available via `mark format-md` (or `vcbuild format-md` on Windows). PR-URL: #40181 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
1 parent a68f91c commit 26db6db

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,13 @@ tools/.mdlintstamp: $(LINT_MD_FILES)
12531253
# Lints the markdown documents maintained by us in the codebase.
12541254
lint-md: lint-js-doc | tools/.mdlintstamp
12551255

1256+
run-format-md = tools/lint-md/lint-md.mjs --format $(LINT_MD_FILES)
1257+
.PHONY: format-md
1258+
# Formats the markdown documents maintained by us in the codebase.
1259+
format-md:
1260+
@$(call available-node,$(run-format-md))
1261+
1262+
12561263

12571264
LINT_JS_TARGETS = .eslintrc.js benchmark doc lib test tools
12581265

tools/lint-md/lint-md.mjs

+17-4
Original file line numberDiff line numberDiff line change
@@ -28936,7 +28936,7 @@ function reporter(files, options = {}) {
2893628936
files = [files];
2893728937
}
2893828938

28939-
return format(transform(files, options), one, options)
28939+
return format$1(transform(files, options), one, options)
2894028940
}
2894128941

2894228942
/**
@@ -29013,7 +29013,7 @@ function transform(files, options) {
2901329013
* @param {Options} options
2901429014
*/
2901529015
// eslint-disable-next-line complexity
29016-
function format(map, one, options) {
29016+
function format$1(map, one, options) {
2901729017
/** @type {boolean} */
2901829018
const enabled =
2901929019
options.color === undefined || options.color === null
@@ -29155,6 +29155,18 @@ function size(value) {
2915529155

2915629156
const paths = process.argv.slice(2);
2915729157

29158+
if (!paths.length) {
29159+
console.error('Usage: lint-md.mjs <path> [<path> ...]');
29160+
process.exit(1);
29161+
}
29162+
29163+
let format = false;
29164+
29165+
if (paths[0] === '--format') {
29166+
paths.shift();
29167+
format = true;
29168+
}
29169+
2915829170
const linter = unified()
2915929171
.use(remarkParse)
2916029172
.use(remarkGfm)
@@ -29164,9 +29176,10 @@ const linter = unified()
2916429176
paths.forEach(async (path) => {
2916529177
const file = await read(path);
2916629178
const result = await linter.process(file);
29167-
if (result.messages.length) {
29179+
if (format) {
29180+
fs.writeFileSync(path, result.toString());
29181+
} else if (result.messages.length) {
2916829182
process.exitCode = 1;
2916929183
console.error(reporter(result));
2917029184
}
29171-
// TODO: allow reformatting by writing `String(result)` to the input file
2917229185
});

tools/lint-md/lint-md.src.mjs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'fs';
2+
13
import { unified } from 'unified';
24
import remarkParse from 'remark-parse';
35
import remarkStringify from 'remark-stringify';
@@ -8,6 +10,18 @@ import { reporter } from 'vfile-reporter';
810

911
const paths = process.argv.slice(2);
1012

13+
if (!paths.length) {
14+
console.error('Usage: lint-md.mjs <path> [<path> ...]');
15+
process.exit(1);
16+
}
17+
18+
let format = false;
19+
20+
if (paths[0] === '--format') {
21+
paths.shift();
22+
format = true;
23+
}
24+
1125
const linter = unified()
1226
.use(remarkParse)
1327
.use(gfm)
@@ -17,9 +31,10 @@ const linter = unified()
1731
paths.forEach(async (path) => {
1832
const file = await read(path);
1933
const result = await linter.process(file);
20-
if (result.messages.length) {
34+
if (format) {
35+
fs.writeFileSync(path, result.toString());
36+
} else if (result.messages.length) {
2137
process.exitCode = 1;
2238
console.error(reporter(result));
2339
}
24-
// TODO: allow reformatting by writing `String(result)` to the input file
2540
});

vcbuild.bat

+14
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,20 @@ for /D %%D IN (doc\*) do (
726726
ENDLOCAL
727727
goto exit
728728

729+
:format-md
730+
if not defined lint_md goto exit
731+
echo Running Markdown formatter on docs...
732+
SETLOCAL ENABLEDELAYEDEXPANSION
733+
set lint_md_files=
734+
for /D %%D IN (doc\*) do (
735+
for %%F IN (%%D\*.md) do (
736+
set "lint_md_files="%%F" !lint_md_files!"
737+
)
738+
)
739+
%node_exe% tools\lint-md\lint-md.mjs --format %lint_md_files%
740+
ENDLOCAL
741+
goto exit
742+
729743
:create-msvs-files-failed
730744
echo Failed to create vc project files.
731745
del .used_configure_flags

0 commit comments

Comments
 (0)