Skip to content

Commit

Permalink
feat: allow to "link" frontmatter to another, preventing to recopy lo…
Browse files Browse the repository at this point in the history
…ng frontmatter (#281)
  • Loading branch information
Mara-Li authored Jan 24, 2024
1 parent e708501 commit 6ec7f36
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 116 deletions.
2 changes: 2 additions & 0 deletions src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export class FilesManagement extends Publisher {
const repoFrontatter = getRepoFrontmatter(
this.settings,
repo,
file,
this.plugin.app,
frontMatter
);
const filepath = getReceiptFolder(file, this.settings, repo, this.plugin.app, repoFrontatter);
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class Publisher {
this.plugin,
);
const frontmatter = this.metadataCache.getFileCache(file)?.frontmatter;
const repoFrontmatter = getRepoFrontmatter(this.settings, repo.repo, frontmatter);
const repoFrontmatter = getRepoFrontmatter(this.settings, repo.repo, file, this.plugin.app, frontmatter);
const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, this.plugin);
repo.frontmatter = repoFrontmatter;
if (
Expand Down
10 changes: 5 additions & 5 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,13 +32,13 @@ 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)
) {
if (!checking) {
const multiRepo: MultiRepoProperties = {
frontmatter: getRepoFrontmatter(plugin.settings, repo, frontmatter),
frontmatter: getRepoFrontmatter(plugin.settings, repo, file, plugin.app, frontmatter),
repo,
};
createLink(
Expand Down Expand Up @@ -78,7 +78,7 @@ export async function purgeNotesRemoteCallback(plugin: GithubPublisher, repo: Re
//@ts-ignore
callback: async () => {
logs({settings: plugin.settings}, "Enabling purge command");
const frontmatter = getRepoFrontmatter(plugin.settings, repo);
const frontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app);
const monoRepo: MonoRepoProperties = {
frontmatter: Array.isArray(frontmatter) ? frontmatter[0] : frontmatter,
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
122 changes: 63 additions & 59 deletions src/commands/file_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Menu, MenuItem, Platform, TFile, TFolder} from "obsidian";
import GithubPublisher from "../main";
import {MonoRepoProperties, Repository} from "../settings/interface";
import {defaultRepo, getRepoSharedKey, isExcludedPath, isInDryRunFolder, isShared, multipleSharedKey} from "../utils/data_validation_test";
import { getRepoFrontmatter } from "../utils/parse_frontmatter";
import { getLinkedFrontmatter, getRepoFrontmatter } from "../utils/parse_frontmatter";
import {shareAllMarkedNotes, shareOneNote} from ".";
import {ChooseRepoToRun} from "./suggest_other_repo_commands_modal";

Expand All @@ -18,7 +18,7 @@ import {ChooseRepoToRun} from "./suggest_other_repo_commands_modal";
export async function shareFolderRepo(plugin: GithubPublisher, folder: TFolder, branchName: string, repo: Repository | null) {
const publisher = await plugin.reloadOctokit();
const statusBarItems = plugin.addStatusBarItem();
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, undefined);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app, undefined);
const monoProperties: MonoRepoProperties = {
frontmatter: Array.isArray(repoFrontmatter) ? repoFrontmatter[0] : repoFrontmatter,
repo,
Expand Down Expand Up @@ -55,7 +55,7 @@ export function addSubMenuCommandsFolder(plugin: GithubPublisher, item: MenuItem
}))
.setIcon("folder-up")
.onClick(async () => {
const repo = getRepoSharedKey(plugin.settings, undefined);
const repo = getRepoSharedKey(plugin.settings, plugin.app, undefined);
await shareFolderRepo(plugin, folder, branchName, repo);
});
});
Expand Down Expand Up @@ -99,65 +99,67 @@ export function addSubMenuCommandsFolder(plugin: GithubPublisher, item: MenuItem
* @param {Menu} menu - The menu to add the item to
*/
export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: string, menu: Menu) {
const frontmatter = file instanceof TFile ? plugin.app.metadataCache.getFileCache(file)!.frontmatter : undefined;
let getSharedKey = getRepoSharedKey(plugin.settings, frontmatter);
const allKeysFromFile = multipleSharedKey(frontmatter, plugin.settings);
const frontmatter = plugin.app.metadataCache.getFileCache(file)?.frontmatter;
let getSharedKey = getRepoSharedKey(plugin.settings, plugin.app, frontmatter, file);
const allKeysFromFile = multipleSharedKey(frontmatter, plugin.settings, file, plugin.app);
if (
isShared(frontmatter, plugin.settings, file, getSharedKey) &&
plugin.settings.plugin.fileMenu
!(isShared(frontmatter, plugin.settings, file, getSharedKey) &&
plugin.settings.plugin.fileMenu)
) {
const repoFrontmatter = getRepoFrontmatter(plugin.settings, getSharedKey, frontmatter);
menu.addItem((item) => {
/**
* Create a submenu if multiple repo exists in the settings & platform is desktop
*/
return;
}
const repoFrontmatter = getRepoFrontmatter(plugin.settings, getSharedKey, file, plugin.app, frontmatter);

if (allKeysFromFile.length > 1 || (repoFrontmatter instanceof Array && repoFrontmatter.length > 1)) {
if (Platform.isDesktop) {
item
.setTitle("Github Publisher")
.setIcon("upload-cloud");
} else {
//add the line to separate the commands
menu.addSeparator();
item.setIsLabel(true);
}
subMenuCommandsFile(
plugin,
item,
file,
menu.addItem((item) => {
/**
* Create a submenu if multiple repo exists in the settings & platform is desktop
*/

if (allKeysFromFile.length > 1 || (repoFrontmatter instanceof Array && repoFrontmatter.length > 1)) {
if (Platform.isDesktop) {
item
.setTitle("Github Publisher")
.setIcon("upload-cloud");
} else {
//add the line to separate the commands
menu.addSeparator();
item.setIsLabel(true);
}
subMenuCommandsFile(
plugin,
item,
file,
branchName,
getSharedKey,
menu
);
return;
}
const fileName = plugin.getTitleFieldForCommand(file, plugin.app.metadataCache.getFileCache(file)?.frontmatter).replace(".md", "");

if (!frontmatter || !frontmatter[plugin.settings.plugin.shareKey]) {
const otherRepo = plugin.settings.github.otherRepo.find((repo) => repo.shareAll?.enable);
if (otherRepo) getSharedKey = otherRepo;
else if (plugin.settings.plugin.shareAll?.enable) getSharedKey = defaultRepo(plugin.settings);
} else if (frontmatter[plugin.settings.plugin.shareKey]) {
getSharedKey = defaultRepo(plugin.settings);
}
item
.setTitle(i18next.t("commands.shareViewFiles.multiple.on", {
doc: fileName,
smartKey: getSharedKey?.smartKey.toUpperCase() || i18next.t("common.default").toUpperCase()
}))
.setIcon("file-up")
.onClick(async () => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
file,
getSharedKey,
menu
fileName
);
return;
}
const fileName = plugin.getTitleFieldForCommand(file, plugin.app.metadataCache.getFileCache(file)?.frontmatter).replace(".md", "");

if (!frontmatter || !frontmatter[plugin.settings.plugin.shareKey]) {
const otherRepo = plugin.settings.github.otherRepo.find((repo) => repo.shareAll?.enable);
if (otherRepo) getSharedKey = otherRepo;
else if (plugin.settings.plugin.shareAll?.enable) getSharedKey = defaultRepo(plugin.settings);
} else if (frontmatter[plugin.settings.plugin.shareKey]) {
getSharedKey = defaultRepo(plugin.settings);
}
item
.setTitle(i18next.t("commands.shareViewFiles.multiple.on", {
doc: fileName,
smartKey: getSharedKey?.smartKey.toUpperCase() || i18next.t("common.default").toUpperCase()
}))
.setIcon("file-up")
.onClick(async () => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
file,
getSharedKey,
fileName
);
});
});
}
});
});
}

