Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path normalization is required also for the theme string from the configuration file. (marp.config.js etc)

Expand Down
20 changes: 18 additions & 2 deletions src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was from debugging, but I can remove it in the next commit.

import { isError } from './error'
import { File } from './file'

Expand Down Expand Up @@ -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
}
}
Comment on lines +46 to 53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isUrl() may be better to make public class method, and also use for testing whether apply path.resolve to the passed theme. In addition, it might be good to limit the URL protocol to http or https.

static isRemoteUrl(filename: string) {
  try {
    const url = new URL(filename)
    return url.protocol === 'http' || url.protocol === 'https'
  } catch {
    return false
  }
}
{
  path: Theme.isRemoteUrl(this.args.theme) ? this.args.theme : path.resolve(this.args.theme)
}


private genUniqName() {
Expand Down
Loading