From e3d5c075afa4807654cab810c412111d386bcdd0 Mon Sep 17 00:00:00 2001 From: Mara Date: Wed, 24 Jan 2024 14:17:39 +0100 Subject: [PATCH] Update parse_frontmatter utility functions --- src/commands/callback.ts | 6 +++--- src/utils/data_validation_test.ts | 7 ++----- src/utils/parse_frontmatter.ts | 12 ++++++++++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/commands/callback.ts b/src/commands/callback.ts index cd1fe070..ef406f15 100644 --- a/src/commands/callback.ts +++ b/src/commands/callback.ts @@ -11,7 +11,7 @@ import GithubPublisher from "../main"; import {MonoRepoProperties, MultiRepoProperties, Repository} from "../settings/interface"; import {createLink, logs} from "../utils"; import {checkRepositoryValidity, isShared} from "../utils/data_validation_test"; -import { getRepoFrontmatter } from "../utils/parse_frontmatter"; +import { frontmatterFromFile, getRepoFrontmatter } from "../utils/parse_frontmatter"; import {purgeNotesRemote, shareOneNote} from "."; import {shareEditedOnly, uploadAllEditedNotes, uploadAllNotes, uploadNewNotes} from "./plugin_commands"; @@ -32,7 +32,7 @@ export async function createLinkCallback(repo: Repository | null, plugin: Github hotkeys: [], checkCallback: (checking) => { const file = plugin.app.workspace.getActiveFile(); - const frontmatter = file ? plugin.app.metadataCache.getFileCache(file)?.frontmatter : null; + const frontmatter = frontmatterFromFile(file, plugin); if ( file && frontmatter && isShared(frontmatter, plugin.settings, file, repo) ) { @@ -115,7 +115,7 @@ export async function shareOneNoteCallback(repo: Repository|null, plugin: Github hotkeys: [], checkCallback: (checking) => { const file = plugin.app.workspace.getActiveFile(); - const frontmatter = file ? plugin.app.metadataCache.getFileCache(file)?.frontmatter : null; + const frontmatter = frontmatterFromFile(file, plugin); if ( file && frontmatter && isShared(frontmatter, plugin.settings, file, repo) ) { diff --git a/src/utils/data_validation_test.ts b/src/utils/data_validation_test.ts index 7431c8ca..fc50c217 100644 --- a/src/utils/data_validation_test.ts +++ b/src/utils/data_validation_test.ts @@ -6,7 +6,7 @@ import GithubPublisher from "src/main"; import {GithubBranch} from "../GitHub/branch"; import {FIND_REGEX, FrontmatterConvert, GitHubPublisherSettings, MultiProperties, RepoFrontmatter, Repository} from "../settings/interface"; import {logs, notif} from "."; -import { getLinkedFrontmatter, getRepoFrontmatter } from "./parse_frontmatter"; +import { frontmatterFromFile, getLinkedFrontmatter, getRepoFrontmatter } from "./parse_frontmatter"; /** * - Check if the file is a valid file to publish @@ -297,11 +297,8 @@ export async function checkRepositoryValidity( file: TFile | null, silent: boolean=false): Promise { const settings = PublisherManager.settings; - const metadataCache = PublisherManager.plugin.app.metadataCache; try { - let frontmatter = file ? metadataCache.getFileCache(file)?.frontmatter : undefined; - const linkedFrontmatter = getLinkedFrontmatter(frontmatter, settings, file, PublisherManager.plugin.app); - frontmatter = linkedFrontmatter ? {...linkedFrontmatter, ...frontmatter} : frontmatter; + const frontmatter = frontmatterFromFile(file, PublisherManager.plugin); const repoFrontmatter = getRepoFrontmatter(settings, repository, file, PublisherManager.plugin.app, frontmatter); const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, PublisherManager.plugin, silent); if (isNotEmpty) { diff --git a/src/utils/parse_frontmatter.ts b/src/utils/parse_frontmatter.ts index b860d946..79384955 100644 --- a/src/utils/parse_frontmatter.ts +++ b/src/utils/parse_frontmatter.ts @@ -4,6 +4,7 @@ */ import { App, FrontMatterCache, normalizePath,TFile } from "obsidian"; +import GithubPublisher from "src/main"; import { FolderSettings, FrontmatterConvert, GitHubPublisherSettings, Path, RepoFrontmatter, Repository } from "../settings/interface"; @@ -505,7 +506,14 @@ export function getLinkedFrontmatter( const linked = metadataCache.getFileCache(linkedFrontmatterFile)?.frontmatter; if (!linked) return originalFrontmatter; return linked; +} - - +export function frontmatterFromFile(file: TFile | null, plugin: GithubPublisher) { + let frontmatter = null; + if (file) { + frontmatter = plugin.app.metadataCache.getFileCache(file)?.frontmatter; + const linkedFrontmatter = getLinkedFrontmatter(frontmatter, plugin.settings, file, plugin.app); + frontmatter = linkedFrontmatter ? { ...linkedFrontmatter, ...frontmatter } : frontmatter; + } + return frontmatter; } \ No newline at end of file