@kawapress/plugin-code-block 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,7 +16,7 @@
16
16
  </p>
17
17
 
18
18
  <p align="center">
19
- <a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress/beta?label=npm%20beta&color=42b883" alt="npm beta version"></a>
19
+ <a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress?label=npm&color=42b883" alt="npm version"></a>
20
20
  <a href="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml"><img src="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml/badge.svg" alt="documentation build"></a>
21
21
  <a href="https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE"><img src="https://img.shields.io/github/license/ShenQingchuan/kawapress" alt="MIT license"></a>
22
22
  <img src="https://img.shields.io/badge/Node.js-%E2%89%A522.12-339933?logo=nodedotjs&logoColor=white" alt="Node.js 22.12 or newer">
@@ -28,8 +28,7 @@
28
28
  <a href="https://github.com/ShenQingchuan/kawapress/blob/main/README.zh-CN.md">简体中文</a>
29
29
  </p>
30
30
 
31
- > [!IMPORTANT]
32
- > KawaPress is in beta. It is ready for testing and early projects, but APIs may change before 0.1.0. Please pin the version when adopting it today.
31
+ If you run into any problems while using KawaPress, please [open an issue](https://github.com/ShenQingchuan/kawapress/issues). Your feedback helps shape the project during its early development.
33
32
 
34
33
  ## Why KawaPress?
35
34
 
@@ -47,6 +46,18 @@ KawaPress is an independent project. It is not a VitePress fork or a drop-in rep
47
46
 
48
47
  You need [Node.js](https://nodejs.org/) 22.12 or newer.
49
48
 
49
+ Create a new site with the scaffolder:
50
+
51
+ ```sh
52
+ npm create kawapress@latest my-kawapress-site
53
+ cd my-kawapress-site
54
+ npm run dev
55
+ ```
56
+
57
+ `pnpm create kawapress my-kawapress-site` and `yarn create kawapress my-kawapress-site` do the same thing. The scaffolder asks for a site title and a default language, and can install dependencies for you.
58
+
59
+ If you would rather see every file from the start, set the site up by hand:
60
+
50
61
  ```sh
51
62
  mkdir my-kawapress-site
52
63
  cd my-kawapress-site
package/README.zh-CN.md CHANGED
@@ -16,7 +16,7 @@
16
16
  </p>
17
17
 
18
18
  <p align="center">
19
- <a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress/beta?label=npm%20beta&color=42b883" alt="npm beta 版本"></a>
19
+ <a href="https://www.npmjs.com/package/kawapress"><img src="https://img.shields.io/npm/v/kawapress?label=npm&color=42b883" alt="npm 版本"></a>
20
20
  <a href="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml"><img src="https://github.com/ShenQingchuan/kawapress/actions/workflows/deploy-pages.yml/badge.svg" alt="文档构建状态"></a>
21
21
  <a href="https://github.com/ShenQingchuan/kawapress/blob/main/LICENSE"><img src="https://img.shields.io/github/license/ShenQingchuan/kawapress" alt="MIT 许可"></a>
22
22
  <img src="https://img.shields.io/badge/Node.js-%E2%89%A522.12-339933?logo=nodedotjs&logoColor=white" alt="Node.js 22.12 或更高版本">
@@ -28,8 +28,7 @@
28
28
  <a href="https://github.com/ShenQingchuan/kawapress/blob/main/README.md">English</a>
29
29
  </p>
30
30
 
31
- > [!IMPORTANT]
32
- > KawaPress 目前处于 beta 阶段,适合试用和早期项目。0.1.0 之前,公开 API 仍可能调整;现阶段使用时请固定具体版本。
31
+ 使用 KawaPress 时遇到任何问题,欢迎到 [提交 Issue](https://github.com/ShenQingchuan/kawapress/issues) 反馈,一起参与本项目的早期建设。
33
32
 
34
33
  ## 为什么选择 KawaPress?
35
34
 
@@ -47,6 +46,18 @@ KawaPress 是一个独立项目,不是 VitePress 的分支或配置兼容层
47
46
 
48
47
  请先准备 [Node.js](https://nodejs.org/) 22.12 或更高版本。
49
48
 
49
+ 用脚手架创建新站点:
50
+
51
+ ```sh
52
+ npm create kawapress@latest my-kawapress-site
53
+ cd my-kawapress-site
54
+ npm run dev
55
+ ```
56
+
57
+ `pnpm create kawapress my-kawapress-site` 和 `yarn create kawapress my-kawapress-site` 的作用完全一样。脚手架会询问站点标题和默认语言,也可以顺便帮你安装依赖。
58
+
59
+ 如果你更想从零看清每个文件,可以手动创建:
60
+
50
61
  ```sh
51
62
  mkdir my-kawapress-site
52
63
  cd my-kawapress-site
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kawapress/plugin-code-block",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.1.0",
5
5
  "description": "Language labels, copy controls, and line numbers for KawaPress code blocks",
6
6
  "license": "MIT",
7
7
  "homepage": "https://shenqingchuan.github.io/kawapress/",
@@ -37,7 +37,7 @@
37
37
  "registry": "https://registry.npmjs.org/"
38
38
  },
39
39
  "peerDependencies": {
40
- "kawapress": "0.0.1"
40
+ "kawapress": "0.1.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "happy-dom": "^20.11.6",
@@ -0,0 +1,267 @@
1
+ @layer kawa.plugin {
2
+ .kawa-code-block {
3
+ position: relative;
4
+ margin: 1rem 0;
5
+ overflow: hidden;
6
+ border: 1px solid var(--kawa-c-divider);
7
+ border-radius: var(--kawa-radius);
8
+ background: var(--kawa-c-bg-soft);
9
+ }
10
+
11
+ .kawa-code-block__body {
12
+ position: relative;
13
+ }
14
+
15
+ .kawa-code-block pre {
16
+ width: 100%;
17
+ /* Top padding leaves room for the language label and the copy button. */
18
+ margin: 0;
19
+ padding: 2.875rem 1.5rem 1.25rem;
20
+ overflow-x: auto;
21
+ border: 0;
22
+ border-radius: 0;
23
+ text-align: left;
24
+ tab-size: 4;
25
+ }
26
+
27
+ .kawa-code-block pre > code {
28
+ display: block;
29
+ width: fit-content;
30
+ min-width: 100%;
31
+ color: var(--kawa-c-text-2);
32
+ font-size: 0.875em;
33
+ line-height: 1.5;
34
+ }
35
+
36
+ .kawa-code-block code :is(mark.line, ins.line, del.line) {
37
+ color: inherit;
38
+ text-decoration: none;
39
+ background: transparent;
40
+ }
41
+
42
+ .kawa-code-block code .line:is(.highlighted, .diff) {
43
+ position: relative;
44
+ display: inline-block;
45
+ width: calc(100% + 3rem);
46
+ margin: 0 -1.5rem;
47
+ padding: 0 1.5rem;
48
+ transition: background-color 160ms cubic-bezier(0.22, 1, 0.36, 1);
49
+ }
50
+
51
+ .kawa-code-block code .line.highlighted {
52
+ background: color-mix(in oklab, var(--kawa-c-brand) 12%, transparent);
53
+ }
54
+
55
+ .kawa-code-block code .line.highlighted.info {
56
+ background: color-mix(in oklab, var(--kawa-c-info) 12%, transparent);
57
+ }
58
+
59
+ .kawa-code-block code .line.highlighted.warning {
60
+ background: color-mix(in oklab, var(--kawa-c-warning) 14%, transparent);
61
+ }
62
+
63
+ .kawa-code-block code .line.highlighted.warning::before {
64
+ position: absolute;
65
+ left: 0.625rem;
66
+ color: var(--kawa-c-warning);
67
+ font-weight: 700;
68
+ content: '!';
69
+ }
70
+
71
+ .kawa-code-block code .line.highlighted.error {
72
+ background: color-mix(in oklab, var(--kawa-c-danger) 13%, transparent);
73
+ }
74
+
75
+ .kawa-code-block code .line.highlighted.error::before {
76
+ position: absolute;
77
+ left: 0.625rem;
78
+ color: var(--kawa-c-danger);
79
+ font-weight: 700;
80
+ content: '×';
81
+ }
82
+
83
+ .kawa-code-block code .line.diff::before {
84
+ position: absolute;
85
+ left: 0.625rem;
86
+ font-weight: 600;
87
+ }
88
+
89
+ .kawa-code-block code .line.diff.add {
90
+ background: color-mix(in oklab, var(--kawa-c-success) 13%, transparent);
91
+ }
92
+
93
+ .kawa-code-block code .line.diff.add::before {
94
+ color: var(--kawa-c-success);
95
+ content: '+';
96
+ }
97
+
98
+ .kawa-code-block code .line.diff.remove {
99
+ background: color-mix(in oklab, var(--kawa-c-danger) 13%, transparent);
100
+ opacity: 0.72;
101
+ }
102
+
103
+ .kawa-code-block code .line.diff.remove::before {
104
+ color: var(--kawa-c-danger);
105
+ content: '-';
106
+ }
107
+
108
+ .kawa-code-block .has-focused-lines .line:not(.has-focus) {
109
+ filter: blur(0.095rem);
110
+ opacity: 0.45;
111
+ transition:
112
+ filter 200ms cubic-bezier(0.22, 1, 0.36, 1),
113
+ opacity 200ms cubic-bezier(0.22, 1, 0.36, 1);
114
+ }
115
+
116
+ .kawa-code-block:is(:hover, :focus-within)
117
+ .has-focused-lines .line:not(.has-focus) {
118
+ filter: blur(0);
119
+ opacity: 1;
120
+ }
121
+
122
+ .kawa-code-block__language {
123
+ position: absolute;
124
+ z-index: 1;
125
+ top: 0.625rem;
126
+ left: 1.5rem;
127
+ max-width: calc(100% - 5rem);
128
+ overflow: hidden;
129
+ color: var(--kawa-c-text-3);
130
+ font-size: 0.75rem;
131
+ font-weight: 600;
132
+ line-height: 2rem;
133
+ text-overflow: ellipsis;
134
+ white-space: nowrap;
135
+ pointer-events: none;
136
+ }
137
+
138
+ .kawa-code-block__copy {
139
+ position: absolute;
140
+ z-index: 2;
141
+ top: 0.75rem;
142
+ right: 0.75rem;
143
+ width: 2rem;
144
+ height: 2rem;
145
+ color: var(--kawa-c-text-3);
146
+ border: 1px solid transparent;
147
+ border-radius: 0.375rem;
148
+ background: transparent;
149
+ transition:
150
+ color 160ms cubic-bezier(0.22, 1, 0.36, 1),
151
+ border-color 160ms cubic-bezier(0.22, 1, 0.36, 1),
152
+ background-color 160ms cubic-bezier(0.22, 1, 0.36, 1);
153
+ }
154
+
155
+ .kawa-code-block__copy:hover {
156
+ color: var(--kawa-c-text-1);
157
+ border-color: var(--kawa-c-divider);
158
+ background: var(--kawa-c-bg-soft);
159
+ }
160
+
161
+ .kawa-code-block__copy:focus-visible {
162
+ outline: 2px solid var(--kawa-c-brand);
163
+ outline-offset: 0.125rem;
164
+ }
165
+
166
+ .kawa-code-block__copy::before,
167
+ .kawa-code-block__copy::after {
168
+ position: absolute;
169
+ top: 0.5rem;
170
+ left: 0.5rem;
171
+ width: 1rem;
172
+ height: 1rem;
173
+ background-color: currentcolor;
174
+ content: '';
175
+ mask-position: center;
176
+ mask-repeat: no-repeat;
177
+ mask-size: contain;
178
+ transition:
179
+ opacity 160ms cubic-bezier(0.22, 1, 0.36, 1),
180
+ transform 160ms cubic-bezier(0.22, 1, 0.36, 1);
181
+ }
182
+
183
+ .kawa-code-block__copy::before {
184
+ mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256'%3E%3Cpath d='M216 32H88a8 8 0 0 0-8 8v40H40a8 8 0 0 0-8 8v128a8 8 0 0 0 8 8h128a8 8 0 0 0 8-8v-40h40a8 8 0 0 0 8-8V40a8 8 0 0 0-8-8m-56 176H48V96h112Zm48-48h-32V88a8 8 0 0 0-8-8H96V48h112Z'/%3E%3C/svg%3E");
185
+ }
186
+
187
+ .kawa-code-block__copy::after {
188
+ opacity: 0.2;
189
+ mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256'%3E%3Cpath d='M216 40v128h-48V88H88V40Z'/%3E%3C/svg%3E");
190
+ }
191
+
192
+ .kawa-code-block__copy[data-copied='true'] {
193
+ color: var(--kawa-c-success);
194
+ }
195
+
196
+ .kawa-code-block__copy[data-copied='true']::before {
197
+ top: 0.5rem;
198
+ left: 0.75rem;
199
+ width: 0.4375rem;
200
+ height: 0.75rem;
201
+ border: solid currentcolor;
202
+ border-width: 0 1.5px 1.5px 0;
203
+ background-color: transparent;
204
+ mask-image: none;
205
+ transform: rotate(45deg);
206
+ }
207
+
208
+ .kawa-code-block__copy[data-copied='true']::after {
209
+ opacity: 0;
210
+ transform: scale(0.7);
211
+ }
212
+
213
+ .kawa-code-block__copy-status {
214
+ position: absolute;
215
+ width: 1px;
216
+ height: 1px;
217
+ padding: 0;
218
+ overflow: hidden;
219
+ clip: rect(0 0 0 0);
220
+ white-space: nowrap;
221
+ border: 0;
222
+ }
223
+
224
+ .kawa-code-block__line-numbers {
225
+ --kawa-code-block-line-number-width: max(
226
+ 3rem,
227
+ calc(var(--kawa-code-block-line-number-digits, 1) * 0.625em + 1.5rem)
228
+ );
229
+
230
+ position: absolute;
231
+ top: 2.875rem;
232
+ bottom: 1.25rem;
233
+ left: 0;
234
+ width: var(--kawa-code-block-line-number-width);
235
+ color: var(--kawa-c-text-3);
236
+ font-family: var(--kawa-font-mono);
237
+ font-size: 0.875em;
238
+ line-height: 1.5;
239
+ text-align: right;
240
+ pointer-events: none;
241
+ user-select: none;
242
+ }
243
+
244
+ .kawa-code-block__line-number {
245
+ display: block;
246
+ padding-right: 0.75rem;
247
+ }
248
+
249
+ .kawa-code-block--line-numbers pre {
250
+ padding-left: calc(
251
+ max(
252
+ 3rem,
253
+ calc(var(--kawa-code-block-line-number-digits, 1) * 0.625em + 1.5rem)
254
+ ) + 1rem
255
+ );
256
+ }
257
+
258
+ @media (prefers-reduced-motion: reduce) {
259
+ .kawa-code-block code .line:is(.highlighted, .diff),
260
+ .kawa-code-block .has-focused-lines .line:not(.has-focus),
261
+ .kawa-code-block__copy,
262
+ .kawa-code-block__copy::before,
263
+ .kawa-code-block__copy::after {
264
+ transition: none;
265
+ }
266
+ }
267
+ }
package/src/index.ts CHANGED
@@ -3,7 +3,7 @@ import type {
3
3
  LocaleConfig,
4
4
  } from 'kawapress'
5
5
  import type { MarkdownExit } from 'markdown-exit'
6
- import { definePlugin } from 'kawapress'
6
+ import { definePlugin, resolveLocaleIndex } from 'kawapress'
7
7
 
8
8
  export interface CodeBlockCopyLabels {
9
9
  copy: string
@@ -256,7 +256,7 @@ function resolveCopyLabels(
256
256
  locales: Record<string, LocaleConfig>,
257
257
  options: CodeBlockPluginOptions,
258
258
  ): CodeBlockCopyLabels {
259
- const localeIndex = resolveLocaleIndex(path, locales)
259
+ const localeIndex = resolveLocaleIndex(path, Object.keys(locales))
260
260
  const language = locales[localeIndex]?.lang
261
261
  const builtIn = language?.toLowerCase().startsWith('zh')
262
262
  ? CHINESE_COPY_LABELS
@@ -269,20 +269,6 @@ function resolveCopyLabels(
269
269
  }
270
270
  }
271
271
 
272
- function resolveLocaleIndex(
273
- path: string,
274
- locales: Record<string, LocaleConfig>,
275
- ): string {
276
- const pathname = path.replace(/([?#].*)$/, '')
277
- return Object.keys(locales)
278
- .filter(locale => locale !== 'root')
279
- .sort((left, right) => right.length - left.length)
280
- .find((locale) => {
281
- const prefix = `/${locale}`
282
- return pathname === prefix || pathname.startsWith(`${prefix}/`)
283
- }) ?? 'root'
284
- }
285
-
286
272
  function normalizeLanguageLabels(
287
273
  labels: Record<string, string> | undefined,
288
274
  ): Record<string, string> {
@@ -1,4 +1,5 @@
1
1
  import { defineRuntimePlugin } from 'kawapress'
2
+ import './code-block.css'
2
3
 
3
4
  const COPY_BUTTON_SELECTOR = '.kawa-code-block__copy'
4
5
  const COPY_IGNORE_SELECTOR = '.kawa-code-block__copy-ignore, .diff.remove'