Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,32 @@ describe('extractHeadings', () => {

assert.equal(result.length, 2);
assert.equal(result[0].slug, 'fs-readfile');
assert.equal(result[0].value, 'fs.readFile()');
assert.equal(result[0].depth, 2);
assert.equal(result[0].stability, 2);
assert.equal(result[1].stability, 2);
});

it('keeps method table of contents labels compact', () => {
const entries = [
{
heading: {
depth: 3,
data: {
text: '`crypto.createHash(algorithm[, options])`',
name: 'createHash',
slug: 'crypto-createhash',
type: 'method',
},
},
},
];

const [result] = extractHeadings(entries);

assert.equal(result.value, 'crypto.createHash()');
});

it('filters out entries with empty heading text', () => {
const entries = [
{
Expand Down
33 changes: 26 additions & 7 deletions src/generators/jsx-ast/utils/buildBarProps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import { visit } from 'unist-util-visit';

import { TOC_MAX_HEADING_DEPTH } from '../constants.mjs';

const SHORT_SIGNATURE_TYPES = new Set(['classMethod', 'ctor', 'method']);

/**
* Shortens method-like headings so the table of contents remains scannable.
* @param {import('../../metadata/types').HeadingData} data
*/
const formatCodeHeading = data => {
const code = data.text.match(/`([^`]+)`/)?.[1] ?? data.text;
const signatureStart = code.indexOf('(');

if (signatureStart === -1) {
return code.replace(/`/g, '').trim();
}

return `${code.slice(0, signatureStart).replace(/^new\s+/, '')}()`;
};

/**
* Generate a combined plain text string from all MDAST entries for estimating reading time.
*
Expand Down Expand Up @@ -40,13 +57,15 @@ const extractHeading = entry => {
const heading =
cliFlagOrEnv.length > 0
? cliFlagOrEnv.at(-1)[1]
: data.text
// Remove any containing code blocks
.replace(/`/g, '')
// Remove any prefixes (i.e. 'Class:')
.replace(/^[^:]+:/, '')
// Trim the remaining whitespace
.trim();
: SHORT_SIGNATURE_TYPES.has(data.type)
? formatCodeHeading(data)
: data.text
// Remove any containing code blocks
.replace(/`/g, '')
// Remove any prefixes (i.e. 'Class:')
.replace(/^[^:]+:/, '')
// Trim the remaining whitespace
.trim();

return {
depth: entry.heading.depth,
Expand Down
Loading