Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

fix: naturally sort raw source files

nash_suf53c8e6e8ab1
diff --git a/src/components/layout/knowledge-tree.tsx b/src/components/layout/knowledge-tree.tsx
index dadaec49..990a7076 100644
--- a/src/components/layout/knowledge-tree.tsx
+++ b/src/components/layout/knowledge-tree.tsx
@@ -16,6 +16,7 @@ import { useTranslation } from "react-i18next"
import { useAppDialog } from "@/stores/app-dialog-store"
import { parseSources } from "@/lib/sources-merge"
import { filterPagesBySource, listPageSourceIdentities } from "@/lib/knowledge-source-filter"
+import { flattenFilesNaturally } from "@/lib/file-tree-order"
interface WikiPageInfo {
path: string
@@ -298,11 +299,20 @@ function RawSourcesSection() {
const [sources, setSources] = useState<FileNode[]>([])
useEffect(() => {
+ setSources([])
if (!project) return
+ let cancelled = false
const pp = normalizePath(project.path)
listDirectory(`${pp}/raw/sources`, true).then(filterRawSourceTree)
- .then((tree) => setSources(flattenAllFiles(tree)))
- .catch(() => setSources([]))
+ .then((tree) => {
+ if (!cancelled) setSources(flattenFilesNaturally(tree))
+ })
+ .catch(() => {
+ if (!cancelled) setSources([])
+ })
+ return () => {
+ cancelled = true
+ }
}, [project])
if (sources.length === 0) return null
@@ -464,15 +474,3 @@ function flattenMdFiles(nodes: FileNode[]): FileNode[] {
}
return files
}
-
-function flattenAllFiles(nodes: FileNode[]): FileNode[] {
- const files: FileNode[] = []
- for (const node of nodes) {
- if (node.is_dir && node.children) {
- files.push(...flattenAllFiles(node.children))
- } else if (!node.is_dir) {
- files.push(node)
- }
- }
- return files
-}
diff --git a/src/components/sources/sources-view.tsx b/src/components/sources/sources-view.tsx
index cd0c3054..45e5b5b1 100644
--- a/src/components/sources/sources-view.tsx
+++ b/src/components/sources/sources-view.tsx
@@ -13,7 +13,7 @@ import { useAppDialog } from "@/stores/app-dialog-store"
import { normalizePath } from "@/lib/path-utils"
import { decideDeleteClick } from "@/lib/sources-tree-delete"
import { rescanProjectFileSync } from "@/lib/project-file-sync"
-import { naturalCompare } from "@/lib/natural-sort"
+import { sortFileNodes } from "@/lib/file-tree-order"
import {
deleteSourceFile,
deleteSourceFolder,
@@ -677,11 +677,7 @@ export function filterSourceTreeByQuery(
}
function sortSourceNodes(nodes: readonly FileNode[]): FileNode[] {
- return [...nodes].sort((a, b) => {
- if (a.is_dir && !b.is_dir) return -1
- if (!a.is_dir && b.is_dir) return 1
- return naturalCompare(a.name, b.name)
- })
+ return sortFileNodes(nodes)
}
function flattenVisibleRows(
diff --git a/src/lib/file-tree-order.test.ts b/src/lib/file-tree-order.test.ts
new file mode 100644
index 00000000..fb02bfc6
--- /dev/null
+++ b/src/lib/file-tree-order.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from "vitest"
+import type { FileNode } from "@/types/wiki"
+import { flattenFilesNaturally } from "./file-tree-order"
+
+function file(path: string): FileNode {
+ return { name: path.split("/").pop()!, path, is_dir: false }
+}
+
+function dir(path: string, children?: FileNode[]): FileNode {
+ return { name: path.split("/").pop()!, path, is_dir: true, children }
+}
+
+describe("flattenFilesNaturally", () => {
+ it("uses directory-first natural ordering at every tree level without mutation", () => {
+ const nestedChildren = [file("/p/chapter 10/0.10 nested.pdf"), file("/p/chapter 10/0.2 nested.pdf")]
+ const tree = [
+ file("/p/0.10 root.pdf"),
+ dir("/p/chapter 10", nestedChildren),
+ file("/p/0.2 root.pdf"),
+ dir("/p/chapter 2", [dir("/p/chapter 2/part 1", [file("/p/chapter 2/part 1/0.1 nested.pdf")])]),
+ file("/p/0.1 root.pdf"),
+ ]
+ const originalRootOrder = tree.map((node) => node.path)
+ const originalNestedOrder = nestedChildren.map((node) => node.path)
+
+ expect(flattenFilesNaturally(tree).map((node) => node.path)).toEqual([
+ "/p/chapter 2/part 1/0.1 nested.pdf",
+ "/p/chapter 10/0.2 nested.pdf",
+ "/p/chapter 10/0.10 nested.pdf",
+ "/p/0.1 root.pdf",
+ "/p/0.2 root.pdf",
+ "/p/0.10 root.pdf",
+ ])
+ expect(tree.map((node) => node.path)).toEqual(originalRootOrder)
+ expect(nestedChildren.map((node) => node.path)).toEqual(originalNestedOrder)
+ })
+
+ it("ignores empty directories and handles an empty tree", () => {
+ expect(flattenFilesNaturally([dir("/p/empty", []), dir("/p/unloaded"), file("/p/a.pdf")]))
+ .toEqual([file("/p/a.pdf")])
+ expect(flattenFilesNaturally([])).toEqual([])
+ })
+})
diff --git a/src/lib/file-tree-order.ts b/src/lib/file-tree-order.ts
new file mode 100644
index 00000000..0192d76a
--- /dev/null
+++ b/src/lib/file-tree-order.ts
@@ -0,0 +1,24 @@
+import type { FileNode } from "@/types/wiki"
+import { naturalCompare } from "@/lib/natural-sort"
+
+export function compareFileNodes(a: FileNode, b: FileNode): number {
+ if (a.is_dir && !b.is_dir) return -1
+ if (!a.is_dir && b.is_dir) return 1
+ return naturalCompare(a.name, b.name)
+}
+
+export function sortFileNodes(nodes: readonly FileNode[]): FileNode[] {
+ return [...nodes].sort(compareFileNodes)
+}
+
+export function flattenFilesNaturally(nodes: readonly FileNode[]): FileNode[] {
+ const files: FileNode[] = []
+ for (const node of sortFileNodes(nodes)) {
+ if (node.is_dir && node.children) {
+ files.push(...flattenFilesNaturally(node.children))
+ } else if (!node.is_dir) {
+ files.push(node)
+ }
+ }
+ return files
+}