-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
229 lines (192 loc) · 7.31 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
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface WikilinkToMarkdownSettings {
language: string;
}
const DEFAULT_SETTINGS: WikilinkToMarkdownSettings = {
language: 'en'
}
interface LanguageStrings {
convertButtonText: string;
openFileNotice: string;
confirmConversionTitle: string;
confirmConversionMessage: string;
confirmButton: string;
cancelButton: string;
conversionCompleteNotice: string;
confirmChangeTitle: string;
confirmChangeMessage: string;
}
const zhStrings: LanguageStrings = {
convertButtonText: '转换WikiLink为Markdown',
openFileNotice: '请打开一篇文章以进行转换',
confirmConversionTitle: '确认转换',
confirmConversionMessage: '是否要转换当前文档中的WikiLinks为Markdown格式?',
confirmButton: '确认',
cancelButton: '取消',
conversionCompleteNotice: '转换完成',
confirmChangeTitle: '确认更改',
confirmChangeMessage: '转换完成,是否确认更改?'
};
const enStrings: LanguageStrings = {
convertButtonText: 'Convert WikiLinks to Markdown',
openFileNotice: 'Please open a file to convert',
confirmConversionTitle: 'Confirm Conversion',
confirmConversionMessage: 'Do you want to convert WikiLinks to Markdown format in the current document?',
confirmButton: 'Confirm',
cancelButton: 'Cancel',
conversionCompleteNotice: 'Conversion complete',
confirmChangeTitle: 'Confirm Changes',
confirmChangeMessage: 'Conversion complete. Do you want to confirm the changes?'
};
export default class WikilinkToMarkdownPlugin extends Plugin {
settings: WikilinkToMarkdownSettings;
strings: LanguageStrings;
async onload() {
await this.loadSettings();
this.addSettingTab(new WikilinkToMarkdownSettingTab(this.app, this));
this.updateStrings();
this.addRibbonIcon('link', this.strings.convertButtonText, (evt: MouseEvent) => {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
new ConfirmModal(this.app, this.strings, async () => {
await this.convertWikilinksToMarkdown(activeView.editor);
}).open();
} else {
new Notice(this.strings.openFileNotice);
}
});
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
this.updateStrings();
}
updateStrings() {
this.strings = this.settings.language === 'zh' ? zhStrings : enStrings;
}
async convertWikilinksToMarkdown(editor: Editor) {
const content = editor.getValue();
const convertedContent = this.convertContent(content);
new ConfirmChangeModal(this.app, this.strings, async () => {
editor.setValue(convertedContent);
new Notice(this.strings.conversionCompleteNotice);
}).open();
}
convertContent(content: string): string {
// Convert wikilinks to Markdown links
content = content.replace(/\[\[([^\]|]+)\|?([^\]]*)\]\]/g, (match, link, alias) => {
if (this.isExternalLink(link)) {
// For external links, always use Markdown format
return `[${alias || link}](${link})`;
} else {
const processedLink = this.processLink(link);
const displayName = alias || this.getDisplayName(link);
return `[${displayName}](${processedLink})`;
}
});
// Convert wikilink images to Markdown images
content = content.replace(/!\[\[([^\]|]+)\|?([^\]]*)\]\]/g, (match, link, alias) => {
const processedLink = this.processLink(link);
if (alias) {
return `![${alias}](${processedLink})`;
} else {
return `![](${processedLink})`;
}
});
return content;
}
private isExternalLink(link: string): boolean {
return link.startsWith('http://') || link.startsWith('https://') || link.startsWith('www.');
}
private processLink(link: string): string {
if (this.isExternalLink(link)) {
return link;
}
// If it's a local link without a file extension, add .md extension
if (!link.match(/\.\w+$/)) {
link += '.md';
}
// Handle spaces in file names
return link.replace(/ /g, '%20');
}
private getDisplayName(link: string): string {
// Remove path, keep only the file name
const fileName = link.split('/').pop() || link;
// Remove file extension
return fileName.replace(/\.\w+$/, '');
}
}
class ConfirmModal extends Modal {
constructor(app: App, private strings: LanguageStrings, private onConfirm: () => void) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.createEl('h2', {text: this.strings.confirmConversionTitle});
contentEl.createEl('p', {text: this.strings.confirmConversionMessage});
new Setting(contentEl)
.addButton(btn => btn
.setButtonText(this.strings.confirmButton)
.setCta()
.onClick(() => {
this.close();
this.onConfirm();
}))
.addButton(btn => btn
.setButtonText(this.strings.cancelButton)
.onClick(() => this.close()));
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class ConfirmChangeModal extends Modal {
constructor(app: App, private strings: LanguageStrings, private onConfirm: () => void) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.createEl('h2', {text: this.strings.confirmChangeTitle});
contentEl.createEl('p', {text: this.strings.confirmChangeMessage});
new Setting(contentEl)
.addButton(btn => btn
.setButtonText(this.strings.confirmButton)
.setCta()
.onClick(() => {
this.close();
this.onConfirm();
}))
.addButton(btn => btn
.setButtonText(this.strings.cancelButton)
.onClick(() => this.close()));
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class WikilinkToMarkdownSettingTab extends PluginSettingTab {
plugin: WikilinkToMarkdownPlugin;
constructor(app: App, plugin: WikilinkToMarkdownPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Language')
.setDesc('Choose the language for the plugin interface')
.addDropdown(dropdown => dropdown
.addOption('en', 'English')
.addOption('zh', '中文')
.setValue(this.plugin.settings.language)
.onChange(async (value) => {
this.plugin.settings.language = value;
await this.plugin.saveSettings();
}));
}
}