generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
90 lines (65 loc) · 2.61 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { App, Plugin, PluginSettingTab, Setting, Notice } from 'obsidian';
import * as path from 'path';
import { convertJSONToTiddlers, convertTiddlersToObsidianMarkdown, writeObsidianMarkdownFiles } from 'services/TiddlyWikiToMarkdownService';
import { exportAllMarkdownFilesToJSON } from 'services/MarkdownToTiddlyWikiService';
import { downloadJsonAsFile } from 'utils/downloadJsonAsFile';
export default class ObsidianTiddlyWikiPlugin extends Plugin {
async onload() {
this.addSettingTab(new SampleSettingTab(this.app, this));
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: ObsidianTiddlyWikiPlugin;
constructor(app: App, plugin: ObsidianTiddlyWikiPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Import' });
const form = containerEl.createEl('form', { attr: { "encType": "multipart/form-data", "hidden": true } })
const input = containerEl.createEl('input')
input.type = "file"
input.id = "file-upload"
input.multiple = false
input.accept = ".json"
input.addEventListener("change", async (event) => {
if (input.files && input.files.length > 0) {
for (let fileIndex = 0; fileIndex < input.files.length; fileIndex++) {
const file = input.files.item(fileIndex)
if (!file) {
throw new Error('File is not defined')
}
const currentDate = new Date().toISOString().replace(/:/g, '_')
const directoryPath = `TiddlyWiki-Import-${currentDate}`
//@ts-ignore
const exportPath = path.join(this.app.vault.adapter.basePath, directoryPath)
const tiddlers = await convertJSONToTiddlers(file);
const obsidianMarkdownArray = convertTiddlersToObsidianMarkdown(tiddlers);
writeObsidianMarkdownFiles(obsidianMarkdownArray, exportPath);
new Notice(`✅ Successfuly imported TiddlyWiki to ${exportPath}`, 10000)
}
}
})
form.appendChild(input)
new Setting(containerEl)
.setName('Import JSON')
.setDesc('To export from TiddlyWiki : Tools->Export all->JSON File')
.addButton(button => button
.setButtonText("Import .json").onClick(() => {
input.click()
}))
containerEl.createEl('h2', { text: 'Export' });
new Setting(containerEl)
.setName('Export JSON')
.setDesc('To import in TiddlyWiki : Tools->Import')
.addButton(button => button
.setButtonText("Export .json").onClick(async () => {
//@ts-ignore
const basePath = this.app.vault.adapter.basePath
const jsonExport = await exportAllMarkdownFilesToJSON(basePath)
downloadJsonAsFile(jsonExport, "test.json")
}))
}
}