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

WIP feat: Support custom theme #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions src/core/compiler.js
Original file line number Diff line number Diff line change
@@ -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',
26 changes: 25 additions & 1 deletion src/utils/config.js
Original file line number Diff line number Diff line change
@@ -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
}

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