-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
149 lines (121 loc) · 5.07 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
import { App, Modal, Notice, Plugin, Setting, TFile } from 'obsidian';
import { dirname, relative } from './lib/path';
class ConfirmModal extends Modal {
content: string;
onConfirm: () => void;
constructor(app: App, content: string, onConfirm: () => void) {
super(app);
this.content = content;
this.onConfirm = onConfirm;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl('h1', { text: 'Update Releate Links Plugin' });
contentEl.createEl('p', { text: this.content });
new Setting(contentEl)
.addButton((btn) => btn
.setButtonText('Yes')
.setCta()
.onClick(() => {
this.close();
this.onConfirm();
}))
.addButton((btn) => btn
.setButtonText('No')
.onClick(() => {
this.close();
}));
}
onClose() {
this.contentEl.empty();
}
}
export default class UpdateRelativeLinksPlugin extends Plugin {
async onload() {
const { app } = this;
const { metadataCache, vault } = app;
this.addCommand({
id: 'update-all-relative-links',
name: 'Update all relative links',
callback() {
new ConfirmModal(app, 'Are you sure you want to convert all relative links?', () => {
const promises = vault.getMarkdownFiles().map(file => replace(file, false));
Promise.all(promises).then(linkCounts => {
const updatedLinkCounts = linkCounts.filter(count => count > 0);
const linkCount = updatedLinkCounts.reduce((sum, count) => sum + count, 0);
const fileCount = updatedLinkCounts.length;
new Notice(`Update ${linkCount} links in ${fileCount} file${fileCount > 1 ? 's' : ''}.`);
}).catch(err => {
new Notice('Update links error, see console.');
console.error(err);
});
}).open();
}
});
this.registerEvent(vault.on('rename', (file, oldPath) => {
if (!oldPath
|| !file.path.toLocaleLowerCase().endsWith('.md')
|| file.parent?.path === dirname(oldPath)) {
return;
}
if (file instanceof TFile) {
setTimeout(() => replace(file, true), 100);
}
}));
async function replace(file: TFile, notice: boolean) {
const metadata = metadataCache.getFileCache(file);
const links = [...(metadata?.links ?? []), ...(metadata?.embeds ?? [])];
const replacePairs = links.map(({ link, original }) => {
const linkPath = link.replace(/#.*$/, '');
if (!linkPath) {
return null;
}
const linkFile = metadataCache.getFirstLinkpathDest(linkPath, file.path);
if (!linkFile) {
return null;
}
const newLinkPath = file.parent?.path === '/' ? linkFile.path : relative(file.path, linkFile.path);
if (linkPath === newLinkPath) {
return null;
}
const newOriginal = replaceOriginal(original, linkPath, newLinkPath);
return [original, newOriginal];
}).filter(pair => pair);
if (!replacePairs?.length) {
return 0;
}
try {
const content = await vault.read(file);
const replacedContent = replacePairs.reduce((tmpContent, pair) => {
return pair?.length === 2 ? tmpContent.replace(pair[0], pair[1]) : tmpContent;
}, content);
await vault.modify(file, replacedContent);
const msg = `Update ${replacePairs.length} links in ${file.path}.`;
console.log(msg);
if (notice) {
new Notice(msg);
}
return replacePairs.length;
} catch (e) {
console.error(e);
if (notice) {
new Notice('Update links error, see console.');
}
throw e;
}
}
function replaceOriginal(original: string, link: string, newLink: string) {
let newOriginal = replaceWithFormat(original, link, newLink, s => s.replace(/ /g, '%20'));
if (original === newOriginal) {
newOriginal = replaceWithFormat(original, link, newLink, encodeURI);
}
if (original === newOriginal) {
newOriginal = original.replace(/^(!?\[.*?\]).*$/, `$1(${encodeURI(newLink)})`)
}
return newOriginal;
}
function replaceWithFormat(str: string, from: string, to: string, format: (s: string) => string) {
return str.replace(format(from), format(to));
}
}
}