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

Commit

Permalink
fix!: consistent case of Twoslash
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 16, 2024
1 parent a5a8aba commit f740b3e
Show file tree
Hide file tree
Showing 30 changed files with 254 additions and 249 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const REFERENCES: DefaultTheme.NavItemWithLink[] = [
]

const INTEGRATIONS: DefaultTheme.NavItemWithLink[] = [
{ text: 'TypeScript TwoSlash', link: '/packages/twoslash' },
{ text: 'TypeScript Twoslash', link: '/packages/twoslash' },
{ text: 'Markdown It', link: '/packages/markdown-it' },
{ text: 'Rehype', link: '/packages/rehype' },
{ text: 'Monaco Editor', link: '/packages/monaco' },
Expand Down
4 changes: 2 additions & 2 deletions docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Theme from 'vitepress/theme'
import type { EnhanceAppContext } from 'vitepress'
import { createPinia } from 'pinia'
import TwoSlashFloatingVue from '../../../packages/vitepress-plugin-twoslash/src/client'
import TwoslashFloatingVue from '../../../packages/vitepress-plugin-twoslash/src/client'

import '../../../packages/shikiji-twoslash/style-rich.css'
import 'floating-vue/dist/style.css'
Expand All @@ -14,6 +14,6 @@ export default {
extends: Theme,
enhanceApp({ app }: EnhanceAppContext) {
app.use(createPinia())
app.use(TwoSlashFloatingVue)
app.use(TwoslashFloatingVue)
},
}
2 changes: 1 addition & 1 deletion docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Oh btw, the code blocks in this document are highlighted by Shikiji, as you'd ex
- [Light/Dark themes support](/guide/dual-themes).
- [AST-based transformers addons](/guide/transformers).
- [`hast` support](/guide/transformers#codetohast).
- [TypeScript TwoSlash](/packages/twoslash).
- [TypeScript Twoslash](/packages/twoslash).
- [Shiki-compatible build](/guide/compat).

## Playground
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ We also provide some integrations:

- [Markdown It Plugin](/packages/markdown-it)
- [Rehype Plugin](/packages/rehype)
- [TypeScript TwoSlash Integration](/packages/twoslash)
- [TypeScript Twoslash Integration](/packages/twoslash)
- [Monaco Editor Syntax Highlight](/packages/monaco)
- [CLI](/packages/cli)
- [Common Transformers](/packages/transformers)
Expand Down
6 changes: 3 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"@iconify-json/svg-spinners": "^1.1.2",
"@unocss/reset": "^0.58.3",
"@vueuse/core": "^10.7.2",
"floating-vue": "^5.0.3",
"floating-vue": "^5.1.0",
"pinia": "^2.1.7",
"shikiji": "workspace:*",
"shikiji-transformers": "workspace:*",
"shikiji-twoslash": "workspace:*",
"unocss": "^0.58.3",
"unplugin-vue-components": "^0.26.0",
"vitepress": "^1.0.0-rc.36",
"vue": "^3.4.13"
"vitepress": "^1.0.0-rc.39",
"vue": "^3.4.14"
}
}
38 changes: 19 additions & 19 deletions docs/packages/twoslash.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ import {
codeToHtml,
} from 'shikiji'
import {
transformerTwoSlash,
transformerTwoslash,
} from 'shikiji-twoslash'

const html = await codeToHtml(`console.log()`, {
lang: 'ts',
theme: 'vitesse-dark',
transformers: [
transformerTwoSlash(), // <-- here
transformerTwoslash(), // <-- here
// ...
],
})
```

Just like `shiki-twoslash`, the output is unstyled. You need to add some extra CSS to make them look good.

If you want to run TwoSlash on browsers or workers, reference to the [CDN Usage](#cdn-usage) section.
If you want to run Twoslash on browsers or workers, reference to the [CDN Usage](#cdn-usage) section.

## Renderers

Expand All @@ -58,9 +58,9 @@ This renderer provides a more explicit class name is prefixed with `twoslash-` f
In addition, it runs syntax highlighting on the hover information.

```ts twoslash
import { rendererRich, transformerTwoSlash } from 'shikiji-twoslash'
import { rendererRich, transformerTwoslash } from 'shikiji-twoslash'

