Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/man-page-last-env-var.md
Original file line number Diff line number Diff line change
@@ -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.
56 changes: 56 additions & 0 deletions packages/node/src/man-page/__tests__/generate.test.mjs
Original file line number Diff line number Diff line change
@@ -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/);
});
});
8 changes: 6 additions & 2 deletions packages/node/src/man-page/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading