Skip to content

Commit

Permalink
feat: support local theme
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 8, 2021
1 parent d5aad3c commit f90e56d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
22 changes: 17 additions & 5 deletions packages/slidev/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,25 @@ export function getCLIRoot() {
return resolve(__dirname, '..')
}

export function getThemeRoots(name: string) {
export function isRelative(name: string) {
return /^\.\.?[\/\\]/.test(name)
}

export function getThemeRoots(name: string, entry: string) {
if (!name)
return []

return [
dirname(require.resolve(`${name}/package.json`)),
]
// TODO: handle theme inherit
if (isRelative(name)) {
return [
resolve(dirname(entry), name),
]
}
else {
return [
dirname(require.resolve(`${name}/package.json`)),
]
}
}

export async function resolveOptions(
Expand Down Expand Up @@ -95,7 +107,7 @@ export async function resolveOptions(

const clientRoot = getClientRoot()
const cliRoot = getCLIRoot()
const themeRoots = getThemeRoots(theme)
const themeRoots = getThemeRoots(theme, entry)
const roots = uniq([clientRoot, ...themeRoots, userRoot])

return {
Expand Down
5 changes: 4 additions & 1 deletion packages/slidev/node/themes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import prompts from 'prompts'
import { parseNi, run } from '@antfu/ni'
import { isRelative } from './options'

const officialThemes: Record<string, string> = {
none: '',
Expand All @@ -21,6 +22,8 @@ export function resolveThemeName(name: string) {
return ''
if (name.startsWith('@slidev/theme-') || name.startsWith('slidev-theme-'))
return name
if (name.startsWith('.'))
return name
if (officialThemes[name] !== null)
return officialThemes[name]

Expand All @@ -32,7 +35,7 @@ export async function promptForThemeInstallation(name: string) {
if (!name)
return name

if (packageExists(name))
if (isRelative(name) || packageExists(name))
return name

const { confirm } = await prompts({
Expand Down

0 comments on commit f90e56d

Please sign in to comment.