Skip to content

Commit 1e00a93

Browse files
author
vhtmui
committed
docs: 更新代码块语言标识并优化文档显示
- 将 PowerShell 文档中的代码块标识从 'Powershell' 改为 'powershell' - 将 Web 文档中的代码块标识从 'js' 改为 'javascript',CSS 标识从 'CSS' 改为 'css' - 统一代码块语法标识以提高语法高亮准确性 feat: 优化代码复制功能防止页面跳转 - 在复制文本时添加固定定位样式防止页面自动滚动 - 使用 preventScroll 选项避免焦点切换导致的页面跳动 - 添加异常处理确保临时元素正确移除 style: 改进代码块预览组件样式 - 为代码块添加边框样式增强视觉效果 - 修改按钮样式为 outline 变体提升外观 - 添加悬停显示复制按钮的 CSS 规则 - 调整代码块内边距预留按钮空间 refactor: 优化文档页面加载和语法高亮 - 移除服务端的标题提取逻辑简化数据传递 - 根据文档名称动态加载对应的语法高亮语言包 - PowerShell 文档加载 powershell 语言支持 - Web 文档加载 javascript、html、css 语言支持 - Rust 文档加载 rust 语言支持 build: 支持通过环境变量配置基础路径 - 将硬编码的基础路径改为环境变量读取 - 允许通过 BASE_PATH 环境变量自定义部署路径
1 parent 436909a commit 1e00a93

7 files changed

Lines changed: 75 additions & 43 deletions

File tree

docs/PowerShell.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ vscode中默认使用UTF8编码,powershell旧版本中默认使用系统编码
1414

1515
输入Get-Help查找帮助时
1616

17-
```Powershell
17+
```powershell
1818
Get-Help Get-FTPChildItem
1919
```
2020

2121
输出内容信息大多为空,此时应先导入PSFTP模块
2222

23-
```Powershell
23+
```powershell
2424
Import-Module PSFTP
2525
```
2626

docs/Web.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
带标签的模版形式如:
1010

11-
```js
11+
```javascript
1212
`我需要做:
1313
${todos}
1414
当前进度为:${progress}
@@ -17,7 +17,7 @@
1717

1818
带标签的模板字面量是一个函数调用的语法糖,上文语法表示实际如下:
1919

20-
```js
20+
```javascript
2121
fn`我需要做:
2222
${todos}
2323
当前进度为:${progress}
@@ -52,7 +52,7 @@
5252
* `valuel`即迭代产生的值
5353
* 可使用生成器函数快速创建一个可迭代对象。如:
5454

