@varlet/vite-plugins 2.18.0-alpha.1697608760113 → 2.18.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/lib/index.js +6 -10
- package/package.json +8 -7
- package/src/markdown.ts +7 -11
package/lib/index.js
CHANGED
|
@@ -2,11 +2,15 @@
|
|
|
2
2
|
import markdownIt from "markdown-it";
|
|
3
3
|
import hljs from "highlight.js";
|
|
4
4
|
import { kebabCase } from "@varlet/shared";
|
|
5
|
+
import { pinyin } from "pinyin";
|
|
6
|
+
function transformHash(hash) {
|
|
7
|
+
return pinyin(hash, { style: "tone2" }).flat(Infinity).join("-").replaceAll(" ", "-");
|
|
8
|
+
}
|
|
5
9
|
function htmlWrapper(html2) {
|
|
6
10
|
const matches = html2.matchAll(/<h3>(.*?)<\/h3>/g);
|
|
7
11
|
const hGroup = html2.replace(/<h3>/g, () => {
|
|
8
|
-
const
|
|
9
|
-
return `:::<h3 id="${
|
|
12
|
+
const hash = transformHash(matches.next().value[1]);
|
|
13
|
+
return `:::<h3 id="${hash}"><router-link to="#${hash}">#</router-link>`;
|
|
10
14
|
}).replace(/<h2/g, ":::<h2").split(":::");
|
|
11
15
|
const cardGroup = hGroup.map((fragment) => fragment.includes("<h3") ? `<div class="card">${fragment}</div>` : fragment).join("");
|
|
12
16
|
return cardGroup.replace(/<code>/g, "<code v-pre>");
|
|
@@ -95,14 +99,6 @@ function markdown(options) {
|
|
|
95
99
|
} catch (e) {
|
|
96
100
|
this.error(e);
|
|
97
101
|
}
|
|
98
|
-
},
|
|
99
|
-
async handleHotUpdate(ctx) {
|
|
100
|
-
if (!/\.md$/.test(ctx.file))
|
|
101
|
-
return;
|
|
102
|
-
const readSource = ctx.read;
|
|
103
|
-
ctx.read = async function() {
|
|
104
|
-
return markdownToVue(await readSource(), options);
|
|
105
|
-
};
|
|
106
102
|
}
|
|
107
103
|
};
|
|
108
104
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@varlet/vite-plugins",
|
|
3
|
-
"version": "2.18.0
|
|
3
|
+
"version": "2.18.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "vite plugins of varlet",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -25,16 +25,17 @@
|
|
|
25
25
|
"fs-extra": "^9.0.1",
|
|
26
26
|
"highlight.js": "^10.7.2",
|
|
27
27
|
"markdown-it": "^12.2.3",
|
|
28
|
-
"
|
|
28
|
+
"pinyin": "3.0.0-alpha.4",
|
|
29
|
+
"@varlet/shared": "2.18.0"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"tsup": "7.2.0",
|
|
32
|
-
"vite": "4.3.5",
|
|
33
|
-
"typescript": "^5.1.5",
|
|
34
|
-
"@types/node": "^18.7.20",
|
|
35
32
|
"@types/ejs": "^3.1.1",
|
|
33
|
+
"@types/fs-extra": "^9.0.2",
|
|
36
34
|
"@types/markdown-it": "^12.2.3",
|
|
37
|
-
"@types/
|
|
35
|
+
"@types/node": "^18.7.20",
|
|
36
|
+
"tsup": "7.2.0",
|
|
37
|
+
"typescript": "^5.1.5",
|
|
38
|
+
"vite": "4.3.5"
|
|
38
39
|
},
|
|
39
40
|
"scripts": {
|
|
40
41
|
"dev": "tsup src/index.ts --format esm --out-dir=lib --watch --dts",
|
package/src/markdown.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import markdownIt from 'markdown-it'
|
|
2
2
|
import hljs from 'highlight.js'
|
|
3
3
|
import { kebabCase } from '@varlet/shared'
|
|
4
|
+
import { pinyin } from 'pinyin'
|
|
4
5
|
import type { Plugin } from 'vite'
|
|
5
6
|
|
|
7
|
+
function transformHash(hash: string) {
|
|
8
|
+
return pinyin(hash, { style: 'tone2' }).flat(Infinity).join('-').replaceAll(' ', '-')
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
function htmlWrapper(html: string) {
|
|
7
12
|
const matches = html.matchAll(/<h3>(.*?)<\/h3>/g)
|
|
8
13
|
const hGroup = html
|
|
9
14
|
.replace(/<h3>/g, () => {
|
|
10
|
-
const
|
|
15
|
+
const hash = transformHash(matches.next().value[1])
|
|
11
16
|
|
|
12
|
-
return `:::<h3 id="${
|
|
17
|
+
return `:::<h3 id="${hash}"><router-link to="#${hash}">#</router-link>`
|
|
13
18
|
})
|
|
14
19
|
.replace(/<h2/g, ':::<h2')
|
|
15
20
|
.split(':::')
|
|
@@ -135,14 +140,5 @@ export function markdown(options: MarkdownOptions): Plugin {
|
|
|
135
140
|
this.error(e)
|
|
136
141
|
}
|
|
137
142
|
},
|
|
138
|
-
|
|
139
|
-
async handleHotUpdate(ctx) {
|
|
140
|
-
if (!/\.md$/.test(ctx.file)) return
|
|
141
|
-
|
|
142
|
-
const readSource = ctx.read
|
|
143
|
-
ctx.read = async function () {
|
|
144
|
-
return markdownToVue(await readSource(), options)
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
143
|
}
|
|
148
144
|
}
|