Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: implement markdown formatting #40181

Merged
merged 2 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,13 @@ tools/.mdlintstamp: $(LINT_MD_FILES)
# Lints the markdown documents maintained by us in the codebase.
lint-md: lint-js-doc | tools/.mdlintstamp

run-format-md = tools/lint-md/lint-md.mjs --format $(LINT_MD_FILES)
.PHONY: format-md
# Formats the markdown documents maintained by us in the codebase.
format-md:
@$(call available-node,$(run-format-md))



LINT_JS_TARGETS = .eslintrc.js benchmark doc lib test tools

Expand Down
29 changes: 23 additions & 6 deletions tools/lint-md/lint-md.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27475,8 +27475,12 @@ function findUrl(_, protocol, domain, path, match) {
* @param {RegExpMatchObject} match
*/
function findEmail(_, atext, label, match) {
// Not an expected previous character.
if (!previous(match, true) || /[_-]$/.test(label)) {
if (
// Not an expected previous character.
!previous(match, true) ||
// Label ends in not allowed character.
/[_-\d]$/.test(label)
) {
return false
}

Expand Down Expand Up @@ -28936,7 +28940,7 @@ function reporter(files, options = {}) {
files = [files];
}

return format(transform(files, options), one, options)
return format$1(transform(files, options), one, options)
}

/**
Expand Down Expand Up @@ -29013,7 +29017,7 @@ function transform(files, options) {
* @param {Options} options
*/
// eslint-disable-next-line complexity
function format(map, one, options) {
function format$1(map, one, options) {
/** @type {boolean} */
const enabled =
options.color === undefined || options.color === null
Expand Down Expand Up @@ -29155,6 +29159,18 @@ function size(value) {

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

if (!paths.length) {
console.error('Usage: lint-md.mjs <path> [<path> ...]');
process.exit(1);
}

let format = false;

if (paths[0] === '--format') {
paths.shift();
format = true;
}

const linter = unified()
.use(remarkParse)
.use(remarkGfm)
Expand All @@ -29164,9 +29180,10 @@ const linter = unified()
paths.forEach(async (path) => {
const file = await read(path);
const result = await linter.process(file);
if (result.messages.length) {
if (format) {
fs.writeFileSync(path, result.toString());
} else if (result.messages.length) {
process.exitCode = 1;
console.error(reporter(result));
}
// TODO: allow reformatting by writing `String(result)` to the input file
});
19 changes: 17 additions & 2 deletions tools/lint-md/lint-md.src.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'fs';

import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
Expand All @@ -8,6 +10,18 @@ import { reporter } from 'vfile-reporter';

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

if (!paths.length) {
console.error('Usage: lint-md.mjs <path> [<path> ...]');
process.exit(1);
}

let format = false;

if (paths[0] === '--format') {
paths.shift();
format = true;
}

const linter = unified()
.use(remarkParse)
.use(gfm)
Expand All @@ -17,9 +31,10 @@ const linter = unified()
paths.forEach(async (path) => {
const file = await read(path);
const result = await linter.process(file);
if (result.messages.length) {
if (format) {
fs.writeFileSync(path, result.toString());
} else if (result.messages.length) {
process.exitCode = 1;
console.error(reporter(result));
}
// TODO: allow reformatting by writing `String(result)` to the input file
});
12 changes: 6 additions & 6 deletions tools/lint-md/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vcbuild.bat
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,20 @@ for /D %%D IN (doc\*) do (
ENDLOCAL
goto exit

:format-md
if not defined lint_md goto exit
echo Running Markdown formatter on docs...
SETLOCAL ENABLEDELAYEDEXPANSION
set lint_md_files=
for /D %%D IN (doc\*) do (
for %%F IN (%%D\*.md) do (
set "lint_md_files="%%F" !lint_md_files!"
)
)
%node_exe% tools\lint-md\lint-md.mjs --format %lint_md_files%
ENDLOCAL
goto exit

:create-msvs-files-failed
echo Failed to create vc project files.
del .used_configure_flags
Expand Down