Skip to content

Commit

Permalink
[FIX] Folder ignore sorts all folder starting with string
Browse files Browse the repository at this point in the history
st3v3nmw#972
Squashed commit 5b29ae1
  • Loading branch information
ronzulu committed Jul 16, 2024
1 parent c3bc74b commit 438365b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- [FIX] Cards missing when horizontal rule present in document https://github.com/st3v3nmw/obsidian-spaced-repetition/pull/970
- [FIX] Include link parsing for Review context https://github.com/st3v3nmw/obsidian-spaced-repetition/pull/964
- Fixed notes selection when all notes are reviewed. [`#548`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/548)
- [FIX] Folder ignore sorts all folder starting with string https://github.com/st3v3nmw/obsidian-spaced-repetition/pull/972

#### [1.12.4](https://github.com/st3v3nmw/obsidian-spaced-repetition/compare/1.12.3...1.12.4)

Expand Down
3 changes: 2 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Notice, PluginSettingTab, Setting, App, Platform } from "obsidian";
import type SRPlugin from "src/main";
import { t } from "src/lang/helpers";
import { isEqualOrSubPath } from "./util/utils";

export interface SRSettings {
// flashcards
Expand Down Expand Up @@ -112,7 +113,7 @@ export class SettingsUtil {
}

static isPathInNoteIgnoreFolder(settings: SRSettings, path: string): boolean {
return settings.noteFoldersToIgnore.some((folder) => path.startsWith(folder));
return settings.noteFoldersToIgnore.some((folder) => isEqualOrSubPath(path, folder));
}

static isAnyTagANoteReviewTag(settings: SRSettings, tags: string[]): boolean {
Expand Down
35 changes: 34 additions & 1 deletion src/util/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from "moment";
import { Moment } from "moment";
import { PREFERRED_DATE_FORMAT } from "src/constants";
import { normalize, sep } from "path";
import { PREFERRED_DATE_FORMAT, YAML_FRONT_MATTER_REGEX } from "src/constants";

type Hex = number;

Expand Down Expand Up @@ -105,6 +106,38 @@ export function convertToStringOrEmpty(v: any): string {
return result;
}

/**
* Checks a path is equal or a subpath of the other rootPath
*
* @param toCheck The path to check it is equal or a subpath of path.
* @param rootPath The ref path to check the other is equal to or a subpath of this.
* @tutorial
* rootPath = "root/sub/sub2"
* if toCheck = "notRoot/..." -> false
* if toCheck = "root" -> true
* if toCheck = "root/sub" -> true
* if toCheck = "root/s" -> false
*/
export function isEqualOrSubPath(toCheck: string, rootPath: string): boolean {
const rootPathSections = normalize(rootPath.toLowerCase())
.replaceAll(/(\\|\/)/g, sep)
.split(sep)
.filter((p) => p !== "");
const pathSections = normalize(toCheck.toLowerCase())
.replaceAll(/(\\|\/)/g, sep)
.split(sep)
.filter((p) => p !== "");
if (pathSections.length < rootPathSections.length) {
return false;
}
for (let i = 0; i < rootPathSections.length; i++) {
if (rootPathSections[i] !== pathSections[i]) {
return false;
}
}
return true;
}

/**
* The returned content has the same number of lines as the supplied string, but with the frontmatter lines (if present) blanked out.
*
Expand Down
111 changes: 111 additions & 0 deletions tests/unit/util/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
convertToStringOrEmpty,
splitNoteIntoFrontmatterAndContent,
findLineIndexOfSearchStringIgnoringWs,
isEqualOrSubPath,
literalStringReplace,
} from "src/util/utils";

Expand Down Expand Up @@ -436,3 +437,113 @@ describe("findLineIndexOfSearchStringIgnoringWs", () => {
expect(findLineIndexOfSearchStringIgnoringWs(lines, "??")).toEqual(2);
});
});

describe("isEqualOrSubPath", () => {
const winSep = "\\";
const linSep = "/";
const root = "root";
const sub_1 = "plugins";
const sub_2 = "obsidian-spaced-repetition";
const sub_3 = "data";
const noMatch = "notRoot";
const caseMatch = "Root";

describe("Windows", () => {
const sep = winSep;
const rootPath = root + sep + sub_1;

test("Upper and lower case letters", () => {
expect(isEqualOrSubPath(caseMatch, root)).toBe(true);
expect(isEqualOrSubPath(caseMatch.toUpperCase(), root)).toBe(true);
});

test("Seperator auto correction", () => {
expect(isEqualOrSubPath(root + winSep + sub_1, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + winSep + sub_1 + winSep, rootPath)).toBe(true);

expect(isEqualOrSubPath(root + linSep + sub_1, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + linSep + sub_1 + linSep, rootPath)).toBe(true);
});

test("Differnent path", () => {
expect(isEqualOrSubPath(noMatch, rootPath)).toBe(false);
expect(isEqualOrSubPath(noMatch + sep, rootPath)).toBe(false);
expect(isEqualOrSubPath(noMatch + sep + sub_1, rootPath)).toBe(false);
expect(isEqualOrSubPath(noMatch + sep + sub_1 + sep + sub_2, rootPath)).toBe(false);
});

test("Partially Match path", () => {
expect(isEqualOrSubPath("roo", rootPath)).toBe(false);
expect(isEqualOrSubPath("roo" + sep, rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep + "plug", rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep + "plug" + sep, rootPath)).toBe(false);
});

test("Same path", () => {
expect(isEqualOrSubPath(rootPath, rootPath)).toBe(true);
});

test("Subpath", () => {
expect(isEqualOrSubPath(root, rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep, rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep + sub_1, rootPath)).toBe(true);
expect(isEqualOrSubPath(rootPath, rootPath + sep)).toBe(true);
expect(isEqualOrSubPath(rootPath + sep, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep + sub_2, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep + sub_2 + sep, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep + sub_2 + sep + sub_3, rootPath)).toBe(
true,
);
});
});
describe("Linux", () => {
const sep = linSep;
const rootPath = root + sep + sub_1;

test("Upper and lower case letters", () => {
expect(isEqualOrSubPath(caseMatch, root)).toBe(true);
expect(isEqualOrSubPath(caseMatch.toUpperCase(), root)).toBe(true);
});

test("Seperator auto correction", () => {
expect(isEqualOrSubPath(root + winSep + sub_1, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + winSep + sub_1 + winSep, rootPath)).toBe(true);

expect(isEqualOrSubPath(root + linSep + sub_1, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + linSep + sub_1 + linSep, rootPath)).toBe(true);
});

test("Differnent path", () => {
expect(isEqualOrSubPath(noMatch, rootPath)).toBe(false);
expect(isEqualOrSubPath(noMatch + sep, rootPath)).toBe(false);
expect(isEqualOrSubPath(noMatch + sep + sub_1, rootPath)).toBe(false);
expect(isEqualOrSubPath(noMatch + sep + sub_1 + sep + sub_2, rootPath)).toBe(false);
});

test("Partially Match path", () => {
expect(isEqualOrSubPath("roo", rootPath)).toBe(false);
expect(isEqualOrSubPath("roo" + sep, rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep + "plug", rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep + "plug" + sep, rootPath)).toBe(false);
});

test("Same path", () => {
expect(isEqualOrSubPath(rootPath, rootPath)).toBe(true);
});

test("Subpath", () => {
expect(isEqualOrSubPath(root, rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep, rootPath)).toBe(false);
expect(isEqualOrSubPath(root + sep + sub_1, rootPath)).toBe(true);
expect(isEqualOrSubPath(rootPath, rootPath + sep)).toBe(true);
expect(isEqualOrSubPath(rootPath + sep, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep + sub_2, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep + sub_2 + sep, rootPath)).toBe(true);
expect(isEqualOrSubPath(root + sep + sub_1 + sep + sub_2 + sep + sub_3, rootPath)).toBe(
true,
);
});
});
});

0 comments on commit 438365b

Please sign in to comment.