review and new module
This commit is contained in:
1 parent
4cf5e9da4a
commit
6ff146eaaf
5 files changed
+368
-1
No files matched your search
@@ -12,6 +12,14 @@ export interface Locale {
|
||||
extensionsDesc: string;
|
||||
extensionsPlaceholder: string;
|
||||
};
|
||||
'preserve-link-aliases': {
|
||||
name: string;
|
||||
description: string;
|
||||
processEmbedsLabel: string;
|
||||
processEmbedsDesc: string;
|
||||
processCanvasLabel: string;
|
||||
processCanvasDesc: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -32,5 +40,14 @@ export const en: Locale = {
|
||||
'Comma-separated extensions (e.g.: png, jpg, pdf, mp3). Files with these extensions will be inserted as [[path/file|name]] instead of ![[path/file]].',
|
||||
extensionsPlaceholder: 'png, jpg, jpeg, gif, svg, webp, pdf',
|
||||
},
|
||||
'preserve-link-aliases': {
|
||||
name: 'Preserve Link Aliases',
|
||||
description:
|
||||
'Preserves original link aliases when attachments are renamed',
|
||||
processEmbedsLabel: 'Process embed links',
|
||||
processEmbedsDesc: 'Also process ![[embed]] links (disabled by default)',
|
||||
processCanvasLabel: 'Process Canvas files',
|
||||
processCanvasDesc: 'Also process links in Canvas (.canvas) files',
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -17,5 +17,14 @@ export const ru: Locale = {
|
||||
'Расширения через запятую (например: png, jpg, pdf, mp3). Файлы с этими расширениями будут вставляться как [[path/file|name]] вместо ![[path/file]].',
|
||||
extensionsPlaceholder: 'png, jpg, jpeg, gif, svg, webp, pdf',
|
||||
},
|
||||
'preserve-link-aliases': {
|
||||
name: 'Сохранение алиасов ссылок',
|
||||
description:
|
||||
'Сохраняет оригинальные алиасы ссылок при переименовании вложений',
|
||||
processEmbedsLabel: 'Обрабатывать embed-ссылки',
|
||||
processEmbedsDesc: 'Также обрабатывать ![[embed]] ссылки (по умолчанию выключено)',
|
||||
processCanvasLabel: 'Обрабатывать Canvas-файлы',
|
||||
processCanvasDesc: 'Также обрабатывать ссылки в Canvas (.canvas) файлах',
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,4 +1,8 @@
|
||||
import { AttachmentCleanPasteModule } from './attachment-clean-paste';
|
||||
import { PreserveLinkAliasesModule } from './preserve-link-aliases';
|
||||
import { QoLModule } from '../types';
|
||||
|
||||
export const MODULES: QoLModule[] = [new AttachmentCleanPasteModule()];
|
||||
export const MODULES: QoLModule[] = [
|
||||
new AttachmentCleanPasteModule(),
|
||||
new PreserveLinkAliasesModule(),
|
||||
];
|
||||
@@ -0,0 +1,143 @@
|
||||
import { Plugin, TFile, Vault, Setting } from 'obsidian';
|
||||
import type ZeroQoLModulesPlugin from '../../main';
|
||||
import { BaseModule } from '../base-module';
|
||||
import { locale } from '../../locales';
|
||||
|
||||
const MODULE_ID = 'preserve-link-aliases';
|
||||
|
||||
interface PreserveLinkAliasesSettings {
|
||||
processEmbeds: boolean;
|
||||
processCanvas: boolean;
|
||||
}
|
||||
|
||||
export class PreserveLinkAliasesModule extends BaseModule {
|
||||
id = MODULE_ID;
|
||||
name = locale.modules['preserve-link-aliases'].name;
|
||||
description = locale.modules['preserve-link-aliases'].description;
|
||||
|
||||
private vault: Vault;
|
||||
|
||||
get defaultSettings(): Record<string, unknown> {
|
||||
return {
|
||||
processEmbeds: false,
|
||||
processCanvas: false,
|
||||
};
|
||||
}
|
||||
|
||||
onload(plugin: ZeroQoLModulesPlugin, moduleSettings: Record<string, unknown>): void {
|
||||
this.vault = plugin.app.vault;
|
||||
const rawSettings = moduleSettings as PreserveLinkAliasesSettings;
|
||||
|
||||
const renameRef = plugin.app.vault.on(
|
||||
'rename',
|
||||
(file: TFile, oldPath: string) => this.handleRename(file, oldPath, rawSettings),
|
||||
);
|
||||
this.registerCleanup(() => plugin.app.vault.offref(renameRef));
|
||||
}
|
||||
|
||||
renderInlineSettings(
|
||||
containerEl: HTMLElement,
|
||||
settings: Record<string, unknown>,
|
||||
saveSettings: () => Promise<void>,
|
||||
): void {
|
||||
const loc = locale.modules['preserve-link-aliases'];
|
||||
const s = settings as unknown as PreserveLinkAliasesSettings;
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(loc.processEmbedsLabel)
|
||||
.setDesc(loc.processEmbedsDesc)
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(s.processEmbeds)
|
||||
.onChange(async (value) => {
|
||||
s.processEmbeds = value;
|
||||
await saveSettings();
|
||||
}),
|
||||
);
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(loc.processCanvasLabel)
|
||||
.setDesc(loc.processCanvasDesc)
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(s.processCanvas)
|
||||
.onChange(async (value) => {
|
||||
s.processCanvas = value;
|
||||
await saveSettings();
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
private async handleRename(
|
||||
file: TFile,
|
||||
oldPath: string,
|
||||
settings: PreserveLinkAliasesSettings,
|
||||
): Promise<void> {
|
||||
const oldName = oldPath.split('/').pop() ?? '';
|
||||
const newName = file.name;
|
||||
|
||||
const oldBasename = oldName.replace(/\.[^/.]+$/, '');
|
||||
const newBasename = newName.replace(/\.[^/.]+$/, '');
|
||||
|
||||
if (oldBasename === newBasename) return;
|
||||
|
||||
const files = this.getFilesToProcess(settings);
|
||||
|
||||
for (const f of files) {
|
||||
const content = await this.vault.read(f);
|
||||
const updated = this.processContent(content, newName, newBasename, oldBasename, settings);
|
||||
|
||||
if (updated !== content) {
|
||||
await this.vault.modify(f, updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private getFilesToProcess(settings: PreserveLinkAliasesSettings): TFile[] {
|
||||
const files: TFile[] = [];
|
||||
|
||||
for (const file of this.vault.getMarkdownFiles()) {
|
||||
files.push(file);
|
||||
}
|
||||
|
||||
if (settings.processCanvas) {
|
||||
const abstractFiles = this.vault.getFiles();
|
||||
for (const file of abstractFiles) {
|
||||
if (file.extension === 'canvas') {
|
||||
files.push(file as TFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
private processContent(
|
||||
content: string,
|
||||
newFileName: string,
|
||||
newBasename: string,
|
||||
oldBasename: string,
|
||||
settings: PreserveLinkAliasesSettings,
|
||||
): string {
|
||||
const wikiLinkRe = /(!?)\[\[([^\]]*?)]]/g;
|
||||
|
||||
return content.replace(wikiLinkRe, (fullMatch, prefix, inner) => {
|
||||
if (prefix === '!' && !settings.processEmbeds) {
|
||||
return fullMatch;
|
||||
}
|
||||
|
||||
const pipeIndex = inner.indexOf('|');
|
||||
if (pipeIndex === -1) return fullMatch;
|
||||
|
||||
const linkPath = inner.slice(0, pipeIndex);
|
||||
const alias = inner.slice(pipeIndex + 1);
|
||||
|
||||
if (alias !== newBasename) return fullMatch;
|
||||
|
||||
const linkFilename = linkPath.split('/').pop();
|
||||
if (linkFilename !== newFileName) return fullMatch;
|
||||
|
||||
return `${prefix}[[${linkPath}|${oldBasename}]]`;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user