/**
Expand All @@ -172,11 +174,13 @@ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: st
* @return {Menu} - The submenu created
*/
export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, file: TFile, branchName: string, repo: Repository | null, originalMenu: Menu): Menu {
const frontmatter = plugin.app.metadataCache.getFileCache(file)?.frontmatter;
let frontmatter = plugin.app.metadataCache.getFileCache(file)?.frontmatter;
const linkedFrontmatter = getLinkedFrontmatter(frontmatter, plugin.settings, file, plugin.app);
frontmatter = linkedFrontmatter ? { ...linkedFrontmatter, ...frontmatter } : frontmatter;
const fileName = plugin.getTitleFieldForCommand(file, frontmatter).replace(".md", "");
//@ts-ignore
const subMenu = Platform.isDesktop ? item.setSubmenu() as Menu : originalMenu;
let repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, frontmatter);
let repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, file, plugin.app, frontmatter);
repoFrontmatter = repoFrontmatter instanceof Array ? repoFrontmatter : [repoFrontmatter];
/**
* default repo
Expand Down Expand Up @@ -306,7 +310,7 @@ export async function addMenuFolder(menu: Menu, folder: TFolder, branchName: str
}))
.setIcon("folder-up")
.onClick(async () => {
const repo = getRepoSharedKey(plugin.settings, undefined);
const repo = getRepoSharedKey(plugin.settings, plugin.app, undefined);
await shareFolderRepo(plugin, folder, branchName, repo);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export async function shareOneNote(
const metadataCache = app.metadataCache;
try {
const frontmatter = metadataCache.getFileCache(file)?.frontmatter;
const repoFrontmatter = getRepoFrontmatter(settings, repository, frontmatter);
const repoFrontmatter = getRepoFrontmatter(settings, repository, file, PublisherManager.plugin.app, frontmatter);
const isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, repoFrontmatter);
const multiRepo: MultiRepoProperties = {
frontmatter: repoFrontmatter,
Expand Down Expand Up @@ -243,7 +243,7 @@ export async function shareOneNote(
logs({settings, e: true}, error);
const notif = document.createDocumentFragment();
notif.createSpan({ cls: ["error", "obsidian-publisher", "icons", "notification"] }).innerHTML = ERROR_ICONS;
notif.createSpan({ cls: ["error", "obsidian-publisher", "notification"] }).innerHTML = i18next.t("error.errorPublish", { repo: getRepoFrontmatter(settings, repository, metadataCache.getFileCache(file)?.frontmatter) });
notif.createSpan({ cls: ["error", "obsidian-publisher", "notification"] }).innerHTML = i18next.t("error.errorPublish", { repo: getRepoFrontmatter(settings, repository, file, app, metadataCache.getFileCache(file)?.frontmatter) });
new Notice(notif);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/commands/plugin_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function createLinkOnActiveFile(repo: Repository | null, plugin: Gi
file && frontmatter && isShared(frontmatter, plugin.settings, file, repo)
) {
const multiRepo: MultiRepoProperties = {
frontmatter: getRepoFrontmatter(plugin.settings, repo, frontmatter),
frontmatter: getRepoFrontmatter(plugin.settings, repo, file, plugin.app, frontmatter),
repo
};
await createLink(
Expand Down Expand Up @@ -80,7 +80,7 @@ export async function shareActiveFile(plugin: GithubPublisher, repo: Repository
* @return {Promise<void>}
*/
export async function deleteCommands(plugin : GithubPublisher, repo: Repository | null, branchName: string): Promise<void> {
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app);
const publisher = await plugin.reloadOctokit();
const mono: MonoRepoProperties = {
frontmatter: Array.isArray(repoFrontmatter) ? repoFrontmatter[0] : repoFrontmatter,
Expand All @@ -106,7 +106,7 @@ export async function uploadAllNotes(plugin: GithubPublisher, repo: Repository |
const statusBarItems = plugin.addStatusBarItem();
const publisher = await plugin.reloadOctokit();
const sharedFiles = publisher.getSharedFiles(repo);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app);
const mono: MonoRepoProperties = {
frontmatter: Array.isArray(repoFrontmatter) ? repoFrontmatter[0] : repoFrontmatter,
repo
Expand All @@ -132,7 +132,7 @@ export async function uploadAllNotes(plugin: GithubPublisher, repo: Repository |

export async function uploadNewNotes(plugin: GithubPublisher, branchName: string, repo: Repository|null): Promise<void> {
const publisher = await plugin.reloadOctokit();
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app);
await shareNewNote(
publisher,
branchName,
Expand Down Expand Up @@ -173,7 +173,7 @@ export async function repositoryValidityActiveFile(plugin:GithubPublisher, branc
*/
export async function uploadAllEditedNotes(plugin: GithubPublisher ,branchName: string, repo: Repository|null=null): Promise<void> {
const publisher = await plugin.reloadOctokit();
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app);

await shareAllEditedNotes(
publisher,
Expand All @@ -195,7 +195,7 @@ export async function uploadAllEditedNotes(plugin: GithubPublisher ,branchName:
*/
export async function shareEditedOnly(branchName: string, repo: Repository|null, plugin: GithubPublisher) {
const publisher = await plugin.reloadOctokit();
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app);
await shareOnlyEdited(
publisher,
branchName,
Expand Down
4 changes: 2 additions & 2 deletions src/conversion/file_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function createRelativePath(
const shortRepo = properties.repository;
const sourcePath = getReceiptFolder(sourceFile, settings, shortRepo, app, properties.frontmatter.repo);
const frontmatterTarget = metadataCache.getFileCache(targetFile.linked)!.frontmatter;
const targetRepo = getRepoFrontmatter(settings, shortRepo, frontmatterTarget);
const targetRepo = getRepoFrontmatter(settings, shortRepo, targetFile.linked, app, frontmatterTarget);
const isFromAnotherRepo = checkIfRepoIsInAnother(properties.frontmatter.repo, targetRepo);
const shared = isInternalShared(
frontmatterTarget,
Expand Down Expand Up @@ -376,7 +376,7 @@ export function getReceiptFolder(
const { vault, metadataCache } = app;
if (file.extension === "md") {
const frontmatter = metadataCache.getCache(file.path)?.frontmatter;
if (!repoFrontmatter) repoFrontmatter = getRepoFrontmatter(settings, otherRepo, frontmatter);
if (!repoFrontmatter) repoFrontmatter = getRepoFrontmatter(settings, otherRepo, file, app, frontmatter);
repoFrontmatter = repoFrontmatter instanceof Array ? repoFrontmatter : [repoFrontmatter];
let targetRepo = repoFrontmatter.find((repo) => repo.path?.smartkey === otherRepo?.smartKey || "default");
if (!targetRepo) targetRepo = repoFrontmatter[0];
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@
"desc": "Allows you to reopen the settings on the previously used tab",
"title": "Save tab"
},
"set": {
"desc": "Choose the property key you want to use to link the property of a file to another, without rewrite them each time. Work only for file linked by a wikilink in the frontmatter.",
"title": "Set of options"
},
"shareKey": {
"all": {
"desc": "Share all files regardless of the state of the share key of the notes",
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@
"desc": "Permet de rouvrir les paramètres sur l'onglet précédemment utilisé",
"title": "Sauvegarder l'onglet"
},
"set": {
"desc": "Choisissez la clé de propriété que vous souhaitez utiliser pour lier la propriété d'un fichier à un autre, sans les réécrire à chaque fois. Ne fonctionne que pour les fichiers liés par un lien wiki dans le frontmatter.",
"title": "Set d'options"
},
"shareKey": {
"all": {
"desc": "Autoriser le partage de tous les fichiers et ignorer l'état de la clé de partage",
Expand Down
13 changes: 13 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,19 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
})
);

new Setting(this.settingsPage)
.setName(i18next.t("settings.plugin.set.title"))
.setDesc(i18next.t("settings.plugin.set.desc"))
.addText((text) =>
text
.setPlaceholder("Set")
.setValue(pluginSettings.setFrontmatterKey)
.onChange(async (value) => {
pluginSettings.setFrontmatterKey = value.trim();
await this.plugin.saveSettings();
})
);

this.settingsPage.createEl("h3", {text: i18next.t("settings.plugin.head.menu")});

new Setting(this.settingsPage)
Expand Down
2 changes: 2 additions & 0 deletions src/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface GitHubPublisherSettings {
displayModalRepoEditing: boolean;
migrated?: boolean;
saveTabId?: boolean;
setFrontmatterKey: string;

}
}
Expand Down Expand Up @@ -300,6 +301,7 @@ export const DEFAULT_SETTINGS: Partial<GitHubPublisherSettings> = {
},
noticeError: false,
displayModalRepoEditing: false,
setFrontmatterKey: "Set"
}
};

Expand Down
Loading

0 comments on commit 6ec7f36

Please sign in to comment.