Skip to content

Commit f5452e2

Browse files
committedJun 1, 2023
feat: 支持自定义处理函数、支持禁用自带处理函数
·
v1.8.1v1.6.0
1 parent f4a28a7 commit f5452e2

File tree

5 files changed

+135
-21
lines changed

5 files changed

+135
-21
lines changed
 

‎src/i18n/en_US.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@
4545
"customFnHandlerPlaceholder": "Custom handler",
4646
"enableCustomFn": "Enable custom functions",
4747
"enableCustomFnTips": "Are you sure you want to enable custom functions? This action may cause imports to be unavailable. Please confirm that you have understood how custom functions work?",
48-
"importConfigSaveSuccess": "Configuration saved successfully"
48+
"importConfigSaveSuccess": "Configuration saved successfully",
49+
"testOutputPlaceholder": "Here is the output of the test",
50+
"testInput": "Test input",
51+
"testOutput": "Test output"
4952
}

‎src/i18n/zh_CN.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@
4242
"setting": "设置",
4343
"cancel": "取消",
4444
"save": "保存",
45+
"bundledFnHandler": "插件自带的处理函数",
46+
"bundledFnSwitch": "插件自带的处理函数开关",
47+
"bundledFnSwitchTips": "启用或者禁用插件自带的处理函数,目前自带的处理函数已包括删除目录中链接、去除空行、资源路径修复、去除脚注等功能",
48+
"disableBundledFnSwitchTips": "确认禁用插件自带的处理函数吗,将不会对转换结果做任何处理,包括去除链接,脚注优化等,将按照转换结果原样导入?",
4549
"customFnHandler": "自定义处理函数",
4650
"customFnSwitch": "自定义处理函数开关",
4751
"customFnSwitchTips": "启用或者禁用自定义处理函数",
4852
"customFnHandlerTips": "可以在这里自定义处理函数,导入过程中会自动调用,请注意:修改此处可能会导致导入不工作,若出现问题可禁用自定义处理函数。正则表达式在线测试可参考:",
4953
"customFnHandlerPlaceholder": "自定义处理函数",
5054
"enableCustomFn": "启用自定义函数",
5155
"enableCustomFnTips": "确认要启用自定义函数吗,此操作可能导致导入不可用,请确认您已经了解自定义函数的工作原理?",
52-
"importConfigSaveSuccess": "配置保存成功"
56+
"importConfigSaveSuccess": "配置保存成功",
57+
"testOutputPlaceholder": "这里是测试的输出结果",
58+
"testInput": "测试输入",
59+
"testOutput": "测试输出",
60+
"customFnHandlerError": "自定义处理函数处理失败,请检查。您也可以暂时禁用自定义处理函数完成导入"
5361
}

‎src/lib/ImportSetting.svelte

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@
2828
import { onMount } from "svelte"
2929
import { confirm, showMessage } from "siyuan"
3030
import { loadImporterConfig, saveImporterConfig } from "../store/config"
31+
import { getExports } from "../utils/utils"
3132
3233
export let pluginInstance: ImporterPlugin
3334
export let dialog
3435
36+
let bundledFnSwitch = true
3537
let customFnSwitch = false
3638
let customFn
3739
let importerConfig = {}
3840
3941
const onSaveSetting = async () => {
4042
dialog.destroy()
4143
44+
importerConfig.bundledFnSwitch = bundledFnSwitch
4245
importerConfig.customFnSwitch = customFnSwitch
4346
importerConfig.customFn = customFn
4447
await saveImporterConfig(pluginInstance, importerConfig)
@@ -68,25 +71,72 @@
6871
}
6972
}
7073
74+
const updateBundledFnSwitch = (event) => {
75+
event.stopPropagation()
76+
77+
if (bundledFnSwitch) {
78+
confirm(
79+
`⚠️${pluginInstance.i18n.bundledFnSwitch}`,
80+
`${pluginInstance.i18n.disableBundledFnSwitchTips}`,
81+
() => {
82+
const inputEl = document.querySelector("#bundledFnSwitch")
83+
inputEl.checked = bundledFnSwitch
84+
},
85+
() => {
86+
bundledFnSwitch = !bundledFnSwitch
87+
}
88+
)
89+
}
90+
}
91+
92+
let testInput = `我衷心期盼,子孙后代们读到这封信时,会带着一种自豪感和正当的优越感。
93+
94+
## 评伯特兰·罗素的知识论^([\\[16\\]](#part0019.html#footnote_16))
95+
96+
当编者要我就罗素写点东西时,出于对这位作者的钦佩和尊敬,我立刻答应了下来。`
97+
let testOutput = ""
98+
const testFn = () => {
99+
const exportsFn = getExports(customFn)
100+
const result = exportsFn(testInput)
101+
testOutput = result
102+
console.log("test exportsFn=>", result)
103+
}
104+
71105
onMount(async () => {
72106
// 加载配置
73107
importerConfig = await loadImporterConfig(pluginInstance)
74108
109+
bundledFnSwitch = importerConfig.bundledFnSwitch ?? true
75110
customFnSwitch = importerConfig.customFnSwitch ?? false
76111
customFn =
77112
importerConfig.customFn ??
78-
`
79-
// 您可以参考这个案例进行修改,注意:请勿修改方法名,只需修改实现即可
80-
// 将字符串中形如"xxx^yyy"的部分替换成"xxx"
81-
export function customFn(mdText) {
82-
const regex = /\\^\\(\\[.*[0-9].*]\\(#.*#.*\\)\\)/g // 匹配格式为 ^[[数字]](#链接) 的脚注
83-
return mdText.replace(regex, "") // 使用空字符串替换匹配到的脚注
84-
}
85-
`
113+
`// 您可以参考这个案例进行修改,注意:请勿修改方法名和参数名,只需修改customFn内部实现即可
114+
// 将字符串中形如"xxx^yyy"的部分替换成"xxx"
115+
const customFn = (mdText) => {
116+
const regex = /\\^\\(\\[.*[0-9].*]\\(#.*#.*\\)\\)/g // 匹配格式为 ^[[数字]](#链接) 的脚注
117+
return mdText.replace(regex, "") // 使用空字符串替换匹配到的脚注
118+
}
119+
120+
module.exports = customFn`
86121
})
87122
</script>
88123

