Skip to content

Commit

Permalink
feat: 使用@shikijs/vitepress-twoslash在代码块中添加类型提示
Browse files Browse the repository at this point in the history
  • Loading branch information
ikenxuan committed Jul 23, 2024
1 parent 93db801 commit 321f948
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 47 deletions.
10 changes: 9 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import {
PageProperties,
PagePropertiesMarkdownSection
} from '@nolebase/vitepress-plugin-page-properties/vite'
// 缩略图模糊哈希生成
import { ThumbnailHashImages } from '@nolebase/vitepress-plugin-thumbnail-hash/vite'
// 代码块内的代码类型提示
import { transformerTwoslash } from '@shikijs/vitepress-twoslash'

export default defineConfig({
lang: 'zh-CN',
Expand Down Expand Up @@ -49,10 +53,15 @@ export default defineConfig({
// 行内链接预览
md.use(InlineLinkPreviewElementTransform)
},
codeTransformers: [
transformerTwoslash()
]
},
vite: {
plugins: [
ThumbnailHashImages(),
GitChangelog({
maxGitLogCount: 2000,
// 要获取git日志的仓库
repoURL: () => 'https://github.com/KarinJS/Karin',
}),
Expand All @@ -69,7 +78,6 @@ export default defineConfig({
PageProperties(),
PagePropertiesMarkdownSection({
excludes: [
'toc.md',
'index.md',
],
}),
Expand Down
13 changes: 9 additions & 4 deletions docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import DefaultTheme from 'vitepress/theme'
import mediumZoom from 'medium-zoom'
import { onMounted, watch, nextTick, h } from 'vue'
import { type Plugin, onMounted, watch, nextTick, h } from 'vue'
import { useData, useRoute } from 'vitepress'
import 'vitepress-markdown-timeline/dist/theme/index.css'
import './style/index.css'
Expand Down Expand Up @@ -29,6 +29,7 @@ import {
} from '@nolebase/vitepress-plugin-highlight-targeted-heading/client'
// 快速复制当前页的url
import Share from './components/Share.vue'
// 卡片
import Ncard from './components/Ncard.vue'
// 页面属性
import {
Expand All @@ -39,16 +40,20 @@ import '@nolebase/vitepress-plugin-page-properties/client/style.css'
import '@nolebase/vitepress-plugin-enhanced-mark/client/style.css'
// 页面底部评论
import giscusTalk from 'vitepress-plugin-comment-with-giscus'
// 代码块内的代码类型提示
import TwoslashFloatingVue from '@shikijs/vitepress-twoslash/client'
import '@shikijs/vitepress-twoslash/style.css'

export default {
extends: DefaultTheme,
enhanceApp ({ app }) {
app.component('NCard', Ncard)
app.use(NolebaseGitChangelogPlugin)
app.use(NolebaseInlineLinkPreviewPlugin)
app.use(TwoslashFloatingVue as Plugin)
app.use(NolebaseGitChangelogPlugin as Plugin)
app.use(NolebaseInlineLinkPreviewPlugin as Plugin)
app.use(NolebasePagePropertiesPlugin<{
progress: number
}>(), {
}>() as Plugin, {
properties: {
'zh-CN': [
{
Expand Down
2 changes: 1 addition & 1 deletion docs/utils/Renderer.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Renderer

```js
```js twoslash
import { Renderer } from '#Karin'
```
# 正在编写中,咕咕咕~
18 changes: 9 additions & 9 deletions docs/utils/YamlEditor.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

### 导入和实例化

```js
```js twoslash
import { YamlEditor } from 'node-karin'

// 创建一个 YamlEditor 实例,指定文件路径
Expand All @@ -27,7 +27,7 @@ const yamlEditor = new YamlEditor('./path/to/your/file.yaml')

### 获取 YAML 中的值

```js
```js twoslash
// 获取指定路径的值,路径用点号分隔
const value = yamlEditor.get('root.child.key')

Expand All @@ -42,7 +42,7 @@ if (value === false) {

### 设置 YAML 中的值

```js
```js twoslash
// 设置指定路径的值
yamlEditor.set('root.child.key', 'newValue')
```
Expand All @@ -51,7 +51,7 @@ yamlEditor.set('root.child.key', 'newValue')

### 向 YAML 中添加新值

```js
```js twoslash
// 向指定路径添加新值
yamlEditor.add('root.newPath', 'newValue')
```
Expand All @@ -60,7 +60,7 @@ yamlEditor.add('root.newPath', 'newValue')

### 删除 YAML 中的值

```js
```js twoslash
// 删除指定路径的值
const success = yamlEditor.del('root.child.key')

Expand All @@ -75,7 +75,7 @@ if (success) {

### 向 YAML 中的数组添加新元素

```js
```js twoslash
// 向指定路径的数组添加新元素
yamlEditor.append('root.array', 'newElement', true) // 在数组开头添加

Expand All @@ -86,7 +86,7 @@ yamlEditor.append('root.array', 'newElement') // 在数组末尾添加

### 检查指定路径的键是否存在

```js
```js twoslash
// 向指定路径的数组添加新元素
yamlEditor.has('a.b.c') // true
yamlEditor.has('x.y.z') // false
Expand All @@ -96,7 +96,7 @@ yamlEditor.has('x.y.z') // false

### 查询指定路径中是否包含指定的值

```js
```js twoslash
// 查询指定路径中是否包含指定的值
yamlEditor.hasVal('a.b.c', 'val') // true
yamlEditor.hasVal('x.y.z', 'val') // false
Expand All @@ -106,7 +106,7 @@ yamlEditor.hasVal('x.y.z', 'val') // false

### 保存文件

```js
```js twoslash
// 保存修改后的 YAML 文件
yamlEditor.save()
```
Expand Down
2 changes: 1 addition & 1 deletion docs/utils/common.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# common

```js
```js twoslash
import { common } from 'node-karin'

/** 休眠函数 延迟5秒 */
Expand Down
10 changes: 5 additions & 5 deletions docs/utils/karin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

通过使用别名,开发者可以更简洁地引入所需的模块,无需指定完整的路径。

```js
```js twoslash
// 不建议这么做!
import segment from 'node_modules/node-karin/lib/bot/segment.js'
import plugin from 'node_modules/node-karin/lib/plugins/plugin.js'
Expand All @@ -23,28 +23,28 @@ import { segment, Plugin } from 'node-karin'

- ## `APP`

```js
```js twoslash
import { App } from 'node-karin'
// ...
```

- ## `Bot`

```js
```js twoslash
import { Bot } from 'node-karin'
// ...
```

- ## `Cfg`

```js
```js twoslash
import { Cfg } from 'node-karin'
// ...
```

- ## `plugin`

```js
```js twoslash
import { Plugin } from 'node-karin'
// ...
```
2 changes: 1 addition & 1 deletion docs/utils/logger.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# logger

```js
```js twoslash
import { logger } from 'node-karin'

/** 打印trace日志 */
Expand Down
2 changes: 1 addition & 1 deletion docs/utils/redis.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# redis

```js
```js twoslash
import { redis } from 'node-karin'

/** 存储一个键值对 */
Expand Down
Loading

0 comments on commit 321f948

Please sign in to comment.