Skip to content

Commit 824cf2a

Browse files
committed
feat: #20 导入epub后,里面的图片会出现在未引用资源里面,容易被误清除
1 parent a9fcaa8 commit 824cf2a

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,5 @@
3535
"vite": "^4.3.7",
3636
"vite-plugin-static-copy": "^0.15.0",
3737
"vitest": "^0.31.1"
38-
},
39-
"dependencies": {
40-
"shorthash2": "^1.0.3"
4138
}
4239
}

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
* questions.
2424
*/
2525

26+
export const workspaceDir = `${(window as any).siyuan.config.system.workspaceDir}`
2627
export const dataDir = `${(window as any).siyuan.config.system.dataDir}`
27-
export const mediaDir = `${dataDir}/assets/import`
28+
export const mediaDir = `./assets`
2829
export const isDev = process.env.DEV_MODE === "true"
2930
export const siyuanApiUrl = ""
3031
export const siyuanApiToken = ""

src/api/kernel-api.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
*/
2525

2626
import { BaseApi, SiyuanData } from "./base-api"
27-
import {mediaDir, siyuanApiToken, siyuanApiUrl} from "../Constants"
27+
import { mediaDir, siyuanApiToken, siyuanApiUrl } from "../Constants"
2828
import { fetchPost } from "siyuan"
29-
import shortHash from "shorthash2";
3029

3130
/**
3231
* 思源笔记服务端API v2.8.8
@@ -80,6 +79,20 @@ class KernelApi extends BaseApi {
8079
})
8180
}
8281

82+
public async saveTextData(fileName: string, data: any) {
83+
return new Promise((resolve) => {
84+
const pathString = `/temp/convert/pandoc/${fileName}`
85+
const file = new File([new Blob([data])], pathString.split("/").pop())
86+
const formData = new FormData()
87+
formData.append("path", pathString)
88+
formData.append("file", file)
89+
formData.append("isDir", "false")
90+
fetchPost("/api/file/putFile", formData, (response) => {
91+
resolve(response)
92+
})
93+
})
94+
}
95+
8396
/**
8497
* 转换服务
8598
*
@@ -88,9 +101,8 @@ class KernelApi extends BaseApi {
88101
* @param to - 转换后的文件名,不包括路径,路径相对于 /temp/convert/pandoc
89102
*/
90103
public async convertPandoc(type: string, from: string, to: string): Promise<SiyuanData> {
91-
const toMediaDir = [mediaDir, shortHash(to).toLowerCase()].join("/")
92104
const args = {
93-
args: ["--to", type, from, "-o", to, "--extract-media", toMediaDir],
105+
args: ["--to", type, from, "-o", to, "--extract-media", mediaDir],
94106
}
95107
return await this.siyuanRequest("/api/convert/pandoc", args)
96108
}
@@ -123,7 +135,7 @@ class KernelApi extends BaseApi {
123135
}
124136

125137
/**
126-
* 通过Markdown创建文档
138+
* 删除文件
127139
*
128140
* @param path - 路径
129141
*/
@@ -135,7 +147,7 @@ class KernelApi extends BaseApi {
135147
}
136148

137149
/**
138-
* 通过Markdown创建文档
150+
* 通过 Markdown 创建文档
139151
*
140152
* @param notebook - 笔记本
141153
* @param path - 路径
@@ -149,6 +161,23 @@ class KernelApi extends BaseApi {
149161
}
150162
return await this.siyuanRequest("/api/filetree/createDocWithMd", params)
151163
}
164+
165+
/**
166+
* 导入 Markdown 文件
167+
*
168+
* @param localPath - 本地 MD 文档绝对路径
169+
* @param notebook - 笔记本
170+
* @param path - 路径
171+
*/
172+
public async importStdMd(localPath, notebook: string, path: string): Promise<SiyuanData> {
173+
const params = {
174+
// Users/terwer/Documents/mydocs/SiYuanWorkspace/public/temp/convert/pandoc/西蒙学习法:如何在短时间内快速学会新知识-友荣方略.md
175+
localPath: localPath,
176+
notebook: notebook,
177+
toPath: path,
178+
}
179+
return await this.siyuanRequest("/api/import/importStdMd", params)
180+
}
152181
}
153182

154183
export default KernelApi

src/lib/ImportForm.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import { removeEmptyLines, removeFootnotes, removeLinks, replaceImagePath } from "../utils/utils"
3030
import { onMount } from "svelte"
3131
import { loadImporterConfig, saveImporterConfig } from "../store/config"
32-
import { isDev } from "../Constants"
32+
import { isDev, workspaceDir } from "../Constants"
3333
3434
export let pluginInstance: ImporterPlugin
3535
export let dialog
@@ -104,9 +104,13 @@
104104
mdText = replaceImagePath(mdText)
105105
// 去除脚注
106106
mdText = removeFootnotes(mdText)
107+
await pluginInstance.kernelApi.saveTextData(`${toFilename}`, mdText)
107108
108109
// 创建 MD 文档
109-
const mdResult = await pluginInstance.kernelApi.createDocWithMd(toNotebookId, `/${filename}`, mdText)
110+
// const mdResult = await pluginInstance.kernelApi.createDocWithMd(toNotebookId, `/${filename}`, mdText)
111+
// 导入 MD 文档
112+
const localPath = `${workspaceDir}/temp/convert/pandoc/${toFilename}`
113+
const mdResult = await pluginInstance.kernelApi.importStdMd(localPath, toNotebookId, `/`)
110114
if (mdResult.code !== 0) {
111115
showMessage(`${pluginInstance.i18n.msgDocCreateFailed}=>${toFilePath}`, 7000, "error")
112116
}

src/utils/utils.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,15 @@ export const isFileExists = async (kernelApi: KernelApi, p: string, type: "text"
6868
// 保留图片
6969
export const removeEmptyLines = (str: string): string => str.replace(/^#+\s*\n|\n\u00A0+/gm, "\n")
7070

71+
function convertPathToUnixStyle(path) {
72+
// 使用 replace() 函数将所有反斜杠替换为斜杠
73+
return path.replace(/\\/g, "/")
74+
}
7175
/**
7276
* 修复图片路径
7377
*
7478
* @param mdText - markdown 文本
7579
*/
76-
function convertPathToUnixStyle(path) {
77-
// 使用 replace() 函数将所有反斜杠替换为斜杠
78-
return path.replace(/\\/g, "/")
79-
}
80-
8180
export function replaceImagePath(mdText) {
8281
const regex = /!\[(.*?)\]\(([^\s]*?)\)/g
8382
return mdText.replace(regex, (match, p1, p2) => {
@@ -103,8 +102,6 @@ export function removeFootnotes(text) {
103102
export function removeLinks(text) {
104103
const regex = /\[([^\]]+)]\(([^)]+)\)/g
105104
return text.replace(regex, (match, p1, p2) => {
106-
console.log(p2)
107-
console.log(typeof p2)
108105
if (p2.includes("./Text") || p2.includes("#") || p2.includes("kindle:")) {
109106
return p1
110107
} else {

0 commit comments

Comments
 (0)