Skip to content

Commit

Permalink
adjust submenu when sharing a excluded file or from dryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jan 21, 2024
1 parent dac77f3 commit d8eb7c4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
29 changes: 16 additions & 13 deletions src/commands/file_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Menu, MenuItem, Platform, TFile, TFolder} from "obsidian";

import GithubPublisher from "../main";
import {MonoRepoProperties, Repository} from "../settings/interface";
import {defaultRepo, getRepoSharedKey, isShared, multipleSharedKey} from "../utils/data_validation_test";
import {defaultRepo, getRepoSharedKey, isExcludedPath, isInDryRunFolder, isShared, multipleSharedKey} from "../utils/data_validation_test";
import { getRepoFrontmatter } from "../utils/parse_frontmatter";
import {shareAllMarkedNotes, shareOneNote} from ".";
import {ChooseRepoToRun} from "./suggest_other_repo_commands_modal";
Expand Down Expand Up @@ -46,21 +46,24 @@ export async function shareFolderRepo(plugin: GithubPublisher, folder: TFolder,
export function addSubMenuCommandsFolder(plugin: GithubPublisher, item: MenuItem, folder: TFolder, branchName: string, originalMenu: Menu): Menu {
//@ts-ignore
const subMenu = Platform.isDesktop ? item.setSubmenu() as Menu : originalMenu;
subMenu.addItem((subItem) => {
subItem
.setTitle(i18next.t("commands.shareViewFiles.multiple.on", {
smartKey: i18next.t("common.default").toUpperCase(),
doc: folder.name
}))
.setIcon("folder-up")
.onClick(async () => {
const repo = getRepoSharedKey(plugin.settings, undefined);
await shareFolderRepo(plugin, folder, branchName, repo);
});
});
if (!isExcludedPath(plugin.settings,folder, defaultRepo(plugin.settings))) {
subMenu.addItem((subItem) => {
subItem
.setTitle(i18next.t("commands.shareViewFiles.multiple.on", {
smartKey: i18next.t("common.default").toUpperCase(),
doc: folder.name
}))
.setIcon("folder-up")
.onClick(async () => {
const repo = getRepoSharedKey(plugin.settings, undefined);
await shareFolderRepo(plugin, folder, branchName, repo);
});
});
}
const activatedRepoCommands = plugin.settings.github.otherRepo.filter((repo) => repo.createShortcuts);
if (activatedRepoCommands.length > 0) {
activatedRepoCommands.forEach((otherRepo) => {
if (isInDryRunFolder(plugin.settings, otherRepo, folder)) return;
subMenu.addItem((item) => {
item.setTitle(
i18next.t("commands.shareViewFiles.multiple.on", {
Expand Down
38 changes: 29 additions & 9 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 {FrontMatterCache, Notice, TFile} from "obsidian";
import {FrontMatterCache, normalizePath,Notice, TFile, TFolder} from "obsidian";
import GithubPublisher from "src/main";

import {GithubBranch} from "../GitHub/branch";
Expand Down Expand Up @@ -31,10 +31,10 @@ export function isInternalShared(
if (properties.repository?.shareAll?.enable)
{
const excludedFileName = properties.repository.shareAll.excludedFileName;
return !file.basename.startsWith(excludedFileName);
return !file.basename.startsWith(excludedFileName) && !isInDryRunFolder(properties.settings, properties.repository, file);
}
if (!frontmatter) return false;
if (isExcludedPath(properties.settings, file)) return false;
if (isExcludedPath(properties.settings, file, properties.repository)) return false;
const shareKey = properties.repository?.shareKey || properties.settings.plugin.shareKey;
logs({settings: properties.settings}, "shareKey", shareKey, "frontmatter", frontmatter[shareKey]);
if (frontmatter[shareKey] == null || frontmatter[shareKey] === undefined || ["false", "0", "no"].includes(frontmatter[shareKey].toString().toLowerCase())) return false;
Expand Down Expand Up @@ -80,16 +80,16 @@ export function isShared(
const otherRepoWithShareAll = settings.github.otherRepo.filter((repo) => repo.shareAll?.enable);
if (!settings.plugin.shareAll?.enable && otherRepoWithShareAll.length === 0) {
const shareKey = otherRepo ? otherRepo.shareKey : settings.plugin.shareKey;
if ( meta == null || !meta[shareKey] || meta[shareKey] == null || isExcludedPath(settings, file) || meta[shareKey] === undefined || ["false", "0", "no"].includes(meta[shareKey].toString().toLowerCase())) {
if ( meta == null || !meta[shareKey] || meta[shareKey] == null || isExcludedPath(settings, file, otherRepo) || meta[shareKey] === undefined || ["false", "0", "no"].includes(meta[shareKey].toString().toLowerCase())) {
return false;
}
const shareKeyInFrontmatter:string = meta[shareKey].toString().toLowerCase();
return ["true", "1", "yes"].includes(shareKeyInFrontmatter);
} else if (settings.plugin.shareAll?.enable || otherRepoWithShareAll.length > 0) {
const allExcludedFileName = otherRepoWithShareAll.map((repo) => repo.shareAll!.excludedFileName);
allExcludedFileName.push(settings.plugin.shareAll!.excludedFileName);
if (allExcludedFileName.some(prefix => !file.basename.startsWith(prefix)) && !isExcludedPath(settings, file)) {
return true;
if (allExcludedFileName.some(prefix => !file.basename.startsWith(prefix))) {
return !isExcludedPath(settings, file, otherRepo);
}
}
return false;
Expand All @@ -100,16 +100,22 @@ export function isShared(
* @param file {TFile}
* @returns boolean
*/
function isExcludedPath(settings: GitHubPublisherSettings, file: TFile):boolean {
export function isExcludedPath(settings: GitHubPublisherSettings, file: TFile | TFolder, repository: Repository | null):boolean {
const excludedFolder = settings.plugin.excludedFolder;
if (settings.plugin.shareAll?.enable || repository?.shareAll?.enable) {
const excludedFromShare = repository?.shareAll?.excludedFileName ?? settings.plugin.shareAll?.excludedFileName;
if (excludedFromShare && file.name.startsWith(excludedFromShare)) {
return true;
}
}
for (const folder of excludedFolder) {
const isRegex = folder.match(FIND_REGEX);
const regex = isRegex ? new RegExp(isRegex[1], isRegex[2]) : null;
if ((regex && regex.test(file.path)) || file.path.contains(folder.trim())) {
if ((regex?.test(file.path)) || file.path.contains(folder.trim())) {
return true;
}
}
return false;
return isInDryRunFolder(settings, repository, file);
}


Expand Down Expand Up @@ -423,4 +429,18 @@ export function isFolderNote(properties: MultiProperties) {
return filename === model;
}
return false;
}

export function isInDryRunFolder(settings: GitHubPublisherSettings, repo: Repository | null, file: TFile | TFolder) {
if (!settings.github.dryRun.enable) return false;
const variables = {
owner: repo?.user ?? settings.github.user,
repo: repo?.repo ?? settings.github.repo,
branch: repo?.branch ?? settings.github.branch,
};
const folder = settings.github.dryRun.folderName
.replace("{{owner}}", variables.owner)
.replace("{{repo}}", variables.repo)
.replace("{{branch}}", variables.branch);
return file.path.startsWith(normalizePath(folder));
}

0 comments on commit d8eb7c4

Please sign in to comment.