Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
feat: Support custom theme
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Dec 28, 2018
1 parent d9a11ab commit d5ff7a0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/core/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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: {
Expand All @@ -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',
Expand Down
26 changes: 25 additions & 1 deletion src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -94,6 +116,8 @@ async function getConfig (customFile) {
masterConfig.static,
)

masterConfig.theme = resolveThemeDirectory(masterConfig)

return masterConfig
}

Expand Down
3 changes: 1 addition & 2 deletions themes/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<StaticRouter
Expand Down

0 comments on commit d5ff7a0

Please sign in to comment.