Skip to content

Commit 861512d

Browse files
authored
perf(plugin-shikiji): improve twoslash (#459)
1 parent 90db034 commit 861512d

File tree

3 files changed

+20
-94
lines changed

3 files changed

+20
-94
lines changed

plugins/plugin-shikiji/src/node/twoslash/renderer-floating-vue.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,11 @@ function renderMarkdown(this: ShikiTransformerContextCommon, md: string): Elemen
165165
if (lang) {
166166
return <Element>{
167167
type: 'element',
168-
tagName: 'code',
169-
properties: {},
168+
tagName: 'div',
169+
properties: {
170+
'class': `language-${lang}`,
171+
'data-ext': lang,
172+
},
170173
children: this.codeToHast(
171174
node.value,
172175
{

plugins/plugin-shikiji/src/node/twoslash/rendererTransformer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface TransformerTwoslashVueOptions extends TransformerTwoslashOptions {
1414
twoslashOptions?: TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptions
1515
}
1616

17-
export interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions, TwoslashFloatingVueRendererOptions {
17+
export interface VuePressTwoslashOptions extends TransformerTwoslashVueOptions, TwoslashFloatingVueRendererOptions {
1818
/**
1919
* Requires adding `twoslash` to the code block explicitly to run twoslash
2020
* @default true
@@ -23,9 +23,9 @@ export interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOp
2323
}
2424

2525
/**
26-
* Create a Shiki transformer for VitePress to enable twoslash integration
26+
* Create a Shiki transformer for VuePress to enable twoslash integration
2727
*/
28-
export function transformerTwoslash(options: VitePressPluginTwoslashOptions = {}): ShikiTransformer {
28+
export function transformerTwoslash(options: VuePressTwoslashOptions = {}): ShikiTransformer {
2929
const {
3030
explicitTrigger = true,
3131
} = options
@@ -67,7 +67,7 @@ export function transformerTwoslash(options: VitePressPluginTwoslashOptions = {}
6767

6868
// Disable v-pre for twoslash, because we need render it with FloatingVue
6969
if (!explicitTrigger || options.meta?.__raw?.match(trigger)) {
70-
const vPre = options.transformers?.find(i => i.name === 'vitepress:v-pre')
70+
const vPre = options.transformers?.find(i => i.name === 'vuepress:v-pre')
7171
if (vPre)
7272
options.transformers?.splice(options.transformers.indexOf(vPre), 1)
7373
}

theme/src/client/styles/twoslash.css

Lines changed: 11 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
/* ===== Basic ===== */
44
:root {
5-
--twoslash-border-color: var(--vp-c-divider);
65
--twoslash-jsdoc-color: #888;
7-
--twoslash-underline-color: currentcolor;
8-
--twoslash-popup-bg: var(--vp-c-neutral-inverse);
96
--twoslash-popup-shadow: var(--vp-shadow-2);
10-
--twoslash-matched-color: inherit;
11-
--twoslash-unmatched-color: #888;
12-
--twoslash-cursor-color: #8888;
137
--twoslash-error-color: var(--vp-c-danger-1);
148
--twoslash-error-bg: var(--vp-c-danger-soft);
159
--twoslash-tag-color: var(--vp-c-tip-1);
@@ -20,6 +14,17 @@
2014
--twoslash-tag-annotate-bg: var(--vp-c-green-soft);
2115
--twoslash-highlighted-bg: var(--vp-c-gray-soft);
2216
--twoslash-highlighted-border: var(--vp-c-border);
17+
--twoslash-popup-bg: var(--vp-c-bg, inherit);
18+
--twoslash-popup-color: var(--vp-c-text-1);
19+
--twoslash-docs-color: var(--vp-c-text-1);
20+
--twoslash-docs-font: var(--vp-font-family-base);
21+
--twoslash-code-font: var(--vp-font-family-mono);
22+
--twoslash-code-size: var(--vp-code-font-size);
23+
--twoslash-underline-color: var(--vp-c-text-3);
24+
--twoslash-border-color: var(--vp-c-border);
25+
--twoslash-cursor-color: var(--vp-c-brand-1);
26+
--twoslash-matched-color: var(--vp-c-brand-1);
27+
--twoslash-unmatched-color: var(--vp-c-text-2);
2328
}
2429

2530
/* Respect people's wishes to not have animations */
@@ -41,74 +46,6 @@
4146
transition-timing-function: ease;
4247
}
4348

44-
.twoslash .twoslash-popup-container {
45-
position: absolute;
46-
z-index: 10;
47-
display: inline-flex;
48-
flex-direction: column;
49-
text-align: left;
50-
pointer-events: none;
51-
user-select: none;
52-
background: var(--twoslash-popup-bg);
53-
border: 1px solid var(--twoslash-border-color);
54-
border-radius: 4px;
55-
box-shadow: var(--twoslash-popup-shadow);
56-
opacity: 0;
57-
transition: opacity 0.3s;
58-
transform: translateY(1.5em);
59-
}
60-
61-
.twoslash .twoslash-query-presisted .twoslash-popup-container {
62-
left: 50%;
63-
z-index: 9;
64-
transform: translate(-1.3em, 1.8em);
65-
}
66-
67-
.twoslash .twoslash-hover:hover .twoslash-popup-container,
68-
.twoslash .twoslash-query-presisted .twoslash-popup-container {
69-
pointer-events: auto;
70-
opacity: 1;
71-
}
72-
73-
.twoslash .twoslash-popup-container:hover {
74-
user-select: auto;
75-
}
76-
77-
.twoslash .twoslash-popup-arrow {
78-
position: absolute;
79-
top: -4px;
80-
left: 1em;
81-
width: 6px;
82-
height: 6px;
83-
pointer-events: none;
84-
background: var(--twoslash-popup-bg);
85-
border-top: 1px solid var(--twoslash-border-color);
86-
border-right: 1px solid var(--twoslash-border-color);
87-
transform: rotate(-45deg);
88-
}
89-
90-
.twoslash .twoslash-popup-code,
91-
.twoslash .twoslash-popup-docs {
92-
padding: 6px 8px !important;
93-
}
94-
95-
.twoslash .twoslash-popup-docs {
96-
font-family: sans-serif;
97-
font-size: 0.8em;
98-
color: var(--twoslash-jsdoc-color);
99-
border-top: 1px solid var(--twoslash-border-color);
100-
}
101-
102-
.twoslash .twoslash-popup-docs-tags {
103-
display: flex;
104-
flex-direction: column;
105-
}
106-
107-
.twoslash .twoslash-popup-docs-tags,
108-
.twoslash .twoslash-popup-docs-tag-name {
109-
margin-right: 0.5em;
110-
}
111-
11249
/* ===== Error Line ===== */
11350
.twoslash .twoslash-error-line {
11451
position: relative;
@@ -240,20 +177,6 @@
240177
}
241178

242179
/* ========== floating vue ================== */
243-
:root {
244-
--twoslash-popup-bg: var(--vp-c-bg, inherit);
245-
--twoslash-popup-color: var(--vp-c-text-1);
246-
--twoslash-docs-color: var(--vp-c-text-1);
247-
--twoslash-docs-font: var(--vp-font-family-base);
248-
--twoslash-code-font: var(--vp-font-family-mono);
249-
--twoslash-code-size: var(--vp-code-font-size);
250-
--twoslash-underline-color: #8888;
251-
--twoslash-border-color: var(--vp-c-border);
252-
--twoslash-cursor-color: var(--vp-c-brand-1);
253-
--twoslash-matched-color: var(--vp-c-brand-1);
254-
--twoslash-unmatched-color: var(--vp-c-text-2);
255-
}
256-
257180
.v-popper--theme-twoslash {
258181
z-index: calc(var(--vp-z-index-local-nav) - 1);
259182
}

0 commit comments

Comments
 (0)