Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontmatter): Ensure consistent behavior across various commands #282

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "../settings/interface";
import {logs} from "../utils";
import { isAttachment, isShared } from "../utils/data_validation_test";
import { getRepoFrontmatter } from "../utils/parse_frontmatter";
import { frontmatterFromFile, getRepoFrontmatter } from "../utils/parse_frontmatter";
import Publisher from "./upload";

export class FilesManagement extends Publisher {
Expand Down Expand Up @@ -99,20 +99,18 @@ export class FilesManagement extends Publisher {
real: file.path,
});
} else if (file.extension == "md") {
const frontMatter = this.metadataCache.getCache(file.path)?.frontmatter;
const frontMatter = frontmatterFromFile(file, this.plugin);
if (isShared(frontMatter, this.settings, file, repo)) {
const repoFrontatter = getRepoFrontmatter(
const repoFrontmatter = getRepoFrontmatter(
this.settings,
repo,
file,
this.plugin.app,
frontMatter
);
const filepath = getReceiptFolder(file, this.settings, repo, this.plugin.app, repoFrontatter);
const filepath = getReceiptFolder(file, repo, this.plugin, repoFrontmatter);
allFileWithPath.push({
converted: filepath,
real: file.path,
repoFrontmatter: repoFrontatter,
repoFrontmatter: repoFrontmatter,
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
isShared,
} from "../utils/data_validation_test";
import { LOADING_ICON } from "../utils/icons";
import { getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { frontmatterFromFile, getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { ShareStatusBar } from "../utils/status_bar";
import { deleteFromGithub } from "./delete";
import { FilesManagement } from "./files";
Expand Down Expand Up @@ -178,8 +178,8 @@ export default class Publisher {
this.octokit,
this.plugin,
);
const frontmatter = this.metadataCache.getFileCache(file)?.frontmatter;
const repoFrontmatter = getRepoFrontmatter(this.settings, repo.repo, file, this.plugin.app, frontmatter);
const frontmatter = frontmatterFromFile(file, this.plugin);
const repoFrontmatter = getRepoFrontmatter(this.settings, repo.repo, frontmatter);
const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, this.plugin);
repo.frontmatter = repoFrontmatter;
if (
Expand Down Expand Up @@ -220,7 +220,7 @@ export default class Publisher {
repo: repo.frontmatter,
},
repository: repo.repo,
filepath: getReceiptFolder(file, this.settings, repo.repo, this.plugin.app, repo.frontmatter),
filepath: getReceiptFolder(file, repo.repo, this.plugin, repo.frontmatter),
};
text = await mainConverting(text, file, this.plugin.app, frontmatter, linkedFiles, this.plugin, multiProperties);
const path = multiProperties.filepath;
Expand Down
10 changes: 4 additions & 6 deletions src/commands/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ export async function createLinkCallback(repo: Repository | null, plugin: Github
) {
if (!checking) {
const multiRepo: MultiRepoProperties = {
frontmatter: getRepoFrontmatter(plugin.settings, repo, file, plugin.app, frontmatter),
frontmatter: getRepoFrontmatter(plugin.settings, repo, frontmatter),
repo,
};
createLink(
file,
multiRepo,
plugin.settings,
plugin.app
plugin
);
new Notice(i18next.t("commands.copyLink.onActivation"));
}
Expand Down Expand Up @@ -78,7 +77,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, null, plugin.app);
const frontmatter = getRepoFrontmatter(plugin.settings, repo);
const monoRepo: MonoRepoProperties = {
frontmatter: Array.isArray(frontmatter) ? frontmatter[0] : frontmatter,
repo,
Expand All @@ -102,7 +101,7 @@ export async function purgeNotesRemoteCallback(plugin: GithubPublisher, repo: Re
* @param {string} branchName - The branch name to upload the file
* @return {Promise<Command>}
*/
export async function shareOneNoteCallback(repo: Repository|null, plugin: GithubPublisher, branchName: string): Promise<Command> {
export async function shareOneNoteCallback(repo: Repository|null, plugin: GithubPublisher): Promise<Command> {
const id = repo ? `publisher-one-K${repo.smartKey}` : "publisher-one";
let name = i18next.t("commands.shareActiveFile");
const common = i18next.t("common.repository");
Expand All @@ -121,7 +120,6 @@ export async function shareOneNoteCallback(repo: Repository|null, plugin: Github
) {
if (!checking) {
shareOneNote(
branchName,
octokit,
file,
repo,
Expand Down
34 changes: 13 additions & 21 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 { getLinkedFrontmatter, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { frontmatterFromFile, 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, null, plugin.app, undefined);
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null);
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, plugin.app, undefined);
const repo = getRepoSharedKey(plugin, undefined);
await shareFolderRepo(plugin, folder, branchName, repo);
});
});
Expand Down Expand Up @@ -99,16 +99,15 @@ 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 = plugin.app.metadataCache.getFileCache(file)?.frontmatter;
let getSharedKey = getRepoSharedKey(plugin.settings, plugin.app, frontmatter, file);
const allKeysFromFile = multipleSharedKey(frontmatter, plugin.settings, file, plugin.app);
const frontmatter = frontmatterFromFile(file, plugin);
let getSharedKey = getRepoSharedKey(plugin, frontmatter, file);
const allKeysFromFile = multipleSharedKey(frontmatter, file, plugin);
if (
!(isShared(frontmatter, plugin.settings, file, getSharedKey) &&
plugin.settings.plugin.fileMenu)
) {
return;
}
const repoFrontmatter = getRepoFrontmatter(plugin.settings, getSharedKey, file, plugin.app, frontmatter);
plugin.settings.plugin.fileMenu)
) return;

const repoFrontmatter = getRepoFrontmatter(plugin.settings, getSharedKey, frontmatter);

menu.addItem((item) => {
/**
Expand Down Expand Up @@ -152,7 +151,6 @@ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: st
.setIcon("file-up")
.onClick(async () => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
file,
getSharedKey,
Expand All @@ -174,13 +172,11 @@ 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 {
let frontmatter = plugin.app.metadataCache.getFileCache(file)?.frontmatter;
const linkedFrontmatter = getLinkedFrontmatter(frontmatter, plugin.settings, file, plugin.app);
frontmatter = linkedFrontmatter ? { ...linkedFrontmatter, ...frontmatter } : frontmatter;
const frontmatter = frontmatterFromFile(file, plugin);
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, file, plugin.app, frontmatter);
let repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, frontmatter);
repoFrontmatter = repoFrontmatter instanceof Array ? repoFrontmatter : [repoFrontmatter];
/**
* default repo
Expand All @@ -196,7 +192,6 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
.setIcon("file-up")
.onClick(async () => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
file,
defaultRepo(plugin.settings),
Expand All @@ -219,7 +214,6 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
.setIcon("file-up")
.onClick(async () => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
file,
otherRepo,
Expand All @@ -241,7 +235,6 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
.setIcon("file-up")
.onClick(async () => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
file,
repo,
Expand All @@ -259,7 +252,6 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
.onClick(async () => {
new ChooseRepoToRun(plugin.app, plugin, repo?.shareKey, branchName, "file", file.basename, async (item: Repository) => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
file,
item,
Expand Down Expand Up @@ -310,7 +302,7 @@ export async function addMenuFolder(menu: Menu, folder: TFolder, branchName: str
}))
.setIcon("folder-up")
.onClick(async () => {
const repo = getRepoSharedKey(plugin.settings, plugin.app, undefined);
const repo = getRepoSharedKey(plugin);
await shareFolderRepo(plugin, folder, branchName, repo);
});
});
Expand Down
93 changes: 45 additions & 48 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import {
logs,
notif, publisherNotification} from "../utils";
import {checkRepositoryValidityWithRepoFrontmatter} from "../utils/data_validation_test";
import { getRepoFrontmatter } from "../utils/parse_frontmatter";
import { frontmatterFromFile, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { ShareStatusBar } from "../utils/status_bar";


/**
* Share all marked note (share: true) from Obsidian to GitHub
* @param {GithubBranch} PublisherManager
* @param {HTMLElement} statusBarItems - The status bar element
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo where to share the files
* @param {TFile[]} sharedFiles - The files to share
* @param {boolean} createGithubBranch - If true, create the branch before sharing the files
*/
* Share all marked note (share: true) from Obsidian to GitHub
* @param {GithubBranch} PublisherManager
* @param {HTMLElement} statusBarItems - The status bar element
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo where to share the files
* @param {TFile[]} sharedFiles - The files to share
* @param {boolean} createGithubBranch - If true, create the branch before sharing the files
*/
export async function shareAllMarkedNotes(
PublisherManager: GithubBranch,
statusBarItems: HTMLElement,
Expand Down Expand Up @@ -122,12 +122,12 @@ export async function shareAllMarkedNotes(
}

/**
* Delete unshared/deleted in the repo
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo where to delete the files
* @returns {Promise<void>}
*/
* Delete unshared/deleted in the repo
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo where to delete the files
* @returns {Promise<void>}
*/
export async function purgeNotesRemote(
PublisherManager: GithubBranch,
branchName: string,
Expand Down Expand Up @@ -158,27 +158,25 @@ export async function purgeNotesRemote(
}

/**
* Share only **one** note and their embedded contents, including note and attachments
* @param {string} branchName - The branch name created by the plugin
* @param {GithubBranch} PublisherManager
* @param {TFile} file - The file to share
* @param {Repository|null} repository
* @param {string} title The title from frontmatter + regex (if any)
* @returns {Promise<void>}
*/
* Share only **one** note and their embedded contents, including note and attachments
* @param {string} branchName - The branch name created by the plugin
* @param {GithubBranch} PublisherManager
* @param {TFile} file - The file to share
* @param {Repository|null} repository
* @param {string} title The title from frontmatter + regex (if any)
* @returns {Promise<void>}
*/
export async function shareOneNote(
branchName: string,
PublisherManager: GithubBranch,
file: TFile,
repository: Repository | null = null,
title?: string,
title?: string
): Promise<void|false> {
const settings = PublisherManager.settings;
const {settings, plugin} = PublisherManager;
const app = PublisherManager.plugin.app;
const metadataCache = app.metadataCache;
const frontmatter = frontmatterFromFile(file, PublisherManager.plugin);
try {
const frontmatter = metadataCache.getFileCache(file)?.frontmatter;
const repoFrontmatter = getRepoFrontmatter(settings, repository, file, PublisherManager.plugin.app, frontmatter);
const repoFrontmatter = getRepoFrontmatter(settings, repository, frontmatter);
const isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, repoFrontmatter);
const multiRepo: MultiRepoProperties = {
frontmatter: repoFrontmatter,
Expand Down Expand Up @@ -223,8 +221,7 @@ export async function shareOneNote(
await createLink(
file,
multiRepo,
settings,
app
plugin
);
if (settings.plugin.displayModalRepoEditing) {
const listEdited = createListEdited(publishSuccess.uploaded, publishSuccess.deleted, publishSuccess.error);
Expand All @@ -243,19 +240,19 @@ 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, file, app, metadataCache.getFileCache(file)?.frontmatter) });
notif.createSpan({ cls: ["error", "obsidian-publisher", "notification"] }).innerHTML = i18next.t("error.errorPublish", { repo: getRepoFrontmatter(settings, repository, frontmatter) });
new Notice(notif);
}
}
}

/**
* Deep scan the repository and send only the note that not exist in the repository
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo
* @returns {Promise<void>}
*/
* Deep scan the repository and send only the note that not exist in the repository
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo
* @returns {Promise<void>}
*/
export async function shareNewNote(
PublisherManager: GithubBranch,
branchName: string,
Expand Down Expand Up @@ -297,11 +294,11 @@ export async function shareNewNote(
}

/**
* Share edited notes : they exist on the repo, BUT the last edited time in Obsidian is after the last upload. Also share new notes.
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo
*/
* Share edited notes : they exist on the repo, BUT the last edited time in Obsidian is after the last upload. Also share new notes.
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo
*/
export async function shareAllEditedNotes(
PublisherManager: GithubBranch,
branchName: string,
Expand Down Expand Up @@ -347,11 +344,11 @@ export async function shareAllEditedNotes(
}

/**
* share **only** edited notes : they exist on the repo, but the last edited time is after the last upload.
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo
*/
* share **only** edited notes : they exist on the repo, but the last edited time is after the last upload.
* @param {GithubBranch} PublisherManager
* @param {string} branchName - The branch name created by the plugin
* @param {MonoRepoProperties} monoRepo - The repo
*/
export async function shareOnlyEdited(
PublisherManager: GithubBranch,
branchName: string,
Expand Down
Loading
Loading