55-
```js
55+
```javascript
5656
//example1 生成器函数直接生成可迭代对象
5757
function* generator(i) {
5858
yield i;
@@ -129,7 +129,7 @@
129129

130130
JS异步函数会立刻返回一个 `promise``promise`拥有一个状态。可以对返回的 `promise`对象调用 `then()`方法,`then()`方法接收一个函数,`promise`会用使用**原异步函数调用`resolve`函数的参数**作为参数调用这个函数。如*Example 1*
131131

132-
```js
132+
```javascript
133133
//Example 1
134134
// 使用Promise构造函数创建一个promise对象
135135
// resolve函数的参数"done"将在fulfillment时返回,并作为调用then方法的参数
@@ -141,7 +141,7 @@ promise.then((result) => {
141141

142142
使用 `asycn`关键字注释的函数可以在函数内部使用 `await`关键字调用异步函数,使异步函数直接返回 `fulfillment`,而不是先返回 `promise`对象。`asycn\await`写法也更直观,看起来和同步函数一样。如*Example 2*:
143143

144-
```js
144+
```javascript
145145
//Example 2
146146
function alarm(person, delay) {
147147
return new Promise((resolve, reject) => {
@@ -177,7 +177,7 @@ button.addEventListener("click", async () => {//使用async关键字
177177
2. 请求成功时存储数据库对象 `db = openRequest.result;`
178178
3. 创建数据库表,新建自增列:
179179

180-
```JS
180+
```javascript
181181
const objectStore = db.createObjectStore("notes_os", {//创建一个新表命名为notes_os
182182
keyPath: "id",//创建自增列
183183
autoIncrement: true,
@@ -186,7 +186,7 @@ button.addEventListener("click", async () => {//使用async关键字
186186

187187
4. 创建新的列字段,创建了 `title``body`字段(列)。
188188

189-
```js
189+
```javascript
190190
objectStore.createIndex("title", "title", { unique: false });
191191
objectStore.createIndex("body", "body", { unique: false });
192192
```
@@ -209,7 +209,7 @@ button.addEventListener("click", async () => {//使用async关键字
209209

210210
Proxy用于拦截一些操作,替换成自定义实现。如:
211211

212-
```js
212+
```javascript
213213
const handler = {
214214
get(target, property){
215215
return name in target ? target[name] : 42;
@@ -226,7 +226,7 @@ proxy操作不是随意的,需要保持`不可拓展对象`和`不可配置属
226226

227227
对象的原型为其`[[Propotype]]`属性,由构造器对象的`prototype`属性定义。即:
228228

229-
```js
229+
```javascript
230230
let s = new String('a');
231231
Object.getPrototypeOf(s) === String.prototype;
232232
```
@@ -243,7 +243,7 @@ Object.getPrototypeOf(s) === String.prototype;
243243

244244
#### 组合选择器
245245

246-
```CSS
246+
```css
247247
.a{
248248
.b {} /* 相当于 .a .b 即属于class="a"的子元素,且自身class="b"的元素 */
249249
&.b {} /* 相当于 .a.b 即class="a b"或"b a" */
@@ -256,7 +256,7 @@ Object.getPrototypeOf(s) === String.prototype;
256256

257257
#### 后附嵌套选择器
258258

259-
```CSS
259+
```css
260260
.a{
261261
.b& {} /* 相当于 .b.a 即class="a b"或"b a" */
262262
.b & {} /* 相当于 .b .a 即属于class="b"的子元素,且自身class="a"的元素 */
@@ -268,7 +268,7 @@ Object.getPrototypeOf(s) === String.prototype;
268268

269269
### 优先级
270270

271-
一条CSS声明的优先级分三层判断
271+
一条css声明的优先级分三层判断
272272

273273
* 来源,用户代理、用户样式、作者样式优先级从低到高按如下排序。
274274
1. 用户代理普通样式
@@ -588,7 +588,7 @@ sveltekit完成一次任务需要经过如下三步
588588
1. 安装静态设配器 `npm i -D @sveltejs/adapter-static`
589589
2. 配置根目录的svelte.config.js文件:
590590

591-
```js
591+
```javascript
592592
import adapter from '@sveltejs/adapter-static';
593593

594594
/** @type {import('@sveltejs/kit').Config} */
@@ -617,7 +617,7 @@ sveltekit完成一次任务需要经过如下三步
617617

618618
3. 配置src文件夹下的+layout.js文件
619619

620-
```js
620+
```javascript
621621
export const prerender = true;
622622
```
623623

@@ -699,7 +699,7 @@ sveltekit完成一次任务需要经过如下三步
699699

700700
函数如下,函数接受`event`对象,`resolve`函数。返回一个`Response`
701701

702-
```js
702+
```javascript
703703
export async function handle({ event, resolve }) {
704704
return await resolve(event);
705705
}

src/lib/my-utils/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,27 @@ export async function copyToClipboard(text) {
3535
console.error('Failed to copy text: ', err);
3636
}
3737
} else {
38-
// 降级到传统方法
38+
// 降级到传统方法(避免自动滚动页面)
3939
const textArea = document.createElement('textarea');
4040
textArea.value = text;
41+
textArea.style.position = 'fixed';
42+
textArea.style.left = '-9999px';
43+
textArea.style.top = '0';
44+
textArea.style.opacity = '0';
45+
textArea.style.pointerEvents = 'none';
46+
47+
const previousActive = document.activeElement instanceof HTMLElement ? document.activeElement : null;
4148
document.body.appendChild(textArea);
42-
textArea.focus();
49+
textArea.focus({ preventScroll: true });
4350
textArea.select();
51+
4452
try {
4553
document.execCommand('copy');
4654
} catch (err) {
4755
console.error('Failed to copy text: ', err);
56+
} finally {
57+
document.body.removeChild(textArea);
58+
if (previousActive) previousActive.focus({ preventScroll: true });
4859
}
49-
document.body.removeChild(textArea);
5060
}
5161
}

src/routes/docs/MyMarkdown/Pre.svelte

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,28 @@
3030
<pre
3131
contenteditable="false"
3232
bind:this={pre}
33-
class={cn('', className)}
33+
class={cn('border-2', className)}
3434
{...rest}>{@render children?.()}</pre>
3535
<Button
3636
size="icon"
37-
variant="ghost"
38-
class="absolute top-2 right-2"
37+
variant="outline"
38+
class="local-btn absolute top-2 right-2"
3939
disabled={buttonState.copied}
4040
onclick={handleClick}><CopyIcon /></Button
4141
>
4242
</div>
43+
44+
<style>
45+
pre {
46+
padding-right: 3rem;
47+
scrollbar-width: thin;
48+
}
49+
50+
.relative:hover :global(.local-btn) {
51+
display: inline-flex
52+
}
53+
54+
.relative :global(.local-btn) {
55+
display: none;
56+
}
57+
</style>

src/routes/docs/[...docs]/+page.server.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,7 @@ import { readFile } from 'node:fs/promises';
22

33
/** @type {import('./$types').PageServerLoad} */
44
export async function load({ params }) {
5-
const content = await readFile(`docs/${params.docs}.md`, 'utf-8');
5+
const content = await readFile(`docs/${params.docs}.md`, 'utf-8');
66

7-
// 提取标题
8-
const headings = [];
9-
const lines = content.split(/\r?\n/);
10-
lines.forEach((line) => {
11-
const match = line.match(/^(#{1,6})\s+(.+)$/);
12-
if (match) {
13-
const level = match[1].length;
14-
const text = match[2].trim();
15-
const id = text.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]/g, '');
16-
headings.push({ level, text, id });
17-
}
18-
});
19-
20-
return { content, headings };
21-
}
7+
return { content, docName: params.docs };
8+
}

src/routes/docs/[...docs]/+page.svelte

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,29 @@
1313
let { data } = $props();
1414
let md = $derived(data.content);
1515
16-
const shikiPluginPromise = createHighlighterCore({
16+
const docName = $derived(data.docName);
17+
18+
function getLangs() {
19+
switch (true) {
20+
case docName.match(/powershell/i) && true:
21+
return [import('shiki/langs/powershell.mjs')];
22+
case docName.match(/rust/i) && true:
23+
return [import('shiki/langs/rust.mjs')];
24+
case docName.match(/web/i) && true:
25+
return [
26+
import('shiki/langs/javascript.mjs'),
27+
import('shiki/langs/html.mjs'),
28+
import('shiki/langs/css.mjs')
29+
];
30+
default:
31+
return [];
32+
}
33+
}
34+
35+
36+
const shikiPluginPromise = $derived(createHighlighterCore({
1737
themes: [import('shiki/themes/slack-dark.mjs'), import('shiki/themes/slack-ochin.mjs')],
18-
langs: [import('shiki/langs/rust.mjs')],
38+
langs: [...getLangs()],
1939
engine: createOnigurumaEngine(import('shiki/wasm'))
2040
}).then((highlighter) => {
2141
return {
@@ -25,7 +45,7 @@
2545
{ themes: { light: 'slack-ochin', dark: 'slack-dark' } }
2646
]
2747
};
28-
});
48+
}));
2949
3050
/** @type {import('svelte-exmarkdown').Plugin} */
3151
const csPlugin = {

svelte.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import adapter from '@sveltejs/adapter-static';
22

3-
const base = process.argv.includes('dev') ? '' : '/vhtmui.github.io';
3+
const base = process.argv.includes('dev') ? '' : process.env.BASE_PATH;
44

55
/** @type {import('@sveltejs/kit').Config} */
66
const config = {

0 commit comments

Comments
 (0)