Skip to content

Commit

Permalink
feat: 支持图片导入
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 17, 2023
1 parent dbd4346 commit 6387db2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* questions.
*/

export const dataDir = `${(window as any).siyuan.config.system.dataDir}`
export const mediaDir = `${dataDir}/assets/import`
export const isDev = process.env.DEV_MODE === "true"
export const siyuanApiUrl = ""
export const siyuanApiToken = ""
4 changes: 2 additions & 2 deletions src/api/kernel-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import { BaseApi, SiyuanData } from "./base-api"
import { siyuanApiToken, siyuanApiUrl } from "../Constants"
import {mediaDir, siyuanApiToken, siyuanApiUrl} from "../Constants"
import { fetchPost } from "siyuan"

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ class KernelApi extends BaseApi {
*/
public async convertPandoc(type: string, from: string, to: string): Promise<SiyuanData> {
const args = {
args: ["--to", type, from, "-o", to],
args: ["--to", type, from, "-o", to, "--extract-media", mediaDir],
}
return await this.siyuanRequest("/api/convert/pandoc", args)
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import { App, IObject, Plugin } from "siyuan"
import { createLogger } from "./utils/simple-logger"
import KernelApi from "./api/kernel-api"
import { isDev } from "./Constants"
import {isDev, mediaDir} from "./Constants"
import "./index.styl"
import { removeImporterConfig } from "./store/config"
import { initTopbar } from "./topbar"
Expand Down Expand Up @@ -56,6 +56,7 @@ export default class ImporterPlugin extends Plugin {

// 初始化顶栏按钮
await initTopbar(this)
this.logger.info("mediaDir=>", mediaDir)
this.logger.info(this.i18n.importerLoaded)
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib/ImportForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<script lang="ts">
import { showMessage } from "siyuan"
import ImporterPlugin from "../index"
import { removeEmptyLines } from "../utils/utils"
import { removeEmptyLines, replaceImagePath } from "../utils/utils"
import { onMount } from "svelte"
import { loadImporterConfig, saveImporterConfig } from "../store/config"
import { isDev } from "../Constants"
Expand Down Expand Up @@ -94,7 +94,10 @@
}
// 文本处理
// 去除空行
mdText = removeEmptyLines(mdText)
// 资源路径
mdText = replaceImagePath(mdText)
// 创建 MD 文档
const mdResult = await pluginInstance.kernelApi.createDocWithMd(toNotebookId, `/${filename}`, mdText)
Expand Down
22 changes: 21 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

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

/**
* 文件是否存在
Expand Down Expand Up @@ -62,4 +63,23 @@ export const isFileExists = async (kernelApi: KernelApi, p: string, type: "text"
*
* @param str - 字符串
*/
export const removeEmptyLines = (str: string): string => str.replace(/^#+\s*\n|!\[.*?\]\(.*?\)\n+|\n\u00A0+/gm, "\n")
// 删除图片
// export const removeEmptyLines = (str: string): string => str.replace(/^#+\s*\n|!\[.*?\]\(.*?\)\n+|\n\u00A0+/gm, "\n")
// 保留图片
export const removeEmptyLines = (str: string): string => str.replace(/^#+\s*\n|\n\u00A0+/gm, "\n")

/**
* 修复图片路径
*
* @param mdText - markdown 文本
*/
export const replaceImagePath = (mdText: string): string => {
const basePath = dataDir
const regex = /!\[(.*?)\]\((\/.*?)\)/g
return mdText.replace(regex, (match, p1, p2) => {
if (p2.includes(basePath)) {
return `![${p1}](${p2.replace(basePath, "")})`
}
return match
})
}

0 comments on commit 6387db2

Please sign in to comment.