MCPcopy Create free account
hub / github.com/blacksmithgu/datacore / resolveSource

Method resolveSource

src/api/script-cache.ts:132–181  ·  view source on GitHub ↗

Attempts to resolve the source to load given a path or link to a markdown section.

(
        path: string | Link
    )

Source from the content-addressed store, hash-verified

130
131 /** Attempts to resolve the source to load given a path or link to a markdown section. */
132 private async resolveSource(
133 path: string | Link
134 ): Promise<Result<{ code: string; language: ScriptLanguage }, string>> {
135 const object = this.store.resolveLink(path);
136 if (!object) return Result.failure("Could not find a script at the given path: " + path.toString());
137
138 const tfile = this.store.vault.getFileByPath(object.$file!);
139 if (!tfile) return Result.failure(`File "${object.$file}" not found.`);
140
141 // Check if this is a JS file we should load directly.
142 if (tfile.extension.toLocaleLowerCase() in ScriptCache.FILE_EXTENSIONS) {
143 const language = ScriptCache.FILE_EXTENSIONS[tfile.extension.toLocaleLowerCase()];
144
145 try {
146 const code = await this.store.vault.cachedRead(tfile);
147 return Result.success({ code, language });
148 } catch (error) {
149 return Result.failure("Failed to load javascript/typescript source file: " + error);
150 }
151 }
152
153 // If the object is a markdown section, search for any javascript codeblocks; otherwise, check if it is a full script file.
154 if (object instanceof MarkdownSection) {
155 const maybeBlock = object.$blocks
156 .filter((b): b is MarkdownCodeblock => b.$type === "codeblock")
157 .find((cb) =>
158 cb.$languages.some((language) => language.toLocaleLowerCase() in ScriptCache.SCRIPT_LANGUAGES)
159 );
160
161 if (!maybeBlock)
162 return Result.failure("Could not find a script in the given markdown section: " + path.toString());
163
164 const language =
165 ScriptCache.SCRIPT_LANGUAGES[
166 maybeBlock.$languages.find((lang) => lang.toLocaleLowerCase() in ScriptCache.SCRIPT_LANGUAGES)!
167 ];
168 return (await this.readCodeblock(tfile, maybeBlock)).map((code) => ({ code, language }));
169 } else if (object instanceof MarkdownCodeblock) {
170 const maybeLanguage = object.$languages.find(
171 (lang) => lang.toLocaleLowerCase() in ScriptCache.SCRIPT_LANGUAGES
172 );
173 if (!maybeLanguage)
174 return Result.failure(`The codeblock referenced by '${path}' is not a JS/TS codeblock.`);
175
176 const language = ScriptCache.SCRIPT_LANGUAGES[maybeLanguage];
177 return (await this.readCodeblock(tfile, object)).map((code) => ({ code, language }));
178 }
179
180 return Result.failure(`Cannot import '${path.toString()}: not a JS/TS file or codeblock reference.`);
181 }
182
183 /** Read the contents of a codeblock from a file. */
184 private async readCodeblock(file: TFile, block: MarkdownCodeblock): Promise<Result<string, string>> {

Callers 1

loadUncachedMethod · 0.95

Calls 7

readCodeblockMethod · 0.95
resolveLinkMethod · 0.80
findMethod · 0.65
filterMethod · 0.65
someMethod · 0.65
mapMethod · 0.65
toStringMethod · 0.45

Tested by

no test coverage detected