Skip to content

Commit

Permalink
Added reverse alphabetical order for files/headers
Browse files Browse the repository at this point in the history
  • Loading branch information
RubbaBoy committed Apr 25, 2024
1 parent c7d0bfa commit ba970a4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
56 changes: 42 additions & 14 deletions src/models/PluginSettingsTab.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {App, PluginSettingTab, Setting, TextAreaComponent} from "obsidian";
import FolderIndexPlugin from "../main";

export enum SortBy {
None = "Disabled",
Alphabetically = "Alphabetically",
ReverseAlphabetically = "Reverse Alphabetically"
}

export interface PluginSetting {
graphOverwrite: boolean;
//skipFirstHeadline: boolean;
Expand All @@ -12,8 +18,8 @@ export interface PluginSetting {
autoRenameIndexFile: boolean;
hideIndexFiles: boolean;
autoPreviewMode: boolean;
sortIndexFilesAlphabetically: boolean;
sortHeadersAlphabetically: boolean;
sortIndexFiles: SortBy;
sortHeaders: SortBy;
recursiveIndexFiles: boolean;
renderFolderBold: boolean;
renderFolderItalic: boolean;
Expand All @@ -34,8 +40,8 @@ export const DEFAULT_SETTINGS: PluginSetting = {
hideIndexFiles: false,
indexFileInitText: "---\ntags: MOCs\n---\n```folder-index-content\n```",
autoPreviewMode: false,
sortIndexFilesAlphabetically: true,
sortHeadersAlphabetically: false,
sortIndexFiles: SortBy.Alphabetically,
sortHeaders: SortBy.None,
recursiveIndexFiles: false,
renderFolderBold: true,
renderFolderItalic: false,
Expand Down Expand Up @@ -214,20 +220,42 @@ export class PluginSettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Sort Files Alphabetically")
.setDesc("This will sort the Files alphabetically")
.addToggle(component => component.setValue(this.plugin.settings.sortIndexFilesAlphabetically)
.onChange(async (value) => {
this.plugin.settings.sortIndexFilesAlphabetically = value
await this.plugin.saveSettings()
}))
.addDropdown(component => {
for (const key in SortBy) {
if (!isNaN(Number(key))) continue;

const enumKey: SortBy = SortBy[key as keyof typeof SortBy];
const enumValue: string = SortBy[key as keyof typeof SortBy];

component.addOption(enumKey, enumValue);
}

component.setValue(this.plugin.settings.sortIndexFiles)
.onChange(async (value) => {
this.plugin.settings.sortIndexFiles = value as SortBy
await this.plugin.saveSettings()
})
})

new Setting(containerEl)
.setName("Sort Headers Alphabetically")
.setDesc("This will sort the Headers within a file alphabetically")
.addToggle(component => component.setValue(this.plugin.settings.sortHeadersAlphabetically)
.onChange(async (value) => {
this.plugin.settings.sortHeadersAlphabetically = value
await this.plugin.saveSettings()
}))
.addDropdown(component => {
for (const key in SortBy) {
if (!isNaN(Number(key))) continue;

const enumKey: SortBy = SortBy[key as keyof typeof SortBy];
const enumValue: string = SortBy[key as keyof typeof SortBy];

component.addOption(enumKey, enumValue);
}

component.setValue(this.plugin.settings.sortHeaders)
.onChange(async (value) => {
this.plugin.settings.sortHeaders = value as SortBy
await this.plugin.saveSettings()
})
})

new Setting(containerEl)
.setName("Build IndexFiles Recursively")
Expand Down
9 changes: 7 additions & 2 deletions src/types/MarkdownTextRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {App, HeadingCache, TAbstractFile, TFile, TFolder} from "obsidian";
import FolderIndexPlugin from "../main";
import {isExcludedPath, isIndexFile} from "./Utilities";
import {SortBy} from "../models/PluginSettingsTab";

type FileTree = (TFile | TFolder)[]
type HeaderWrapper = {
Expand Down Expand Up @@ -71,8 +72,10 @@ export class MarkdownTextRenderer {
private buildHeaderMarkdownText(file: TFile, headerTree: HeaderWrapper[], indentLevel: number, headlineLevel: number): string {
let markdownText = ""

if (this.plugin.settings.sortHeadersAlphabetically) {
if (this.plugin.settings.sortHeaders === SortBy.Alphabetically) {
headerTree.sort((a, b) => a.header.heading.localeCompare(b.header.heading))
} else if (this.plugin.settings.sortHeaders === SortBy.ReverseAlphabetically) {
headerTree.sort((a, b) => b.header.heading.localeCompare(a.header.heading))
}

for (const headerWrapper of headerTree) {
Expand Down Expand Up @@ -192,8 +195,10 @@ export class MarkdownTextRenderer {
fileTree.push(file)
}
}
if (this.plugin.settings.sortIndexFilesAlphabetically) {
if (this.plugin.settings.sortIndexFiles === SortBy.Alphabetically) {
fileTree.sort((a, b) => a.name.localeCompare(b.name))
} else if (this.plugin.settings.sortIndexFiles === SortBy.ReverseAlphabetically) {
fileTree.sort((a, b) => b.name.localeCompare(a.name))
}
return fileTree
}
Expand Down

0 comments on commit ba970a4

Please sign in to comment.