Skip to content

Commit

Permalink
feat: 完成导出epub、docx、opml、html测试
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 17, 2023
1 parent e6cf277 commit 1d671b6
Show file tree
Hide file tree
Showing 13 changed files with 367 additions and 79 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

# siyuan-plugin-importer

Import from epub, docx, pdf, html etc. to siyuan-note
Import from epub, docx, html etc. to siyuan-note

## Thanks

Thanks to [siyuan-note](https://github.com/siyuan-note/siyuan) and [pandoc](https://github.com/jgm/pandoc) for their strong support for this plugin
6 changes: 5 additions & 1 deletion README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

# 导入工具

导入 epub, docx, pdf, html 等格式到思源笔记
导入 epub, docx, html 等格式到思源笔记

## 感谢

感谢 [思源笔记](https://github.com/siyuan-note/siyuan)[pandoc](https://github.com/jgm/pandoc) 对本插件的大力支持
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "siyuan-plugin-importer",
"version": "0.0.1",
"type": "module",
"description": "Import from epub, docx, pdf, html etc. to siyuan-note",
"description": "Import from epub, docx, html etc. to siyuan-note",
"repository": "terwer/siyuan-plugin-importer",
"homepage": "https://github.com/terwer/siyuan-plugin-importer",
"author": "terwer",
Expand Down
4 changes: 2 additions & 2 deletions public/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"zh_CN": "导入工具"
},
"description": {
"default": "Import from epub, docx, pdf, html etc. to siyuan-note",
"zh_CN": "导入 epub, docx, pdf, html 等格式到思源笔记"
"default": "Import from epub, docx, html etc. to siyuan-note",
"zh_CN": "导入 epub, docx, html 等格式到思源笔记"
},
"readme": {
"default": "README.md",
Expand Down
62 changes: 56 additions & 6 deletions src/api/kernel-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,71 @@
*/

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

/**
* 思源笔记服务端API v2.8.8
*
* @see {@link https://github.com/siyuan-note/siyuan/blob/master/API_zh_CN.md API}
*
* @author terwer
* @version 1.0.0
* @since 1.0.0
*/
class KernelApi extends BaseApi {
/**
* 列出笔记本
*/
public async lsNotebooks(): Promise<SiyuanData> {
return await this.siyuanRequest("/api/notebook/lsNotebooks", {})
}

/**
* 打开笔记本
*
* @param notebookId - 笔记本ID
*/
public async openNotebook(notebookId: string): Promise<SiyuanData> {
return await this.siyuanRequest("/api/notebook/openNotebook", {
notebook: notebookId,
})
}

/**
* 写入文件
*
* @param path - 文件路径,例如:/data/20210808180117-6v0mkxr/20200923234011-ieuun1p.sy
* @param file - 上传的文件
*/
public async putFile(path: string, file: any): Promise<SiyuanData> {
const params = { path: path, isDir: false, modTime: Math.floor(Date.now() / 1000), file: file }
return await this.siyuanRequest("/api/file/putFile", params)
public putFile(path: string, file: any): Promise<SiyuanData> {
const formData = new FormData()
formData.append("path", path)
formData.append("isDir", "false")
formData.append("modTime", Math.floor(Date.now() / 1000).toString())
formData.append("file", file)

return new Promise((resolve, reject) => {
fetchPost("/api/file/putFile", formData, (data) => {
if (data.code === 0) {
resolve(data)
} else {
reject(data)
}
})
})
}

/**
* 转换服务
*
* @param type - 类型
* @param from - 原始文件名,不包括路径,路径必须放在 /temp/convert/pandoc
* @param to - 转换后的文件名,不包括路径,路径相对于 /temp/convert/pandoc
*/
public async convertPandoc(): Promise<SiyuanData> {
public async convertPandoc(type: string, from: string, to: string): Promise<SiyuanData> {
const args = {
args: ["--to", "markdown_strict-raw_html", "foo.epub", "-o", "foo.md"],
args: ["--to", type, from, "-o", to],
}
return await this.siyuanRequest("/api/convert/pandoc", args)
}
Expand Down Expand Up @@ -82,6 +120,18 @@ class KernelApi extends BaseApi {
return null
}

/**
* 通过Markdown创建文档
*
* @param path - 路径
*/
public async removeFile(path: string): Promise<SiyuanData> {
const params = {
path: path,
}
return await this.siyuanRequest("/api/file/removeFile", params)
}

/**
* 通过Markdown创建文档
*
Expand Down
50 changes: 19 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@
* questions.
*/

import { App, Dialog, IObject, isMobile, Plugin } from "siyuan"
import { App, IObject, Plugin } from "siyuan"
import { createLogger } from "./utils/simple-logger"
import KernelApi from "./api/kernel-api"
import { isDev } from "./Constants"
import ImportForm from "./lib/ImportForm.svelte"
import "./index.styl"
import { removeImporterConfig } from "./store/config"
import { initTopbar } from "./topbar"

/**
* 导入插件
*
* @author terwer
* @version 1.0.0
* @since 1.0.0
*/
export default class ImporterPlugin extends Plugin {
private logger
private kernelApi
public logger
public kernelApi: KernelApi

constructor(options: { app: App; id: string; name: string; i18n: IObject }) {
super(options)
Expand All @@ -41,39 +49,19 @@ export default class ImporterPlugin extends Plugin {
this.kernelApi = new KernelApi()
}

onload() {
async onload() {
if (isDev) {
this.logger.warn("DEV mode is enabled")
}

const topBarElement = this.addTopBar({
icon: "iconEmoji",
title: this.i18n.importer,
position: "right",
callback: () => {
this.logger.info(`this.i18n.importer added toolbar`)
},
})
topBarElement.addEventListener("click", async () => {
const importFormId = "siyuan-import-form"
const d = new Dialog({
title: `${this.i18n.selectFile} - ${this.i18n.importer}`,
content: `<div id="${importFormId}"></div>`,
width: isMobile() ? "92vw" : "720px",
})
new ImportForm({
target: document.getElementById(importFormId) as HTMLElement,
props: {
pluginInstance: this,
dialog: d,
},
})
})

// 初始化顶栏按钮
await initTopbar(this)
this.logger.info("Importer loaded")
}

onunload() {
this.logger.info("Importer loaded")
async onunload() {
// 卸载删除配置
await removeImporterConfig(this)
this.logger.info("Importer unloaded")
}
}
Loading

0 comments on commit 1d671b6

Please sign in to comment.