Attempts to resolve the source to load given a path or link to a markdown section.
(
path: string | Link
)
| 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>> { |
no test coverage detected