Skip to content

Commit d5cc89d

Browse files
committed
tools: implement markdown formatting
Markdown formatter is now available via `mark format-md` (or `vcbuild format-md` on Windows).
1 parent 0cf5936 commit d5cc89d

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
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+
format-lint-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-lint-md))
1261+
1262+
12561263

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

tools/lint-md/lint-md.mjs

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import fs from 'fs';
1+
import * as fs from 'fs';
2+
import fs__default from 'fs';
23
import path$1 from 'path';
34
import { fileURLToPath, pathToFileURL, URL as URL$1 } from 'url';
45
import process$1 from 'process';
@@ -19485,7 +19486,7 @@ function validateLinks(tree, vfile) {
1948519486
for (const node of getLinksRecursively(tree)) {
1948619487
if (node.url[0] !== "#") {
1948719488
const targetURL = new URL(node.url, currentFileURL);
19488-
if (targetURL.protocol === "file:" && !fs.existsSync(targetURL)) {
19489+
if (targetURL.protocol === "file:" && !fs__default.existsSync(targetURL)) {
1948919490
vfile.message("Broken link", node);
1949019491
} else if (targetURL.pathname === currentFileURL.pathname) {
1949119492
const expected = node.url.includes("#")
@@ -28313,7 +28314,7 @@ function toVFile(options) {
2831328314
*/
2831428315
function readSync(description, options) {
2831528316
const file = toVFile(description);
28316-
file.value = fs.readFileSync(path$1.resolve(file.cwd, file.path), options);
28317+
file.value = fs__default.readFileSync(path$1.resolve(file.cwd, file.path), options);
2831728318
return file
2831828319
}
2831928320

@@ -28326,7 +28327,7 @@ function readSync(description, options) {
2832628327
*/
2832728328
function writeSync(description, options) {
2832828329
const file = toVFile(description);
28329-
fs.writeFileSync(path$1.resolve(file.cwd, file.path), file.value || '', options);
28330+
fs__default.writeFileSync(path$1.resolve(file.cwd, file.path), file.value || '', options);
2833028331
return file
2833128332
}
2833228333

@@ -28381,7 +28382,7 @@ const read =
2838128382
return reject(error)
2838228383
}
2838328384

28384-
fs.readFile(fp, options, done);
28385+
fs__default.readFile(fp, options, done);
2838528386

2838628387
/**
2838728388
* @param {Error} error
@@ -28451,7 +28452,7 @@ const write =
2845128452
return reject(error)
2845228453
}
2845328454

28454-
fs.writeFile(fp, file.value || '', options, done);
28455+
fs__default.writeFile(fp, file.value || '', options, done);
2845528456

2845628457
/**
2845728458
* @param {Error} error
@@ -28936,7 +28937,7 @@ function reporter(files, options = {}) {
2893628937
files = [files];
2893728938
}
2893828939

28939-
return format(transform(files, options), one, options)
28940+
return format$1(transform(files, options), one, options)
2894028941
}
2894128942

2894228943
/**
@@ -29013,7 +29014,7 @@ function transform(files, options) {
2901329014
* @param {Options} options
2901429015
*/
2901529016
// eslint-disable-next-line complexity
29016-
function format(map, one, options) {
29017+
function format$1(map, one, options) {
2901729018
/** @type {boolean} */
2901829019
const enabled =
2901929020
options.color === undefined || options.color === null
@@ -29155,6 +29156,18 @@ function size(value) {
2915529156

2915629157
const paths = process.argv.slice(2);
2915729158

29159+
if (!paths.length) {
29160+
console.error('Usage: lint-md.mjs <path> [<path> ...]');
29161+
process.exit(1);
29162+
}
29163+
29164+
let format = false;
29165+
29166+
if (paths[0] === '--format') {
29167+
paths.shift();
29168+
format = true;
29169+
}
29170+
2915829171
const linter = unified()
2915929172
.use(remarkParse)
2916029173
.use(remarkGfm)
@@ -29164,9 +29177,10 @@ const linter = unified()
2916429177
paths.forEach(async (path) => {
2916529178
const file = await read(path);
2916629179
const result = await linter.process(file);
29167-
if (result.messages.length) {
29180+
if (format) {
29181+
fs.writeFileSync(path, String(result));
29182+
} else if (result.messages.length) {
2916829183
process.exitCode = 1;
2916929184
console.error(reporter(result));
2917029185
}
29171-
// TODO: allow reformatting by writing `String(result)` to the input file
2917229186
});

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as 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, String(result));
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)