-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import path from "path"; | ||
import { Files, PathStatsFile } from "../../files"; | ||
import { SKIPPABLE_FILES } from "../importer"; | ||
|
||
// For resolving ![[Wiki Embedding]] links, I need a complete list of all non-.md | ||
// files in the import directory, these ridiuclous things are not absolute or | ||
// relative and could be anywhere in the import directory. | ||
export class WikiFileResolver { | ||
// maps filename (ex: 2024-11-17-20241118102000781.webp) to PathStatsFile | ||
private nameMapping: Record<string, PathStatsFile> = {}; | ||
|
||
private constructor(files: PathStatsFile[]) { | ||
files.forEach((file) => { | ||
let name = path.basename(file.path); | ||
this.nameMapping[name] = file; | ||
}); | ||
} | ||
|
||
static async init(importDir: string) { | ||
let files = []; | ||
|
||
for await (const file of Files.walk(importDir, (filestats) => { | ||
if (!filestats.stats.isFile()) return false; | ||
|
||
const name = path.basename(filestats.path); | ||
if (name.startsWith(".")) return false; | ||
if (SKIPPABLE_FILES.has(name)) return false; | ||
return !filestats.path.endsWith(".md"); | ||
})) { | ||
files.push(file); | ||
} | ||
|
||
return new WikiFileResolver(files); | ||
} | ||
|
||
resolve(name: string): string | undefined { | ||
const filestats = this.nameMapping[name]; | ||
if (!filestats) { | ||
return; | ||
} | ||
|
||
return filestats.path; | ||
} | ||
} |