Skip to content

Commit bd12cae

Browse files
committed
feat(vitepress-twoslash): support rendering jsdoc tags
1 parent ba420e1 commit bd12cae

File tree

6 files changed

+117
-67
lines changed

6 files changed

+117
-67
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"vitepress-plugin-twoslash": "workspace:*",
6363
"vitest": "^1.2.0",
6464
"vue-tsc": "^1.8.27",
65-
"wrangler": "^3.22.4"
65+
"wrangler": "^3.22.5"
6666
},
6767
"resolutions": {
6868
"@types/hast": "^3.0.3",

packages/shikiji-twoslash/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"dependencies": {
5858
"shikiji-core": "workspace:*",
59-
"twoslash": "^0.0.8"
59+
"twoslash": "^0.0.9"
6060
},
6161
"devDependencies": {
6262
"@iconify-json/carbon": "^1.1.27",

packages/vitepress-plugin-twoslash/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"mdast-util-to-hast": "^13.1.0",
6060
"shikiji": "workspace:*",
6161
"shikiji-twoslash": "workspace:*",
62-
"twoslash-vue": "^0.0.8",
62+
"twoslash-vue": "^0.0.9",
6363
"vue": "^3.4.14"
6464
}
6565
}

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

+69-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defaultCompletionIcons, defaultHoverInfoProcessor, rendererRich } from 'shikiji-twoslash'
22
import type { RendererRichOptions, TwoslashRenderer } from 'shikiji-twoslash'
3-
import type { Element, Text } from 'hast'
3+
import type { NodeHover, NodeQuery } from 'twoslash'
4+
import type { Element, ElementContent, Text } from 'hast'
45
import type { ShikijiTransformerContext } from 'shikiji'
56
import { gfmFromMarkdown } from 'mdast-util-gfm'
67
import { fromMarkdown } from 'mdast-util-from-markdown'
@@ -21,10 +22,9 @@ export function rendererFloatingVue(options: VitePressPluginTwoslashOptions & Re
2122
...options,
2223
})
2324

24-
function createFloatingVueWarpper(this: ShikijiTransformerContext, text: string, docs: string | undefined, node: Element | Text, presisted = false): Element | undefined {
25-
const content = processHoverInfo(text)
26-
27-
if ((!content || content === 'any'))
25+
function createFloatingVueWarpper(this: ShikijiTransformerContext, info: NodeHover | NodeQuery, node: Element | Text, presisted = false): Element | undefined {
26+
const content = processHoverInfo(info.text)
27+
if (!content || content === 'any')
2828
return undefined
2929

3030
const themedContent = (this.codeToHast(
@@ -41,12 +41,11 @@ export function rendererFloatingVue(options: VitePressPluginTwoslashOptions & Re
4141
addClassToHast(node, 'twoslash-popup-type')
4242
})
4343

44-
if (docs) {
45-
docs = processHoverDocs(docs) ?? docs
46-
const mdast = fromMarkdown(docs, {
44+
const markdownToHast = (md: string): ElementContent[] => {
45+
const mdast = fromMarkdown(md, {
4746
mdastExtensions: [gfmFromMarkdown()],
4847
})
49-
const hast = toHast(mdast, {
48+
return (toHast(mdast, {
5049
handlers: {
5150
code: (state, node) => {
5251
const lang = node.lang || ''
@@ -63,14 +62,71 @@ export function rendererFloatingVue(options: VitePressPluginTwoslashOptions & Re
6362
return defaultHandlers.code(state, node)
6463
},
6564
},
66-
})
65+
}) as Element).children
66+
}
67+
68+
const markdownInlineToHast = (md: string): ElementContent[] => {
69+
const children = markdownToHast(md)
70+
if (children.length === 1 && children[0].type === 'element' && children[0].tagName === 'p')
71+
return children[0].children
72+
return children
73+
}
74+
75+
if (info.docs) {
76+
const docs = processHoverDocs(info.docs) ?? info.docs
77+
const children = markdownToHast(docs)
78+
6779
themedContent.push({
6880
type: 'element',
6981
tagName: 'div',
7082
properties: {
7183
class: 'twoslash-popup-jsdoc vp-doc',
7284
},
73-
children: (hast as any).children,
85+
children,
86+
})
87+
}
88+
89+
if (info.tags?.length) {
90+
themedContent.push({
91+
type: 'element',
92+
tagName: 'div',
93+
properties: {
94+
class: 'twoslash-popup-jsdoc twoslash-popup-jsdoc-tags vp-doc',
95+
},
96+
children: info.tags.map(tag => (<Element>{
97+
type: 'element',
98+
tagName: 'span',
99+
properties: {
100+
class: `twoslash-popup-jsdoc-tag`,
101+
},
102+
children: [
103+
{
104+
type: 'element',
105+
tagName: 'span',
106+
properties: {
107+
class: 'twoslash-popup-jsdoc-tag-name',
108+
},
109+
children: [
110+
{
111+
type: 'text',
112+
value: `@${tag[0]}`,
113+
},
114+
],
115+
},
116+
...tag[1]
117+
? [
118+
<Element>{
119+
type: 'element',
120+
tagName: 'span',
121+
properties: {
122+
class: 'twoslash-popup-jsdoc-tag-value',
123+
},
124+
children: markdownInlineToHast(tag[1]),
125+
},
126+
]
127+
: [],
128+
],
129+
})),
74130
})
75131
}
76132

@@ -129,12 +185,12 @@ export function rendererFloatingVue(options: VitePressPluginTwoslashOptions & Re
129185
return {
130186
...rich,
131187
nodeStaticInfo(info, node) {
132-
return createFloatingVueWarpper.call(this, info.text, info.docs, node) || {}
188+
return createFloatingVueWarpper.call(this, info, node) || {}
133189
},
134190
nodeQuery(query, node) {
135191
if (!query.text)
136192
return {}
137-
return createFloatingVueWarpper.call(this, query.text, query.docs, node, true) || {}
193+
return createFloatingVueWarpper.call(this, query, node, true) || {}
138194
},
139195
nodeCompletion(query, node) {
140196
if (node.type !== 'text')

packages/vitepress-plugin-twoslash/src/style.css

+15
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
overflow-x: auto;
7979
}
8080

81+
.floating-vue-twoslash .twoslash-popup-jsdoc-tags {
82+
display: flex;
83+
flex-direction: column;
84+
padding: 8px 12px !important;
85+
}
86+
87+
.floating-vue-twoslash
88+
.twoslash-popup-jsdoc-tags
89+
.twoslash-popup-jsdoc-tag-name {
90+
font-family: var(--vp-font-family-mono);
91+
color: var(--vp-c-brand-1);
92+
margin-right: 0.5em;
93+
font-size: 0.9em;
94+
}
95+
8196
.twoslash-completion-cursor {
8297
height: 1.2em;
8398
width: 2px;

0 commit comments

Comments
 (0)