From 7e89e5a7c3445381e771ee9ab5e9a3c22f8e9241 Mon Sep 17 00:00:00 2001 From: kgeminicdev Date: Thu, 29 Oct 2026 19:41:49 -0700 Subject: [PATCH] fix(man-page): keep the last environment variable when its section ends the input When nothing follows the environment variables section, findIndex returns -1 and slice(start, -1) drops the last entry. Read to the end of the input in that case. Fixes #1198 Signed-off-by: kgeminicdev --- .changeset/man-page-last-env-var.md | 5 ++ .../src/man-page/__tests__/generate.test.mjs | 56 +++++++++++++++++++ packages/node/src/man-page/generate.mjs | 8 ++- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 .changeset/man-page-last-env-var.md create mode 100644 packages/node/src/man-page/__tests__/generate.test.mjs diff --git a/.changeset/man-page-last-env-var.md b/.changeset/man-page-last-env-var.md new file mode 100644 index 00000000..2d1737f6 --- /dev/null +++ b/.changeset/man-page-last-env-var.md @@ -0,0 +1,5 @@ +--- +'@node-core/doc-kit': patch +--- + +The man page no longer drops the last environment variable when that section ends the input. diff --git a/packages/node/src/man-page/__tests__/generate.test.mjs b/packages/node/src/man-page/__tests__/generate.test.mjs new file mode 100644 index 00000000..2435325e --- /dev/null +++ b/packages/node/src/man-page/__tests__/generate.test.mjs @@ -0,0 +1,56 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs'; +import { u } from 'unist-builder'; + +import { generate } from '../generate.mjs'; + +const config = await setConfig({ target: ['man-page'] }); +config['man-page'].output = undefined; + +const entry = (depth, slug, text, description = '') => { + const heading = u('heading', { depth }, [u('text', text)]); + + return { + heading: { depth, data: { slug, text } }, + content: u('root', [heading, u('paragraph', [u('text', description)])]), + }; +}; + +describe('man-page generate', () => { + it('keeps the last environment variable when the section ends the input', async () => { + const input = [ + entry(1, 'command-line-api', 'Command-line API'), + entry(2, 'environment-variables', 'Environment variables', 'Intro.'), + entry(2, 'options', 'Options'), + entry(3, '--foo', '`--foo`', 'Foo option.'), + entry(2, 'environment-variables-1', 'Environment variables'), + entry(3, 'node_onevalue', '`NODE_ONE=value`', 'First var.'), + entry(3, 'node_twovalue', '`NODE_TWO=value`', 'Second var.'), + entry(3, 'node_threevalue', '`NODE_THREE=value`', 'Third var.'), + ]; + + const output = await generate(input); + + assert.match(output, /\.It Ev NODE_ONE Ar value\nFirst var\./); + assert.match(output, /\.It Ev NODE_TWO Ar value\nSecond var\./); + assert.match(output, /\.It Ev NODE_THREE Ar value\nThird var\./); + }); + + it('stops the environment variables at the next section', async () => { + const input = [ + entry(2, 'options', 'Options'), + entry(3, '--foo', '`--foo`', 'Foo option.'), + entry(2, 'environment-variables-1', 'Environment variables'), + entry(3, 'node_onevalue', '`NODE_ONE=value`', 'First var.'), + entry(2, 'useful-v8-options', 'Useful V8 options'), + entry(3, '--max-old-space-size', '`--max-old-space-size=SIZE`', 'Heap.'), + ]; + + const output = await generate(input); + + assert.match(output, /\.It Ev NODE_ONE Ar value\nFirst var\./); + assert.doesNotMatch(output, /max-old-space-size/); + }); +}); diff --git a/packages/node/src/man-page/generate.mjs b/packages/node/src/man-page/generate.mjs index e6a75054..56fb9cf8 100644 --- a/packages/node/src/man-page/generate.mjs +++ b/packages/node/src/man-page/generate.mjs @@ -43,11 +43,15 @@ export async function generate(input) { ({ heading }) => heading.data.slug === config.envVarsHeaderSlug ); - // The first header that is <3 in depth after environmentStart - const environmentEnd = input.findIndex( + // The first header that is <3 in depth after environmentStart, + // or the end of the input when the section is the last one + const nextSectionStart = input.findIndex( ({ heading }, index) => heading.depth < 3 && index > environmentStart ); + const environmentEnd = + nextSectionStart === -1 ? input.length : nextSectionStart; + const output = { // Extract the CLI options. options: extractMandoc(