From d5ff7a0b33a0e0e59c5d045d08024c2ba1ccbe19 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Fri, 28 Dec 2018 19:00:42 +0100 Subject: [PATCH] feat: Support custom theme --- src/core/compiler.js | 5 ++--- src/utils/config.js | 26 +++++++++++++++++++++++++- themes/server.js | 3 +-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/core/compiler.js b/src/core/compiler.js index 2ead723..f3e28e0 100644 --- a/src/core/compiler.js +++ b/src/core/compiler.js @@ -3,7 +3,6 @@ const webpack = require('webpack') const ProgressPlugin = require('webpack/lib/ProgressPlugin') const { babelOptions } = require('../utils/babel') -const THEMES_DIR = syspath.resolve(__dirname, '../../themes') const NODE_MODS_DIR = syspath.resolve(__dirname, '../../node_modules') async function getCompiler (env, props) { @@ -18,7 +17,7 @@ async function getCompiler (env, props) { entry: { main: [ isDev && 'webpack-hot-middleware/client', - `${THEMES_DIR}/${props.config.theme}/index.js`, + `${props.config.theme}/index.js`, ].filter(Boolean), }, output: { @@ -38,7 +37,7 @@ async function getCompiler (env, props) { rules: [ { test: /\.js$/, - include: THEMES_DIR, + include: syspath.dirname(props.config.theme), exclude: NODE_MODS_DIR, use: { loader: 'babel-loader', diff --git a/src/utils/config.js b/src/utils/config.js index 407b4a3..6286e9e 100644 --- a/src/utils/config.js +++ b/src/utils/config.js @@ -28,7 +28,7 @@ const DEFAULT_CONFIG = { port: 8000, languages: ['bash', 'json'], header_links: [], - theme: 'default', + theme: '~default', breadcrumbs: true, prefix_titles: false, table_of_contents: { @@ -65,6 +65,28 @@ function getExternalConfig (dir, name) { return file ? readConfigFile(file) : {} } +function resolveThemeDirectory (config) { + const theme = config.theme + if (theme.indexOf('/') === 0) { + return theme + } else if (theme.indexOf('.') === 0) { + // local path + return syspath.resolve( + config.root, + theme + ) + } else if (theme.indexOf('~') === 0) { + // gitdocs built-in theme + return syspath.resolve( + __dirname, + '../../themes', + theme.substr(1) + ) + } + // package theme + return require.resolve(theme) +} + async function getConfig (customFile) { // prioritize custom config file if passed, // but still fallback to default files @@ -94,6 +116,8 @@ async function getConfig (customFile) { masterConfig.static, ) + masterConfig.theme = resolveThemeDirectory(masterConfig) + return masterConfig } diff --git a/themes/server.js b/themes/server.js index 4cc1426..2e1f460 100644 --- a/themes/server.js +++ b/themes/server.js @@ -2,8 +2,7 @@ import React from 'react' import { StaticRouter } from 'react-router-dom' export default function (props, route) { - const { theme } = props.config - const { default: App } = require(`./${theme}/application`) + const App = require(`${props.config.theme}/application`).default return (