add: auto-sync tags

resolve: #1024
This commit is contained in:
windingwind 2024-07-10 10:08:00 +08:00
parent db4056d89a
commit 2d5b78f648
12 changed files with 66 additions and 3 deletions

View File

@ -72,6 +72,11 @@
preference="__prefsPrefix__.syncAttachmentFolder" preference="__prefsPrefix__.syncAttachmentFolder"
></html:input> ></html:input>
</hbox> </hbox>
<checkbox
data-l10n-id="sync-updateTags"
native="true"
preference="__prefsPrefix__.sync.updateTags"
/>
<hbox align="center"> <hbox align="center">
<button <button
onclick="Zotero.__addonInstance__.hooks.onShowSyncManager()" onclick="Zotero.__addonInstance__.hooks.onShowSyncManager()"

View File

@ -16,6 +16,10 @@ sync-period-label = Auto-sync period (seconds)
sync-attachmentFolder-label = Attachment folder sync-attachmentFolder-label = Attachment folder
sync-manager = sync-manager =
.label = Open Sync Manager .label = Open Sync Manager
sync-updateTags =
.label = Always update tags
.tooltiptext = Keep note tags in sync with tags in YAML header of the markdown file
template-title = Template template-title = Template
template-editor = template-editor =
.label = Open Template Editor .label = Open Template Editor

View File

@ -16,6 +16,10 @@ sync-period-label = Intervallo della sincronizzazione automatica (secondi)
sync-attachmentFolder-label = Cartella degli allegati sync-attachmentFolder-label = Cartella degli allegati
sync-manager = sync-manager =
.label = Apri Manager di sincronizzazione .label = Apri Manager di sincronizzazione
sync-updateTags =
.label = Aggiorna sempre i tag
.tooltiptext = Keep note tags in sync with tags in YAML header of the markdown file
template-title = Template template-title = Template
template-editor = template-editor =
.label = Apri Editor dei template .label = Apri Editor dei template

View File

@ -16,6 +16,10 @@ sync-period-label = Авто-синк период (сек)
sync-attachmentFolder-label = Attachment folder sync-attachmentFolder-label = Attachment folder
sync-manager = sync-manager =
.label = Открыть Синк менеджер .label = Открыть Синк менеджер
sync-updateTags =
.label = Всегда обновлять теги
.tooltiptext = Keep note tags in sync with tags in YAML header of the markdown file
template-title = Шаблон template-title = Шаблон
template-editor = template-editor =
.label = Открыть редактор шаблонов .label = Открыть редактор шаблонов

View File

@ -16,6 +16,10 @@ sync-period-label = Oto-Senkronize aralığı (saniye)
sync-attachmentFolder-label = Ek Klasörü sync-attachmentFolder-label = Ek Klasörü
sync-manager = sync-manager =
.label = Senkronizasyon Yönetimini Aç .label = Senkronizasyon Yönetimini Aç
sync-updateTags =
.label = Her zaman etiketleri güncelle
.tooltiptext = Keep note tags in sync with tags in YAML header of the markdown file
template-title = Şablon template-title = Şablon
template-editor = template-editor =
.label = Şablon Düzenleyiciyi Aç .label = Şablon Düzenleyiciyi Aç

View File

@ -16,6 +16,10 @@ sync-period-label = 自动同步周期 (秒)
sync-attachmentFolder-label = 附件文件夹 sync-attachmentFolder-label = 附件文件夹
sync-manager = sync-manager =
.label = 打开同步管理器 .label = 打开同步管理器
sync-updateTags =
.label = 总是更新标签
.tooltiptext = 保持笔记标签与 markdown 的 YAML 头标签同步
template-title = 模板 template-title = 模板
template-editor = template-editor =
.label = 打开模板编辑器 .label = 打开模板编辑器

View File

@ -1,6 +1,7 @@
pref("__prefsPrefix__.syncNoteIds", ""); pref("__prefsPrefix__.syncNoteIds", "");
pref("__prefsPrefix__.syncPeriodSeconds", 30); pref("__prefsPrefix__.syncPeriodSeconds", 30);
pref("__prefsPrefix__.syncAttachmentFolder", "attachments"); pref("__prefsPrefix__.syncAttachmentFolder", "attachments");
pref("__prefsPrefix__.sync.updateTags", false);
pref("__prefsPrefix__.autoAnnotation", false); pref("__prefsPrefix__.autoAnnotation", false);

