Skip to content

Build your personal knowledge base with Trilium Notes

feat(core): write a level's direction as a word after its key

BeatLinkb95219a0d75d
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 9e139f95c9..2196b10018 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -577,8 +577,8 @@
"run_on_instance": "Define which trilium instance should run this on. Default to all instances.",
"run_at_hour": "On which hour should this run. Should be used together with <code>#run=hourly</code>. Can be defined multiple times for more runs during the day.",
"disable_inclusion": "Scripts with this label won't be included into parent script execution.",
- "sorted": "Keeps child notes sorted by title alphabetically. A value sorts by other criteria instead: comma-separated levels of <code>title</code>, <code>dateCreated</code>, <code>dateModified</code> or a label name, each with an optional <code>:asc</code> or <code>:desc</code>, e.g. <code>priority:desc,dueDate</code>.",
- "sort_direction": "Direction <code>#sorted</code> orders the child notes in: <code>desc</code> for descending, anything else for ascending. A level with its own <code>:asc</code>/<code>:desc</code> ignores it. Ignored unless <code>#sorted</code> is set.",
+ "sorted": "Keeps child notes sorted by title alphabetically. A value sorts by other criteria instead: comma-separated levels of <code>title</code>, <code>dateCreated</code>, <code>dateModified</code> or a label name, each optionally followed by <code>asc</code> or <code>desc</code>, e.g. <code>priority desc, dueDate</code>.",
+ "sort_direction": "Direction <code>#sorted</code> orders the child notes in: <code>desc</code> for descending, anything else for ascending. A level followed by its own <code>asc</code> or <code>desc</code> ignores it. Ignored unless <code>#sorted</code> is set.",
"sort_folders_first": "Folders (notes with children) should be sorted on top.",
"top": "Keep given note on top in its parent (applies only on sorted parents).",
"hide_promoted_attributes": "Hide promoted attributes on this note.",
diff --git a/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsx b/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsx
index e5d76cc668..7be425dd21 100644
--- a/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsx
+++ b/apps/client/src/widgets/dialogs/sort_child_notes.spec.tsx
@@ -18,7 +18,7 @@ describe("serializeSortLevels", () => {
{ kind: "label", labelName: "", descending: false },
{ kind: "dateCreated", labelName: "", descending: false },
{ kind: "title", labelName: "ignored", descending: true }
- ])).toBe("priority:desc,dateCreated:asc,title:desc");
+ ])).toBe("priority desc, dateCreated, title desc");
});
});
@@ -85,7 +85,7 @@ describe("SortChildNotesDialog", () => {
expect(levels()).toHaveLength(1);
await submit();
expect(put).toHaveBeenCalledWith("notes/parent/sort-children", expect.objectContaining({
- sortBy: "title:asc",
+ sortBy: "title",
foldersFirst: false,
sortNatural: false
}));
@@ -108,7 +108,7 @@ describe("SortChildNotesDialog", () => {
await submit();
expect(put).toHaveBeenCalledWith("notes/parent/sort-children", expect.objectContaining({
- sortBy: "priority:desc,dateModified:asc"
+ sortBy: "priority desc, dateModified"
}));
});
@@ -122,7 +122,7 @@ describe("SortChildNotesDialog", () => {
await click(second.querySelector(".sort-level-up"));
await submit();
expect(put).toHaveBeenCalledWith("notes/parent/sort-children", expect.objectContaining({
- sortBy: "dateCreated:asc,title:asc"
+ sortBy: "dateCreated, title"
}));
});
diff --git a/apps/client/src/widgets/dialogs/sort_child_notes.tsx b/apps/client/src/widgets/dialogs/sort_child_notes.tsx
index 13fcbeccac..4219e0065a 100644
--- a/apps/client/src/widgets/dialogs/sort_child_notes.tsx
+++ b/apps/client/src/widgets/dialogs/sort_child_notes.tsx
@@ -173,7 +173,7 @@ export default function SortChildNotesDialog() {
)
}
-/** Writes the levels in the `#sorted` format the server parses, e.g. `priority:desc,title:asc`. */
+/** Writes the levels in the `#sorted` format the server parses, e.g. `priority desc, title`. */
export function serializeSortLevels(levels: SortLevel[]) {
return levels
.map((level) => ({
@@ -181,6 +181,6 @@ export function serializeSortLevels(levels: SortLevel[]) {
key: level.kind === "label" ? level.labelName.trim() : level.kind
}))
.filter((level) => level.key)
- .map((level) => `${level.key}:${level.descending ? "desc" : "asc"}`)
- .join(",");
+ .map((level) => (level.descending ? `${level.key} desc` : level.key))
+ .join(", ");
}
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md
index 3e6cbfe1c1..0dc6d95f77 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md
@@ -24,11 +24,11 @@ Child notes can be automatically sorted by attaching specific [labels](../../Adv
<tbody>
<tr>
<td><code spellcheck="false">#sorted</code></td>
- <td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each <code spellcheck="false">title</code>, <code spellcheck="false">dateCreated</code>, <code spellcheck="false">dateModified</code> or the name of a label on the child notes, optionally followed by <code spellcheck="false">:asc</code> or <code spellcheck="false">:desc</code>. For example <code spellcheck="false">#sorted="priority:desc,dueDate"</code> sorts by priority, highest first, and notes of equal priority by due date. If one of the child notes doesn't have a level's label, its title is used in its place.</p></td>
+ <td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each <code spellcheck="false">title</code>, <code spellcheck="false">dateCreated</code>, <code spellcheck="false">dateModified</code> or the name of a label on the child notes, optionally followed by <code spellcheck="false">asc</code> or <code spellcheck="false">desc</code>, as in a search's <code spellcheck="false">orderBy</code>. For example <code spellcheck="false">#sorted="priority desc, dueDate"</code> sorts by priority, highest first, and notes of equal priority by due date. If one of the child notes doesn't have a level's label, its title is used in its place.</p></td>
</tr>
<tr>
<td><code spellcheck="false">#sortDirection</code></td>
- <td><p>If <code spellcheck="false">sorted</code> is applied, specifies the direction of the sort:</p><ul><li><code spellcheck="false">ASC</code>, ascending (default)</li><li><code spellcheck="false">DESC</code>, descending</li></ul><p>A level of <code spellcheck="false">sorted</code> with its own <code spellcheck="false">:asc</code> or <code spellcheck="false">:desc</code> keeps that direction regardless.</p></td>
+ <td><p>If <code spellcheck="false">sorted</code> is applied, specifies the direction of the sort:</p><ul><li><code spellcheck="false">ASC</code>, ascending (default)</li><li><code spellcheck="false">DESC</code>, descending</li></ul><p>A level of <code spellcheck="false">sorted</code> followed by its own <code spellcheck="false">asc</code> or <code spellcheck="false">desc</code> keeps that direction regardless.</p></td>
</tr>
<tr>
<td><code spellcheck="false">#sortFoldersFirst</code></td>
@@ -61,7 +61,7 @@ Sorting is done by comparing note properties or specific labels on child notes.
* **Default Sorting**: If `#sorted` has no value, notes are sorted alphabetically.
* **Property Sorting**: If `#sorted` is set to `title`, `dateModified`, or `dateCreated`, notes are sorted based on the specified property.
* **Label Sorting**: If `#sorted` has any other value, this value is treated as the name of a child note's label, and sorting is based on the values of this label. For example, setting `#sorted=myOrder` on the parent note and using `#myOrder=001`, `#myOrder=002`, etc., on child notes.
- * **Multi-Level Sorting**: Several of the above can be combined, separated by commas; each level is applied only where the previous ones are equal. Every level can carry its own direction with `:asc` or `:desc`, otherwise it follows `#sortDirection`. For example `#sorted="priority:desc,area,dateCreated"` sorts by priority, highest first, then by area, then by creation date.
+ * **Multi-Level Sorting**: Several of the above can be combined, separated by commas; each level is applied only where the previous ones are equal. Every level can carry its own direction as a word after the name, `asc` or `desc`, otherwise it follows `#sortDirection`. For example `#sorted="priority desc, area, dateCreated"` sorts by priority, highest first, then by area, then by creation date.
4. **Alphabetical Sorting**: Used as a last resort when other criteria result in equality.
All comparisons are made string-wise (e.g., "1" \< "2" or "2020-10-10" < "2021-01-15", but also "2" \> "10").
\ No newline at end of file
diff --git a/packages/trilium-core/src/services/backend_script_api.ts b/packages/trilium-core/src/services/backend_script_api.ts
index a6a64ec942..090a6be752 100644
--- a/packages/trilium-core/src/services/backend_script_api.ts
+++ b/packages/trilium-core/src/services/backend_script_api.ts
@@ -345,7 +345,7 @@ export interface Api {
parentNoteId: string,
sortConfig: {
/** Comma-separated levels of 'title', 'dateCreated', 'dateModified' or a label name,
- * each with an optional ':asc' or ':desc' flag, e.g. 'priority:desc,dueDate'. The
+ * each optionally followed by 'asc' or 'desc', e.g. 'priority desc, dueDate'. The
* format is that of the #sorted label, see "Sorting Notes" in the User Guide. */
sortBy?: string;
/** Sorts the levels without a flag of their own descending. */
diff --git a/packages/trilium-core/src/services/handlers.spec.ts b/packages/trilium-core/src/services/handlers.spec.ts
index 16777649f6..2d344765b2 100644
--- a/packages/trilium-core/src/services/handlers.spec.ts
+++ b/packages/trilium-core/src/services/handlers.spec.ts
@@ -113,7 +113,7 @@ describe("handlers", () => {
it("re-sorts the parent only when the changed label is one of the #sorted levels", () => {
buildNote({ id: "ml-par", children: [{ id: "ml-chld" }] });
- addAttribute("ml-par", "label", "sorted", "priority:desc,dueDate");
+ addAttribute("ml-par", "label", "sorted", "priority desc, dueDate");
const unrelated = addAttribute("ml-chld", "label", "priorityNote", "x");
eventService.emit(eventService.ENTITY_CHANGED, {
diff --git a/packages/trilium-core/src/services/tree.spec.ts b/packages/trilium-core/src/services/tree.spec.ts
index ea159ae0ce..05dffe641b 100644
--- a/packages/trilium-core/src/services/tree.spec.ts
+++ b/packages/trilium-core/src/services/tree.spec.ts
@@ -186,7 +186,7 @@ describe("Tree", () => {
{title: "d", "#priority": "1", "#area": "work"},
{title: "e", "#priority": "1", "#area": "home"}
],
- "#sorted": "priority:desc, area, title:desc"
+ "#sorted": "priority desc, area, title desc"
});
getContext().init(() => {
tree.sortNotesIfNeeded(note.noteId);
@@ -204,7 +204,7 @@ describe("Tree", () => {
{title: "top", "#top": ""},
{title: "bottom", "#bottom": ""}
],
- "#sorted": "priority:asc,dateCreated",
+ "#sorted": "priority asc,dateCreated",
"#sortDirection": "desc"
});
getContext().init(() => {
@@ -273,15 +273,17 @@ describe("Tree", () => {
expect(parseSortCriteria("")).toEqual([{ key: "title", descending: undefined }]);
expect(parseSortCriteria(null)).toEqual([{ key: "title", descending: undefined }]);
expect(parseSortCriteria("myOrder")).toEqual([{ key: "myOrder", descending: undefined }]);
- expect(parseSortCriteria(" priority : DESC , , area:asc, title ")).toEqual([
+ expect(parseSortCriteria(" priority DESC , , area asc, title ")).toEqual([
{ key: "priority", descending: true },
{ key: "area", descending: false },
{ key: "title", descending: undefined }
]);
- // A colon inside a label name is not a direction flag.
- expect(parseSortCriteria("calendar:view:desc,calendar:view")).toEqual([
+ // A colon is an ordinary character of a label name.
+ expect(parseSortCriteria("calendar:view desc,calendar:view")).toEqual([
{ key: "calendar:view", descending: true },
{ key: "calendar:view", descending: undefined }
]);
+ // A direction word on its own is a key, not a direction.
+ expect(parseSortCriteria("desc")).toEqual([{ key: "desc", descending: undefined }]);
});
});
diff --git a/packages/trilium-core/src/services/tree.ts b/packages/trilium-core/src/services/tree.ts
index 043c0b82c4..853a30f086 100644
--- a/packages/trilium-core/src/services/tree.ts
+++ b/packages/trilium-core/src/services/tree.ts
@@ -80,24 +80,24 @@ function wouldAddingBranchCreateCycle(parentNoteId: string, childNoteId: string)
export interface SortCriterion {
/** `title`, `dateCreated`, `dateModified` or the name of a label on the child notes. */
key: string;
- /** Set by an explicit `:asc`/`:desc` flag; `undefined` follows `#sortDirection`. */
+ /** Set by an explicit `asc`/`desc` after the key; `undefined` follows `#sortDirection`. */
descending?: boolean;
}
/**
- * Parses a `#sorted` value such as `priority:desc,dueDate,title` into its levels: comma-separated
- * sort keys, each optionally followed by `:asc` or `:desc`. An empty value sorts by title.
+ * Parses a `#sorted` value such as `priority desc, dueDate, title` into its levels: comma-separated
+ * sort keys, each optionally followed by `asc` or `desc`, the way search's `orderBy` is written.
+ * An empty value sorts by title.
*/
export function parseSortCriteria(value: string | null | undefined): SortCriterion[] {
const criteria: SortCriterion[] = [];
for (const level of (value ?? "").split(",")) {
- // Only a trailing flag is a direction, so a label name with a colon still works as a key.
- const separator = level.lastIndexOf(":");
- const flag = separator === -1 ? "" : level.slice(separator + 1).trim().toLowerCase();
- const isDirection = flag === "asc" || flag === "desc";
- const key = (isDirection ? level.slice(0, separator) : level).trim();
+ const words = level.trim().split(/\s+/);
+ const last = words[words.length - 1].toLowerCase();
+ const isDirection = words.length > 1 && (last === "asc" || last === "desc");
+ const key = (isDirection ? words.slice(0, -1) : words).join(" ");
if (key) {
- criteria.push({ key, descending: isDirection ? flag === "desc" : undefined });
+ criteria.push({ key, descending: isDirection ? last === "desc" : undefined });
}
}
return criteria.length > 0 ? criteria : [{ key: "title" }];