mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 08:08:34 +08:00
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import { App, Notice, TFile } from "obsidian";
|
|
import ObsidianSyncNotionPlugin from "../main";
|
|
import { DatabaseDetails } from "../ui/settingTabs";
|
|
import { i18nConfig } from "src/lang/I18n";
|
|
import { ensureAutoSyncDatabaseEntry } from "src/utils/frontmatter";
|
|
|
|
export async function updateYamlInfo(
|
|
yamlContent: string,
|
|
nowFile: TFile,
|
|
res: any,
|
|
app: App,
|
|
plugin: ObsidianSyncNotionPlugin,
|
|
dbDetails: DatabaseDetails,
|
|
) {
|
|
let { url, id } = res;
|
|
// replace www to notionID
|
|
const { notionUser, NotionLinkDisplay } = plugin.settings;
|
|
const { abName } = dbDetails
|
|
const notionIDKey = `NotionID-${abName}`;
|
|
const linkKey = `link-${abName}`;
|
|
const autoSyncKey = plugin.getAutoSyncFrontmatterKey();
|
|
|
|
if (notionUser !== "") {
|
|
// replace url str "www" to notionID
|
|
url = url.replace("www.notion.so", `${notionUser}.notion.site`)
|
|
}
|
|
|
|
await app.fileManager.processFrontMatter(nowFile, yamlContent => {
|
|
if (yamlContent[notionIDKey]) {
|
|
delete yamlContent[notionIDKey]
|
|
}
|
|
if (yamlContent[linkKey]) {
|
|
delete yamlContent[linkKey]
|
|
}
|
|
// add new notionID and link
|
|
yamlContent[notionIDKey] = id;
|
|
(NotionLinkDisplay) ? yamlContent[linkKey] = url : null;
|
|
|
|
// ensure auto sync database list contains current short name
|
|
yamlContent[autoSyncKey] = ensureAutoSyncDatabaseEntry(
|
|
yamlContent[autoSyncKey],
|
|
abName
|
|
);
|
|
});
|
|
|
|
// copy url to clipboard only if autoCopyNotionLink is enabled
|
|
if (plugin.settings.autoCopyNotionLink) {
|
|
try {
|
|
await navigator.clipboard.writeText(url);
|
|
} catch (error) {
|
|
console.log(error);
|
|
new Notice(`${i18nConfig.CopyErrorMessage}`);
|
|
}
|
|
}
|
|
}
|