89124
<div class="config__tab-container">
125+
<label class="fn__flex b3-label">
126+
<div class="fn__flex-1">
127+
{pluginInstance.i18n.bundledFnSwitch}
128+
<div class="b3-label__text">{pluginInstance.i18n.bundledFnSwitchTips}</div>
129+
</div>
130+
<span class="fn__space" />
131+
<input
132+
id="bundledFnSwitch"
133+
class="b3-switch fn__flex-center"
134+
type="checkbox"
135+
on:click={(event) => updateBundledFnSwitch(event)}
136+
bind:checked={bundledFnSwitch}
137+
/>
138+
</label>
139+
90140
<label class="fn__flex b3-label">
91141
<div class="fn__flex-1">
92142
{pluginInstance.i18n.customFnSwitch}
@@ -113,16 +163,38 @@
113163
<textarea
114164
class="b3-text-field fn__block"
115165
placeholder={pluginInstance.i18n.customFnHandlerPlaceholder}
116-
rows="10"
166+
rows="8"
117167
spellcheck="false"
118168
bind:value={customFn}
119169
/>
120170
</div>
121171
</label>
122172

173+
<label class="fn__flex b3-label">
174+
<div class="fn__flex-1">
175+
<button class="b3-button b3-button--outline fn__flex-right fn__size200" on:click={testFn}> 测试 </button>
176+
<div class="fn__hr" />
177+
{pluginInstance.i18n.testInput}
178+
<textarea class="b3-text-field fn__block test-data-item" rows="6" spellcheck="false" bind:value={testInput} />
179+
{pluginInstance.i18n.testOutput}
180+
<textarea
181+
class="b3-text-field fn__block test-data-item"
182+
placeholder={pluginInstance.i18n.testOutputPlaceholder}
183+
rows="6"
184+
spellcheck="false"
185+
bind:value={testOutput}
186+
/>
187+
</div>
188+
</label>
189+
123190
<div class="b3-dialog__action">
124191
<button class="b3-button b3-button--cancel" on:click={onCancel}>{pluginInstance.i18n.cancel}</button>
125192
<div class="fn__space" />
126193
<button class="b3-button b3-button--text" on:click={onSaveSetting}>{pluginInstance.i18n.save}</button>
127194
</div>
128195
</div>
196+
197+
<style lang="stylus">
198+
.test-data-item
199+
margin 4px 0
200+
</style>

‎src/service/importService.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@
2626
import { mediaDir, workspaceDir } from "../Constants"
2727
import { getBackend, getFrontend, showMessage } from "siyuan"
2828
import ImporterPlugin from "../index"
29-
import { copyDir, isPC, removeEmptyLines, removeFootnotes, removeLinks, replaceImagePath } from "../utils/utils"
29+
import {
30+
copyDir,
31+
getExports,
32+
isPC,
33+
removeEmptyLines,
34+
removeFootnotes,
35+
removeLinks,
36+
replaceImagePath,
37+
} from "../utils/utils"
3038
import shortHash from "shorthash2"
39+
import { loadImporterConfig } from "../store/config"
3140

3241
export class ImportService {
3342
/**
@@ -104,14 +113,32 @@ export class ImportService {
104113
}
105114

106115
// 文本处理
107-
// 删除目录中链接
108-
mdText = removeLinks(mdText)
109-
// 去除空行
110-
mdText = removeEmptyLines(mdText)
111-
// 资源路径
112-
mdText = replaceImagePath(mdText)
113-
// 去除脚注
114-
mdText = removeFootnotes(mdText)
116+
const importConfig = await loadImporterConfig(pluginInstance)
117+
if (importConfig.bundledFnSwitch) {
118+
pluginInstance.logger.info("Using bundled handler process text")
119+
// 删除目录中链接
120+
mdText = removeLinks(mdText)
121+
// 去除空行
122+
mdText = removeEmptyLines(mdText)
123+
// 资源路径
124+
mdText = replaceImagePath(mdText)
125+
// 去除脚注
126+
mdText = removeFootnotes(mdText)
127+
}
128+
129+
// 自定义文本处理
130+
if (importConfig.customFnSwitch) {
131+
pluginInstance.logger.warn("Using custom handler process text")
132+
try {
133+
const customFn = importConfig.customFn
134+
const exportsFn = getExports(customFn)
135+
mdText = exportsFn(mdText)
136+
} catch (e) {
137+
showMessage(`${pluginInstance.i18n.customFnHandlerError} ${e.toString()}`, 5000, "error")
138+
}
139+
}
140+
141+
// 保存处理的最终文本
115142
await pluginInstance.kernelApi.saveTextData(`${toFilename}`, mdText)
116143

117144
return {

‎src/utils/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const copyDir = async (src, dest) => {
146146
const fs = window.require("fs")
147147
const path = window.require("path")
148148

149-
if(!fs.existsSync(src)){
149+
if (!fs.existsSync(src)) {
150150
console.warn("Can not get path")
151151
return
152152
}
@@ -171,3 +171,7 @@ export const copyDir = async (src, dest) => {
171171
}
172172
}
173173
}
174+
175+
export const getExports = (jsText) => {
176+
return eval(jsText)
177+
}

0 commit comments

Comments
 (0)
Please sign in to comment.