Skip to content

Commit

Permalink
file: add comparator to file
Browse files Browse the repository at this point in the history
  • Loading branch information
julesvirallinen committed Oct 2, 2023
1 parent 91c6640 commit c6699f5
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 271 deletions.
7 changes: 7 additions & 0 deletions src/publishFile/PublishFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export class PublishFile {
return this.metadataCache.getCache(this.file.path)?.frontmatter ?? {};
}

/** Add other possible sorting logic here, eg if we add dg-sortWeight
* We might also want to sort by meta.getPath for rewritten garden path
*/
compare(other: PublishFile) {
return this.file.path.localeCompare(other.file.path);
}

getPath = () => this.file.path;
getCompiledFrontmatter() {
const frontmatterCompiler = new FrontmatterCompiler(this.settings);
Expand Down
9 changes: 5 additions & 4 deletions src/publisher/PublishStatusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ export default class PublishStatusManager implements IPublishStatusManager {
remoteImageHashes,
marked.images,
);
unpublishedNotes.sort((a, b) => (a.getPath() > b.getPath() ? 1 : -1));
publishedNotes.sort((a, b) => (a.getPath() > b.getPath() ? 1 : -1));
changedNotes.sort((a, b) => (a.getPath() > b.getPath() ? 1 : -1));
deletedNotePaths.sort((a, b) => (a > b ? 1 : -1));
// These might already be sorted, as getFilesMarkedForPublishing sorts already
publishedNotes.sort((a, b) => a.compare(b));
publishedNotes.sort((a, b) => a.compare(b));
changedNotes.sort((a, b) => a.compare(b));
deletedNotePaths.sort((a, b) => a.path.localeCompare(b.path));

return {
unpublishedNotes,
Expand Down
2 changes: 1 addition & 1 deletion src/publisher/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class Publisher {
}

return {
notes: notesToPublish,
notes: notesToPublish.sort((a, b) => a.compare(b)),
images: Array.from(imagesToPublish),
};
}
Expand Down
11 changes: 4 additions & 7 deletions src/test/snapshot/generateGardenSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ export const generateGardenSnapshot = async (
}

const marked = await publisher.getFilesMarkedForPublishing();
let fileString = "---\n";

const notesSortedByCreationDate = marked.notes.sort(
(note) => note.file.stat.ctime,
);
let fileString = "IMAGES: \n";
fileString += marked.images.map((path) => `${path}\n`);

const assetPaths = new Set<string>();

for (const file of notesSortedByCreationDate) {
for (const file of marked.notes) {
fileString += "==========\n";
fileString += `${file.getPath()}\n`;
fileString += "==========\n";
Expand All @@ -36,9 +33,9 @@ export const generateGardenSnapshot = async (
assets.images.map((image) => assetPaths.add(image.path));

fileString += `${content}\n`;
fileString += Array.from(assetPaths).map((path) => `${path}\n`);
}
fileString += "==========\n";
fileString += Array.from(assetPaths).map((path) => `${path}\n`);

const fullSnapshotPath = `${devPluginPath}/${SNAPSHOT_PATH}`;

Expand Down
Loading

0 comments on commit c6699f5

Please sign in to comment.