Skip to content

Commit 250b58e

Browse files
committed
feat(web): make headings more distinct
1 parent 1aef493 commit 250b58e

File tree

5 files changed

+28
-74
lines changed

5 files changed

+28
-74
lines changed

src/generators/jsx-ast/constants.mjs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,7 @@ export const OVERRIDDEN_POSITIONS = [
154154
];
155155

156156
// These types are methods, and have signatures we should enumerate
157-
export const TYPES_WITH_METHOD_SIGNATURES = [
158-
'class',
159-
'ctor',
160-
'method',
161-
'classMethod',
162-
];
157+
export const TYPES_WITH_METHOD_SIGNATURES = ['ctor', 'method', 'classMethod'];
163158

164159
// Regex to trim leading whitespace, colons, and hyphens from strings
165160
export const TRIMMABLE_PADDING_REGEX = /^[\s:-]+/;

src/generators/jsx-ast/utils/buildBarProps.mjs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import readingTime from 'reading-time';
44
import { visit } from 'unist-util-visit';
55

6-
import { getFullName } from './signature.mjs';
76
import getConfig from '../../../utils/configuration/index.mjs';
87
import {
98
GITHUB_EDIT_URL,
@@ -49,25 +48,16 @@ const extractHeading = entry => {
4948

5049
const cliFlagOrEnv = [...data.text.matchAll(/`(-[\w-]+|[A-Z0-9_]+=)/g)];
5150

52-
let heading;
53-
54-
if (cliFlagOrEnv.length > 0) {
55-
heading = cliFlagOrEnv.at(-1)[1];
56-
} else {
57-
const rawName = data.name
58-
// Remove any containing code blocks
59-
.replace(/`/g, '')
60-
// Remove any prefixes (i.e. 'Class:')
61-
.replace(/^[^:]+:/, '')
62-
// Trim the remaining whitespace
63-
.trim();
64-
65-
heading = getFullName(data, rawName);
66-
}
67-
68-
if (data.type === 'ctor') {
69-
heading += ' Constructor';
70-
}
51+
const heading =
52+
cliFlagOrEnv.length > 0
53+
? cliFlagOrEnv.at(-1)[1]
54+
: data.text
55+
// Remove any containing code blocks
56+
.replace(/`/g, '')
57+
// Remove any prefixes (i.e. 'Class:')
58+
.replace(/^[^:]+:/, '')
59+
// Trim the remaining whitespace
60+
.trim();
7161

7262
return {
7363
depth: entry.heading.depth,

src/generators/jsx-ast/utils/buildContent.mjs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
TYPE_PREFIX_LENGTH,
2222
} from '../constants.mjs';
2323
import {
24-
insertSignatureCodeBlock,
2524
createSignatureTable,
25+
createSignatureCodeBlock,
2626
getFullName,
2727
} from './signature.mjs';
2828
import getConfig from '../../../utils/configuration/index.mjs';
@@ -125,12 +125,15 @@ export const extractHeadingContent = content => {
125125
/**
126126
* Creates a heading wrapper element with anchors, icons, and optional change history.
127127
* @param {import('mdast').Node} content - The content node to extract text from
128+
* @param {import('mdast.Node')} parent - The parent
128129
* @param {import('unist').Node|null} changeElement - The change history element, if available
129130
*/
130-
export const createHeadingElement = (content, changeElement) => {
131+
export const createHeadingElement = (content, parent, changeElement) => {
131132
const { type, depth, slug } = content.data;
132133

133-
let headingContent = extractHeadingContent(content);
134+
let headingContent = TYPES_WITH_METHOD_SIGNATURES.includes(type)
135+
? createSignatureCodeBlock(parent, content)
136+
: extractHeadingContent(content);
134137

135138
// Build heading with anchor link
136139
const headingWrapper = createElement('div', [
@@ -212,6 +215,7 @@ export const transformHeadingNode = async (
212215
// Replace heading node with our enhanced heading element
213216
parent.children[index] = createHeadingElement(
214217
node,
218+
parent,
215219
createChangeElement(entry, remark)
216220
);
217221

@@ -243,11 +247,6 @@ export const transformHeadingNode = async (
243247
parent.children.splice(index + 1, 0, sourceLink);
244248
}
245249

246-
// If the heading type supports method signatures, insert signature block
247-
if (TYPES_WITH_METHOD_SIGNATURES.includes(node.data.type)) {
248-
insertSignatureCodeBlock(parent, node, index + 1);
249-
}
250-
251250
return [SKIP];
252251
};
253252

src/generators/jsx-ast/utils/signature.mjs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { h as createElement } from 'hastscript';
2-
31
import { createJSXElement } from './ast.mjs';
42
import { parseListIntoProperties } from './types.mjs';
53
import { highlighter } from '../../../utils/highlighter.mjs';
@@ -48,20 +46,6 @@ export const generateSignature = (
4846
return `${prefix}${functionName}(${paramsStr})${returnStr}`;
4947
};
5048

51-
/**
52-
* Creates a syntax-highlighted code block for a signature using rehype-shiki.
53-
*
54-
* @param {string} functionName - The function name to display.
55-
* @param {import('../../legacy-json/types').MethodSignature} signature - Signature object with parameter and return type info.
56-
* @param {string} prefix - Optional prefix like `'new '`.
57-
*/
58-
export const createSignatureCodeBlock = (functionName, signature, prefix) => {
59-
const sig = generateSignature(functionName, signature, prefix);
60-
const highlighted = highlighter.highlightToHast(sig, 'typescript');
61-
62-
return createElement('div', { class: 'signature' }, [highlighted]);
63-
};
64-
6549
/**
6650
* Infers the "real" function name from a heading node.
6751
* Useful when auto-generated headings differ from code tokens.
@@ -87,14 +71,12 @@ export const getFullName = ({ name, text }, fallback = name) => {
8771
};
8872

8973
/**
90-
* Transforms a heading + list structure into a function/class signature block.
91-
* Mutates the `children` array by injecting the signature HAST node.
74+
* Creates a syntax-highlighted code block for a signature using rehype-shiki.
9275
*
9376
* @param {import('@types/mdast').Parent} parent - The parent MDAST node (usually a section).
9477
* @param {import('@types/mdast').Heading} heading - The heading node with metadata.
95-
* @param {number} idx - The index at which the heading occurs in `parent.children`.
9678
*/
97-
export const insertSignatureCodeBlock = ({ children }, { data }, idx) => {
79+
export const createSignatureCodeBlock = ({ children }, { data }) => {
9880
// Try to locate the parameter list immediately following the heading
9981
const listIdx = children.findIndex(createQueries.UNIST.isStronglyTypedList);
10082

@@ -119,16 +101,15 @@ export const insertSignatureCodeBlock = ({ children }, { data }, idx) => {
119101
children.splice(listIdx, 1); // Remove class param list
120102
}
121103

122-
// Insert the highlighted signature block above the heading
123-
children.splice(
124-
idx,
125-
0,
126-
createSignatureCodeBlock(
127-
displayName,
128-
signature,
129-
data.type === 'ctor' ? 'new ' : ''
130-
)
104+
const sig = generateSignature(
105+
displayName,
106+
signature,
107+
data.type === 'ctor' ? 'new ' : ''
131108
);
109+
110+
const highlighted = highlighter.highlightToHast(sig, 'typescript');
111+
112+
return highlighted.children[0].children;
132113
};
133114

134115
/**

src/generators/web/ui/index.css

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,4 @@ main {
106106
display: inline;
107107
margin-left: var(--spacing, 0.25rem);
108108
}
109-
110-
.signature {
111-
/* Signature styling - remove line numbers and padding */
112-
.line {
113-
padding-left: unset !important;
114-
115-
&::after {
116-
all: unset !important;
117-
}
118-
}
119-
}
120109
}

0 commit comments

Comments
 (0)