Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Folder ignore sorts all folder starting with string #972

Merged
merged 4 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { NoteEaseCalculator } from "./NoteEaseCalculator";
import { DeckTreeStatsCalculator } from "./DeckTreeStatsCalculator";
import { NoteEaseList } from "./NoteEaseList";
import { QuestionPostponementList } from "./QuestionPostponementList";
import { isEqualOrSubPath } from "./util/utils";

interface PluginData {
settings: SRSettings;
Expand Down Expand Up @@ -379,7 +380,7 @@ export default class SRPlugin extends Plugin {
for (const noteFile of notes) {
if (
this.data.settings.noteFoldersToIgnore.some((folder) =>
noteFile.path.startsWith(folder),
isEqualOrSubPath(noteFile.path, folder),
)
) {
continue;
Expand Down Expand Up @@ -567,7 +568,11 @@ export default class SRPlugin extends Plugin {
fileCachedData.frontmatter || {};

const tags = getAllTags(fileCachedData) || [];
if (this.data.settings.noteFoldersToIgnore.some((folder) => note.path.startsWith(folder))) {
if (
this.data.settings.noteFoldersToIgnore.some((folder) =>
isEqualOrSubPath(note.path, folder),
)
) {
new Notice(t("NOTE_IN_IGNORED_FOLDER"));
return;
}
Expand Down
33 changes: 33 additions & 0 deletions src/util/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from "moment";
import { Moment } from "moment";
import { normalize, sep } from "path";
import { PREFERRED_DATE_FORMAT, YAML_FRONT_MATTER_REGEX } from "src/constants";

type Hex = number;
Expand Down Expand Up @@ -96,6 +97,38 @@ export function stringTrimStart(str: string): [string, string] {
return [ws, trimmed];
}

/**
* 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand the confusion now. Yes we do have the same understanding of the word "sub", this might just be a documentation mistake?

if toCheck = "root" -> true
if toCheck = "root/sub" -> true

Should those be false? I think one of the test cases checks that it is false
expect(isEqualOrSubPath(root, rootPath)).toBe(false);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW your English is great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think your right the documentation seems to be wrong. And now I understand what you mean with root path is not the correct naming. With rootPath I meant the path and all of its sub paths but It is not actually the root path.

But the test should be correct if the path to check is above the rootPath it is not a part of the sub folders.

* 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;
}

//
// This returns [frontmatter, content]
//
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 @@ -2,6 +2,7 @@ import { YAML_FRONT_MATTER_REGEX } from "src/constants";
import {
extractFrontmatter,
findLineIndexOfSearchStringIgnoringWs,
isEqualOrSubPath,
literalStringReplace,
} from "src/util/utils";

Expand Down Expand Up @@ -243,3 +244,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,
);
});
});
});
Loading