Skip to content

Commit

Permalink
feat(repository): allow to set a file in settings
Browse files Browse the repository at this point in the history
Set a file in the settings allow to rewrite/override all the settings provided for a specific repository, preventing error during autoclean and prevent using the `set` key in frontmatter.
  • Loading branch information
Mara-Li authored May 4, 2024
1 parent 1ea27f7 commit eba4d78
Show file tree
Hide file tree
Showing 45 changed files with 354 additions and 110 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"bump": "node commit-and-tag-version.js",
"postbump": "git push --follow-tags origin master",
"predeploy": "pnpm run bump",
"deploy": "pnpm run export"
"deploy": "pnpm run export",
"tsc": "tsc --noEmit --skipLibCheck"
},
"commit-and-tag-version": {
"t": ""
Expand Down Expand Up @@ -73,4 +74,4 @@
"got@<11.8.5": ">=11.8.5"
}
}
}
}
1 change: 1 addition & 0 deletions src/GitHub/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export class GithubBranch extends FilesManagement {
async updateRepositoryOnOne(
repoFrontmatter: RepoFrontmatter
): Promise<boolean> {
if (this.settings.github.dryRun.enable) return true;
try {
const pullRequest = await this.pullRequestOnRepo(
repoFrontmatter
Expand Down
3 changes: 2 additions & 1 deletion src/GitHub/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "../settings/interface";
import { logs, notif, trimObject} from "../utils";
import {isAttachment, verifyRateLimitAPI} from "../utils/data_validation_test";
import { frontmatterSettingsRepository } from "../utils/parse_frontmatter";
import { FilesManagement } from "./files";

/**
Expand All @@ -39,7 +40,7 @@ export async function deleteFromGithub(
const monoProperties: MonoRepoProperties = {
frontmatter: repo,
repo: repoProperties.repo,
convert: repoProperties.convert,
convert: frontmatterSettingsRepository(filesManagement.plugin, repo),
};
deleted.push(await deleteFromGithubOneRepo(
silent,
Expand Down
9 changes: 5 additions & 4 deletions src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,27 @@ export class FilesManagement extends Publisher {
getAllFileWithPath(repo: Repository | null, convert: FrontmatterConvert): ConvertedLink[] {
const files = this.vault.getFiles().filter((x) => !x.path.startsWith(this.settings.github.dryRun.folderName));
const allFileWithPath: ConvertedLink[] = [];
const sourceFrontmatter = getRepoFrontmatter(this.plugin, repo, null, true);
for (const file of files) {
if (isAttachment(file.name, this.settings.embed.unHandledObsidianExt)) {
const filepath = getImagePath(file, this.settings, convert);
const filepath = getImagePath(file, this.plugin, convert, sourceFrontmatter );
allFileWithPath.push({
converted: filepath,
real: file.path,
});
} else if (file.extension == "md") {
const frontMatter = frontmatterFromFile(file, this.plugin);
const frontMatter = frontmatterFromFile(file, this.plugin, repo);
if (isShared(frontMatter, this.settings, file, repo)) {
const repoFrontmatter = getRepoFrontmatter(
this.settings,
this.plugin,
repo,
frontMatter
);
const filepath = getReceiptFolder(file, repo, this.plugin, repoFrontmatter);
allFileWithPath.push({
converted: filepath,
real: file.path,
repoFrontmatter: repoFrontmatter,
repoFrontmatter,
});
}
}
Expand Down
31 changes: 21 additions & 10 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import i18next from "i18next";
import { Base64 } from "js-base64";
import {
arrayBufferToBase64,
FrontMatterCache,
MetadataCache,
normalizePath,
Notice,
TFile,
TFolder,
Vault,
} from "obsidian";
import merge from "ts-deepmerge";

import { mainConverting } from "../conversion";
import { convertToHTMLSVG } from "../conversion/compiler/excalidraw";
Expand Down Expand Up @@ -41,7 +43,7 @@ import {
isShared,
} from "../utils/data_validation_test";
import { LOADING_ICON } from "../utils/icons";
import { frontmatterFromFile, getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { frontmatterFromFile, frontmatterSettingsRepository, getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { ShareStatusBar } from "../utils/status_bar";
import { deleteFromGithub } from "./delete";
import { FilesManagement } from "./files";
Expand Down Expand Up @@ -88,6 +90,7 @@ export default class Publisher {
fileHistory: TFile[],
deepScan: boolean,
properties: MonoProperties,

) {
const uploadedFile: UploadedFiles[] = [];
const fileError: string[] = [];
Expand Down Expand Up @@ -115,7 +118,8 @@ export default class Publisher {
false,
repoProperties,
fileHistory,
true
true,
properties.frontmatter.source
);
if (published) {
uploadedFile.push(...published.uploaded);
Expand Down Expand Up @@ -173,13 +177,15 @@ export default class Publisher {
repo: MultiRepoProperties | MonoRepoProperties,
fileHistory: TFile[] = [],
deepScan: boolean = false,
sourceFrontmatter: FrontMatterCache | null | undefined,
) {
const shareFiles = new FilesManagement(
this.octokit,
this.plugin,
);
const frontmatter = frontmatterFromFile(file, this.plugin);
const repoFrontmatter = getRepoFrontmatter(this.settings, repo.repo, frontmatter);
let frontmatter = frontmatterFromFile(file, this.plugin, null);
if (sourceFrontmatter && frontmatter) frontmatter = merge(frontmatter, sourceFrontmatter);
const repoFrontmatter = getRepoFrontmatter(this.plugin, repo.repo, frontmatter);
const isNotEmpty = await checkEmptyConfiguration(repoFrontmatter, this.plugin);
repo.frontmatter = repoFrontmatter;
if (
Expand All @@ -195,11 +201,13 @@ export default class Publisher {
try {
logs({ settings: this.settings }, `Publishing file: ${file.path}`);
fileHistory.push(file);
const frontmatterSettings = getFrontmatterSettings(
const frontmatterSettingsFromFile = getFrontmatterSettings(
frontmatter,
this.settings,
repo.repo
);
const frontmatterRepository = frontmatterSettingsRepository(this.plugin, repo.repo);
const frontmatterSettings = merge(frontmatterRepository, frontmatterSettingsFromFile);
let embedFiles = shareFiles.getSharedEmbed(
file,
frontmatterSettings,
Expand Down Expand Up @@ -240,7 +248,8 @@ export default class Publisher {
plugin: this.plugin,
frontmatter: {
general: frontmatterSettings,
repo
repo,
source: sourceFrontmatter
},
repository: multiProperties.repository,
filepath: multiProperties.filepath,
Expand Down Expand Up @@ -444,8 +453,9 @@ export default class Publisher {
}
const path = getImagePath(
imageFile,
this.settings,
properties.frontmatter.general
this.plugin,
properties.frontmatter.general,
properties.frontmatter.repo
);
if (this.settings.github.dryRun.enable) {
const folderName = this.settings.github.dryRun.folderName
Expand Down Expand Up @@ -633,8 +643,9 @@ export default class Publisher {
if (isAttachment(file.name, this.settings.embed.unHandledObsidianExt)) {
const imagePath = getImagePath(
file,
this.settings,
properties.frontmatter.general
this.plugin,
properties.frontmatter.general,
properties.frontmatter.repo
);
const repoFrontmatter = properties.frontmatter;
if (this.settings.github.dryRun.enable) {
Expand Down
19 changes: 7 additions & 12 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} from "../utils";
import {checkRepositoryValidity, isShared} from "../utils/data_validation_test";
import { frontmatterFromFile, getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { frontmatterFromFile, frontmatterSettingsRepository, 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 = frontmatterFromFile(file, plugin);
const frontmatter = frontmatterFromFile(file, plugin, repo);
if (
file && frontmatter && isShared(frontmatter, plugin.settings, file, repo)
) {
if (!checking) {
const multiRepo: MultiRepoProperties = {
frontmatter: getRepoFrontmatter(plugin.settings, repo, frontmatter),
frontmatter: getRepoFrontmatter(plugin, repo, frontmatter, true),
repo,
};
createLink(
Expand Down Expand Up @@ -76,17 +76,12 @@ export async function purgeNotesRemoteCallback(plugin: GithubPublisher, repo: Re
hotkeys: [],
//@ts-ignore
callback: async () => {
const frontmatter = getRepoFrontmatter(plugin.settings, repo);
const frontmatter = getRepoFrontmatter(plugin, repo, undefined, true);
const monoRepo: MonoRepoProperties = {
frontmatter: Array.isArray(frontmatter) ? frontmatter[0] : frontmatter,
repo,
convert: getFrontmatterSettings(
null,
plugin.settings,
repo
)
convert: frontmatterSettingsRepository(plugin, repo)
};
//@ts-ignore
const publisher = await plugin.reloadOctokit(repo?.smartKey);
await purgeNotesRemote(
publisher,
Expand All @@ -102,7 +97,6 @@ export async function purgeNotesRemoteCallback(plugin: GithubPublisher, repo: Re
* @call shareOneNote
* @param {Repository | null} repo - Other repo if the command is called from the suggest_other_repo_command.ts
* @param {GithubPublisher} plugin - The plugin instance
* @param {string} branchName - The branch name to upload the file
* @return {Promise<Command>}
*/
export async function shareOneNoteCallback(repo: Repository|null, plugin: GithubPublisher): Promise<Command> {
Expand All @@ -118,7 +112,7 @@ export async function shareOneNoteCallback(repo: Repository|null, plugin: Github
hotkeys: [],
checkCallback: (checking) => {
const file = plugin.app.workspace.getActiveFile();
const frontmatter = frontmatterFromFile(file, plugin);
const frontmatter = frontmatterFromFile(file, plugin, repo);
if (
file && frontmatter && isShared(frontmatter, plugin.settings, file, repo)
) {
Expand All @@ -127,6 +121,7 @@ export async function shareOneNoteCallback(repo: Repository|null, plugin: Github
octokit,
file,
repo,
frontmatter,
file.basename,
);
}
Expand Down
27 changes: 15 additions & 12 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 { frontmatterFromFile, getFrontmatterSettings, getRepoFrontmatter } from "../utils/parse_frontmatter";
import { frontmatterFromFile, frontmatterSettingsRepository, getRepoFrontmatter } from "../utils/parse_frontmatter";
import {shareAllMarkedNotes, shareOneNote} from ".";
import {ChooseRepoToRun} from "./suggest_other_repo_commands_modal";

Expand All @@ -18,15 +18,11 @@ 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(repo?.smartKey);
const statusBarItems = plugin.addStatusBarItem();
const repoFrontmatter = getRepoFrontmatter(plugin.settings, repo, null);
const repoFrontmatter = getRepoFrontmatter(plugin, repo, null, true);
const monoProperties: MonoRepoProperties = {
frontmatter: Array.isArray(repoFrontmatter) ? repoFrontmatter[0] : repoFrontmatter,
repo,
convert: getFrontmatterSettings(
null,
plugin.settings,
repo
)
convert: frontmatterSettingsRepository(plugin, repo)
};
await shareAllMarkedNotes(
publisher,
Expand Down Expand Up @@ -104,15 +100,16 @@ 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 = frontmatterFromFile(file, plugin);
let getSharedKey = getRepoSharedKey(plugin, frontmatter, file);
const frontmatterSharedKey = frontmatterFromFile(file, plugin, null);
let getSharedKey = getRepoSharedKey(plugin, frontmatterSharedKey, file);
const frontmatter = frontmatterFromFile(file, plugin, getSharedKey);
const allKeysFromFile = multipleSharedKey(frontmatter, file, plugin);
if (
!(isShared(frontmatter, plugin.settings, file, getSharedKey) &&
plugin.settings.plugin.fileMenu)
) return;

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

menu.addItem((item) => {
/**
Expand Down Expand Up @@ -159,6 +156,7 @@ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: st
await plugin.reloadOctokit(getSharedKey?.smartKey),
file,
getSharedKey,
frontmatter,
fileName
);
});
Expand All @@ -177,11 +175,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 {
const frontmatter = frontmatterFromFile(file, plugin);
const frontmatter = frontmatterFromFile(file, plugin, repo);
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, repo, frontmatter, true);
repoFrontmatter = repoFrontmatter instanceof Array ? repoFrontmatter : [repoFrontmatter];
/**
* default repo
Expand All @@ -200,6 +198,7 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
await plugin.reloadOctokit(),
file,
defaultRepo(plugin.settings),
null,
fileName
);
});
Expand All @@ -222,6 +221,7 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
await plugin.reloadOctokit(otherRepo?.smartKey),
file,
otherRepo,
frontmatter,
fileName
);
});
Expand All @@ -243,6 +243,7 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
await plugin.reloadOctokit(),
file,
repo,
frontmatter,
fileName
);
});
Expand All @@ -256,10 +257,12 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
.setIcon("file-input")
.onClick(async () => {
new ChooseRepoToRun(plugin.app, plugin, repo?.shareKey, branchName, "file", file.basename, async (item: Repository) => {
const sourceFrontmatter = frontmatterFromFile(file, plugin, item);
await shareOneNote(
await plugin.reloadOctokit(item.smartKey),
file,
item,
sourceFrontmatter,
fileName
);
}).open();
Expand Down
Loading

0 comments on commit eba4d78

Please sign in to comment.