add the notion of noteWatchFolder for auto sync

This commit is contained in:
Floper 2026-06-05 10:47:10 +02:00
parent 3001edd6ec
commit f36641da8a
2 changed files with 43 additions and 4 deletions

View file

@ -212,11 +212,24 @@ class VaultWriter {
async writeMarkdownFile(file: TFile, sn: SupernoteX, imgs: TFile[] | null, overwrite = false) {
let content = '';
// Derive the output path. When a mirror folder is configured, replicate the
// note's relative path under that folder; otherwise save alongside the note.
// Derive the output path. The mirror folder applies only when the file is
// inside the watched folder (or no watched folder is set). Files outside the
// watched folder always save alongside the note. When both are set, the
// watched folder prefix is stripped from the mirror path to avoid duplication.
const relMdPath = file.path.replace(/\.note$/i, '.md');
const mirrorFolder = this.settings.markdownMirrorFolder;
const baseFilename = mirrorFolder ? `${mirrorFolder}/${relMdPath}` : relMdPath;
const watchFolder = this.settings.noteWatchFolder.replace(/\/$/, '');
let baseFilename: string;
if (mirrorFolder && (!watchFolder || file.path.startsWith(watchFolder + '/'))) {
let mirrorRelPath = relMdPath;
if (watchFolder) {
const prefix = watchFolder + '/';
if (mirrorRelPath.startsWith(prefix)) mirrorRelPath = mirrorRelPath.slice(prefix.length);
}
baseFilename = `${mirrorFolder}/${mirrorRelPath}`;
} else {
baseFilename = relMdPath;
}
const dir = baseFilename.slice(0, baseFilename.length - `${file.basename}.md`.length);
let filename = baseFilename;
@ -755,6 +768,8 @@ export default class SupernotePlugin extends Plugin {
this.app.vault.on('create', (file) => {
if (!this.settings.isAutoSyncMarkdownEnabled) return;
if (!(file instanceof TFile) || file.extension !== 'note') return;
const wf = this.settings.noteWatchFolder.replace(/\/$/, '');
if (wf && !file.path.startsWith(wf + '/')) return;
vw.attachMarkdownFile(file, true).catch(e => console.error('Supernote auto-sync (create) error:', e));
})
);
@ -763,6 +778,8 @@ export default class SupernotePlugin extends Plugin {
this.app.vault.on('modify', (file) => {
if (!this.settings.isAutoSyncMarkdownEnabled) return;
if (!(file instanceof TFile) || file.extension !== 'note') return;
const wf = this.settings.noteWatchFolder.replace(/\/$/, '');
if (wf && !file.path.startsWith(wf + '/')) return;
const existing = syncDebounceMap.get(file.path);
if (existing) clearTimeout(existing);
syncDebounceMap.set(file.path, setTimeout(() => {

View file

@ -16,6 +16,7 @@ export interface SupernotePluginSettings extends CustomDictionarySettings {
isKeywordsAndLinksEnabled: boolean;
isHashtagsMentionsEnabled: boolean;
markdownMirrorFolder: string;
noteWatchFolder: string;
}
export const DEFAULT_SETTINGS: SupernotePluginSettings = {
@ -29,6 +30,7 @@ export const DEFAULT_SETTINGS: SupernotePluginSettings = {
isKeywordsAndLinksEnabled: true,
isHashtagsMentionsEnabled: true,
markdownMirrorFolder: '',
noteWatchFolder: '',
...CUSTOM_DICTIONARY_DEFAULT_SETTINGS,
}
@ -110,7 +112,11 @@ export class SupernoteSettingTab extends PluginSettingTab {
}),
);
new Setting(containerEl)
const autoSyncGroup = containerEl.createDiv();
autoSyncGroup.style.cssText =
'border: 1px solid var(--background-modifier-border); border-radius: var(--radius-m, 8px); overflow: hidden; margin-bottom: 16px;';
new Setting(autoSyncGroup)
.setName('Auto-sync .note files to markdown')
.setDesc(
'Automatically export recognized text to a .md file whenever a .note file is added or modified in the vault.',
@ -120,10 +126,26 @@ export class SupernoteSettingTab extends PluginSettingTab {
.setValue(this.plugin.settings.isAutoSyncMarkdownEnabled)
.onChange(async (value) => {
this.plugin.settings.isAutoSyncMarkdownEnabled = value;
watchFolderSetting.settingEl.style.display = value ? '' : 'none';
await this.plugin.saveSettings();
}),
);
const watchFolderSetting = new Setting(autoSyncGroup)
.setName('Watched folder')
.setDesc(
'Only auto-sync .note files inside this vault folder. Leave empty to watch the whole vault. When set alongside a mirror folder, the watched folder prefix is stripped from the mirror path. Example: Supernote',
)
.addText(text => text
.setPlaceholder('Supernote')
.setValue(this.plugin.settings.noteWatchFolder)
.onChange(async (value) => {
this.plugin.settings.noteWatchFolder = value.trim().replace(/\/$/, '');
await this.plugin.saveSettings();
})
);
watchFolderSetting.settingEl.style.display = this.plugin.settings.isAutoSyncMarkdownEnabled ? '' : 'none';
new Setting(containerEl)
.setName('Markdown mirror folder')
.setDesc(