View File

@ -1,5 +1,6 @@
import { addLineToNote } from "../../utils/note"; import { addLineToNote } from "../../utils/note";
import { config } from "../../../package.json"; import { config } from "../../../package.json";
import { getPref } from "../../utils/prefs";
export async function fromMD( export async function fromMD(
filepath: string, filepath: string,
@ -67,5 +68,15 @@ export async function fromMD(
}, },
}); });
} }
if (getPref("sync.updateTags")) {
const tags = mdStatus.meta?.tags || [];
noteItem.setTags(tags);
await noteItem.saveTx({
notifierData: {
autoSyncDelay: Zotero.Notes.AUTO_SYNC_DELAY,
},
});
}
return noteItem; return noteItem;
} }

View File

@ -246,7 +246,18 @@ async function doCompare(
} else if (MDAhead) { } else if (MDAhead) {
return SyncCode.MDAhead; return SyncCode.MDAhead;
} else { } else {
return SyncCode.UpToDate; const maxLastModifiedPeriod = 3000;
if (
mdStatus.lastmodify &&
syncStatus.lastsync &&
// If the file is modified after the last sync, it's ahead
Math.abs(mdStatus.lastmodify.getTime() - syncStatus.lastsync) >
maxLastModifiedPeriod
) {
return SyncCode.MDAhead;
} else {
return SyncCode.UpToDate;
}
} }
} }

View File

@ -1,5 +1,6 @@
import YAML = require("yamljs"); import YAML = require("yamljs");
import { getNoteLink } from "../../utils/link"; import { getNoteLink } from "../../utils/link";
import { getPref } from "../../utils/prefs";
export { renderTemplatePreview }; export { renderTemplatePreview };
@ -55,6 +56,12 @@ async function renderTemplatePreview(
libraryID: data.libraryID, libraryID: data.libraryID,
itemKey: data.key, itemKey: data.key,
}); });
if (getPref("sync.updateTags")) {
const tags = data.getTags();
if (tags.length) {
header.tags = tags.map((tag) => tag.tag);
}
}
html = `<pre>${YAML.stringify(header, 10)}</pre>`; html = `<pre>${YAML.stringify(header, 10)}</pre>`;
} }
} else if (templateName.includes("ExportMDFileContent")) { } else if (templateName.includes("ExportMDFileContent")) {

View File

@ -126,6 +126,13 @@ async function note2md(
$libraryID: noteItem.libraryID, $libraryID: noteItem.libraryID,
$itemKey: noteItem.key, $itemKey: noteItem.key,
}); });
if (getPref("sync.updateTags")) {
const tags = noteItem.getTags();
header.tags = [];
if (tags.length) {
tags.forEach((tag) => header.tags.push(tag.tag));
}
}
const yamlFrontMatter = `---\n${YAML.stringify(header, 10)}\n---`; const yamlFrontMatter = `---\n${YAML.stringify(header, 10)}\n---`;
md = `${yamlFrontMatter}\n${md}`; md = `${yamlFrontMatter}\n${md}`;
} }
@ -192,7 +199,7 @@ async function link2html(
usePosition?: boolean; usePosition?: boolean;
} = {}, } = {},
) { ) {
ztoolkit.log("link2html", link, options); ztoolkit.log("link2html", link);
const linkParams = getNoteLinkParams(link); const linkParams = getNoteLinkParams(link);
if (!linkParams.noteItem) { if (!linkParams.noteItem) {
return ""; return "";
@ -1223,7 +1230,7 @@ async function processM2NRehypeImageNodes(
src = jointPath(fileDir, src); src = jointPath(fileDir, src);
} }
if (!(await fileExists(src))) { if (!(await fileExists(src))) {
ztoolkit.log("parse image, path invalid", src); ztoolkit.log("parse image, path invalid", src.slice(0, 50));
continue; continue;
} }
} }

1
typings/note.d.ts vendored
View File

@ -28,6 +28,7 @@ declare interface MDStatus {
$version: number; $version: number;
$libraryID?: number; $libraryID?: number;
$itemKey?: string; $itemKey?: string;
tags?: string[];
} | null; } | null;
content: string; content: string;
filedir: string; filedir: string;