-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
296 lines (237 loc) · 10.2 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { App, Plugin, PluginSettingTab, Setting, Notice, TFile, Vault, MarkdownView, TextFileView, WorkspaceLeaf } from 'obsidian';
import * as path from 'path';
interface Obsidian2MuSettings {
outputDir: string;
autoConvert: boolean;
}
const DEFAULT_SETTINGS: Obsidian2MuSettings = {
outputDir: 'mu-output',
autoConvert: false
}
// Add MuView class for rendering .mu files
class MuView extends TextFileView {
constructor(leaf: WorkspaceLeaf) {
super(leaf);
}
getViewType() {
return "mu-view";
}
getDisplayText() {
return this.file?.basename || "Mu View";
}
async onOpen() {
// Handle file open
}
async onClose() {
// Handle file close
}
getViewData() {
return this.data;
}
setViewData(data: string, clear: boolean) {
this.data = data;
const htmlContent = this.muToHtml(data);
this.contentEl.innerHTML = `<div class="mu-content">${htmlContent}</div>`;
}
private muToHtml(muContent: string): string {
return muContent
// Headers
.replace(/^>>>\s*(.*$)/gm, '<h3>$1</h3>')
.replace(/^>>\s*(.*$)/gm, '<h2>$1</h2>')
.replace(/^>\s*(.*$)/gm, '<h1>$1</h1>')
// Bold
.replace(/`!(.*?)`!/g, '<strong>$1</strong>')
// Italic
.replace(/`\*(.*?)`\*/g, '<em>$1</em>')
// Links
.replace(/`\[(.*?)`(.*?)\]/g, '<a href="$2">$1</a>')
// Code blocks
.replace(/`=\n([\s\S]*?)\n`=/g, '<pre><code>$1</code></pre>')
.replace(/`=(.*?)`=/g, '<code>$1</code>')
// Underline
.replace(/`_(.*?)`_/g, '<u>$1</u>')
// Lists
.replace(/^-\s*(.*$)/gm, '<li>$1</li>')
// Blockquotes
.replace(/^>>>>(.*$)/gm, '<blockquote>$1</blockquote>')
// Horizontal rules
.replace(/^-$/gm, '<hr>')
// Comments
.replace(/^#\s*(.*$)/gm, '<!-- $1 -->');
}
clear(): void {
this.data = "";
}
}
export default class Obsidian2MuPlugin extends Plugin {
settings: Obsidian2MuSettings;
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async convertToMu() {
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile || !/^(md|markdown)$/.test(activeFile.extension)) {
throw new Error('No markdown file is currently active');
}
await this.convertFile(activeFile);
}
async convertFile(file: TFile) {
try {
const content = await this.app.vault.read(file);
const converted = this.markdownToMu(content);
const outputPath = path.join(this.settings.outputDir, `${file.basename}.mu`);
// Ensure output directory exists
await this.ensureFolder(this.settings.outputDir);
// Check if file exists and modify it instead of recreating
const existingFile = this.app.vault.getAbstractFileByPath(outputPath);
if (existingFile instanceof TFile) {
await this.app.vault.modify(existingFile, converted);
} else {
// Create new file only if it doesn't exist
await this.app.vault.create(outputPath, converted);
}
// Update any open views
const muFile = this.app.vault.getAbstractFileByPath(outputPath);
if (muFile instanceof TFile) {
this.app.workspace.getLeavesOfType('mu-view').forEach(leaf => {
if (leaf.view instanceof MuView && leaf.view.file === muFile) {
leaf.view.setViewData(converted, false);
}
});
}
} catch (error) {
console.error('Failed to convert file:', error);
throw new Error(`Failed to convert ${file.basename}: ${error.message}`);
}
}
private markdownToMu(markdown: string): string {
return markdown
// Headers (exactly as specified)
.replace (/^####\s+(.*$)/gm, '>>>> $1')
.replace(/^###\s+(.*$)/gm, '>>> $1')
.replace(/^##\s+(.*$)/gm, '>> $1')
.replace(/^#\s+(.*$)/gm, '> $1')
// Bold (using `!)
.replace(/\*\*(.*?)\*\*/g, '`!$1`!')
// Italic (using `*)
.replace(/\*(.*?)\*/g, '`*$1`*')
// Links (using `[text`url])
.replace(/\[(.*?)\]\((.*?)\)/g, '`[$1`$2]')
// Code blocks (using `= for both inline and blocks)
.replace(/```(.*?)\n([\s\S]*?)```/g, '`=\n$2\n`=')
.replace(/`([^`]+)`/g, '`=$1`=')
// Underline (using `_)
.replace(/__(.*?)__/g, '`_$1`_')
// Lists (simple -)
.replace(/^\s*[-*+]\s/gm, '- ')
// Blockquotes (using >>>>)
.replace(/^>\s*(.*$)/gm, '>>>>$1')
// Horizontal rules (simple -)
.replace(/^[-*_]{3,}\s*$/gm, '-')
// Comments (using #)
.replace(/^#\s*(.*$)/gm, '# $1')
// Add newlines after sections
.replace(/^(>+.*$)/gm, '$1\n')
// Ensure proper spacing
.replace(/\n{3,}/g, '\n\n') // Normalize multiple newlines to double
.trim();
}
private async ensureFolder(folderPath: string) {
const abstractFile = this.app.vault.getAbstractFileByPath(folderPath);
if (!abstractFile) {
await this.app.vault.createFolder(folderPath);
}
}
private showNotice(message: string) {
const notice = new Notice(message);
return notice;
}
async onload() {
console.log('Loading Obsidian2Mu Plugin');
try {
await this.loadSettings();
this.registerExtensions(['mu'], 'mu-view');
this.registerView('mu-view', (leaf) => new MuView(leaf));
this.addSettingTab(new Obsidian2MuSettingTab(this.app, this));
this.addRibbonIcon('file-down', 'Convert to .mu', async () => {
try {
await this.convertToMu();
this.showNotice('Conversion completed successfully');
} catch (error) {
console.error('Conversion failed:', error);
this.showNotice(`Conversion failed: ${error.message}`);
}
});
if (this.settings.autoConvert) {
let timeoutId: NodeJS.Timeout;
this.registerEvent(
this.app.vault.on('modify', async (file: TFile) => {
if (/^(md|markdown)$/.test(file.extension)) {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(async () => {
try {
await this.convertFile(file);
// Try to find and refresh the corresponding .mu file view
const muPath = `${this.settings.outputDir}/${file.basename}.mu`;
const muFile = this.app.vault.getAbstractFileByPath(muPath);
if (muFile instanceof TFile) {
// Get the file content once
const muContent = await this.app.vault.read(muFile);
// Refresh all instances of this file's view
for (const leaf of this.app.workspace.getLeavesOfType('mu-view')) {
if (leaf.view instanceof MuView && leaf.view.file === muFile) {
leaf.view.setViewData(muContent, false);
}
}
}
} catch (error) {
console.error('Auto-conversion failed:', error);
this.showNotice(`Auto-conversion failed: ${error.message}`);
}
}, 500);
}
})
);
}
} catch (error) {
console.error('Failed to load plugin:', error);
this.showNotice(`Failed to load plugin: ${error.message}`);
}
}
async onunload() {
console.log('Unloading Obsidian2Mu Plugin');
}
}
class Obsidian2MuSettingTab extends PluginSettingTab {
plugin: Obsidian2MuPlugin;
constructor(app: App, plugin: Obsidian2MuPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Output Directory')
.setDesc('Directory where .mu files will be saved')
.addText(text => text
.setPlaceholder('mu-output')
.setValue(this.plugin.settings.outputDir)
.onChange(async (value) => {
this.plugin.settings.outputDir = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Auto Convert')
.setDesc('Automatically convert files when they are modified')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.autoConvert)
.onChange(async (value) => {
this.plugin.settings.autoConvert = value;
await this.plugin.saveSettings();
}));
}
}