Skip to content

Commit

Permalink
remove multiple call and use only plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jan 24, 2024
1 parent a7a43de commit 1a159b9
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub/upload.ts
Original file line number Diff line number Diff line change
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, 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
3 changes: 1 addition & 2 deletions src/commands/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/file_menu.ts
Original file line number Diff line number Diff line change
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 @@ -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)
Expand Down Expand Up @@ -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);
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export async function shareOneNote(
repository: Repository | null = null,
title?: string
): Promise<void|false> {
const settings = PublisherManager.settings;
const {settings, plugin} = PublisherManager;
const app = PublisherManager.plugin.app;
const frontmatter = frontmatterFromFile(file, PublisherManager.plugin);
try {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/commands/plugin_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/conversion/file_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function createRelativePath(
): Promise<string> {
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);
Expand All @@ -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,
Expand Down Expand Up @@ -145,7 +145,6 @@ export async function createRelativePath(
//in case of errors
return getReceiptFolder(
targetFile.linked,
settings,
shortRepo,
plugin,
targetRepo
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions src/settings/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -292,7 +297,8 @@ async function migrateOldSettings(plugin: GithubPublisher, old: OldSettings) {
addCmd: false,
},
noticeError: old.logNotice,
displayModalRepoEditing: false
displayModalRepoEditing: false,
setFrontmatterKey: "Set"
}
};
//@ts-ignore
Expand Down
16 changes: 8 additions & 8 deletions src/utils/data_validation_test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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

Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GitHubPublisherSettings>,
Expand Down Expand Up @@ -231,23 +232,23 @@ function checkSlash(link: string): string {
export async function createLink(
file: TFile,
multiRepo: MultiRepoProperties,
settings: GitHubPublisherSettings,
app:App
plugin: GithubPublisher,
): Promise<void> {
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) {
Expand Down
22 changes: 11 additions & 11 deletions src/utils/parse_frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand Down

0 comments on commit 1a159b9

Please sign in to comment.