Theme Guide

This guide provides a comprehensive walkthrough for building a custom Methanol theme. You will learn how to implement core features such as navigation, tables of contents, and multi-language support, while adhering to Methanol's theme architecture.

A standard theme typically follows this structure:

Theme Configuration

To enable a custom theme, specify its root directory and template component in methanol.config.js:

import PageTemplate from './theme/page.jsx'

export default () => ({
	theme: {
		root: './theme',
		template: PageTemplate
	}
})

Theme Object Definition

A theme can also be defined as an object in its entry point (index.js). This is particularly useful for distributive themes:

import PageTemplate from './page.jsx'

export default () => ({
	theme: {
		root: '.',
		template: PageTemplate,
		componentsDir: './components',
		pagesDir: './pages',
		publicDir: './public',
		sources: {
			'/theme': './sources'
		}
	}
})

Publishing and Resolution

Themes can be published as npm packages named methanol-theme-xxx. Methanol automatically resolves theme names using the following priority:

  1. Built-in Themes: Internal presets (e.g., default, blog).
  2. Package Resolution: Searches for methanol-theme-xxx.

For local themes (files inside your project), import them in methanol.config.* and set theme to the imported theme object/factory.

Using a Published Theme

Via CLI:

methanol build --theme xxx

Via methanol.config.js:

export default () => ({
	theme: 'xxx'
})

Template Specification

The theme.template function receives a context object with the following properties:

Minimal Implementation

import { HTMLRenderer } from 'methanol'

export default ({ PageContent, ExtraHead, ctx }) => (
	<>
		{HTMLRenderer.rawHTML`<!doctype html>`}
		<html lang="en">
			<head>
				<meta charset="UTF-8" />
				<meta name="viewport" content="width=device-width" />
				<ExtraHead />
				<title>{ctx.page.title || ctx.site.name}</title>
			</head>
			<body>
				<PageContent />
			</body>
		</html>
	</>
)

For production-ready themes, refer to the following specialized guides: