Skip to content

Commit d837c78

Browse files
authored
Merge pull request #25 from terwer/dev
fix: #22 优化去除链接规则
2 parents 69ca57a + 46c43e5 commit d837c78

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.DS_Store
33
node_modules
44
dist
5-
build
5+
build
6+
__pycache__

src/api/kernel-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class KernelApi extends BaseApi {
8888
* @param to - 转换后的文件名,不包括路径,路径相对于 /temp/convert/pandoc
8989
*/
9090
public async convertPandoc(type: string, from: string, to: string): Promise<SiyuanData> {
91-
const toMediaDir = [mediaDir, shortHash(to)].join("/")
91+
const toMediaDir = [mediaDir, shortHash(to).toLowerCase()].join("/")
9292
const args = {
9393
args: ["--to", type, from, "-o", to, "--extract-media", toMediaDir],
9494
}

src/utils/utils.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525

2626
import KernelApi from "../api/kernel-api"
27-
import {dataDir} from "../Constants"
27+
import { dataDir } from "../Constants"
2828

2929
/**
3030
* 文件是否存在
@@ -75,40 +75,40 @@ export const removeEmptyLines = (str: string): string => str.replace(/^#+\s*\n|\
7575
*/
7676
function convertPathToUnixStyle(path) {
7777
// 使用 replace() 函数将所有反斜杠替换为斜杠
78-
return path.replace(/\\/g, '/');
78+
return path.replace(/\\/g, "/")
7979
}
8080

8181
export function replaceImagePath(mdText) {
82-
const regex = /!\[(.*?)\]\(([^\s]*?)\)/g;
82+
const regex = /!\[(.*?)\]\(([^\s]*?)\)/g
8383
return mdText.replace(regex, (match, p1, p2) => {
84-
let imagePath = p2;
84+
const imagePath = p2
8585

8686
if (!imagePath.startsWith(dataDir)) {
87-
return match;
87+
return match
8888
}
8989

90-
const relativePath = convertPathToUnixStyle(
91-
imagePath.substring(dataDir.length)
92-
);
90+
const relativePath = convertPathToUnixStyle(imagePath.substring(dataDir.length))
9391

94-
return `![${p1}](${relativePath})`;
95-
});
92+
return `![${p1}](${relativePath})`
93+
})
9694
}
9795

9896
// 将字符串中形如"xxx^yyy"的部分替换成"xxx"
9997
export function removeFootnotes(text) {
100-
const regex = /\^\(\[.*[0-9].*\]\(\#.*\#.*\)\)/g; // 匹配格式为 ^[[数字]](#链接) 的脚注
101-
return text.replace(regex, ''); // 使用空字符串替换匹配到的脚注
98+
const regex = /\^\(\[.*[0-9].*]\(#.*#.*\)\)/g // 匹配格式为 ^[[数字]](#链接) 的脚注
99+
return text.replace(regex, "") // 使用空字符串替换匹配到的脚注
102100
}
103101

104102
// 删除目录中的内部链接
105103
export function removeLinks(text) {
106104
const regex = /\[([^\]]+)]\(([^)]+)\)/g
107-
return text.replace(regex, (match, p1, p2) => {
108-
if (p2.includes('./Text')) {
105+
return text.replace(regex, (match, p1, p2) => {
106+
console.log(p2)
107+
console.log(typeof p2)
108+
if (p2.includes("./Text") || p2.includes("#") || p2.includes("kindle:")) {
109109
return p1
110110
} else {
111111
return match
112112
}
113113
})
114-
}
114+
}

vite.config.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/// <reference types="vitest" />
22

3-
import {resolve} from "path"
4-
import {defineConfig, loadEnv} from "vite"
3+
import { resolve } from "path"
4+
import { defineConfig } from "vite"
55
import minimist from "minimist"
6-
import {viteStaticCopy} from "vite-plugin-static-copy"
6+
import { viteStaticCopy } from "vite-plugin-static-copy"
77
import livereload from "rollup-plugin-livereload"
8-
import {svelte} from "@sveltejs/vite-plugin-svelte"
9-
import fg from 'fast-glob'
8+
import { svelte } from "@sveltejs/vite-plugin-svelte"
9+
import fg from "fast-glob"
1010

1111
const args = minimist(process.argv.slice(2))
1212
const isWatch = args.watch || args.w || false
@@ -66,7 +66,7 @@ export default defineConfig({
6666
// 或是用来指定是应用哪种混淆器
6767
// boolean | 'terser' | 'esbuild'
6868
// 不压缩,用于调试
69-
minify: isWatch,
69+
minify: !isWatch,
7070

7171
lib: {
7272
// Could also be a dictionary or array of multiple entry points
@@ -76,20 +76,23 @@ export default defineConfig({
7676
formats: ["cjs"],
7777
},
7878
rollupOptions: {
79-
plugins: [...(isWatch ? [livereload(devDistDir), {
80-
//监听静态资源文件
81-
name: 'watch-external',
82-
async buildStart() {
83-
const files = await fg([
84-
'src/i18n/*.json',
85-
'./README*.md',
86-
'./plugin.json'
87-
]);
88-
for (let file of files) {
89-
this.addWatchFile(file);
90-
}
91-
}
92-
}] : [])] as Plugin[],
79+
plugins: [
80+
...(isWatch
81+
? [
82+
livereload(devDistDir),
83+
{
84+
//监听静态资源文件
85+
name: "watch-external",
86+
async buildStart() {
87+
const files = await fg(["src/i18n/*.json", "./README*.md", "./plugin.json"])
88+
for (const file of files) {
89+
this.addWatchFile(file)
90+
}
91+
},
92+
},
93+
]
94+
: []),
95+
] as Plugin[],
9396

9497
// make sure to externalize deps that shouldn't be bundled
9598
// into your library

0 commit comments

Comments
 (0)