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

Commit

Permalink
perf(vitepress): add v-pre to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 26, 2024
1 parent 46b4bf8 commit 72440a9
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 53 deletions.
7 changes: 4 additions & 3 deletions packages/vitepress-plugin-twoslash/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { TransformerTwoslashOptions } from 'shikiji-twoslash/core'
import { createTransformerFactory } from 'shikiji-twoslash/core'
import { createTwoslasher } from 'twoslash-vue'
import type { RendererRichOptions, TransformerTwoslashOptions } from 'shikiji-twoslash'
import type { ShikijiTransformer } from 'shikiji'
import type { TwoslashFloatingVueRendererOptions } from './renderer-floating-vue'
import { rendererFloatingVue } from './renderer-floating-vue'

export * from './renderer-floating-vue'

export interface VitePressPluginTwoslashOptions extends TransformerTwoslashOptions, RendererRichOptions {
export interface VitePressPluginTwoslashOptions extends TransformerTwoslashOptions, TwoslashFloatingVueRendererOptions {
/**
* Requires adding `twoslash` to the code block explicitly to run twoslash
* @default true
Expand All @@ -25,7 +26,7 @@ export function transformerTwoslash(options: VitePressPluginTwoslashOptions = {}
)({
langs: ['ts', 'tsx', 'js', 'jsx', 'json', 'vue'],
explicitTrigger: true,
renderer: rendererFloatingVue(),
renderer: rendererFloatingVue(options),
...options,
})

Expand Down
19 changes: 15 additions & 4 deletions packages/vitepress-plugin-twoslash/src/renderer-floating-vue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defaultHoverInfoProcessor, rendererRich } from 'shikiji-twoslash'
import type { RendererRichOptions, TwoslashRenderer } from 'shikiji-twoslash'
import type { RendererRichOptions, TransformerTwoslashOptions, TwoslashRenderer } from 'shikiji-twoslash'

Check failure on line 2 in packages/vitepress-plugin-twoslash/src/renderer-floating-vue.ts

View workflow job for this annotation

GitHub Actions / lint

'TransformerTwoslashOptions' is defined but never used
import type { Element, ElementContent, Text } from 'hast'
import type { ShikijiTransformerContext, ShikijiTransformerContextCommon } from 'shikiji'
import { gfmFromMarkdown } from 'mdast-util-gfm'
Expand All @@ -19,7 +19,10 @@ export interface TwoslashFloatingVueOptions {
floatingVueThemeCompletion?: string
}

export interface TwoslashFloatingVueRendererOptions extends RendererRichOptions, TwoslashFloatingVueOptions {
export interface TwoslashFloatingVueRendererOptions extends RendererRichOptions {
/**
* Class and themes for floating-vue specific nodes
*/
floatingVue?: TwoslashFloatingVueOptions
}

Expand Down Expand Up @@ -56,7 +59,7 @@ export function rendererFloatingVue(options: TwoslashFloatingVueRendererOptions
},
content: {
type: 'root',
children: [parts.popup],
children: [vPre(parts.popup)],
},
children: [],
},
Expand Down Expand Up @@ -108,7 +111,7 @@ export function rendererFloatingVue(options: TwoslashFloatingVueRendererOptions
},
content: {
type: 'root',
children: [popup],
children: [vPre(popup)],
},
},
],
Expand All @@ -121,6 +124,14 @@ export function rendererFloatingVue(options: TwoslashFloatingVueRendererOptions
return rich
}

function vPre<T extends ElementContent>(el: T): T {
if (el.type === 'element') {
el.properties = el.properties || {}
el.properties['v-pre'] = ''
}
return el
}

function renderMarkdown(this: ShikijiTransformerContextCommon, md: string): ElementContent[] {
const mdast = fromMarkdown(md, { mdastExtensions: [gfmFromMarkdown()] })
return (toHast(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -421,7 +422,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -590,7 +592,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -461,7 +462,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -991,7 +993,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -1597,7 +1600,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -1767,7 +1771,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -2280,7 +2285,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -2686,7 +2692,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -2845,7 +2852,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -3200,7 +3208,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down
54 changes: 36 additions & 18 deletions packages/vitepress-plugin-twoslash/test/out/completion-end.ts.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -305,7 +306,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -740,7 +742,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -1333,7 +1336,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -1741,7 +1745,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -2175,7 +2180,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -2334,7 +2340,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -2758,7 +2765,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -3113,7 +3121,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -3406,7 +3415,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -3830,7 +3840,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -4183,7 +4194,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -4409,7 +4421,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -4833,7 +4846,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -5201,7 +5215,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -5453,7 +5468,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -5877,7 +5893,8 @@
"type": "element",
"tagName": "span",
"properties": {
"class": "twoslash-popup-container vp-copy-ignore"
"class": "twoslash-popup-container vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down Expand Up @@ -6232,7 +6249,8 @@
"type": "element",
"tagName": "ul",
"properties": {
"class": "twoslash-completion-list vp-copy-ignore"
"class": "twoslash-completion-list vp-copy-ignore",
"v-pre": ""
},
"children": [
{
Expand Down
Loading

0 comments on commit 72440a9

Please sign in to comment.