Skip to content

Commit 820834a

Browse files
committed
feat(vitepress): improve jsdocs handling
1 parent 5fc2fb3 commit 820834a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

packages/shikiji-twoslash/src/renderer-rich.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export interface RendererRichOptions {
7474
*
7575
* By default it pass-through the markdown.
7676
*/
77-
renderMarkdownInline?(this: ShikijiTransformerContextCommon, markdown: string): ElementContent[]
77+
renderMarkdownInline?(this: ShikijiTransformerContextCommon, markdown: string, context: string): ElementContent[]
7878

7979
/**
8080
* Extensions for the genreated HAST tree.
@@ -276,7 +276,7 @@ export function rendererRich(options: RendererRichOptions = {}): TwoslashRendere
276276
properties: {
277277
class: 'twoslash-popup-docs-tag-value',
278278
},
279-
children: renderMarkdownInline.call(this, tag[1]),
279+
children: renderMarkdownInline.call(this, tag[1], `tag:${tag[0]}`),
280280
},
281281
]
282282
: [],

packages/vitepress-plugin-twoslash/src/renderer-floating-vue.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ function vPre<T extends ElementContent>(el: T): T {
133133
}
134134

135135
function renderMarkdown(this: ShikijiTransformerContextCommon, md: string): ElementContent[] {
136-
const mdast = fromMarkdown(md, { mdastExtensions: [gfmFromMarkdown()] })
136+
const mdast = fromMarkdown(
137+
md.replace(/{@link ([^}]*)}/g, '$1'), // replace jsdoc links
138+
{ mdastExtensions: [gfmFromMarkdown()] },
139+
)
140+
137141
return (toHast(
138142
mdast,
139143
{
@@ -157,12 +161,11 @@ function renderMarkdown(this: ShikijiTransformerContextCommon, md: string): Elem
157161
) as Element).children
158162
}
159163

160-
function renderMarkdownInline(this: ShikijiTransformerContext, md: string): ElementContent[] {
161-
const children = renderMarkdown.call(
162-
this,
163-
md
164-
.replace(/{@link ([^}]*)}/g, '$1'), // replace jsdoc links
165-
)
164+
function renderMarkdownInline(this: ShikijiTransformerContext, md: string, context?: string): ElementContent[] {
165+
if (context === 'tag:param')
166+
md = md.replace(/^([\w$-]+)/, '`$1` ')
167+
168+
const children = renderMarkdown.call(this, md)
166169
if (children.length === 1 && children[0].type === 'element' && children[0].tagName === 'p')
167170
return children[0].children
168171
return children

0 commit comments

Comments
 (0)