( source: SourceConfig, baseDir: string, fetcher: Fetcher, cache?: ContentCache )
| 219 | } |
| 220 | |
| 221 | async function loadSourceText( |
| 222 | source: SourceConfig, |
| 223 | baseDir: string, |
| 224 | fetcher: Fetcher, |
| 225 | cache?: ContentCache |
| 226 | ): Promise<string> { |
| 227 | if (source.type === "local-text") { |
| 228 | return readFile(path.resolve(baseDir, source.path), "utf8"); |
| 229 | } |
| 230 | |
| 231 | if (source.type === "remote-text") { |
| 232 | return fetcher(source.url); |
| 233 | } |
| 234 | |
| 235 | const cacheKey = `html-extract:${source.url}#${source.selector}`; |
| 236 | const fresh = async () => extractHtml(await defaultFetcher(source.url), source.url, source.selector); |
| 237 | |
| 238 | return cache ? cache.cached(cacheKey, fresh) : fresh(); |
| 239 | } |
| 240 | |
| 241 | function extractHtml(html: string, url: string, selector: string): string { |
| 242 | const $ = load(html); |
no test coverage detected