Skip to content

Commit

Permalink
refactor(frontmatter): prevent sharing /not sharing by fusion of fron…
Browse files Browse the repository at this point in the history
…tmatter

As by default, linked file metadata is merged with parent

fix #363
  • Loading branch information
Mara-Li committed Sep 22, 2024
1 parent 0b923fe commit 60ff8fe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
9 changes: 2 additions & 7 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
frontmatterFromFile,
frontmatterSettingsRepository,
getFrontmatterSettings,
getProperties,
getProperties, mergeFrontmatter,
} from "src/utils/parse_frontmatter";
import { ShareStatusBar } from "src/utils/status_bar";
import merge from "ts-deepmerge";
Expand Down Expand Up @@ -173,12 +173,7 @@ export default class Publisher {
const shareFiles = new FilesManagement(this.octokit, this.plugin);
let frontmatter = frontmatterFromFile(file, this.plugin, null);
if (!isShared(frontmatter, this.settings, file, repo.repository)) return false;
if (sourceFrontmatter && frontmatter)
frontmatter = merge.withOptions(
{ allowUndefinedOverrides: false },
sourceFrontmatter,
frontmatter
);
frontmatter = mergeFrontmatter(frontmatter, sourceFrontmatter, this.settings.plugin.shareKey);
const prop = getProperties(this.plugin, repo.repository, frontmatter);
const isNotEmpty = await checkEmptyConfiguration(prop, this.plugin);
repo.frontmatter = prop;
Expand Down
9 changes: 2 additions & 7 deletions src/commands/share/unique_note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
checkRepositoryValidityWithProperties,
isShared,
} from "src/utils/data_validation_test";
import { frontmatterFromFile, getProperties } from "src/utils/parse_frontmatter";
import {frontmatterFromFile, getProperties, mergeFrontmatter} from "src/utils/parse_frontmatter";
import merge from "ts-deepmerge";

/**
Expand Down Expand Up @@ -71,12 +71,7 @@ export async function shareOneNote(
const { settings, plugin } = PublisherManager;
const app = PublisherManager.plugin.app;
let frontmatter = frontmatterFromFile(file, PublisherManager.plugin, null);
if (sourceFrontmatter && frontmatter)
frontmatter = merge.withOptions(
{ allowUndefinedOverrides: false },
sourceFrontmatter,
frontmatter
);
frontmatter = mergeFrontmatter(frontmatter, sourceFrontmatter, PublisherManager.settings.plugin.shareKey);
try {
const prop = getProperties(plugin, repository, frontmatter);
let isValid: boolean;
Expand Down
16 changes: 4 additions & 12 deletions src/utils/data_validation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type Enveloppe from "src/main";
import {
frontmatterFromFile,
getLinkedFrontmatter,
getProperties,
getProperties, mergeFrontmatter,
} from "src/utils/parse_frontmatter";
import merge from "ts-deepmerge";
import { escapeRegex } from "../conversion/links";
Expand Down Expand Up @@ -85,13 +85,7 @@ export function getRepoSharedKey(
return defaultRepo(settings);
} else if (!frontmatter) return null;
const linkedFrontmatter = getLinkedFrontmatter(frontmatter, file, plugin);
frontmatter = linkedFrontmatter
? merge.withOptions(
{ allowUndefinedOverrides: false },
linkedFrontmatter,
frontmatter
)
: frontmatter;
frontmatter = mergeFrontmatter(frontmatter, linkedFrontmatter, settings.plugin.shareKey);
return (
allOtherRepo.find((repo) => frontmatter?.[repo.shareKey]) ?? defaultRepo(settings)
);
Expand Down Expand Up @@ -203,14 +197,12 @@ export function multipleSharedKey(
}
if (!frontmatter) return keysInFile;
const linkedRepo = getLinkedFrontmatter(frontmatter, file, plugin);
frontmatter = linkedRepo
? merge.withOptions({ allowUndefinedOverrides: false }, linkedRepo, frontmatter)
: frontmatter;
frontmatter = mergeFrontmatter(frontmatter, linkedRepo, settings.plugin.shareKey);
const allKey = settings.github.otherRepo.map((repo) => repo.shareKey);
allKey.push(settings.plugin.shareKey);

for (const key of allKey) {
if (frontmatter[key]) {
if (frontmatter?.[key]) {
keysInFile.push(key);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/parse_frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export function getProperties(
let github = repository ?? settings.github;
if (checkSet && repository && plugin.repositoryFrontmatter[repository.smartKey]) {
const setFrontmatter = plugin.repositoryFrontmatter[repository.smartKey];
delete setFrontmatter?.[settings.plugin.shareKey];
frontmatter = merge.withOptions(
{ allowUndefinedOverrides: false },
setFrontmatter ?? {},
Expand Down Expand Up @@ -567,6 +568,7 @@ export function frontmatterFromFile(
if (file) {
frontmatter = plugin.app.metadataCache.getFileCache(file)?.frontmatter;
const linkedFrontmatter = getLinkedFrontmatter(frontmatter, file, plugin);
delete linkedFrontmatter?.[plugin.settings.plugin.shareKey];
frontmatter = merge.withOptions(
{ allowUndefinedOverrides: false },
setFrontmatter ?? {},
Expand Down Expand Up @@ -694,3 +696,14 @@ function settingAttachment(
}
return settingsConversion;
}

export function mergeFrontmatter(frontmatter: FrontMatterCache | null, sourceFrontmatter: FrontMatterCache | null | undefined, shareKey: string) {
delete sourceFrontmatter?.[shareKey];
if (sourceFrontmatter && frontmatter)
frontmatter = merge.withOptions(
{allowUndefinedOverrides: false},
sourceFrontmatter,
frontmatter
);
return frontmatter;
}

0 comments on commit 60ff8fe

Please sign in to comment.