Skip to content

Commit

Permalink
feat: 优化 pandoc 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jun 1, 2023
1 parent b626917 commit 6e0ca9f
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 21 deletions.
11 changes: 9 additions & 2 deletions src/api/kernel-api.ts
Expand Up @@ -112,7 +112,7 @@ class KernelApi extends BaseApi {
* @param to - 转换后的文件名,不包括路径,路径相对于 /temp/convert/pandoc
*/
public async convertPandoc(type: string, from: string, to: string): Promise<SiyuanData> {
const args = {
const params = {
args: [
"--to",
type,
Expand All @@ -124,7 +124,14 @@ class KernelApi extends BaseApi {
"--wrap=none",
],
}
return await this.siyuanRequest("/api/convert/pandoc", args)
return await this.siyuanRequest("/api/convert/pandoc", params)
}

public async convertPandocCustom(args: string[]): Promise<SiyuanData> {
const params = {
args: args,
}
return await this.siyuanRequest("/api/convert/pandoc", params)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/ImportForm.svelte
Expand Up @@ -149,9 +149,9 @@
showMessage(`${pluginInstance.i18n.msgConverting} ${file.name}...`, 1000, "info")
// 转换
const toFilePath = await ImportService.uploadAndConvert(pluginInstance, file)
const uploadResult = await ImportService.uploadAndConvert(pluginInstance, file)
// 导入
await ImportService.singleImport(pluginInstance, toFilePath, toNotebookId)
await ImportService.singleImport(pluginInstance, uploadResult.toFilePath, toNotebookId,uploadResult.isMd)
}
// =================
// 单文件转换结束
Expand Down Expand Up @@ -324,7 +324,7 @@
color: var(--b3-theme-primary);
}
.config__item:hover .highlight {
.b3-label__text:hover .highlight {
display: block;
}
</style>
70 changes: 54 additions & 16 deletions src/service/importService.ts
Expand Up @@ -23,10 +23,11 @@
* questions.
*/

import { workspaceDir } from "../Constants"
import { showMessage } from "siyuan"
import { mediaDir, workspaceDir } from "../Constants"
import { getBackend, getFrontend, showMessage } from "siyuan"
import ImporterPlugin from "../index"
import { removeEmptyLines, removeFootnotes, removeLinks, replaceImagePath } from "../utils/utils"
import { copyDir, isPC, removeEmptyLines, removeFootnotes, removeLinks, replaceImagePath } from "../utils/utils"
import shortHash from "shorthash2"

export class ImportService {
/**
Expand All @@ -51,15 +52,25 @@ export class ImportService {
if (ext === "md") {
const filePath = file.path
pluginInstance.logger.info(`import md from ${filePath}`)
return filePath
return {
toFilePath: filePath,
isMd: true,
}
}

if (ext === "html") {
// 仅在客户端复制资源文件
const filePath = file.path
const lastSlashIndex = filePath.lastIndexOf("/")
const dirPath = filePath.substring(0, lastSlashIndex)
pluginInstance.logger.info(`${dirPath}${originalFilename}_files`)
if (isPC()) {
pluginInstance.logger.info(`copying html assets...`)
// 仅在客户端复制资源文件
const filePath = file.path
const lastSlashIndex = filePath.lastIndexOf("/")
const dirPath = filePath.substring(0, lastSlashIndex)
const path = window.require("path")
const fullDirPath = path.join(dirPath, `${originalFilename}_files`)
pluginInstance.logger.info("fullDirPath=>", fullDirPath)

await copyDir(fullDirPath, `${workspaceDir}/temp/convert/pandoc/${filename}_files`)
}
}

// =================================================
Expand All @@ -79,11 +90,32 @@ export class ImportService {
}

// 文件转换
const convertResult = await pluginInstance.kernelApi.convertPandoc(
const args: string[] = [
"--to",
"markdown_strict-raw_html",
fromFilename,
toFilename
)
"-o",
toFilename,
"--extract-media",
`${mediaDir}/${shortHash(fromFilename).toLowerCase()}`,
"--wrap=none",
]
// const args = [
// "--to",
// "markdown_strict-raw_html",
// fromFilename,
// "-o",
// toFilename,
// "--extract-media",
// `${mediaDir}/${shortHash(fromFilename).toLowerCase()}`,
// "--wrap=none",
// ]
const convertResult = await pluginInstance.kernelApi.convertPandocCustom(args)
// const convertResult = await pluginInstance.kernelApi.convertPandoc(
// "markdown_strict-raw_html",
// fromFilename,
// toFilename
// )
if (convertResult.code !== 0) {
showMessage(`${pluginInstance.i18n.msgFileConvertError}${convertResult.msg}`, 7000, "error")
return
Expand All @@ -107,12 +139,18 @@ export class ImportService {
mdText = removeFootnotes(mdText)
await pluginInstance.kernelApi.saveTextData(`${toFilename}`, mdText)

return toFilePath
return {
toFilePath: toFilePath,
isMd: false,
}
}

public static async singleImport(pluginInstance: ImporterPlugin, toFilePath: string, toNotebookId: string) {
const ext = toFilePath.split(".").pop().toLowerCase()
const isMd = ext === "md"
public static async singleImport(
pluginInstance: ImporterPlugin,
toFilePath: string,
toNotebookId: string,
isMd: boolean
) {
// 导入 MD 文档
const localPath = isMd ? toFilePath : `${workspaceDir}${toFilePath}`
const mdResult = await pluginInstance.kernelApi.importStdMd(localPath, toNotebookId, `/`)
Expand Down
57 changes: 57 additions & 0 deletions src/utils/utils.ts
Expand Up @@ -25,6 +25,7 @@

import KernelApi from "../api/kernel-api"
import { dataDir } from "../Constants"
import { getBackend, getFrontend } from "siyuan"

/**
* 文件是否存在
Expand Down Expand Up @@ -109,3 +110,59 @@ export function removeLinks(text) {
}
})
}

export const isPC = () => {
const backEnd = getBackend()
const frontEnd = getFrontend()
const isPcBack = backEnd === "windows" || "linux" || "darvin"
const isPcFront = frontEnd === "desktop"
return isPcBack && isPcFront
}

async function mkdirp(dir) {
const fs = window.require("fs/promises")
const path = window.require("path")
const absPath = path.isAbsolute(dir) ? dir : path.join(process.cwd(), dir)
try {
await fs.access(absPath)
// 如果路径已经存在,则直接返回
return absPath
} catch (e) {
if (e.code === "ENOENT") {
// 如果路径不存在,则递归创建上级目录
await mkdirp(path.dirname(absPath))
return fs.mkdir(absPath)
} else {
throw e
}
}
}

export const copyDir = async (src, dest) => {
if (!isPC()) {
console.warn("Not PC, it will not work")
return
}
const fs = window.require("fs")
const path = window.require("path")

// 创建文件夹
if (!fs.existsSync(dest)) {
await mkdirp(dest)
}
// 读取源文件夹
const files = fs.readdirSync(src)
// 遍历源文件夹中的文件/文件夹
for (let file of files) {
const srcPath = path.join(src, file)
const destPath = path.join(dest, file)
// 判断是否为文件夹
if (fs.statSync(srcPath).isDirectory()) {
// 递归拷贝文件夹
copyDir(srcPath, destPath)
} else {
// 拷贝文件
fs.copyFileSync(srcPath, destPath)
}
}
}

0 comments on commit 6e0ca9f

Please sign in to comment.