Skip to content

Commit a478fbb

Browse files
committed
feat: HTML code preview is supported in markdown.
1 parent fdcfcb2 commit a478fbb

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

docs/chmod.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ $ chmod -R 755 my_directory
3131

3232
### Chmod 生成器
3333

34-
<div style="display: flex; justify-content: space-evenly; align-items: center;padding: 1rem 1rem;">
34+
```html preview
35+
<div>
3536
权限:<input type="text" id="num" placeholder="777" maxlength="3" style="padding: 0.3rem 0.3rem;" /> <input type="text" id="let" placeholder="rwxrwxrwx" maxlength="9" style="padding: 0.3rem 0.3rem;" />
3637
</div>
38+
```
3739

3840
---
3941

@@ -275,8 +277,10 @@ $ find /path -type f -exec chmod 644 {} \;
275277
* [使用 chmod 修改文件权限](https://www.linode.com/docs/guides/modify-file-permissions-with-chmod/) _(linode.com)_
276278

277279
<!--rehype:ignore:start-->
278-
### 以下是 Chmod 生成器 JS 代码(用于网站请忽略
280+
### 以下是 Chmod 生成器 JS 代码(代码用于网站请忽略不要删除
279281
<!--rehype:ignore:end-->
282+
283+
```html preview
280284
<!-- Chmod 生成器 JS 代码 -->
281285
<script type="text/javascript">
282286
const reg_num = /^[0-7]{3}$/; // 一些正则表达式来检查 num 输入
@@ -499,3 +503,4 @@ $ find /path -type f -exec chmod 644 {} \;
499503
return perm_num;
500504
}
501505
</script>
506+
```

docs/quickreference.md

+10
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ const school = <div>学校</div>;
228228
```
229229
<!--rehype:className=wrap-text -->
230230

231+
### HTML 代码预览
232+
233+
```
234+
```html preview
235+
这里是你的 HTML 代码
236+
\```
237+
```
238+
239+
上面的 `markdown` 代码在 `meta` 位置添加 `preview` 标识,HTML 代码将被执行预览
240+
231241

232242
布局
233243
---

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"keywords": [],
2121
"devDependencies": {
22-
"@wcj/markdown-to-html": "^2.1.0",
22+
"@wcj/markdown-to-html": "^2.1.1",
2323
"chokidar": "^3.5.3",
2424
"fs-extra": "^10.1.0",
2525
"recursive-readdir-files": "^2.3.0",

scripts/create.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { homeCardIcons } from './utils/homeCardIcons.mjs';
1212
import { getTocsTree } from './utils/getTocsTree.mjs';
1313
import { rehypeTitle } from './utils/rehypeTitle.mjs';
1414
import { anchorPoint } from './utils/anchorPoint.mjs';
15+
import { rehypePreviewHTML } from './utils/rehypePreviewHTML.mjs';
1516

1617
const favicon = `data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E`;
1718

@@ -41,6 +42,7 @@ export function create(str = '', options = {}) {
4142
}],
4243
],
4344
rewrite: (node, index, parent) => {
45+
rehypePreviewHTML(node, parent);
4446
rehypeTitle(node, options.filename);
4547
homeCardIcons(node, parent, options.isHome);
4648
tooltips(node, index, parent);

scripts/utils/getSVGNode.mjs

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ import rehypeParse from 'rehype-parse';
33
import {unified} from 'unified';
44
import { VFile } from 'vfile';
55

6-
export function getSVGNode(iconPath) {
6+
export function getSVGNode(iconPath, space = 'svg') {
77
const svgStr = fs.readFileSync(iconPath);
8-
const processor = unified().use(rehypeParse,{ fragment: true, space: "svg" })
8+
const processor = unified().use(rehypeParse,{ fragment: true, space })
99
const file = new VFile();
1010
file.value = svgStr.toString();
1111
const hastNode = processor.runSync(processor.parse(file), file);
1212
return hastNode.children || []
13+
}
14+
15+
export function getVNode(str = '', space = 'html') {
16+
const processor = unified().use(rehypeParse,{ fragment: true, space })
17+
const file = new VFile();
18+
file.value = str.toString();
19+
const hastNode = processor.runSync(processor.parse(file), file);
20+
return hastNode.children || []
1321
}

scripts/utils/rehypePreviewHTML.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getCodeString } from 'rehype-rewrite';
2+
import { getVNode } from './getSVGNode.mjs';
3+
4+
export function rehypePreviewHTML(node, parent) {
5+
if (node.type === 'element' && node.tagName === 'pre' && node.properties?.className?.includes('language-html')) {
6+
const child = node.children[0];
7+
if (child?.tagName === 'code' && child.data?.meta === 'preview') {
8+
const code = getCodeString(node.children)
9+
const vnode = getVNode(code || '')
10+
node.children = vnode
11+
}
12+
}
13+
14+
}

0 commit comments

Comments
 (0)