|
1 | 1 | import { h as createElement } from 'hastscript'; |
2 | 2 |
|
3 | | -import { isTypedList } from '../../../utils/generators.mjs'; |
| 3 | +import createQueries from '../../../utils/queries/index.mjs'; |
4 | 4 | import { parseListItem } from '../../legacy-json/utils/parseList.mjs'; |
5 | 5 | import parseSignature from '../../legacy-json/utils/parseSignature.mjs'; |
6 | 6 |
|
@@ -70,10 +70,13 @@ export const processListItemsIntoProperties = (items, prefix = '') => { |
70 | 70 | let description = []; |
71 | 71 |
|
72 | 72 | // Special handling for "Returns: " nodes with a description, and no type |
73 | | - if (propName.startsWith('Returns: ') && propName.length > 9) { |
| 73 | + if ( |
| 74 | + (propName.startsWith('Returns: ') || propName.startsWith('Returns ')) && |
| 75 | + propName.length > 9 |
| 76 | + ) { |
74 | 77 | description = propName.substring(9).trim(); |
75 | | - propName = 'Returns'; |
76 | | - displayName = { ...propNode, value: 'Returns' }; |
| 78 | + propName = 'Returns:'; |
| 79 | + displayName = { ...propNode, value: propName }; |
77 | 80 | } else { |
78 | 81 | const extracted = extractTypesAndDescription(paragraph.children.slice(1)); |
79 | 82 | types = extracted.types; |
@@ -106,7 +109,7 @@ export const processListItemsIntoProperties = (items, prefix = '') => { |
106 | 109 | }); |
107 | 110 |
|
108 | 111 | // Process nested properties |
109 | | - const nestedList = nestedContent.find(isTypedList); |
| 112 | + const nestedList = nestedContent.find(createQueries.UNIST.isTypedList); |
110 | 113 | if (nestedList) { |
111 | 114 | const nestedProperties = processListItemsIntoProperties( |
112 | 115 | nestedList.children, |
@@ -228,37 +231,48 @@ export const createSignatureCodeBlock = (functionName, signature) => { |
228 | 231 | /** |
229 | 232 | * Gets the full name of a function. |
230 | 233 | * @param {HeadingMetadataEntry} heading |
| 234 | + * @param {any} fallback |
231 | 235 | */ |
232 | | -export const getFullName = ({ name, text }) => { |
233 | | - const code = text.match(/`([^`]+)`/)?.[1]; |
| 236 | +export const getFullName = ({ name, text }, fallback = name) => { |
| 237 | + // Exit early if the name wasn't processed |
| 238 | + if (name === text) { |
| 239 | + return fallback; |
| 240 | + } |
| 241 | + |
| 242 | + const code = text.trim().match(/`([^`]+)`/)?.[1]; |
234 | 243 | return code?.includes(name) |
235 | | - ? code.slice(0, code.indexOf(name) + name.length) |
236 | | - : name; |
| 244 | + ? code |
| 245 | + .slice(0, code.indexOf(name) + name.length) |
| 246 | + .replace(/^["']|new\s*/, '') |
| 247 | + : fallback; |
237 | 248 | }; |
238 | 249 |
|
239 | 250 | /** |
240 | 251 | * Creates documentation from API metadata entries |
241 | 252 | * @param {import('@types/mdast').Parent} parent - The parent node |
242 | 253 | * @param {ApiDocMetadataEntry[]} entries - Array of API documentation metadata entries |
243 | 254 | * @param {number} backupIndex - If there isn't a list, use this index |
| 255 | + * @param {boolean} addSignatureCodebox - Is this a method? |
244 | 256 | */ |
245 | | -export default (parent, entries, backupIndex) => { |
| 257 | +export default (parent, entries, backupIndex, addSignatureCodebox) => { |
246 | 258 | // Find the list index in the parent for later replacement |
247 | | - const listIdx = parent.children.findIndex(isTypedList); |
| 259 | + const listIdx = parent.children.findIndex(createQueries.UNIST.isTypedList); |
248 | 260 | const elements = []; |
249 | 261 |
|
250 | 262 | // Process all entries |
251 | 263 | for (const entry of entries) { |
252 | 264 | // Find the list in the container |
253 | | - const list = entry.content.children.find(isTypedList); |
| 265 | + const list = entry.content.children.find(createQueries.UNIST.isTypedList); |
254 | 266 |
|
255 | | - // Process the entry |
256 | | - const params = list ? list.children.map(parseListItem) : []; |
257 | | - const signature = parseSignature(entry.heading.data.text, params); |
258 | | - const displayName = getFullName(entry.heading.data); |
| 267 | + // Create a signature code box, if this is a method/ctor. |
| 268 | + if (addSignatureCodebox) { |
| 269 | + const params = list ? list.children.map(parseListItem) : []; |
259 | 270 |
|
260 | | - // Create signature code block |
261 | | - elements.push(createSignatureCodeBlock(displayName, signature)); |
| 271 | + const signature = parseSignature(entry.heading.data.text, params); |
| 272 | + const displayName = getFullName(entry.heading.data); |
| 273 | + |
| 274 | + elements.push(createSignatureCodeBlock(displayName, signature)); |
| 275 | + } |
262 | 276 |
|
263 | 277 | // Create property table if a list exists |
264 | 278 | if (list) { |
|
0 commit comments