Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
feat(vitepress): improve jsdocs handling
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 26, 2024
1 parent 5fc2fb3 commit 820834a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/shikiji-twoslash/src/renderer-rich.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface RendererRichOptions {
*
* By default it pass-through the markdown.
*/
renderMarkdownInline?(this: ShikijiTransformerContextCommon, markdown: string): ElementContent[]
renderMarkdownInline?(this: ShikijiTransformerContextCommon, markdown: string, context: string): ElementContent[]

/**
* Extensions for the genreated HAST tree.
Expand Down Expand Up @@ -276,7 +276,7 @@ export function rendererRich(options: RendererRichOptions = {}): TwoslashRendere
properties: {
class: 'twoslash-popup-docs-tag-value',
},
children: renderMarkdownInline.call(this, tag[1]),
children: renderMarkdownInline.call(this, tag[1], `tag:${tag[0]}`),
},
]
: [],
Expand Down
17 changes: 10 additions & 7 deletions packages/vitepress-plugin-twoslash/src/renderer-floating-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ function vPre<T extends ElementContent>(el: T): T {
}

function renderMarkdown(this: ShikijiTransformerContextCommon, md: string): ElementContent[] {
const mdast = fromMarkdown(md, { mdastExtensions: [gfmFromMarkdown()] })
const mdast = fromMarkdown(
md.replace(/{@link ([^}]*)}/g, '$1'), // replace jsdoc links
{ mdastExtensions: [gfmFromMarkdown()] },
)

return (toHast(
mdast,
{
Expand All @@ -157,12 +161,11 @@ function renderMarkdown(this: ShikijiTransformerContextCommon, md: string): Elem
) as Element).children
}

function renderMarkdownInline(this: ShikijiTransformerContext, md: string): ElementContent[] {
const children = renderMarkdown.call(
this,
md
.replace(/{@link ([^}]*)}/g, '$1'), // replace jsdoc links
)
function renderMarkdownInline(this: ShikijiTransformerContext, md: string, context?: string): ElementContent[] {
if (context === 'tag:param')
md = md.replace(/^([\w$-]+)/, '`$1` ')

const children = renderMarkdown.call(this, md)
if (children.length === 1 && children[0].type === 'element' && children[0].tagName === 'p')
return children[0].children
return children
Expand Down

0 comments on commit 820834a

Please sign in to comment.