Skip to content

Commit 2ec8b7b

Browse files
skogsviksaberzero1
andauthored
fix: respect vault path setting (#86)
* Add test for getFilesMarkedForPublishing Test the behaviour of vaultPath setting. Expected behaviour is to return only file paths in the folder. vaultPath = "/" should return all files. * Filter files according to vaultPath Fixes #85. getFilesMarkedForPublishing should only return files under the vaultPath. * Update src/publisher/Publisher.ts --------- Co-authored-by: Emile Bangma <[email protected]>
1 parent 26f6abe commit 2ec8b7b

File tree

3 files changed

+107
-6
lines changed

3 files changed

+107
-6
lines changed

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module.exports = {
22
preset: "ts-jest/presets/js-with-ts",
33
testEnvironment: "node",
4+
moduleNameMapper: {
5+
"^src/(.*)$": "<rootDir>/src/$1",
6+
},
47
};

src/publisher/Publisher.test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import Publisher from "./Publisher";
2+
import QuartzSyncer from "main";
3+
4+
import { TFile, Vault, MetadataCache, App } from "obsidian";
5+
import QuartzSyncerSettings from "src/models/settings";
6+
import { DataStore } from "src/publishFile/DataStore";
7+
8+
jest.mock("src/publishFile/PublishFile", () => {
9+
return {
10+
PublishFile: jest.fn(({ file }) => ({
11+
file,
12+
getBlobLinks: jest.fn().mockResolvedValue([]),
13+
compare: (other: TFile) => file.path.localeCompare(other.path),
14+
})),
15+
};
16+
});
17+
18+
describe("Publisher", () => {
19+
describe("getFilesMarkedForPublishing", () => {
20+
let publisher: Publisher;
21+
22+
const vaultFiles = Object.freeze([
23+
"note1.md",
24+
"note2.md",
25+
"folder/note3.md",
26+
"folder/note4.md",
27+
"vault-folder/note5.md",
28+
"vault-folder/note6.md",
29+
"outside-folder/note7.md",
30+
"outside-folder/note8.md",
31+
"vault-folder/sub/note9.md",
32+
"vault-folder/sub/note10.md",
33+
"outside-folder/sub/note11.md",
34+
"outside-folder/sub/note12.md",
35+
]);
36+
37+
const vault = {
38+
getMarkdownFiles: jest
39+
.fn()
40+
.mockReturnValue(vaultFiles.map((path) => ({ path }) as TFile)),
41+
} as unknown as Vault;
42+
43+
const metadataCache = {
44+
getCache: jest.fn().mockReturnValue({ frontmatter: {} }),
45+
} as unknown as MetadataCache;
46+
47+
it("includes all markdown files when vaultPath is '/'", async () => {
48+
publisher = new Publisher(
49+
{} as App,
50+
{} as QuartzSyncer,
51+
vault,
52+
metadataCache,
53+
{
54+
vaultPath: "/",
55+
allNotesPublishableByDefault: true,
56+
} as QuartzSyncerSettings,
57+
{} as DataStore,
58+
);
59+
const result = await publisher.getFilesMarkedForPublishing();
60+
61+
expect(result.notes.length).toBe(12);
62+
63+
expect(
64+
new Set(result.notes.map((pFile) => pFile.file.path)),
65+
).toEqual(new Set(vaultFiles));
66+
});
67+
68+
it("includes only files inside vaultPath when vaultPath is not '/'", async () => {
69+
publisher = new Publisher(
70+
{} as App,
71+
{} as QuartzSyncer,
72+
vault,
73+
metadataCache,
74+
{
75+
vaultPath: "vault-folder/",
76+
allNotesPublishableByDefault: true,
77+
} as QuartzSyncerSettings,
78+
{} as DataStore,
79+
);
80+
const result = await publisher.getFilesMarkedForPublishing();
81+
82+
expect(result.notes.length).toBe(4);
83+
84+
expect(
85+
new Set(result.notes.map((pFile) => pFile.file.path)),
86+
).toEqual(
87+
new Set([
88+
"vault-folder/note5.md",
89+
"vault-folder/note6.md",
90+
"vault-folder/sub/note9.md",
91+
"vault-folder/sub/note10.md",
92+
]),
93+
);
94+
});
95+
});
96+
});

src/publisher/Publisher.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ export default class Publisher {
8686
* @returns A promise that resolves to an object containing notes and blobs to be published.
8787
*/
8888
async getFilesMarkedForPublishing(): Promise<MarkedForPublishing> {
89-
const files = this.vault.getMarkdownFiles();
89+
const vaultIsRoot = this.settings.vaultPath === "/";
9090

9191
// Only include files that are within the vaultPath
92-
if (this.settings.vaultPath !== "/") {
93-
files.filter((file) => {
94-
return file.path.startsWith(this.settings.vaultPath);
95-
});
96-
}
92+
const files = this.vault
93+
.getMarkdownFiles()
94+
.filter(
95+
(file: TFile) =>
96+
vaultIsRoot ||
97+
file.path.startsWith(this.settings.vaultPath),
98+
);
9799

98100
const notesToPublish: PublishFile[] = [];
99101
const blobsToPublish: Set<string> = new Set();

0 commit comments

Comments
 (0)