-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add signature metadata handling for improved documentation extractio #2435
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
13a3c67
feat: add signature metadata handling for improved documentation extr…
bjohansebas cae8bba
refactor: enhance tag stripping and metadata handling in documentatio…
bjohansebas 673fb02
fix: preserve indentation in signature metadata replacements for bett…
bjohansebas 7aa5ba6
refactor: improve code readability by formatting multiline expressions
bjohansebas 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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // `<Signature>` and `<Param>` carry API metadata (added-in version, deprecation, | ||
| // runtime requirements, types, defaults) as JSX attributes, which plain-text and | ||
| // llms.txt exports would otherwise discard along with the tags. This rewrites each | ||
| // opening tag into the same plain-text sentences the component renders, so the | ||
| // metadata stays readable next to the member's heading. | ||
|
|
||
| // Attribute values may contain `>` inside quotes or braces (e.g. | ||
| // `runtime={{ 'Node.js': '>=22.2.0' }}`), so tag matching can't stop at the | ||
| // first `>`. | ||
| export const JSX_ATTRS = `(?:"[^"]*"|'[^']*'|\\{(?:[^{}]|\\{[^{}]*\\})*\\}|[^>"'{])*`; | ||
|
|
||
| // Also matches the component's slot markers, so their section titles ("Arguments", | ||
| // "Properties", "Returns") survive as text. | ||
| const JSX_META_TAG = new RegExp( | ||
| `<(Signature|Param)\\b(${JSX_ATTRS})\\/?>|<Fragment\\s+slot=["'](attributes|properties|returns)["']\\s*>`, | ||
| 'g' | ||
| ); | ||
|
|
||
| /** | ||
| * @param {string} attrs | ||
| * @param {string} name | ||
| * @returns {string | undefined} | ||
| */ | ||
| const getAttr = (attrs, name) => { | ||
| const match = attrs.match(new RegExp(`(?:^|\\s)${name}=(?:"([^"]*)"|'([^']*)')`)); | ||
| return match ? (match[1] ?? match[2]) : undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {string} attrs | ||
| * @param {string} name | ||
| * @returns {boolean} | ||
| */ | ||
| const hasFlag = (attrs, name) => new RegExp(`(?:^|\\s)${name}(?=\\s|$)`).test(attrs); | ||
|
|
||
| /** | ||
| * Replace `<Signature>`/`<Param>` opening tags with the sentences the component | ||
| * renders ("Added in v4.16.0.", "options (Object, optional):", …). | ||
| * | ||
| * @param {string} content | ||
| * @returns {string} | ||
| */ | ||
| export const signatureMetaToText = (content) => { | ||
| // Title for the next `attributes` slot; set by the enclosing Signature's | ||
| // `attributesTitle` prop. Matches are visited in document order, so the | ||
| // Signature opening tag is always seen before its slots. | ||
| let attributesTitle = 'Arguments'; | ||
| return content.replace(JSX_META_TAG, (tag, component, attrs, slot, offset, source) => { | ||
| // Keep the tag's own indentation, which mirrors the nesting depth in the | ||
| // source, so replacements stay visually grouped under their section. | ||
| const lineStart = source.lastIndexOf('\n', offset - 1) + 1; | ||
| const beforeTag = source.slice(lineStart, offset); | ||
| const indent = /^[ \t]*$/.test(beforeTag) ? beforeTag : ''; | ||
|
|
||
| if (slot) { | ||
| const title = | ||
| slot === 'attributes' ? attributesTitle : slot === 'properties' ? 'Properties' : 'Returns'; | ||
| return `\n\n${indent}${title}:\n\n`; | ||
| } | ||
|
|
||
| const since = getAttr(attrs, 'since'); | ||
| const deprecated = getAttr(attrs, 'deprecated'); | ||
|
|
||
| if (component === 'Param') { | ||
| const name = getAttr(attrs, 'name'); | ||
| if (!name) return tag; | ||
| const details = [ | ||
| getAttr(attrs, 'type'), | ||
| hasFlag(attrs, 'optional') && 'optional', | ||
| getAttr(attrs, 'default') && `default: ${getAttr(attrs, 'default')}`, | ||
| since && `added in ${since}`, | ||
| deprecated && `deprecated in ${deprecated}`, | ||
| ].filter(Boolean); | ||
| return `\n\n${indent}- ${name}${details.length ? ` (${details.join(', ')})` : ''}:\n\n`; | ||
| } | ||
|
|
||
| attributesTitle = getAttr(attrs, 'attributesTitle') ?? 'Arguments'; | ||
| const runtime = [...attrs.matchAll(/['"]([^'"]+)['"]\s*:\s*['"]([^'"]+)['"]/g)] | ||
| .map(([, engine, constraint]) => `${engine} ${constraint}`) | ||
| .join(', '); | ||
| const lines = [ | ||
| getAttr(attrs, 'type') && `Type: ${getAttr(attrs, 'type')}.`, | ||
| getAttr(attrs, 'returns') && `Returns: ${getAttr(attrs, 'returns')}.`, | ||
| since && `Added in ${since}.`, | ||
| deprecated && `Deprecated in ${deprecated}.`, | ||
| runtime && `Requires runtime: ${runtime}.`, | ||
| ].filter(Boolean); | ||
| return lines.length ? `\n\n${lines.join(' ')}\n\n` : tag; | ||
| }); | ||
| }; |
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.