Skip to content

Commit e21c1f1

Browse files
committed
highlight sig
1 parent deee349 commit e21c1f1

File tree

2 files changed

+14
-36
lines changed

2 files changed

+14
-36
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
STABILITY_PREFIX_LENGTH,
2424
TYPES_WITH_METHOD_SIGNATURES,
2525
} from '../constants.mjs';
26-
import createSignatureElements, {
26+
import insertSignature, {
2727
createPropertyTable,
2828
getFullName,
2929
} from './createSignatureElements.mjs';
@@ -162,12 +162,9 @@ const transformHeadingNode = (entry, node, index, parent) => {
162162
}
163163

164164
// Add method signatures for appropriate types
165-
createSignatureElements(
166-
parent,
167-
node,
168-
index + (sourceLink ? 2 : 1),
169-
TYPES_WITH_METHOD_SIGNATURES.includes(node.data.type)
170-
);
165+
if (TYPES_WITH_METHOD_SIGNATURES.includes(node.data.type)) {
166+
insertSignature(parent, node, index + 1);
167+
}
171168

172169
return [SKIP];
173170
};

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

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { highlightToHast } from '@node-core/rehype-shiki';
12
import { h as createElement } from 'hastscript';
23

34
import createQueries from '../../../utils/queries/index.mjs';
@@ -200,7 +201,7 @@ export const generateSignatures = (functionName, signature) => {
200201
export const createSignatureCodeBlock = (functionName, signature) => {
201202
const signatures = generateSignatures(functionName, signature);
202203
const codeContent = signatures.join('\n');
203-
return createElement('pre', codeContent);
204+
return highlightToHast(codeContent, 'typescript');
204205
};
205206

206207
/**
@@ -226,36 +227,16 @@ export const getFullName = ({ name, text }, fallback = name) => {
226227
* Creates documentation from API metadata entries
227228
* @param {import('@types/mdast').Parent} parent - The parent node
228229
* @param {import('@types/mdast').Heading} heading - The heading
229-
* @param {number} backupIndex - If there isn't a list, use this index
230-
* @param {boolean} addSignatureCodebox - Is this a method?
230+
* @param {number} idx - Index
231231
*/
232-
export default (parent, heading, backupIndex, addSignatureCodebox) => {
233-
// Find the list index in the parent
234-
const listIdx = parent.children.findIndex(createQueries.UNIST.isTypedList);
235-
const list = parent.children[listIdx];
236-
const elements = [];
232+
export default ({ children }, { data }, idx) => {
233+
// Find the list in the parent
234+
const list = children.find(createQueries.UNIST.isTypedList);
237235

238-
// Create a signature code box, if this is a method/ctor.
239-
if (addSignatureCodebox) {
240-
const params = list ? list.children.map(parseListItem) : [];
236+
const params = list ? list.children.map(parseListItem) : [];
241237

242-
const signature = parseSignature(heading.data.text, params);
243-
const displayName = getFullName(heading.data);
238+
const signature = parseSignature(data.text, params);
239+
const displayName = getFullName(data);
244240

245-
elements.push(createSignatureCodeBlock(displayName, signature));
246-
}
247-
248-
// Create property table if a list exists
249-
if (list) {
250-
elements.push(createPropertyTable(list));
251-
}
252-
253-
// Replace the list in the parent with all collected elements
254-
if (elements.length > 0) {
255-
if (listIdx > -1) {
256-
parent.children.splice(listIdx, 1, ...elements);
257-
} else {
258-
parent.children.splice(backupIndex, 0, ...elements);
259-
}
260-
}
241+
children.splice(idx, 0, createSignatureCodeBlock(displayName, signature));
261242
};

0 commit comments

Comments
 (0)