Skip to content

Commit 6e0ca9f

Browse files
committedJun 1, 2023
feat: 优化 pandoc 参数
1 parent b626917 commit 6e0ca9f

File tree

4 files changed

+123
-21
lines changed

4 files changed

+123
-21
lines changed
 

‎src/api/kernel-api.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class KernelApi extends BaseApi {
112112
* @param to - 转换后的文件名,不包括路径,路径相对于 /temp/convert/pandoc
113113
*/
114114
public async convertPandoc(type: string, from: string, to: string): Promise<SiyuanData> {
115-
const args = {
115+
const params = {
116116
args: [
117117
"--to",
118118
type,
@@ -124,7 +124,14 @@ class KernelApi extends BaseApi {
124124
"--wrap=none",
125125
],
126126
}
127-
return await this.siyuanRequest("/api/convert/pandoc", args)
127+
return await this.siyuanRequest("/api/convert/pandoc", params)
128+
}
129+
130+
public async convertPandocCustom(args: string[]): Promise<SiyuanData> {
131+
const params = {
132+
args: args,
133+
}
134+
return await this.siyuanRequest("/api/convert/pandoc", params)
128135
}
129136

130137
/**

‎src/lib/ImportForm.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@
149149
showMessage(`${pluginInstance.i18n.msgConverting} ${file.name}...`, 1000, "info")
150150
151151
// 转换
152-
const toFilePath = await ImportService.uploadAndConvert(pluginInstance, file)
152+
const uploadResult = await ImportService.uploadAndConvert(pluginInstance, file)
153153
// 导入
154-
await ImportService.singleImport(pluginInstance, toFilePath, toNotebookId)
154+
await ImportService.singleImport(pluginInstance, uploadResult.toFilePath, toNotebookId,uploadResult.isMd)
155155
}
156156
// =================
157157
// 单文件转换结束
@@ -324,7 +324,7 @@
324324
color: var(--b3-theme-primary);
325325
}
326326
327-
.config__item:hover .highlight {
327+
.b3-label__text:hover .highlight {
328328
display: block;
329329
}
330330
</style>

‎src/service/importService.ts

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
* questions.
2424
*/
2525

26-
import { workspaceDir } from "../Constants"
27-
import { showMessage } from "siyuan"
26+
import { mediaDir, workspaceDir } from "../Constants"
27+
import { getBackend, getFrontend, showMessage } from "siyuan"
2828
import ImporterPlugin from "../index"
29-
import { removeEmptyLines, removeFootnotes, removeLinks, replaceImagePath } from "../utils/utils"
29+
import { copyDir, isPC, removeEmptyLines, removeFootnotes, removeLinks, replaceImagePath } from "../utils/utils"
30+
import shortHash from "shorthash2"
3031

3132
export class ImportService {
3233
/**
@@ -51,15 +52,25 @@ export class ImportService {
5152
if (ext === "md") {
5253
const filePath = file.path
5354
pluginInstance.logger.info(`import md from ${filePath}`)
54-
return filePath
55+
return {
56+
toFilePath: filePath,
57+
isMd: true,
58+
}
5559
}
5660

5761
if (ext === "html") {
58-
// 仅在客户端复制资源文件
59-
const filePath = file.path
60-
const lastSlashIndex = filePath.lastIndexOf("/")
61-
const dirPath = filePath.substring(0, lastSlashIndex)
62-
pluginInstance.logger.info(`${dirPath}${originalFilename}_files`)
62+
if (isPC()) {
63+
pluginInstance.logger.info(`copying html assets...`)
64+
// 仅在客户端复制资源文件
65+
const filePath = file.path
66+
const lastSlashIndex = filePath.lastIndexOf("/")
67+
const dirPath = filePath.substring(0, lastSlashIndex)
68+
const path = window.require("path")
69+
const fullDirPath = path.join(dirPath, `${originalFilename}_files`)
70+
pluginInstance.logger.info("fullDirPath=>", fullDirPath)
71+
72+
await copyDir(fullDirPath, `${workspaceDir}/temp/convert/pandoc/${filename}_files`)
73+
}
6374
}
6475

6576
// =================================================
@@ -79,11 +90,32 @@ export class ImportService {
7990
}
8091

8192
// 文件转换
82-
const convertResult = await pluginInstance.kernelApi.convertPandoc(
93+
const args: string[] = [
94+
"--to",
8395
"markdown_strict-raw_html",
8496
fromFilename,
85-
toFilename
86-
)
97+
"-o",
98+
toFilename,
99+
"--extract-media",
100+
`${mediaDir}/${shortHash(fromFilename).toLowerCase()}`,
101+
"--wrap=none",
102+
]
103+
// const args = [
104+
// "--to",
105+
// "markdown_strict-raw_html",
106+
// fromFilename,
107+
// "-o",
108+
// toFilename,
109+
// "--extract-media",
110+
// `${mediaDir}/${shortHash(fromFilename).toLowerCase()}`,
111+
// "--wrap=none",
112+
// ]
113+
const convertResult = await pluginInstance.kernelApi.convertPandocCustom(args)
114+
// const convertResult = await pluginInstance.kernelApi.convertPandoc(
115+
// "markdown_strict-raw_html",
116+
// fromFilename,
117+
// toFilename
118+
// )
87119
if (convertResult.code !== 0) {
88120
showMessage(`${pluginInstance.i18n.msgFileConvertError}${convertResult.msg}`, 7000, "error")
89121
return
@@ -107,12 +139,18 @@ export class ImportService {
107139
mdText = removeFootnotes(mdText)
108140
await pluginInstance.kernelApi.saveTextData(`${toFilename}`, mdText)
109141

110-
return toFilePath
142+
return {
143+
toFilePath: toFilePath,
144+
isMd: false,
145+
}
111146
}
112147

113-
public static async singleImport(pluginInstance: ImporterPlugin, toFilePath: string, toNotebookId: string) {
114-
const ext = toFilePath.split(".").pop().toLowerCase()
115-
const isMd = ext === "md"
148+
public static async singleImport(
149+
pluginInstance: ImporterPlugin,
150+
toFilePath: string,
151+
toNotebookId: string,
152+
isMd: boolean
153+
) {
116154
// 导入 MD 文档
117155
const localPath = isMd ? toFilePath : `${workspaceDir}${toFilePath}`
118156
const mdResult = await pluginInstance.kernelApi.importStdMd(localPath, toNotebookId, `/`)

‎src/utils/utils.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import KernelApi from "../api/kernel-api"
2727
import { dataDir } from "../Constants"
28+
import { getBackend, getFrontend } from "siyuan"
2829

2930
/**
3031
* 文件是否存在
@@ -109,3 +110,59 @@ export function removeLinks(text) {
109110
}
110111
})
111112
}
113+
114+
export const isPC = () => {
115+
const backEnd = getBackend()
116+
const frontEnd = getFrontend()
117+
const isPcBack = backEnd === "windows" || "linux" || "darvin"
118+
const isPcFront = frontEnd === "desktop"
119+
return isPcBack && isPcFront
120+
}
121+
122+
async function mkdirp(dir) {
123+
const fs = window.require("fs/promises")
124+
const path = window.require("path")
125+
const absPath = path.isAbsolute(dir) ? dir : path.join(process.cwd(), dir)
126+
try {
127+
await fs.access(absPath)
128+
// 如果路径已经存在,则直接返回
129+
return absPath
130+
} catch (e) {
131+
if (e.code === "ENOENT") {
132+
// 如果路径不存在,则递归创建上级目录
133+
await mkdirp(path.dirname(absPath))
134+
return fs.mkdir(absPath)
135+
} else {
136+
throw e
137+
}
138+
}
139+
}
140+
141+
export const copyDir = async (src, dest) => {
142+
if (!isPC()) {
143+
console.warn("Not PC, it will not work")
144+
return
145+
}
146+
const fs = window.require("fs")
147+
const path = window.require("path")
148+
149+
// 创建文件夹
150+
if (!fs.existsSync(dest)) {
151+
await mkdirp(dest)
152+
}
153+
// 读取源文件夹
154+
const files = fs.readdirSync(src)
155+
// 遍历源文件夹中的文件/文件夹
156+
for (let file of files) {
157+
const srcPath = path.join(src, file)
158+
const destPath = path.join(dest, file)
159+
// 判断是否为文件夹
160+
if (fs.statSync(srcPath).isDirectory()) {
161+
// 递归拷贝文件夹
162+
copyDir(srcPath, destPath)
163+
} else {
164+
// 拷贝文件
165+
fs.copyFileSync(srcPath, destPath)
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)