Skip to content

Commit

Permalink
fix: #22 优化去除链接规则
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 18, 2023
1 parent 075902e commit eec21f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import KernelApi from "../api/kernel-api"
import {dataDir} from "../Constants"
import { dataDir } from "../Constants"

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

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

if (!imagePath.startsWith(dataDir)) {
return match;
return match
}

const relativePath = convertPathToUnixStyle(
imagePath.substring(dataDir.length)
);
const relativePath = convertPathToUnixStyle(imagePath.substring(dataDir.length))

return `![${p1}](${relativePath})`;
});
return `![${p1}](${relativePath})`
})
}

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

// 删除目录中的内部链接
export function removeLinks(text) {
const regex = /\[([^\]]+)]\(([^)]+)\)/g
return text.replace(regex, (match, p1, p2) => {
if (p2.includes('./Text')) {
return text.replace(regex, (match, p1, p2) => {
console.log(p2)
console.log(typeof p2)
if (p2.includes("./Text") || p2.includes("#") || p2.includes("kindle:")) {
return p1
} else {
return match
}
})
}
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default defineConfig({
// 或是用来指定是应用哪种混淆器
// boolean | 'terser' | 'esbuild'
// 不压缩,用于调试
minify: isWatch,
minify: !isWatch,

lib: {
// Could also be a dictionary or array of multiple entry points
Expand Down

0 comments on commit eec21f5

Please sign in to comment.