marp-team / marp-cli

A CLI interface for Marp and Marpit based converters
MIT License
1.85k stars 105 forks source link

Fix: Add ability to reference external themes (closes #183) #571

Open robertjdominguez opened 5 months ago

robertjdominguez commented 5 months ago

Description

I like Marp, but I don't particularly like using VS Code. As a user of the CLI, I'd like to be able to write my markdown in any editor and then reference a hosted stylesheet to use as a particular theme. This is common — as an example — for my organization, as we want to use a single source of truth for our template(s).

Implementation

In the config.ts, we don't try to resolve the local path if http is included within the string passed to the --theme argument:

path: this.args.theme.includes(`http`)
            ? this.args.theme
            : path.resolve(this.args.theme),
        }

Further, on the load() function, we either use the local file via a buffer, or fetch the remote file:

async load() {
    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
    }
  }

Testing

Coverage was above the 95% threshold 🎉

You can use this ref when building the binary:

<PATH_TO_BINARY> --theme https://raw.githubusercontent.com/hasura/marp-template/main/custom-default.css -- <PATH_TO_DECK>