Skip to content

Commit b721bf0

Browse files
authored
feat(plugin-md-power): add support npm-to container (#256)
* feat(plugin-md-power): add support `npm-to` container * chore: tweak * chore: tweak * chore: tweak
1 parent 08eeac7 commit b721bf0

File tree

12 files changed

+619
-133
lines changed

12 files changed

+619
-133
lines changed

docs/.vuepress/notes/zh/theme-guide.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const themeGuide = defineNoteConfig({
4040
'选项组',
4141
'隐秘文本',
4242
'示例容器',
43+
'npm-to',
4344
'caniuse',
4445
'导入文件',
4546
],

docs/.vuepress/theme.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const theme: Theme = plumeTheme({
3030
replit: true,
3131
codeSandbox: true,
3232
jsfiddle: true,
33+
npmTo: ['pnpm', 'yarn', 'npm'],
3334
repl: {
3435
go: true,
3536
rust: true,
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
---
2+
title: npmTo 容器
3+
icon: gg:npm
4+
createTime: 2024/10/08 15:54:12
5+
permalink: /guide/markdown/npm-to/
6+
---
7+
8+
## 概述
9+
10+
npmTo 容器用于将 npm 命令行转换为 `pnpm / yarn / deno / bun` 的命令行,并它们作为 代码块组呈现在页面。
11+
12+
`::: npm-to` 容器中,只需要写 一次 npm 命令 代码块即可。
13+
14+
::: details 为什么需要 npmTo 容器 ?
15+
在我编写文档时,常常需要提供 `pnpm / yarn / npm` 等不同运行环境的命令,需要写多个代码块并包装在 `::: code-tabs`
16+
容器中。它占据了我不少的工作量,而且还占据了 markdown 内容中的很长一部分空间,体验并不友好。
17+
因此我编写了这个 `::: npm-to` 容器以解决这个问题。
18+
:::
19+
20+
## 用法
21+
22+
````md
23+
::: npm-to <!-- [!code hl] -->
24+
``` sh
25+
npm install -D vuepress vuepress-theme-plume
26+
```
27+
::: <!-- [!code hl] -->
28+
````
29+
30+
将 包含 `npm` 命令行的代码块,包裹在 `::: npm-to` 容器中即可。
31+
32+
::: warning npm-to 容器仅支持存在单个代码块,不能包含其他内容
33+
:::
34+
35+
上述代码在内部会被转换为:
36+
37+
````md
38+
::: code-tabs
39+
@tab pnpm
40+
``` sh
41+
pnpm add -D vuepress vuepress-theme-plume
42+
```
43+
@tab yarn
44+
``` sh
45+
yarn add -D vuepress vuepress-theme-plume
46+
```
47+
@tab npm
48+
``` sh
49+
npm install -D vuepress vuepress-theme-plume
50+
```
51+
:::
52+
````
53+
54+
最终会在页面中呈现为:
55+
56+
::: npm-to
57+
58+
``` sh
59+
npm install -D vuepress vuepress-theme-plume
60+
```
61+
62+
:::
63+
64+
还可以控制 代码块组中的代码块的显示顺序,如下所示:
65+
66+
**输入:**
67+
68+
````md
69+
::: npm-to tabs="npm,yarn,pnpm,bun,deno" <!-- [!code hl] -->
70+
``` sh
71+
npm install -D vuepress vuepress-theme-plume
72+
```
73+
::: <!-- [!code hl] -->
74+
````
75+
76+
**输出:**
77+
78+
::: npm-to tabs="npm,yarn,pnpm,bun,deno"
79+
80+
``` sh
81+
npm install -D vuepress vuepress-theme-plume
82+
```
83+
84+
:::
85+
86+
## 配置
87+
88+
该功能默认不启用,您需要在 `theme` 配置中启用它。
89+
90+
```ts
91+
export default defineUserConfig({
92+
theme: plumeTheme({
93+
plugins: {
94+
markdownPower: {
95+
// npmTo: true, // 启用,并使用默认配置
96+
npmTo: {
97+
tabs: ['npm', 'yarn', 'pnpm'], // 代码块组默认显示顺序
98+
}
99+
},
100+
}
101+
})
102+
})
103+
```
104+
105+
`npm-to` 支持将 `npm` 命令行转换为 `pnpm / yarn / deno / bun` 的命令行。可以根据需求进行配置 `tabs`
106+
107+
## 命令行支持
108+
109+
`npmTo` 并不对 `npm` 的所有命令行提供支持,以下是支持的列表:
110+
111+
- `npm install` / `npm i`
112+
- `npm run` / `npm run-script`
113+
- `npm init`
114+
- `npm create`
115+
- `npm uninstall` / `npm rm` / `npm remove` / `npm un` / `npm unlink`
116+
- `npm ci`
117+
- `npx`
118+
119+
::: info
120+
对于不支持的命令行,`npmTo` 不会处理它们,仅将它们复制到其他的代码块中。
121+
:::
122+
123+
## 示例
124+
125+
**输入:**
126+
127+
````md
128+
::: npm-to
129+
```sh
130+
npm install && npm run docs:dev
131+
```
132+
:::
133+
````
134+
135+
**输出:**
136+
137+
::: npm-to
138+
139+
```sh
140+
npm install && npm run docs:dev
141+
```
142+
143+
:::
144+
145+
**输入:**
146+
147+
````md
148+
::: npm-to
149+
```sh
150+
npm i -D vue
151+
npm i --save-peer vuepress
152+
npm i typescript
153+
```
154+
:::
155+
````
156+
157+
**输出:**
158+
::: npm-to
159+
160+
```sh
161+
npm i -D vue
162+
npm i --save-peer vuepress
163+
npm i typescript
164+
```
165+
166+
:::
167+
168+
**输入:**
169+
170+
````md
171+
::: npm-to
172+
```sh
173+
npm run docs:dev -- --clean-cache
174+
```
175+
:::
176+
````
177+
178+
**输出:**
179+
180+
::: npm-to
181+
182+
```sh
183+
npm run docs:dev -- --clean-cache
184+
```
185+
186+
:::
187+
188+
**输入:**
189+
190+
````md
191+
::: npm-to tabs="pnpm,yarn,npm,bun,deno"
192+
```sh
193+
npm ci
194+
```
195+
:::
196+
````
197+
198+
**输出:**
199+
200+
::: npm-to tabs="pnpm,yarn,npm,bun,deno"
201+
202+
```sh
203+
npm ci
204+
```
205+
206+
:::

docs/notes/theme/guide/markdown/图标.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@ permalink: /guide/markdown/iconify/
1515
为了更好的使用该功能,主题推荐你安装 `@iconify/json` 依赖。主题会自动解析 `@iconify/json` 中的图标数据,
1616
将有使用的图标打包为本地资源,以获得更好的访问体验。
1717

18-
::: code-tabs
19-
@tab pnpm
20-
21-
```sh
22-
pnpm add @iconify/json
23-
```
24-
25-
@tab yarn
26-
27-
```sh
28-
yarn add @iconify/json
29-
```
30-
31-
@tab npm
18+
::: npm-to
3219

3320
```sh
3421
npm install @iconify/json

docs/notes/theme/guide/功能/图标.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,7 @@ permalink: /guide/features/icon/
6767

6868
由于 `@iconify/json` 包比较大,需要手动进行安装:
6969

70-
::: code-tabs
71-
@tab pnpm
72-
73-
```sh
74-
pnpm add @iconify/json
75-
```
76-
77-
@tab yarn
78-
79-
```sh
80-
yarn add @iconify/json
81-
```
82-
83-
@tab npm
70+
::: npm-to
8471

8572
```sh
8673
npm install @iconify/json

0 commit comments

Comments
 (0)