From d8c7d76847b77290f7dfef6e1b2f258fbe544b1f Mon Sep 17 00:00:00 2001 From: Rob Dominguez Date: Tue, 2 Apr 2024 10:06:01 -0500 Subject: [PATCH] Fix: Add ability to reference external themes --- src/config.ts | 4 +++- src/theme.ts | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/config.ts b/src/config.ts index f29821d8..7f12dbcb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -377,7 +377,9 @@ export class MarpCLIConfig { return { advice: { use: '--theme-set', insteadOf: '--theme' }, name: this.args.theme, - path: path.resolve(this.args.theme), + path: this.args.theme.includes(`http`) + ? this.args.theme + : path.resolve(this.args.theme), } if (this.conf.theme) diff --git a/src/theme.ts b/src/theme.ts index c77c6410..ed1d190d 100644 --- a/src/theme.ts +++ b/src/theme.ts @@ -3,7 +3,7 @@ import fs from 'fs' import path from 'path' import { Marpit } from '@marp-team/marpit' import { isDynamicPattern } from 'globby' -import { warn } from './cli' +import { warn, info } from './cli' import { isError } from './error' import { File } from './file' @@ -33,7 +33,23 @@ export class Theme { } async load() { - this.readBuffer = await fs.promises.readFile(this.filename) + if (this.isUrl(this.filename)) { + // Fetch the content from a remote URL + const response = await fetch(this.filename) + this.readBuffer = Buffer.from(await response.text()) + } else { + // Read the content from a local file + this.readBuffer = await fs.promises.readFile(this.filename) + } + } + + private isUrl(filename: string): boolean { + try { + new URL(filename) + return true + } catch { + return false + } } private genUniqName() {