From 44b8e1e36f3263674cee517fb64a1ebfef0839d9 Mon Sep 17 00:00:00 2001 From: Mara Date: Wed, 24 Jan 2024 19:00:04 +0100 Subject: [PATCH 1/4] directly take frontmatter of linked before parsing repoFrontmatter --- src/GitHub/upload.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GitHub/upload.ts b/src/GitHub/upload.ts index 34ae5a6a..c66f78d9 100644 --- a/src/GitHub/upload.ts +++ b/src/GitHub/upload.ts @@ -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"; @@ -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, file, this.plugin.app, frontmatter, false); const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, this.plugin); repo.frontmatter = repoFrontmatter; if ( From 4d49d5656048980eafef4f06c5e6aa4d7963908b Mon Sep 17 00:00:00 2001 From: Mara Date: Wed, 24 Jan 2024 19:26:02 +0100 Subject: [PATCH 2/4] big refactor call --- src/GitHub/files.ts | 12 ++- src/GitHub/upload.ts | 4 +- src/commands/callback.ts | 7 +- src/commands/file_menu.ts | 17 ++-- src/commands/index.ts | 88 +++++++++---------- src/commands/plugin_commands.ts | 22 ++--- .../suggest_other_repo_commands_modal.ts | 26 +++--- src/conversion/compiler/dataview.ts | 15 ++-- src/conversion/compiler/embeds.ts | 20 +++-- src/conversion/convert_text.ts | 6 +- src/conversion/file_path.ts | 29 +++--- src/conversion/links.ts | 7 +- src/main.ts | 48 +++++----- src/utils/parse_frontmatter.ts | 10 --- 14 files changed, 146 insertions(+), 165 deletions(-) diff --git a/src/GitHub/files.ts b/src/GitHub/files.ts index 05708517..8bb2f35f 100644 --- a/src/GitHub/files.ts +++ b/src/GitHub/files.ts @@ -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 { @@ -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, this.settings, repo, this.plugin, repoFrontmatter); allFileWithPath.push({ converted: filepath, real: file.path, - repoFrontmatter: repoFrontatter, + repoFrontmatter: repoFrontmatter, }); } } diff --git a/src/GitHub/upload.ts b/src/GitHub/upload.ts index c66f78d9..f060449f 100644 --- a/src/GitHub/upload.ts +++ b/src/GitHub/upload.ts @@ -179,7 +179,7 @@ export default class Publisher { this.plugin, ); const frontmatter = frontmatterFromFile(file, this.plugin); - const repoFrontmatter = getRepoFrontmatter(this.settings, repo.repo, file, this.plugin.app, frontmatter, false); + const repoFrontmatter = getRepoFrontmatter(this.settings, repo.repo, frontmatter); const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, this.plugin); repo.frontmatter = repoFrontmatter; if ( @@ -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, this.settings, repo.repo, this.plugin, repo.frontmatter), }; text = await mainConverting(text, file, this.plugin.app, frontmatter, linkedFiles, this.plugin, multiProperties); const path = multiProperties.filepath; diff --git a/src/commands/callback.ts b/src/commands/callback.ts index ef406f15..8faeaa02 100644 --- a/src/commands/callback.ts +++ b/src/commands/callback.ts @@ -38,7 +38,7 @@ 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( @@ -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, null, plugin.app); + const frontmatter = getRepoFrontmatter(plugin.settings, repo); const monoRepo: MonoRepoProperties = { frontmatter: Array.isArray(frontmatter) ? frontmatter[0] : frontmatter, repo, @@ -102,7 +102,7 @@ export async function purgeNotesRemoteCallback(plugin: GithubPublisher, repo: Re * @param {string} branchName - The branch name to upload the file * @return {Promise} */ -export async function shareOneNoteCallback(repo: Repository|null, plugin: GithubPublisher, branchName: string): Promise { +export async function shareOneNoteCallback(repo: Repository|null, plugin: GithubPublisher): Promise { const id = repo ? `publisher-one-K${repo.smartKey}` : "publisher-one"; let name = i18next.t("commands.shareActiveFile"); const common = i18next.t("common.repository"); @@ -121,7 +121,6 @@ export async function shareOneNoteCallback(repo: Repository|null, plugin: Github ) { if (!checking) { shareOneNote( - branchName, octokit, file, repo, diff --git a/src/commands/file_menu.ts b/src/commands/file_menu.ts index fd54cb0c..90c19d0e 100644 --- a/src/commands/file_menu.ts +++ b/src/commands/file_menu.ts @@ -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"; @@ -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, @@ -108,7 +108,7 @@ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: st ) { return; } - const repoFrontmatter = getRepoFrontmatter(plugin.settings, getSharedKey, file, plugin.app, frontmatter); + const repoFrontmatter = getRepoFrontmatter(plugin.settings, getSharedKey, frontmatter); menu.addItem((item) => { /** @@ -152,7 +152,6 @@ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: st .setIcon("file-up") .onClick(async () => { await shareOneNote( - branchName, await plugin.reloadOctokit(), file, getSharedKey, @@ -174,13 +173,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 @@ -196,7 +193,6 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil .setIcon("file-up") .onClick(async () => { await shareOneNote( - branchName, await plugin.reloadOctokit(), file, defaultRepo(plugin.settings), @@ -219,7 +215,6 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil .setIcon("file-up") .onClick(async () => { await shareOneNote( - branchName, await plugin.reloadOctokit(), file, otherRepo, @@ -241,7 +236,6 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil .setIcon("file-up") .onClick(async () => { await shareOneNote( - branchName, await plugin.reloadOctokit(), file, repo, @@ -259,7 +253,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, diff --git a/src/commands/index.ts b/src/commands/index.ts index 153c1232..b81972f8 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -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, @@ -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} - */ + * 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} + */ export async function purgeNotesRemote( PublisherManager: GithubBranch, branchName: string, @@ -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} - */ + * 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} + */ export async function shareOneNote( - branchName: string, PublisherManager: GithubBranch, file: TFile, repository: Repository | null = null, - title?: string, + title?: string ): Promise { const settings = PublisherManager.settings; 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, @@ -243,19 +241,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} - */ + * 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} + */ export async function shareNewNote( PublisherManager: GithubBranch, branchName: string, @@ -297,11 +295,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, @@ -347,11 +345,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, diff --git a/src/commands/plugin_commands.ts b/src/commands/plugin_commands.ts index 0fd9822f..fe6f8599 100644 --- a/src/commands/plugin_commands.ts +++ b/src/commands/plugin_commands.ts @@ -10,7 +10,7 @@ import GithubPublisher from "../main"; import {MonoRepoProperties, MultiRepoProperties, Repository} from "../settings/interface"; import {createLink} 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, shareAllEditedNotes, @@ -28,12 +28,13 @@ import { */ export async function createLinkOnActiveFile(repo: Repository | null, plugin: GithubPublisher): Promise { 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) ) { const multiRepo: MultiRepoProperties = { - frontmatter: getRepoFrontmatter(plugin.settings, repo, file, plugin.app, frontmatter), + frontmatter: getRepoFrontmatter(plugin.settings, repo, frontmatter), repo }; await createLink( @@ -56,12 +57,11 @@ export async function createLinkOnActiveFile(repo: Repository | null, plugin: Gi * @param {string} branchName * @return {Promise} */ -export async function shareActiveFile(plugin: GithubPublisher, repo: Repository | null, branchName: string): Promise { +export async function shareActiveFile(plugin: GithubPublisher, repo: Repository | null): Promise { const file = plugin.app.workspace.getActiveFile(); const frontmatter = file ? plugin.app.metadataCache.getFileCache(file)?.frontmatter : null; if (file && frontmatter && isShared(frontmatter, plugin.settings, file, repo)) { await shareOneNote( - branchName, await plugin.reloadOctokit(), file, repo, @@ -80,7 +80,7 @@ export async function shareActiveFile(plugin: GithubPublisher, repo: Repository * @return {Promise} */ export async function deleteCommands(plugin : GithubPublisher, repo: Repository | null, branchName: string): Promise { - const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app); + const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo); const publisher = await plugin.reloadOctokit(); const mono: MonoRepoProperties = { frontmatter: Array.isArray(repoFrontmatter) ? repoFrontmatter[0] : repoFrontmatter, @@ -102,11 +102,11 @@ export async function deleteCommands(plugin : GithubPublisher, repo: Repository * @return {Promise} */ -export async function uploadAllNotes(plugin: GithubPublisher, repo: Repository | null, branchName: string) { +export async function uploadAllNotes(plugin: GithubPublisher, repo: Repository | null, branchName: string): Promise { const statusBarItems = plugin.addStatusBarItem(); const publisher = await plugin.reloadOctokit(); const sharedFiles = publisher.getSharedFiles(repo); - const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app); + const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo); const mono: MonoRepoProperties = { frontmatter: Array.isArray(repoFrontmatter) ? repoFrontmatter[0] : repoFrontmatter, repo @@ -132,7 +132,7 @@ export async function uploadAllNotes(plugin: GithubPublisher, repo: Repository | export async function uploadNewNotes(plugin: GithubPublisher, branchName: string, repo: Repository|null): Promise { const publisher = await plugin.reloadOctokit(); - const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app); + const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo); await shareNewNote( publisher, branchName, @@ -173,7 +173,7 @@ export async function repositoryValidityActiveFile(plugin:GithubPublisher, branc */ export async function uploadAllEditedNotes(plugin: GithubPublisher ,branchName: string, repo: Repository|null=null): Promise { const publisher = await plugin.reloadOctokit(); - const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null, plugin.app); + const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo); await shareAllEditedNotes( publisher, @@ -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, null, plugin.app); + const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo); await shareOnlyEdited( publisher, branchName, diff --git a/src/commands/suggest_other_repo_commands_modal.ts b/src/commands/suggest_other_repo_commands_modal.ts index eca8a097..405b8412 100644 --- a/src/commands/suggest_other_repo_commands_modal.ts +++ b/src/commands/suggest_other_repo_commands_modal.ts @@ -20,12 +20,12 @@ interface GithubPublisherCommands { } /** - * @extends FuzzySuggestModal - * @category Command - * @category SuggestModal - * @category GithubPublisherPlugin - * @description This class is used to choose which repo to run the command on - */ + * @extends FuzzySuggestModal + * @category Command + * @category SuggestModal + * @category GithubPublisherPlugin + * @description This class is used to choose which repo to run the command on + */ export class ChooseWhichRepoToRun extends FuzzySuggestModal { plugin: GithubPublisherPlugin; @@ -50,8 +50,8 @@ export class ChooseWhichRepoToRun extends FuzzySuggestModal { } /** - * Just return the repo data - */ + * Just return the repo data + */ export class ChooseRepoToRun extends FuzzySuggestModal { plugin: GithubPublisherPlugin; branchName: string; @@ -87,7 +87,7 @@ export class ChooseRepoToRun extends FuzzySuggestModal { repoFound=repoFound.concat(this.plugin.settings.github.otherRepo.filter((repo: Repository) => repo.shareAll?.enable && !this.fileName?.startsWith(repo.shareAll?.excludedFileName))); repoFound.push(defRepo); repoFound=[...new Set(repoFound)]; - if (repoFound.length === 0) + if (repoFound.length === 0) return this.plugin.settings.github.otherRepo; return repoFound; } @@ -101,9 +101,9 @@ export class ChooseRepoToRun extends FuzzySuggestModal { } /** - * @description This class call the commands on the chosen repo - * @extends FuzzySuggestModal - */ + * @description This class call the commands on the chosen repo + * @extends FuzzySuggestModal + */ export class SuggestOtherRepoCommandsModal extends FuzzySuggestModal { plugin: GithubPublisherPlugin; @@ -179,7 +179,7 @@ export class SuggestOtherRepoCommandsModal extends FuzzySuggestModal} the converted text */ @@ -190,14 +191,14 @@ async function convertDataviewLinks( md: string, frontmatter: FrontMatterCache | undefined | null, sourceFile: TFile, - app: App, + plugin: GithubPublisher, properties: MultiProperties): Promise { const dataviewPath = getDataviewPath(md, properties.settings, app.vault); md = await convertToInternalGithub( md, dataviewPath, sourceFile, - app, + plugin, frontmatter, properties ); diff --git a/src/conversion/compiler/embeds.ts b/src/conversion/compiler/embeds.ts index 1de23cd1..e699923c 100644 --- a/src/conversion/compiler/embeds.ts +++ b/src/conversion/compiler/embeds.ts @@ -15,6 +15,7 @@ import { resolveSubpath, TFile} from "obsidian"; import { getAPI, Link } from "obsidian-dataview"; +import GithubPublisher from "src/main"; import { GitHubPublisherSettings, @@ -145,7 +146,7 @@ function extractSubpath( * @source https://github.com/mgmeyers/obsidian-easy-bake/blob/master/src/BakeModal.ts * @param {TFile} originalFile the file to bake * @param {Set} ancestor the ancestor of the file - * @param {App} app the Obsidian App instance + * @param {GithubPublisher} plugin the Obsidian Plugin instance * @param {MultiProperties} properties the properties of the plugins (settings, repository, frontmatter) * @param {string|null} subpath the subpath to bake * @param {LinkedNotes[]} linkedNotes the linked notes embedded in the file @@ -153,11 +154,12 @@ function extractSubpath( export async function bakeEmbeds( originalFile: TFile, ancestor: Set, - app: App, + plugin: GithubPublisher, properties: MultiProperties, subpath: string|null, linkedNotes: LinkedNotes[]): Promise { - const { vault, metadataCache } = app; + const { app } = plugin; + const { vault, metadataCache } = plugin.app; let text = await vault.cachedRead(originalFile); const cache = metadataCache.getFileCache(originalFile); @@ -186,13 +188,13 @@ export async function bakeEmbeds( const replaceTarget = async (replacement: string) => { if (properties.settings.embed.bake?.textAfter) { - let textAfter = await changeURL(properties.settings.embed.bake?.textAfter, properties, linked, originalFile, app, linkedNotes); + let textAfter = await changeURL(properties.settings.embed.bake?.textAfter, properties, linked, originalFile, plugin, linkedNotes); textAfter = changeTitle(textAfter, linked, app, properties.settings); const newLine = replacement.match(/[\s\n]/g) ? "" : "\n"; replacement = `${replacement}${newLine}${textAfter}`; } if (properties.settings.embed.bake?.textBefore) { - let textBefore = await changeURL(properties.settings.embed.bake?.textBefore, properties, linked, originalFile, app, linkedNotes); + let textBefore = await changeURL(properties.settings.embed.bake?.textBefore, properties, linked, originalFile, plugin, linkedNotes); textBefore = changeTitle(textBefore, linked, app, properties.settings); replacement = `${textBefore}\n${replacement}`; } @@ -207,7 +209,7 @@ export async function bakeEmbeds( //do nothing continue; } - const baked = sanitizeBakedContent(await bakeEmbeds(linked, newAncestors, app, properties, subpath, linkedNotes)); + const baked = sanitizeBakedContent(await bakeEmbeds(linked, newAncestors, plugin, properties, subpath, linkedNotes)); await replaceTarget( listMatch ? applyIndent(stripFirstBullet(baked), listMatch[1]) : baked); } @@ -220,17 +222,17 @@ export async function bakeEmbeds( * @param properties {MultiProperties} contains the settings, repository and frontmatter * @param linked {TFile} The linked note * @param sourceFile {TFile} The source file - * @param app {App} The Obsidian App instance + * @param plugin {GithubPublisher} The Obsidian App instance * @param linkedNotes {LinkedNotes[]} The linked notes embedded in the file * @returns {Promise} */ -async function changeURL(textToAdd: string, properties: MultiProperties, linked: TFile, sourceFile: TFile, app: App, linkedNotes: LinkedNotes[]): Promise { +async function changeURL(textToAdd: string, properties: MultiProperties, linked: TFile, sourceFile: TFile, plugin: GithubPublisher, linkedNotes: LinkedNotes[]): Promise { const frontmatter = app.metadataCache.getFileCache(linked)?.frontmatter; if (!frontmatter) return textToAdd; const linkedNote = linkedNotes.find((note) => note.linked === linked); if (!linkedNote) return textToAdd; if (properties.frontmatter.general.convertInternalLinks) { - const relativePath = await createRelativePath(sourceFile, linkedNote, frontmatter, app, properties); + const relativePath = await createRelativePath(sourceFile, linkedNote, frontmatter, plugin, properties); return textToAdd.replace(/\{{2}url\}{2}/gmi, relativePath); } return textToAdd.replace(/\{{2}url\}{2}/gmi, linkedNote.linked.path); diff --git a/src/conversion/convert_text.ts b/src/conversion/convert_text.ts index a1a303d4..d7be1b3a 100644 --- a/src/conversion/convert_text.ts +++ b/src/conversion/convert_text.ts @@ -181,14 +181,14 @@ export async function mainConverting( ): Promise { if (properties.frontmatter.general.removeEmbed === "bake") - text = await bakeEmbeds(file, new Set(), app, properties, null, linkedFiles); + text = await bakeEmbeds(file, new Set(), plugin, properties, null, linkedFiles); text = findAndReplaceText(text, properties.settings, false); text = await processYaml(properties.settings, file, plugin.app.metadataCache, frontmatter, text, properties); text = await convertToInternalGithub( text, linkedFiles, file, - app, + plugin, frontmatter, properties ); @@ -196,7 +196,7 @@ export async function mainConverting( text = await convertDataviewQueries( text, file.path, - plugin.app, + plugin, frontmatter, file, properties diff --git a/src/conversion/file_path.ts b/src/conversion/file_path.ts index 5449a8f7..f438a662 100644 --- a/src/conversion/file_path.ts +++ b/src/conversion/file_path.ts @@ -1,11 +1,11 @@ import { - App, FrontMatterCache, normalizePath, TFile, TFolder, Vault, } from "obsidian"; +import GithubPublisher from "src/main"; import { FIND_REGEX, @@ -21,7 +21,7 @@ import { logs, } from "../utils"; import {checkIfRepoIsInAnother, isInternalShared, isShared} from "../utils/data_validation_test"; -import { getCategory, getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter"; +import { frontmatterFromFile, getCategory, getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter"; import { createRegexFromText } from "./find_and_replace_text"; @@ -62,7 +62,7 @@ export function textIsInFrontmatter( * @param {TFile} sourceFile the shared file containing all links, embed etc * @param {LinkedNotes} targetFile The target file * @param {FrontMatterCache | null} frontmatter FrontmatterCache or null - * @param app + * @param {plugin} GithubPublisher * @param properties * @return {string} relative path */ @@ -71,15 +71,14 @@ export async function createRelativePath( sourceFile: TFile, targetFile: LinkedNotes, frontmatter: FrontMatterCache | null | undefined, - app: App, + plugin: GithubPublisher, properties: MultiProperties, ): Promise { - const { metadataCache } = app; const settings = properties.settings; 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, targetFile.linked, app, frontmatterTarget); + const sourcePath = getReceiptFolder(sourceFile, settings, shortRepo, plugin, properties.frontmatter.repo); + const frontmatterTarget = frontmatterFromFile(targetFile.linked, plugin); + const targetRepo = getRepoFrontmatter(settings, shortRepo, frontmatterTarget); const isFromAnotherRepo = checkIfRepoIsInAnother(properties.frontmatter.repo, targetRepo); const shared = isInternalShared( frontmatterTarget, @@ -94,12 +93,12 @@ export async function createRelativePath( return targetFile.destinationFilePath ? targetFile.destinationFilePath: targetFile.linked.basename; } if (targetFile.linked.path === sourceFile.path) { - return getReceiptFolder(targetFile.linked, settings, shortRepo, app, targetRepo).split("/").at(-1) as string; + return getReceiptFolder(targetFile.linked, settings, shortRepo, plugin, targetRepo).split("/").at(-1) as string; } const targetPath = targetFile.linked.extension === "md" && !targetFile.linked.name.includes("excalidraw") - ? getReceiptFolder(targetFile.linked, settings, shortRepo, app, targetRepo) + ? getReceiptFolder(targetFile.linked, settings, shortRepo, plugin, targetRepo) : getImagePath( targetFile.linked, settings, @@ -148,7 +147,7 @@ export async function createRelativePath( targetFile.linked, settings, shortRepo, - app, + plugin, targetRepo ).split("/").at(-1) as string; } @@ -370,13 +369,13 @@ export function getReceiptFolder( file: TFile, settings: GitHubPublisherSettings, otherRepo: Repository | null, - app: App, + plugin: GithubPublisher, repoFrontmatter?: RepoFrontmatter | RepoFrontmatter[], ): string { - const { vault, metadataCache } = app; + const { vault} = app; if (file.extension === "md") { - const frontmatter = metadataCache.getCache(file.path)?.frontmatter; - if (!repoFrontmatter) repoFrontmatter = getRepoFrontmatter(settings, otherRepo, file, app, frontmatter); + const frontmatter = frontmatterFromFile(file, plugin); + if (!repoFrontmatter) repoFrontmatter = getRepoFrontmatter(settings, otherRepo, frontmatter); repoFrontmatter = repoFrontmatter instanceof Array ? repoFrontmatter : [repoFrontmatter]; let targetRepo = repoFrontmatter.find((repo) => repo.path?.smartkey === otherRepo?.smartKey || "default"); if (!targetRepo) targetRepo = repoFrontmatter[0]; diff --git a/src/conversion/links.ts b/src/conversion/links.ts index 5c1ff6a6..514120b6 100644 --- a/src/conversion/links.ts +++ b/src/conversion/links.ts @@ -1,5 +1,6 @@ -import { App, FrontMatterCache, TFile } from "obsidian"; +import { FrontMatterCache, TFile } from "obsidian"; import slugify from "slugify"; +import GithubPublisher from "src/main"; import { FrontmatterConvert, @@ -248,7 +249,7 @@ export async function convertToInternalGithub( fileContent: string, linkedFiles: LinkedNotes[], sourceFile: TFile, - app: App, + plugin: GithubPublisher, frontmatter: FrontMatterCache | undefined | null, properties: MultiProperties, ): Promise { @@ -266,7 +267,7 @@ export async function convertToInternalGithub( sourceFile, linkedFile, frontmatter, - app, + plugin, properties, ); pathInGithub = pathInGithub.replace(".md", ""); diff --git a/src/main.ts b/src/main.ts index f7b7b8c4..f312ff4b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,20 +27,20 @@ import {createTokenPath, logs, monkeyPatchConsole, notif} from "./utils"; import {checkRepositoryValidity, verifyRateLimitAPI} from "./utils/data_validation_test"; /** - * Main class of the plugin - * @extends Plugin - */ + * Main class of the plugin + * @extends Plugin + */ export default class GithubPublisher extends Plugin { settings!: GitHubPublisherSettings; branchName: string = ""; /** - * Get the title field of a file - * @param {TFile} file - The file to get the title field from - * @param {FrontMatterCache} frontmatter - The frontmatter of the file - * @return {string} - The title field of the file - */ + * Get the title field of a file + * @param {TFile} file - The file to get the title field from + * @param {FrontMatterCache} frontmatter - The frontmatter of the file + * @return {string} - The title field of the file + */ getTitleFieldForCommand(file:TFile, frontmatter: FrontMatterCache | undefined | null): string { return regexOnFileName(getTitleField(frontmatter, file, this.settings), this.settings); } @@ -49,7 +49,7 @@ export default class GithubPublisher extends Plugin { if (plugin.settings.plugin.copyLink.addCmd) { this.addCommand(await createLinkCallback(repo, this)); } - this.addCommand(await shareOneNoteCallback(repo, this, this.branchName)); + this.addCommand(await shareOneNoteCallback(repo, this)); if (plugin.settings.upload.autoclean.enable) { logs({settings: this.settings}, "Adding purge command"); this.addCommand(await purgeNotesRemoteCallback(this, repo, this.branchName)); @@ -119,13 +119,13 @@ export default class GithubPublisher extends Plugin { } /** - * Read the env file to get the token of the plugin - * Form of the file: - * ``` - * GITHUB_TOKEN=token - * ``` - * @returns {Promise} - The token of the plugin - */ + * Read the env file to get the token of the plugin + * Form of the file: + * ``` + * GITHUB_TOKEN=token + * ``` + * @returns {Promise} - The token of the plugin + */ async loadToken(): Promise { const tokenPath = createTokenPath(this, this.settings.github.tokenPath); @@ -151,7 +151,7 @@ export default class GithubPublisher extends Plugin { } /** - * Create a new instance of Octokit to load a new instance of GithubBranch + * Create a new instance of Octokit to load a new instance of GithubBranch */ async reloadOctokit() { let octokit: Octokit; @@ -175,9 +175,9 @@ export default class GithubPublisher extends Plugin { /** - * Function called when the plugin is loaded - * @return {Promise} - */ + * Function called when the plugin is loaded + * @return {Promise} + */ async onload(): Promise { console.info(`[GITHUB PUBLISHER] v.${this.manifest.version} (lang: ${translationLanguage}) loaded`); await this.loadSettings(); @@ -256,8 +256,8 @@ export default class GithubPublisher extends Plugin { } /** - * Called when the plugin is disabled - */ + * Called when the plugin is disabled + */ onunload() { console.info("[Github Publisher] unloaded"); } @@ -273,8 +273,8 @@ export default class GithubPublisher extends Plugin { } /** - * Save the settings of the plugin - */ + * Save the settings of the plugin + */ async saveSettings() { await this.saveData(this.settings); } diff --git a/src/utils/parse_frontmatter.ts b/src/utils/parse_frontmatter.ts index d79b3909..e8acc137 100644 --- a/src/utils/parse_frontmatter.ts +++ b/src/utils/parse_frontmatter.ts @@ -143,19 +143,9 @@ function translateBooleanForRemoveEmbed(removeEmbed: unknown) { export function getRepoFrontmatter( settings: GitHubPublisherSettings, repository: Repository | null, - sourceFile: TFile | null, - app: App, frontmatter?: FrontMatterCache | null, - parseSet = true, ): RepoFrontmatter[] | RepoFrontmatter { let github = repository ?? settings.github; - if (parseSet && frontmatter) { - const linkedFrontmatter = getLinkedFrontmatter(frontmatter, settings, sourceFile, app); - if (linkedFrontmatter) { - //fusion frontmatter and override the linkedFrontmatter with the frontmatter if the key is the same - frontmatter = {...linkedFrontmatter, ...frontmatter}; - } - } if (frontmatter && typeof frontmatter["shortRepo"] === "string" && frontmatter["shortRepo"] !== "default") { const smartKey = frontmatter.shortRepo.toLowerCase(); const allOtherRepo = settings.github.otherRepo; From a7a43def85c7439fbf61e4fa2e42964237534121 Mon Sep 17 00:00:00 2001 From: Mara Date: Wed, 24 Jan 2024 19:30:59 +0100 Subject: [PATCH 3/4] update call --- src/commands/file_menu.ts | 9 ++++----- src/conversion/file_path.ts | 2 +- src/utils/data_validation_test.ts | 6 +++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/commands/file_menu.ts b/src/commands/file_menu.ts index 90c19d0e..09d5fcce 100644 --- a/src/commands/file_menu.ts +++ b/src/commands/file_menu.ts @@ -99,15 +99,14 @@ 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; + const frontmatter = frontmatterFromFile(file, plugin); 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) - ) { - return; - } + plugin.settings.plugin.fileMenu) + ) return; + const repoFrontmatter = getRepoFrontmatter(plugin.settings, getSharedKey, frontmatter); menu.addItem((item) => { diff --git a/src/conversion/file_path.ts b/src/conversion/file_path.ts index f438a662..4d7c2f87 100644 --- a/src/conversion/file_path.ts +++ b/src/conversion/file_path.ts @@ -372,7 +372,7 @@ export function getReceiptFolder( plugin: GithubPublisher, repoFrontmatter?: RepoFrontmatter | RepoFrontmatter[], ): string { - const { vault} = app; + const { vault} = plugin.app; if (file.extension === "md") { const frontmatter = frontmatterFromFile(file, plugin); if (!repoFrontmatter) repoFrontmatter = getRepoFrontmatter(settings, otherRepo, frontmatter); diff --git a/src/utils/data_validation_test.ts b/src/utils/data_validation_test.ts index e9d3e106..f83589fb 100644 --- a/src/utils/data_validation_test.ts +++ b/src/utils/data_validation_test.ts @@ -50,7 +50,7 @@ export function isInternalShared( * @param file - The TFile object representing the file being processed. * @returns The Repository object representing the repository with the shared key, or null if no repository is found. */ -export function getRepoSharedKey(settings: GitHubPublisherSettings, app: App, frontmatter?: FrontMatterCache, file?: TFile): Repository | null{ +export function getRepoSharedKey(settings: GitHubPublisherSettings, app: App, frontmatter?: FrontMatterCache | null, file?: TFile): Repository | null{ const allOtherRepo = settings.github.otherRepo; if (settings.plugin.shareAll?.enable && !frontmatter) { return defaultRepo(settings); @@ -137,7 +137,7 @@ export function isExcludedPath(settings: GitHubPublisherSettings, file: TFile | * @param {App} app - The Obsidian app instance. * @returns {string[]} - An array of shared keys found in the file. */ -export function multipleSharedKey(frontmatter: FrontMatterCache | undefined, settings: GitHubPublisherSettings, file: TFile | null, app: App): string[] { +export function multipleSharedKey(frontmatter: FrontMatterCache | undefined | null, settings: GitHubPublisherSettings, file: TFile | null, app: App): string[] { const keysInFile: string[] = []; if (settings.plugin.shareAll?.enable) keysInFile.push("share"); //add a key to count the shareAll @@ -313,7 +313,7 @@ export async function checkRepositoryValidity( const settings = PublisherManager.settings; try { const frontmatter = frontmatterFromFile(file, PublisherManager.plugin); - const repoFrontmatter = getRepoFrontmatter(settings, repository, file, PublisherManager.plugin.app, frontmatter); + const repoFrontmatter = getRepoFrontmatter(settings, repository, frontmatter); const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, PublisherManager.plugin, silent); if (isNotEmpty) { await PublisherManager.checkRepository(repoFrontmatter, silent); From 1a159b98e5ef2d831fa80e99e08e15ff02a47a9f Mon Sep 17 00:00:00 2001 From: Mara Date: Wed, 24 Jan 2024 20:43:30 +0100 Subject: [PATCH 4/4] remove multiple call and use only plugin --- src/GitHub/files.ts | 2 +- src/GitHub/upload.ts | 2 +- src/commands/callback.ts | 3 +-- src/commands/file_menu.ts | 8 ++++---- src/commands/index.ts | 5 ++--- src/commands/plugin_commands.ts | 3 +-- src/conversion/file_path.ts | 9 ++++----- src/settings/migrate.ts | 14 ++++++++++---- src/utils/data_validation_test.ts | 16 ++++++++-------- src/utils/index.ts | 9 +++++---- src/utils/parse_frontmatter.ts | 22 +++++++++++----------- 11 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/GitHub/files.ts b/src/GitHub/files.ts index 8bb2f35f..b66f155b 100644 --- a/src/GitHub/files.ts +++ b/src/GitHub/files.ts @@ -106,7 +106,7 @@ export class FilesManagement extends Publisher { repo, frontMatter ); - const filepath = getReceiptFolder(file, this.settings, repo, this.plugin, repoFrontmatter); + const filepath = getReceiptFolder(file, repo, this.plugin, repoFrontmatter); allFileWithPath.push({ converted: filepath, real: file.path, diff --git a/src/GitHub/upload.ts b/src/GitHub/upload.ts index f060449f..2c7cf8f8 100644 --- a/src/GitHub/upload.ts +++ b/src/GitHub/upload.ts @@ -220,7 +220,7 @@ export default class Publisher { repo: repo.frontmatter, }, repository: repo.repo, - filepath: getReceiptFolder(file, this.settings, repo.repo, this.plugin, 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; diff --git a/src/commands/callback.ts b/src/commands/callback.ts index 8faeaa02..282e6c85 100644 --- a/src/commands/callback.ts +++ b/src/commands/callback.ts @@ -44,8 +44,7 @@ export async function createLinkCallback(repo: Repository | null, plugin: Github createLink( file, multiRepo, - plugin.settings, - plugin.app + plugin ); new Notice(i18next.t("commands.copyLink.onActivation")); } diff --git a/src/commands/file_menu.ts b/src/commands/file_menu.ts index 09d5fcce..d51df791 100644 --- a/src/commands/file_menu.ts +++ b/src/commands/file_menu.ts @@ -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); }); }); @@ -100,8 +100,8 @@ export function addSubMenuCommandsFolder(plugin: GithubPublisher, item: MenuItem */ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: string, menu: Menu) { const frontmatter = frontmatterFromFile(file, plugin); - let getSharedKey = getRepoSharedKey(plugin.settings, plugin.app, frontmatter, file); - const allKeysFromFile = multipleSharedKey(frontmatter, plugin.settings, file, plugin.app); + let getSharedKey = getRepoSharedKey(plugin, frontmatter, file); + const allKeysFromFile = multipleSharedKey(frontmatter, file, plugin); if ( !(isShared(frontmatter, plugin.settings, file, getSharedKey) && plugin.settings.plugin.fileMenu) @@ -302,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); }); }); diff --git a/src/commands/index.ts b/src/commands/index.ts index b81972f8..bb122a4e 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -172,7 +172,7 @@ export async function shareOneNote( repository: Repository | null = null, title?: string ): Promise { - const settings = PublisherManager.settings; + const {settings, plugin} = PublisherManager; const app = PublisherManager.plugin.app; const frontmatter = frontmatterFromFile(file, PublisherManager.plugin); try { @@ -221,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); diff --git a/src/commands/plugin_commands.ts b/src/commands/plugin_commands.ts index fe6f8599..0a6fa850 100644 --- a/src/commands/plugin_commands.ts +++ b/src/commands/plugin_commands.ts @@ -40,8 +40,7 @@ export async function createLinkOnActiveFile(repo: Repository | null, plugin: Gi await createLink( file, multiRepo, - plugin.settings, - plugin.app + plugin ); new Notice(i18next.t("commands.copyLink.onActivation")); return; diff --git a/src/conversion/file_path.ts b/src/conversion/file_path.ts index 4d7c2f87..2f8dcb92 100644 --- a/src/conversion/file_path.ts +++ b/src/conversion/file_path.ts @@ -76,7 +76,7 @@ export async function createRelativePath( ): Promise { const settings = properties.settings; const shortRepo = properties.repository; - const sourcePath = getReceiptFolder(sourceFile, settings, shortRepo, plugin, properties.frontmatter.repo); + const sourcePath = getReceiptFolder(sourceFile, shortRepo, plugin, properties.frontmatter.repo); const frontmatterTarget = frontmatterFromFile(targetFile.linked, plugin); const targetRepo = getRepoFrontmatter(settings, shortRepo, frontmatterTarget); const isFromAnotherRepo = checkIfRepoIsInAnother(properties.frontmatter.repo, targetRepo); @@ -93,12 +93,12 @@ export async function createRelativePath( return targetFile.destinationFilePath ? targetFile.destinationFilePath: targetFile.linked.basename; } if (targetFile.linked.path === sourceFile.path) { - return getReceiptFolder(targetFile.linked, settings, shortRepo, plugin, targetRepo).split("/").at(-1) as string; + return getReceiptFolder(targetFile.linked, shortRepo, plugin, targetRepo).split("/").at(-1) as string; } const targetPath = targetFile.linked.extension === "md" && !targetFile.linked.name.includes("excalidraw") - ? getReceiptFolder(targetFile.linked, settings, shortRepo, plugin, targetRepo) + ? getReceiptFolder(targetFile.linked, shortRepo, plugin, targetRepo) : getImagePath( targetFile.linked, settings, @@ -145,7 +145,6 @@ export async function createRelativePath( //in case of errors return getReceiptFolder( targetFile.linked, - settings, shortRepo, plugin, targetRepo @@ -367,12 +366,12 @@ export function getTitleField( export function getReceiptFolder( file: TFile, - settings: GitHubPublisherSettings, otherRepo: Repository | null, plugin: GithubPublisher, repoFrontmatter?: RepoFrontmatter | RepoFrontmatter[], ): string { const { vault} = plugin.app; + const settings = plugin.settings; if (file.extension === "md") { const frontmatter = frontmatterFromFile(file, plugin); if (!repoFrontmatter) repoFrontmatter = getRepoFrontmatter(settings, otherRepo, frontmatter); diff --git a/src/settings/migrate.ts b/src/settings/migrate.ts index fbdadc80..54870dcf 100644 --- a/src/settings/migrate.ts +++ b/src/settings/migrate.ts @@ -218,7 +218,11 @@ async function migrateOldSettings(plugin: GithubPublisher, old: OldSettings) { }, otherRepo: [], rateLimit: 0, - verifiedRepo: false + verifiedRepo: false, + dryRun: { + enable: false, + folderName: "", + } }, upload: { behavior: old.downloadedFolder as FolderSettings, @@ -272,13 +276,14 @@ async function migrateOldSettings(plugin: GithubPublisher, old: OldSettings) { }, }, embed: { - forcePushAttachments: [], attachments: old.embedImage, keySendFile: old.metadataFileFields, notes: old.embedNotes, folder: old.defaultImageFolder, charConvert: "->", - convertEmbedToLinks: "keep" + convertEmbedToLinks: "keep", + overrideAttachments: [], + }, plugin: { shareKey: old.shareKey, @@ -292,7 +297,8 @@ async function migrateOldSettings(plugin: GithubPublisher, old: OldSettings) { addCmd: false, }, noticeError: old.logNotice, - displayModalRepoEditing: false + displayModalRepoEditing: false, + setFrontmatterKey: "Set" } }; //@ts-ignore diff --git a/src/utils/data_validation_test.ts b/src/utils/data_validation_test.ts index f83589fb..200522d6 100644 --- a/src/utils/data_validation_test.ts +++ b/src/utils/data_validation_test.ts @@ -1,6 +1,6 @@ import { Octokit } from "@octokit/core"; import i18next from "i18next"; -import {App, FrontMatterCache, normalizePath,Notice, TFile, TFolder} from "obsidian"; +import { FrontMatterCache, normalizePath,Notice, TFile, TFolder} from "obsidian"; import GithubPublisher from "src/main"; import {GithubBranch} from "../GitHub/branch"; @@ -43,19 +43,18 @@ export function isInternalShared( } /** * Retrieves the shared key for a repository based on the provided settings, app, frontmatter, and file. - * - * @param settings - The GitHubPublisherSettings object containing the configuration settings. - * @param app - The App object representing the Obsidian application. + * @param plugin - The GitHub Publisher plugin instance. * @param frontmatter - The FrontMatterCache object representing the frontmatter of the file. * @param file - The TFile object representing the file being processed. * @returns The Repository object representing the repository with the shared key, or null if no repository is found. */ -export function getRepoSharedKey(settings: GitHubPublisherSettings, app: App, frontmatter?: FrontMatterCache | null, file?: TFile): Repository | null{ +export function getRepoSharedKey(plugin: GithubPublisher, frontmatter?: FrontMatterCache | null, file?: TFile): Repository | null{ + const {settings} = plugin; const allOtherRepo = settings.github.otherRepo; if (settings.plugin.shareAll?.enable && !frontmatter) { return defaultRepo(settings); } else if (!frontmatter) return null; - const linkedFrontmatter = getLinkedFrontmatter(frontmatter, settings, file, app); + const linkedFrontmatter = getLinkedFrontmatter(frontmatter, file, plugin); frontmatter = linkedFrontmatter ? {...linkedFrontmatter, ...frontmatter} : frontmatter; for (const repo of allOtherRepo) { if (frontmatter[repo.shareKey]) { @@ -137,8 +136,9 @@ export function isExcludedPath(settings: GitHubPublisherSettings, file: TFile | * @param {App} app - The Obsidian app instance. * @returns {string[]} - An array of shared keys found in the file. */ -export function multipleSharedKey(frontmatter: FrontMatterCache | undefined | null, settings: GitHubPublisherSettings, file: TFile | null, app: App): string[] { +export function multipleSharedKey(frontmatter: FrontMatterCache | undefined | null, file: TFile | null, plugin: GithubPublisher): string[] { const keysInFile: string[] = []; + const {settings} = plugin; if (settings.plugin.shareAll?.enable) keysInFile.push("share"); //add a key to count the shareAll @@ -149,7 +149,7 @@ export function multipleSharedKey(frontmatter: FrontMatterCache | undefined | nu } } if (!frontmatter) return keysInFile; - const linkedRepo = getLinkedFrontmatter(frontmatter, settings, file, app); + const linkedRepo = getLinkedFrontmatter(frontmatter, file, plugin); frontmatter = linkedRepo ? {...linkedRepo, ...frontmatter} : frontmatter; const allKey = settings.github.otherRepo.map((repo) => repo.shareKey); allKey.push(settings.plugin.shareKey); diff --git a/src/utils/index.ts b/src/utils/index.ts index 965e78d9..6aa9c68d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -13,6 +13,7 @@ import { RepoFrontmatter, TOKEN_PATH, UploadedFiles} from "../settings/interface"; import { HOURGLASS_ICON, SUCCESS_ICON } from "./icons"; +import { frontmatterFromFile } from "./parse_frontmatter"; type LogsParameters = { settings: Partial, @@ -231,23 +232,23 @@ function checkSlash(link: string): string { export async function createLink( file: TFile, multiRepo: MultiRepoProperties, - settings: GitHubPublisherSettings, - app:App + plugin: GithubPublisher, ): Promise { const otherRepo = multiRepo.repo; + const settings = plugin.settings; const repo = multiRepo.frontmatter; const copyLink = otherRepo ? otherRepo.copyLink : settings.plugin.copyLink; const github = otherRepo ? otherRepo : settings.github; if (!settings.plugin.copyLink.enable) { return; } - let filepath = getReceiptFolder(file, settings, otherRepo, app, multiRepo.frontmatter); + let filepath = getReceiptFolder(file, otherRepo, plugin, multiRepo.frontmatter); let baseLink = copyLink.links; if (baseLink.length === 0) { baseLink = repo instanceof Array ? `https://${github.user}.github.io/${settings.github.repo}/` : `https://${repo.owner}.github.io/${repo.repo}/`; } - const frontmatter = app.metadataCache.getFileCache(file)!.frontmatter; + const frontmatter = frontmatterFromFile(file, plugin); let removePart = copyLink.removePart; const smartKey = otherRepo?.smartKey; if (frontmatter) { diff --git a/src/utils/parse_frontmatter.ts b/src/utils/parse_frontmatter.ts index e8acc137..c3ef4f33 100644 --- a/src/utils/parse_frontmatter.ts +++ b/src/utils/parse_frontmatter.ts @@ -3,7 +3,7 @@ * See docs for all the condition */ -import { App, FrontMatterCache, normalizePath,TFile } from "obsidian"; +import { FrontMatterCache, normalizePath,TFile } from "obsidian"; import GithubPublisher from "src/main"; import { FolderSettings, FrontmatterConvert, GitHubPublisherSettings, Path, RepoFrontmatter, Repository } from "../settings/interface"; @@ -133,13 +133,13 @@ function translateBooleanForRemoveEmbed(removeEmbed: unknown) { } /** - * Get the frontmatter from the frontmatter - * @param {GitHubPublisherSettings} settings - * @param repository - * @param {FrontMatterCache} frontmatter - * @return {RepoFrontmatter[] | RepoFrontmatter} + * Retrieves the repository frontmatter based on the provided settings and repository information. + * + * @param {GitHubPublisherSettings} settings - The GitHub Publisher settings. + * @param {Repository | null} repository - The repository information. + * @param {FrontMatterCache | null} frontmatter - The frontmatter cache. + * @returns {RepoFrontmatter[] | RepoFrontmatter} - The repository frontmatter. */ - export function getRepoFrontmatter( settings: GitHubPublisherSettings, repository: Repository | null, @@ -493,11 +493,11 @@ function parseFrontmatterSettingsWithRepository( export function getLinkedFrontmatter( originalFrontmatter: FrontMatterCache | null | undefined, - settings: GitHubPublisherSettings, sourceFile: TFile | null | undefined, - app: App, + plugin: GithubPublisher, ) { - const {metadataCache, vault} = app; + const {settings} = plugin; + const {metadataCache, vault} = plugin.app; const linkedKey = settings.plugin.setFrontmatterKey; if (!linkedKey || !originalFrontmatter || !sourceFile) return originalFrontmatter; const linkedFrontmatter = originalFrontmatter?.[linkedKey]; @@ -521,7 +521,7 @@ 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); + const linkedFrontmatter = getLinkedFrontmatter(frontmatter, file, plugin); frontmatter = linkedFrontmatter ? { ...linkedFrontmatter, ...frontmatter } : frontmatter; } return frontmatter;