Skip to content

Commit

Permalink
Update parse_frontmatter utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jan 24, 2024
1 parent 9918880 commit e3d5c07
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/commands/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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)
) {
Expand Down Expand Up @@ -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)
) {
Expand Down
7 changes: 2 additions & 5 deletions src/utils/data_validation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -297,11 +297,8 @@ export async function checkRepositoryValidity(
file: TFile | null,
silent: boolean=false): Promise<boolean> {
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) {
Expand Down
12 changes: 10 additions & 2 deletions src/utils/parse_frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
}

0 comments on commit e3d5c07

Please sign in to comment.