Skip to content

Commit e9c4811

Browse files
committed
feat(sort): Add sort in meta.
1 parent 0f02335 commit e9c4811

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/html-generation/global-gen.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Path } from "src/utils/path";
22
import { InvioSettingTab } from "src/settings";
33
import { HeadingCache, TAbstractFile, TFile, TFolder } from "obsidian";
4+
import { log } from '../moreOnLog';
45

56
export const enum TreeItemType
67
{
@@ -10,6 +11,9 @@ export const enum TreeItemType
1011
None = "none"
1112
}
1213

14+
export type TFileWithMeta = TFile & { sort?: number };
15+
export type TAbstractFileWithMeta = TAbstractFile & { sort?: number };
16+
1317
function isHeadingCache(obj: any): obj is HeadingCache
1418
{
1519
return obj != undefined && "heading" in obj && "level" in obj;
@@ -26,6 +30,7 @@ export class LinkTree
2630
public href: string | undefined = undefined;
2731
public root: LinkTree | undefined = undefined;
2832
public isRoot: boolean = false;
33+
public sort: number = 0;
2934

3035
/**
3136
* Sets the source of this tree item. This also sets the type, title and href based on the source.
@@ -71,11 +76,12 @@ export class LinkTree
7176
return this.#type;
7277
}
7378

74-
constructor(source: TAbstractFile | HeadingCache | undefined, parent: LinkTree | undefined, depth: number, root: LinkTree | undefined = undefined)
79+
constructor(source: TAbstractFile | HeadingCache | undefined, parent: LinkTree | undefined, depth: number, root: LinkTree | undefined = undefined, sort: number | undefined = 0)
7580
{
7681
this.source = source;
7782
this.parent = parent;
7883
this.depth = depth;
84+
this.sort = sort;
7985

8086
if(root == undefined) this.root = this.findRoot();
8187
}
@@ -99,15 +105,15 @@ export class LinkTree
99105
* Creates a tree from a list of files.
100106
* @returns The root of the tree.
101107
*/
102-
public static fromFiles(files: TFile[]): LinkTree
108+
public static fromFiles(files: TFileWithMeta[]): LinkTree
103109
{
104110
let root = new LinkTree(undefined, undefined, 0);
105111

106112
for (let file of files)
107113
{
108-
let pathSections: TAbstractFile[] = [];
114+
let pathSections: TAbstractFileWithMeta[] = [];
109115

110-
let parentFile: TAbstractFile = file;
116+
let parentFile: TAbstractFileWithMeta = file;
111117
while (parentFile != undefined)
112118
{
113119
pathSections.push(parentFile);
@@ -124,7 +130,7 @@ export class LinkTree
124130
let child = parent.children.find(c => c.title == section.name && c.type == sectionType && c.depth == i);
125131
if (child == undefined)
126132
{
127-
child = new LinkTree(section, parent, i, root);
133+
child = new LinkTree(section, parent, i, root, section.sort);
128134
parent.children.push(child);
129135
}
130136
parent = child;
@@ -194,12 +200,26 @@ export class LinkTree
194200
return list;
195201
}
196202

203+
public sortMeta(reverse: boolean = false)
204+
{
205+
this.children.sort((a, b) => {
206+
const aSort = a.sort || Number.MAX_SAFE_INTEGER;
207+
const bSort = b.sort || Number.MAX_SAFE_INTEGER;
208+
return (reverse ? (bSort >= aSort ? 1 : -1) : (aSort >= bSort ? 1 : -1))
209+
});
210+
for (let child of this.children)
211+
{
212+
child.sortMeta(reverse);
213+
}
214+
}
215+
216+
197217
public sortAlphabetically(reverse: boolean = false)
198218
{
199219
this.children.sort((a, b) => reverse ? b.title.localeCompare(a.title) : a.title.localeCompare(b.title));
200220
for (let child of this.children)
201221
{
202-
child.sortAlphabetically();
222+
child.sortAlphabetically(reverse);
203223
}
204224
}
205225

@@ -338,14 +358,15 @@ export class GlobalDataGenerator
338358
// size is the depth of the file from the root
339359
// the list will be sorted first by size, then by title
340360
// the list will include folders and files
341-
public static getFileTree(exportedFiles: TFile[] | undefined = undefined): LinkTree
361+
public static getFileTree(exportedFiles: TFileWithMeta[] | undefined = undefined): LinkTree
342362
{
343363
if (this.fileTreeCache != undefined) return this.fileTreeCache;
344364
if (exportedFiles == undefined) return new LinkTree(undefined, undefined, 0);
345365

346366
let fileTree = LinkTree.fromFiles(exportedFiles);
347367
fileTree.sortAlphabetically();
348368
fileTree.sortByIsFolder(true);
369+
fileTree.sortMeta();
349370
if(InvioSettingTab.settings.makeNamesWebStyle) fileTree.makeLinksWebStyle();
350371

351372
this.fileTreeCache = fileTree;

src/html-generation/html-generator.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Path } from "../utils/path";
22
import { InvioSettingTab } from "../settings";
3-
import { GlobalDataGenerator, LinkTree } from "./global-gen";
3+
import { GlobalDataGenerator, LinkTree, TFileWithMeta } from "./global-gen";
44
import { MarkdownRenderer } from "./markdown-renderer";
55
import { AssetHandler } from "./asset-handler";
66
import { ExportFile } from "./export-file";
@@ -26,6 +26,7 @@ export interface IMetaConfig {
2626
description?: string;
2727
keywords?: string[];
2828
canonical?: string;
29+
sort?: number;
2930
publish?: boolean;
3031
permalink?: string;
3132
}
@@ -37,7 +38,12 @@ export class HTMLGenerator {
3738
public static async beginBatch(plugin: InvioPlugin, exportingFiles: TFile[]) {
3839
GlobalDataGenerator.clearGraphCache();
3940
GlobalDataGenerator.clearFileTreeCache();
40-
GlobalDataGenerator.getFileTree(exportingFiles);
41+
const files = exportingFiles.map(f => {
42+
const pageConfig = this.getPageOnlyMeta(f);
43+
(f as TFileWithMeta).sort = pageConfig?.sort || 0
44+
return f;
45+
})
46+
GlobalDataGenerator.getFileTree(files);
4147
await StatsView.activateStatsView(plugin);
4248
const view = StatsView.getStatsView(plugin);
4349
await AssetHandler.updateAssetCache(view);
@@ -491,6 +497,10 @@ export class HTMLGenerator {
491497
});
492498
}
493499

500+
private static getPageOnlyMeta(file: TFile) {
501+
const pageConfig = (app.metadataCache.getFileCache(file).frontmatter || {}) as IMetaConfig;
502+
return pageConfig;
503+
}
494504
private static getMeta(file: ExportFile): IMetaConfig {
495505
// Get site config
496506
const indexPath = file.exportedFolder.joinString('index.md');

0 commit comments

Comments
 (0)