date-prefix and sticky tail v0.6 for Obsidian v1.12.7 and v1.13.0 (min v1.0.0)
This commit is contained in:
49
src/main.ts
49
src/main.ts
@@ -28,17 +28,31 @@ import {
|
||||
Notice,
|
||||
moment,
|
||||
} from 'obsidian';
|
||||
import { extractTimeId, generateSlug, buildFilename } from './slug';
|
||||
import {
|
||||
extractPrefix,
|
||||
computeBasename,
|
||||
generateSlug,
|
||||
buildFilename,
|
||||
} from './slug';
|
||||
import {
|
||||
ZettelcleanSettings,
|
||||
DEFAULT_SETTINGS,
|
||||
ZettelcleanSettingTab,
|
||||
} from './settings';
|
||||
|
||||
const SETTLE_MS = 4000; // rename ~4s after the H1 stops changing
|
||||
const COOLDOWN_MS = 6000; // do not rename the same path again within 6s
|
||||
|
||||
export default class ZettelcleanPlugin extends Plugin {
|
||||
settings!: ZettelcleanSettings;
|
||||
private renameTimers = new Map<string, number>();
|
||||
private lastRenamedAt = new Map<string, number>();
|
||||
private isRenameInProgress = false;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
this.addSettingTab(new ZettelcleanSettingTab(this.app, this));
|
||||
|
||||
// Fires after the metadata cache reparses a note, so headings[] is fresh.
|
||||
this.registerEvent(
|
||||
this.app.metadataCache.on('changed', (file) =>
|
||||
@@ -59,6 +73,18 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
this.renameTimers.clear();
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign(
|
||||
{},
|
||||
DEFAULT_SETTINGS,
|
||||
(await this.loadData()) as Partial<ZettelcleanSettings>,
|
||||
);
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
|
||||
private pathFor(file: TFile, base: string): string {
|
||||
const dir =
|
||||
file.parent && file.parent.path !== '/' ? `${file.parent.path}/` : '';
|
||||
@@ -70,7 +96,8 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
private scheduleSync(file: TAbstractFile) {
|
||||
if (this.isRenameInProgress) return; // our own rename, ignore
|
||||
if (!(file instanceof TFile) || file.extension !== 'md') return;
|
||||
if (extractTimeId(file.basename) === null) return; // opt-in gate: no TIMEID
|
||||
const allowDate = this.settings.prefixMode === 'date';
|
||||
if (extractPrefix(file.basename, allowDate) === null) return; // opt-in gate
|
||||
if (this.app.workspace.getActiveFile() !== file) return; // only the edited note
|
||||
|
||||
const last = this.lastRenamedAt.get(file.path);
|
||||
@@ -87,24 +114,27 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
);
|
||||
}
|
||||
|
||||
// Compute the target basename, bail if unchanged, otherwise rename.
|
||||
// Compute the target basename via the sticky rule, bail if unchanged, else rename.
|
||||
private async syncNow(file: TFile) {
|
||||
const id = extractTimeId(file.basename);
|
||||
if (id === null) return; // prefix removed meanwhile
|
||||
|
||||
const first = this.app.metadataCache.getFileCache(file)?.headings?.[0];
|
||||
const h1 = first && first.level === 1 ? first.heading : '';
|
||||
const allowDate = this.settings.prefixMode === 'date';
|
||||
|
||||
const newBase = buildFilename(id, generateSlug(h1));
|
||||
if (newBase === file.basename) return; // nothing to do
|
||||
const newBase = computeBasename(file.basename, h1, allowDate);
|
||||
if (newBase === null || newBase === file.basename) return; // unrecognized or no change
|
||||
|
||||
const newPath = this.pathFor(file, newBase);
|
||||
if (this.app.vault.getAbstractFileByPath(newPath)) {
|
||||
new Notice(`Can't rename to "${newBase}.md" - that name already exists`);
|
||||
return; // conflict: keep current name, tell the user
|
||||
}
|
||||
this.isRenameInProgress = true;
|
||||
try {
|
||||
await this.app.fileManager.renameFile(file, newPath);
|
||||
this.lastRenamedAt.set(newPath, Date.now());
|
||||
} catch (e) {
|
||||
console.error('zettelclean: rename failed', e);
|
||||
new Notice('Rename failed (see console)');
|
||||
} finally {
|
||||
this.isRenameInProgress = false;
|
||||
}
|
||||
@@ -112,7 +142,8 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
|
||||
private addTimeIdMenu(menu: Menu, file: TAbstractFile) {
|
||||
if (!(file instanceof TFile) || file.extension !== 'md') return;
|
||||
if (extractTimeId(file.basename) !== null) return; // already a zettel
|
||||
const allowDate = this.settings.prefixMode === 'date';
|
||||
if (extractPrefix(file.basename, allowDate) !== null) return; // already a zettel
|
||||
menu.addItem((item) =>
|
||||
item
|
||||
.setTitle('Prefix timestamp to filename')
|
||||
|
||||
Reference in New Issue
Block a user