Read the contents of a codeblock from a file.
(file: TFile, block: MarkdownCodeblock)
| 182 | |
| 183 | /** Read the contents of a codeblock from a file. */ |
| 184 | private async readCodeblock(file: TFile, block: MarkdownCodeblock): Promise<Result<string, string>> { |
| 185 | try { |
| 186 | const raw = lineRange( |
| 187 | await this.store.vault.cachedRead(file), |
| 188 | block.$contentPosition.start, |
| 189 | block.$contentPosition.end |
| 190 | ); |
| 191 | |
| 192 | if (block.$style === "fenced") return Result.success(raw); |
| 193 | else |
| 194 | return Result.success( |
| 195 | raw |
| 196 | .split("\n") |
| 197 | .map((line) => line.trimStart()) |
| 198 | .join("\n") |
| 199 | ); |
| 200 | } catch (error) { |
| 201 | return Result.failure(`Failed to read a codeblock from ${file.path}: ${error}`); |
| 202 | } |
| 203 | } |
| 204 | } |
no test coverage detected