-
Notifications
You must be signed in to change notification settings - Fork 65
feat(ui): group overloaded functions into tabs #1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moshams272
wants to merge
11
commits into
nodejs:main
Choose a base branch
from
moshams272:feat/group-overload-functions-tabs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f1c4849
feat(ui): group overloaded functions into tabs
moshams272 47d88f5
test(jsx-ast): add test to increase coverage of handling overloads
moshams272 23ae591
feat(ui): reimplement the tabbed UI for overloaded functions
moshams272 2f142ae
feat(react): adopt standard CodeTabs in overload functions
moshams272 e6521be
fix(react): remove unnecessary dependency
moshams272 9a26504
fixup!
moshams272 739046d
fixup!
moshams272 88170be
chore: trigger CI
moshams272 23d5927
fix(ui): decrease the gap between contents
moshams272 91453f5
fixup!
moshams272 f21af98
fixup!
moshams272 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { | |
| GITHUB_BLOB_URL, | ||
| populate, | ||
| } from '@doc-kit/core/utils/configuration/templates.mjs'; | ||
| import { highlighter } from '@doc-kit/core/utils/highlighter.mjs'; | ||
| import { parseInline } from '@doc-kit/core/utils/inline.mjs'; | ||
| import { omitKeys } from '@doc-kit/core/utils/misc.mjs'; | ||
| import { UNIST } from '@doc-kit/core/utils/queries/index.mjs'; | ||
|
|
@@ -15,7 +16,7 @@ import { slice } from 'mdast-util-slice-markdown'; | |
| import { u as createTree } from 'unist-builder'; | ||
| import { SKIP, visit } from 'unist-util-visit'; | ||
|
|
||
| import { createJSXElement } from './ast.mjs'; | ||
| import { createJSXElement, createAttributeNode } from './ast.mjs'; | ||
| import { extractHeadings, extractTextContent } from './buildBarProps.mjs'; | ||
| import { annotateOverloads } from './overloads.mjs'; | ||
| import { getRemarkRecma as remark } from './remark.mjs'; | ||
|
|
@@ -317,6 +318,147 @@ export const processEntry = entry => { | |
| return entry.content; | ||
| }; | ||
|
|
||
| /** | ||
| * Groups consecutive overloaded function API entries into a single OverloadTabs component. | ||
| * @param {Array<import('estree').Node>} processedChildren - The processed JSX AST nodes for the API entries | ||
| * @param {Array<import('@doc-kit/core/generators/metadata/types').MetadataEntry>} originalEntries - The original API metadata entries containing the overload flags | ||
| * @returns {Array<import('estree').Node>} The final array of layout children with overloads grouped | ||
| */ | ||
| export const groupOverloadsIntoTabs = (processedChildren, originalEntries) => { | ||
| const finalChildren = []; | ||
| let activeOverloadGroup; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder how this handles in asynchronous work? no issues here, right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works on the static AST arrays. That's where it is called: diff |
||
|
|
||
| /** | ||
| * Wraps an AST node's children in a styled panel `div` for tab rendering. | ||
| * @param {import('estree').Node} rootNode - The root node whose children will be wrapped. | ||
| * @returns {import('estree').Node} The new `div` AST node containing the children. | ||
| */ | ||
| const wrapInDiv = rootNode => { | ||
| return createJSXElement('div', { | ||
| inline: false, | ||
| className: 'overload-panel', | ||
| children: rootNode.children || [], | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Extracts the raw signature string from an API entry node and removes the signature node from its children. | ||
| * @param {import('estree').Node} node - The AST node representing the API entry. | ||
| * @returns {string|undefined} The raw TypeScript signature string, or undefined if not found. | ||
| */ | ||
| const extractSignature = ({ children = [] }) => { | ||
| const signatureIndex = children.findIndex( | ||
| c => | ||
| c.properties?.className?.includes('signature') || | ||
| c.properties?.class === 'signature' | ||
| ); | ||
|
|
||
| if (signatureIndex !== -1) { | ||
| const [signatureNode] = children.splice(signatureIndex, 1) ?? []; | ||
|
|
||
| return signatureNode.properties?.dataSignatureRaw; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Finalizes the active overload group by generating a combined signatures block | ||
| */ | ||
| const pushOverloadGroup = () => { | ||
| if (!activeOverloadGroup) { | ||
| return; | ||
| } | ||
|
|
||
| // Deduplicate signatures and join with a single newline | ||
| const uniqueSignatures = [...new Set(activeOverloadGroup.signatures)]; | ||
| const combinedSignatureRaw = uniqueSignatures.join('\n'); | ||
|
|
||
| const highlighted = highlighter.highlightToHast( | ||
| combinedSignatureRaw, | ||
| 'typescript' | ||
| ); | ||
| const combinedSignatureNode = createElement('div', { class: 'signature' }, [ | ||
| highlighted, | ||
| ]); | ||
|
|
||
| // Push combined signatures | ||
| finalChildren.push(combinedSignatureNode); | ||
|
|
||
| // Inject properties needed by CodeTabs component | ||
| const count = activeOverloadGroup.signatures.length; | ||
|
|
||
| const languagesArr = []; | ||
| const displayNamesArr = []; | ||
|
|
||
| for (let i = 0; i < count; i++) { | ||
| languagesArr.push('overload'); | ||
| displayNamesArr.push(`Overload #${i + 1}`); | ||
| } | ||
|
|
||
| activeOverloadGroup.tabsNode.attributes.push( | ||
| createAttributeNode('languages', languagesArr.join('|')), | ||
| createAttributeNode('displayNames', displayNamesArr.join('|')) | ||
| ); | ||
|
|
||
| // Push the tabs | ||
| finalChildren.push(activeOverloadGroup.tabsNode); | ||
|
|
||
| activeOverloadGroup = undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * Processes a single API entry node belonging to an overload group. | ||
| * It extracts its signature and pushes its remaining content into a new tab panel. | ||
| * @param {import('estree').Node} node - The AST node to process and add to the active group. | ||
| */ | ||
| const processOverloadNode = node => { | ||
|
moshams272 marked this conversation as resolved.
|
||
| const signatureRaw = extractSignature(node); | ||
|
|
||
| signatureRaw && activeOverloadGroup.signatures.push(signatureRaw); | ||
| activeOverloadGroup.tabsNode.children.push(wrapInDiv(node)); | ||
| }; | ||
|
|
||
| for (const [i, current] of processedChildren.entries()) { | ||
| const isOverload = originalEntries[i].heading?.data?.isOverload; | ||
|
|
||
| if (!isOverload) { | ||
| pushOverloadGroup(); | ||
| finalChildren.push(current); | ||
| continue; | ||
| } | ||
|
|
||
| // Remove the heading from subsequent overloads as they are grouped under the first heading | ||
| current.children.shift(); | ||
|
|
||
| if (activeOverloadGroup) { | ||
| processOverloadNode(current); | ||
| continue; | ||
| } | ||
|
|
||
| // Pop the previous node as it is the first entry of this overload group | ||
| const last = finalChildren.pop(); | ||
|
moshams272 marked this conversation as resolved.
|
||
| activeOverloadGroup = { | ||
| // Shift out the first node's heading to serve as the main heading for the entire group | ||
| firstHeading: last?.children?.shift?.(), | ||
| signatures: [], | ||
| tabsNode: createJSXElement(JSX_IMPORTS.CodeTabs.name, { | ||
| inline: false, | ||
| children: [], | ||
| }), | ||
| }; | ||
|
|
||
| processOverloadNode(last); | ||
| processOverloadNode(current); | ||
|
|
||
| if (activeOverloadGroup.firstHeading) { | ||
| finalChildren.push(activeOverloadGroup.firstHeading); | ||
| } | ||
| } | ||
|
|
||
| pushOverloadGroup(); | ||
|
|
||
| return finalChildren; | ||
| }; | ||
|
|
||
| /** | ||
| * Builds the overall document layout tree | ||
| * @param {Array<import('@doc-kit/core/generators/metadata/types').MetadataEntry>} entries - API documentation metadata entries | ||
|
|
@@ -336,7 +478,7 @@ export const createDocumentLayout = async (entries, metadata) => { | |
| readingTime: showReadingTime | ||
| ? await readingTime(extractTextContent(entries)) | ||
| : undefined, | ||
| children: entries.map(processEntry), | ||
| children: groupOverloadsIntoTabs(entries.map(processEntry), entries), | ||
| }), | ||
| ]); | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.