transformerTwoSlash({
transformerTwoslash({
renderer: rendererRich() // <--
})
```
Expand Down Expand Up @@ -111,9 +111,9 @@ const c = 1
This renderer aligns with the output of [`shiki-twoslash`](https://shikijs.github.io/twoslash/).

```ts twoslash
import { rendererClassic, transformerTwoSlash } from 'shikiji-twoslash'
import { rendererClassic, transformerTwoslash } from 'shikiji-twoslash'

transformerTwoSlash({
transformerTwoslash({
renderer: rendererClassic() // <--
})
```
Expand All @@ -124,25 +124,25 @@ You might need to reference `shiki-twoslash`'s CSS to make it look good. [Here](

### Explicit Trigger

When integrating with `markdown-it-shikiji` or `rehype-shikiji`, we may not want TwoSlash to run on every code block. In this case, we can set `explicitTrigger` to `true` to only run on code blocks with `twoslash` presented in the codeframe.
When integrating with `markdown-it-shikiji` or `rehype-shikiji`, we may not want Twoslash to run on every code block. In this case, we can set `explicitTrigger` to `true` to only run on code blocks with `twoslash` presented in the codeframe.

```ts twoslash
import { transformerTwoSlash } from 'shikiji-twoslash'
import { transformerTwoslash } from 'shikiji-twoslash'

transformerTwoSlash({
transformerTwoslash({
explicitTrigger: true // <--
})
```

````md
In markdown, you can use the following syntax to trigger TwoSlash:
In markdown, you can use the following syntax to trigger Twoslash:

```ts
// this is a normal code block
```

```ts twoslash
// this will run TwoSlash
// this will run Twoslash
```
````

Expand All @@ -152,7 +152,7 @@ In markdown, you can use the following syntax to trigger TwoSlash:

By default `@typescript/twoslash` runs on Node.js and relies on your local system to resolve TypeScript and types for the imports. Import it directly in non-Node.js environments would not work.

Luckily, TwoSlash implemented a virtual file system, which allow you to provide your own files for TypeScript to resolve in memory. However, loading these files in the browser is still a challenge. Thanks to the work on the [TypeScript WebSite](https://github.com/microsoft/TypeScript-Website), the TypeScript team has provided some utilities to fetch types on demand through CDN, they call it [Automatic Type Acquisition (ATA)](https://github.com/microsoft/TypeScript-Website/tree/v2/packages/ata).
Luckily, Twoslash implemented a virtual file system, which allow you to provide your own files for TypeScript to resolve in memory. However, loading these files in the browser is still a challenge. Thanks to the work on the [TypeScript WebSite](https://github.com/microsoft/TypeScript-Website), the TypeScript team has provided some utilities to fetch types on demand through CDN, they call it [Automatic Type Acquisition (ATA)](https://github.com/microsoft/TypeScript-Website/tree/v2/packages/ata).

We make tiny wrappers around the building blocks and provide an easy-to-use API in [`twoslash-cdn`](https://github.com/antfu/twoslash-cdn). For example:

Expand All @@ -162,7 +162,7 @@ import { createTransformerFactory, rendererRich } from 'https://esm.sh/shikiji-t
import { codeToHtml } from 'https://esm.sh/shikiji@latest'
import { createStorage } from 'https://esm.sh/unstorage@latest'
import indexedDbDriver from 'https://esm.sh/unstorage@latest/drivers/indexedb'
import { createTwoSlashFromCDN } from 'https://esm.sh/twoslash-cdn@latest'
import { createTwoslashFromCDN } from 'https://esm.sh/twoslash-cdn@latest'

// ============= Initialization =============

Expand All @@ -171,14 +171,14 @@ const storage = createStorage({
driver: indexedDbDriver({ base: 'twoslash-cdn' }),
})

const twoslash = createTwoSlashFromCDN({
const twoslash = createTwoslashFromCDN({
storage,
compilerOptions: {
lib: ['esnext', 'dom'],
},
})

const transformerTwoSlash = createTransformerFactory(twoslash.runSync)({
const transformerTwoslash = createTransformerFactory(twoslash.runSync)({
renderer: rendererRich(),
})

Expand All @@ -189,7 +189,7 @@ const app = document.getElementById('app')
const source = `
import { ref } from 'vue'
console.log("Hi! Shikiji + TwoSlash on CDN :)")
console.log("Hi! Shikiji + Twoslash on CDN :)")
const count = ref(0)
// ^?
Expand All @@ -202,10 +202,10 @@ await twoslash.prepareTypes(source)
app.innerHTML = await codeToHtml(source, {
lang: 'ts',
theme: 'vitesse-dark',
transformers: [transformerTwoSlash],
transformers: [transformerTwoslash],
})
```

## Integrations

- [VitePress](/packages/vitepress#twoslash) - Enable TwoSlash support in VitePress.
- [VitePress](/packages/vitepress#twoslash) - Enable Twoslash support in VitePress.
8 changes: 4 additions & 4 deletions docs/packages/vitepress.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ outline: deep

VitePress provides [a few options for customizing Shikiji](https://github.com/vuejs/vitepress/blob/main/src/node/markdown/markdown.ts#L66-L112). Learn more about them in the [VitePress documentation](https://vitepress.dev/reference/site-config#markdown).

## TwoSlash
## Twoslash

To enable [TypeScript TwoSlash](/packages/twoslash) (type hover on code snippets) in VitePress, we provide a VitePress plugin for easy setup. Pre-styled, with [Floating Vue](https://floating-vue.starpad.dev/) to display the type information out side of the code container.
To enable [TypeScript Twoslash](/packages/twoslash) (type hover on code snippets) in VitePress, we provide a VitePress plugin for easy setup. Pre-styled, with [Floating Vue](https://floating-vue.starpad.dev/) to display the type information out side of the code container.

<Badges name="vitepress-plugin-twoslash" />

Expand Down Expand Up @@ -42,14 +42,14 @@ And then in your [`.vitepress/theme/index.ts`](https://vitepress.dev/guide/custo
// @noErrors: true
// .vitepress/theme/index.ts
import Theme from 'vitepress/theme'
import TwoSlashFloatingVue from 'vitepress-plugin-twoslash/client' // [!code hl]
import TwoslashFloatingVue from 'vitepress-plugin-twoslash/client' // [!code hl]
import 'vitepress-plugin-twoslash/style.css' // [!code hl]
import type { EnhanceAppContext } from 'vitepress'

export default {
extends: Theme,
enhanceApp({ app }: EnhanceAppContext) {
app.use(TwoSlashFloatingVue) // [!code hl]
app.use(TwoslashFloatingVue) // [!code hl]
},
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@rollup/plugin-terser": "^0.4.4",
"@types/fs-extra": "^11.0.4",
"@types/hast": "^3.0.3",
"@types/node": "^20.11.0",
"@types/node": "^20.11.4",
"@vitest/coverage-v8": "^1.2.0",
"ansi-sequence-parser": "^1.1.1",
"bumpp": "^9.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/shikiji-twoslash/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# shikiji-twoslash

A [shikiji](https://github.com/antfu/shikiji) transformer for [TypeScript TwoSlash](https://www.typescriptlang.org/dev/twoslash/).
A [shikiji](https://github.com/antfu/shikiji) transformer for [TypeScript Twoslash](https://www.typescriptlang.org/dev/twoslash/).

[Documentations](https://shikiji.netlify.app/packages/twoslash)

Expand Down
2 changes: 1 addition & 1 deletion packages/shikiji-twoslash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"dependencies": {
"shikiji-core": "workspace:*",
"twoslash": "^0.0.7"
"twoslash": "^0.0.8"
},
"devDependencies": {
"@iconify-json/carbon": "^1.1.27",
Expand Down
16 changes: 8 additions & 8 deletions packages/shikiji-twoslash/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
* This file is the core of the shikiji-twoslash package,
* Decoupled from twoslash's implementation and allowing to introduce custom implementation or cache system.
*/
import type { TwoSlashExecuteOptions, TwoSlashReturn } from 'twoslash'
import type { TwoslashExecuteOptions, TwoslashReturn } from 'twoslash'
import type { ShikijiTransformer } from 'shikiji-core'
import type { Element, ElementContent, Text } from 'hast'
import type { ModuleKind, ScriptTarget } from 'typescript'

import { addClassToHast } from 'shikiji-core'
import type { TransformerTwoSlashOptions, TwoSlashRenderer } from './types'
import type { TransformerTwoslashOptions, TwoslashRenderer } from './types'

export * from './types'
export * from './renderer-rich'
export * from './renderer-classic'
export * from './icons'

export function defaultTwoSlashOptions(): TwoSlashExecuteOptions {
export function defaultTwoslashOptions(): TwoslashExecuteOptions {
return {
customTags: ['annotate', 'log', 'warn', 'error'],
}
}

type TwoSlashFunction = (code: string, lang?: string, options?: TwoSlashExecuteOptions) => TwoSlashReturn
type TwoslashFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashReturn

export function createTransformerFactory(
defaultTwoslasher: TwoSlashFunction,
defaultRenderer?: TwoSlashRenderer,
defaultTwoslasher: TwoslashFunction,
defaultRenderer?: TwoslashRenderer,
) {
return function transformerTwoSlash(options: TransformerTwoSlashOptions = {}): ShikijiTransformer {
return function transformerTwoslash(options: TransformerTwoslashOptions = {}): ShikijiTransformer {
const {
langs = ['ts', 'tsx'],
twoslashOptions = defaultTwoSlashOptions(),
twoslashOptions = defaultTwoslashOptions(),
langAlias = {
typescript: 'ts',
json5: 'json',
Expand Down
6 changes: 3 additions & 3 deletions packages/shikiji-twoslash/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createTwoSlasher } from 'twoslash'
import { createTwoslasher } from 'twoslash'
import { createTransformerFactory, rendererRich } from './core'

export * from './core'

/**
* Factory function to create a Shikiji transformer for twoslash integrations.
*/
export const transformerTwoSlash = /* @__PURE__ */ createTransformerFactory(
/* @__PURE__ */ createTwoSlasher(),
export const transformerTwoslash = /* @__PURE__ */ createTransformerFactory(
/* @__PURE__ */ createTwoslasher(),
/* @__PURE__ */ rendererRich(),
)
4 changes: 2 additions & 2 deletions packages/shikiji-twoslash/src/renderer-classic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { TwoSlashRenderer } from './types'
import type { TwoslashRenderer } from './types'

/**
* The default renderer aligning with the original `shiki-twoslash` output.
*/
export function rendererClassic(): TwoSlashRenderer {
export function rendererClassic(): TwoslashRenderer {
return {
nodeStaticInfo(info, node) {
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/shikiji-twoslash/src/renderer-rich.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Element, ElementContent } from 'hast'
import type { ShikijiTransformerContextCommon } from 'shikiji-core'
import type { TwoSlashRenderer } from './types'
import type { TwoslashRenderer } from './types'
import type { CompletionItem } from './icons'
import { defaultCompletionIcons, defaultCustomTagIcons } from './icons'

Expand Down Expand Up @@ -65,7 +65,7 @@ export interface RendererRichOptions {
* An alternative renderer that providers better prefixed class names,
* with syntax highlight for the info text.
*/
export function rendererRich(options: RendererRichOptions = {}): TwoSlashRenderer {
export function rendererRich(options: RendererRichOptions = {}): TwoslashRenderer {
const {
completionIcons = defaultCompletionIcons,
customTagIcons = defaultCustomTagIcons,
Expand Down
12 changes: 6 additions & 6 deletions packages/shikiji-twoslash/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { NodeCompletion, NodeError, NodeHighlight, NodeHover, NodeQuery, NodeTag, TwoSlashOptions, TwoSlashReturn, twoslasher } from 'twoslash'
import type { NodeCompletion, NodeError, NodeHighlight, NodeHover, NodeQuery, NodeTag, TwoslashOptions, TwoslashReturn, twoslasher } from 'twoslash'
import type { CodeToHastOptions, ShikijiTransformerContext } from 'shikiji-core'
import type { Element, ElementContent, Text } from 'hast'

declare module 'shikiji-core' {
interface ShikijiTransformerContextMeta {
twoslash?: TwoSlashReturn
twoslash?: TwoslashReturn
}
}

export interface TransformerTwoSlashOptions {
export interface TransformerTwoslashOptions {
/**
* Languages to apply this transformer to
*/
Expand All @@ -35,19 +35,19 @@ export interface TransformerTwoSlashOptions {
/**
* Options to pass to twoslash
*/
twoslashOptions?: TwoSlashOptions
twoslashOptions?: TwoslashOptions
/**
* Custom renderers to decide how each info should be rendered
*/
renderer?: TwoSlashRenderer
renderer?: TwoslashRenderer
/**
* Strictly throw when there is an error
* @default true
*/
throws?: boolean
}

export interface TwoSlashRenderer {
export interface TwoslashRenderer {
lineError?(this: ShikijiTransformerContext, error: NodeError): ElementContent[]
lineCustomTag?(this: ShikijiTransformerContext, tag: NodeTag): ElementContent[]
lineQuery?(this: ShikijiTransformerContext, query: NodeQuery, targetNode?: Element | Text): ElementContent[]
Expand Down
Loading

0 comments on commit f740b3e

Please sign in to comment.