Skip to content

Commit

Permalink
fixup! move to format-md
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Nov 16, 2024
1 parent 48b37ac commit c53f27b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ NODE_EXE = node$(EXEEXT)
NODE ?= "$(PWD)/$(NODE_EXE)"
NODE_G_EXE = node_g$(EXEEXT)
NPM ?= ./deps/npm/bin/npm-cli.js
NPX ?= ./deps/npm/bin/npx-cli.js

# Flags for packaging.
BUILD_DOWNLOAD_FLAGS ?= --download=all
Expand Down Expand Up @@ -769,7 +768,7 @@ doc-only: tools/doc/node_modules \
doc: $(NODE_EXE) doc-only ## Build Node.js, and then build the documentation with the new binary.

doc/node.1: doc/api/cli.md
$(NPX) github:nodejs/api-docs-tooling -t man-page -i doc/api/cli.md -o doc/node.1
$(NODE) tools/lint-md/node_modules/.bin/api-docs-tooling -t man-page -i doc/api/cli.md -o doc/node.1

out/doc:
mkdir -p $@
Expand Down
34 changes: 28 additions & 6 deletions tools/lint-md/lint-md.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import { parseArgs } from 'node:util';
import { resolve, join } from 'node:path';

import { unified } from 'unified';
import remarkParse from 'remark-parse';
Expand All @@ -8,6 +9,8 @@ import presetLintNode from 'remark-preset-lint-node';
import { read } from 'to-vfile';
import { reporter } from 'vfile-reporter';

import generateManPage from './man-page.mjs';

const { values: { format }, positionals: paths } = parseArgs({
options: {
format: { type: 'boolean', default: false },
Expand All @@ -20,6 +23,16 @@ if (!paths.length) {
process.exit(1);
}

const rootDir = resolve(import.meta.dirname, '..', '..');
const cliPath = resolve(join(rootDir, 'doc', 'api', 'cli.md'));
const manPagePath = join(rootDir, 'doc', 'node.1');

function handleDifference(filePath) {
process.exitCode = 1;
const buildCmd = process.platform === 'win32' ? 'vcbuild' : 'make';
console.error(`${filePath} is not formatted. Please run '${buildCmd} format-md'.`);
}

const linter = unified()
.use(remarkParse)
.use(presetLintNode)
Expand All @@ -34,19 +47,28 @@ paths.forEach(async (path) => {
const fileContents = file.toString();
const result = await linter.process(file);
const isDifferent = fileContents !== result.toString();

let isManPageModified = false;
let generatedManPage;
if (resolve(path) === cliPath) {
generatedManPage = await generateManPage(path);
const oldManPage = fs.readFileSync(manPagePath, 'utf-8');
isManPageModified = oldManPage !== generatedManPage;
}

if (format) {
if (isDifferent) {
fs.writeFileSync(path, result.toString());
}
} else {
if (isDifferent) {
process.exitCode = 1;
const cmd = process.platform === 'win32' ? 'vcbuild' : 'make';
console.error(`${path} is not formatted. Please run '${cmd} format-md'.`);
if (isManPageModified) {
fs.writeFileSync(manPagePath, generatedManPage);
}
} else {
if (isDifferent) handleDifference(path);
if (isManPageModified) handleDifference('doc/node.1');
if (result.messages.length) {
process.exitCode = 1;
console.error(reporter(result));
}
}
});
});
8 changes: 8 additions & 0 deletions tools/lint-md/man-page.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { generators, createParser } from '@node-core/api-docs-tooling';
import { read } from 'to-vfile';

export default async function generateManPage(path) {
const { parseApiDocs } = createParser();

Check failure on line 5 in tools/lint-md/man-page.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4
const parsedApiDocs = await parseApiDocs([read(path)]);

Check failure on line 6 in tools/lint-md/man-page.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4
return generators['man-page'].generate(parsedApiDocs, {});

Check failure on line 7 in tools/lint-md/man-page.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 4
}
1 change: 1 addition & 0 deletions tools/lint-md/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "markdown linting",
"version": "1.0.0",
"dependencies": {
"@node-core/api-docs-tooling": "github:nodejs/api-docs-tooling#bbf44c4a39752d1c7d75784d1ac940dab9ca8a96",
"remark-parse": "^11.0.0",
"remark-preset-lint-node": "^5.1.2",
"remark-stringify": "^11.0.0",
Expand Down

0 comments on commit c53f27b

Please sign in to comment.