|
| 1 | +import type { TFile } from "obsidian"; |
| 2 | +import type DiscourseGraphPlugin from "~/index"; |
| 3 | +import { ensureNodeInstanceId } from "./nodeInstanceId"; |
| 4 | + |
| 5 | +export type DiscourseNodeInVault = { |
| 6 | + file: TFile; |
| 7 | + frontmatter: Record<string, unknown>; |
| 8 | + nodeTypeId: string; |
| 9 | + nodeInstanceId: string; |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * Step 1: Collect all discourse nodes from the vault |
| 14 | + * Filters markdown files that have nodeTypeId in frontmatter |
| 15 | + */ |
| 16 | +export const collectDiscourseNodesFromVault = async ( |
| 17 | + plugin: DiscourseGraphPlugin, |
| 18 | + includeImported?: boolean, |
| 19 | +): Promise<DiscourseNodeInVault[]> => { |
| 20 | + const allFiles = plugin.app.vault.getMarkdownFiles(); |
| 21 | + const dgNodes: DiscourseNodeInVault[] = []; |
| 22 | + |
| 23 | + for (const file of allFiles) { |
| 24 | + const cache = plugin.app.metadataCache.getFileCache(file); |
| 25 | + const frontmatter = cache?.frontmatter; |
| 26 | + |
| 27 | + // Not a discourse node |
| 28 | + if (!frontmatter?.nodeTypeId) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + if ( |
| 33 | + // note: importedFromSpaceUri is legacy |
| 34 | + (frontmatter.importedFromRid || frontmatter.importedFromSpaceUri) && |
| 35 | + includeImported !== true |
| 36 | + ) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + const nodeTypeId = frontmatter.nodeTypeId as string; |
| 41 | + if (!nodeTypeId) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + const nodeInstanceId = await ensureNodeInstanceId( |
| 46 | + plugin, |
| 47 | + file, |
| 48 | + frontmatter as Record<string, unknown>, |
| 49 | + ); |
| 50 | + |
| 51 | + dgNodes.push({ |
| 52 | + file, |
| 53 | + frontmatter: frontmatter as Record<string, unknown>, |
| 54 | + nodeTypeId, |
| 55 | + nodeInstanceId, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return dgNodes; |
| 60 | +}; |
0 commit comments