### Serve Methanol Production Build Locally
Source: https://methanol.sudomaker.com/quick-start
Starts a local web server to preview the production build of your Methanol site. This is useful for testing the final output before deploying it.
```bash
npx methanol serve
```
--------------------------------
### Methanol CLI Commands with npx
Source: https://methanol.sudomaker.com/reference/cli
Demonstrates how to use the Methanol CLI with npx for starting the development server, building the project, and serving the production build. These commands are the quickest way to get started without global installation.
```bash
npx methanol dev # start dev server
npx methanol build # build to dist/
npx methanol serve # serve the production build
```
--------------------------------
### Run Methanol Development Server with Existing Docs Folder
Source: https://methanol.sudomaker.com/quick-start
Starts the Methanol development server, pointing it to an existing folder containing your Markdown or MDX files. This allows you to use Methanol with your pre-existing documentation structure, mapping the specified folder as the content root.
```bash
npx methanol dev ./my-docs
```
--------------------------------
### Install Methanol Globally and Run Development Server
Source: https://methanol.sudomaker.com/quick-start
Installs Methanol globally on your system, allowing you to run the `methanol` command directly without `npx`. This provides a faster CLI startup for local development workflows.
```bash
npm install methanol -g
methanol dev
```
--------------------------------
### Create First Page in Methanol
Source: https://methanol.sudomaker.com/quick-start
Demonstrates the creation of the initial `index.mdx` file for the homepage of a Methanol site. It shows the front matter for setting metadata like the title and the basic content structure.
```markdown
---
title: Home
---
# Welcome
This is my site.
```
--------------------------------
### Run Methanol Development Server Locally
Source: https://methanol.sudomaker.com/quick-start
Starts the Methanol development server for a quick local preview. It automatically watches for changes in your Markdown/MDX files and rebuilds the site. The default development server runs on http://localhost:5173.
```bash
npx methanol dev
```
--------------------------------
### Build Methanol Site for Production
Source: https://methanol.sudomaker.com/quick-start
Generates the production-ready static files for your website. The output is typically placed in a `dist/` folder, optimized for deployment.
```bash
npx methanol build
```
--------------------------------
### Build Static Site with Methanol CLI
Source: https://methanol.sudomaker.com/guide/publishing
This command builds the static site, outputting the files to the `dist/` directory. No external dependencies are required beyond the Methanol CLI installation.
```bash
npx methanol build
```
--------------------------------
### Global Methanol CLI Installation and Usage
Source: https://methanol.sudomaker.com/reference/cli
Explains how to install the Methanol CLI globally using npm for faster startup and then run its commands directly. This is useful for frequent use across multiple projects.
```bash
npm install methanol -g
methanol dev
```
--------------------------------
### Install Pagefind Globally (npm)
Source: https://methanol.sudomaker.com/reference/search
Installs Pagefind globally on your system using npm, making it available as a command-line tool.
```bash
npm i -g pagefind
```
--------------------------------
### Install Pagefind Locally (npm)
Source: https://methanol.sudomaker.com/reference/search
Installs Pagefind as a development dependency in your project using npm.
```bash
npm i -D pagefind
```
--------------------------------
### Example Directory Structure for Multi-language
Source: https://methanol.sudomaker.com/reference/languages
Demonstrates a typical file structure for a multi-language setup in Methanol. Each language has its own directory containing an index file and other content pages.
```tree
pages/
index.mdx # root language
zh/
index.mdx # Chinese language root
guide.mdx
fr/
index.mdx # French language root
guide.mdx
```
--------------------------------
### Install Pagefind Search
Source: https://methanol.sudomaker.com/advanced/themes/default
Installs the Pagefind search library as a development dependency. This is required to enable the search functionality within the Methanol theme.
```bash
npm i -D pagefind
```
--------------------------------
### Theme Configuration Example
Source: https://methanol.sudomaker.com/reference/configuration
Configure the project theme, including the theme root directory, custom HTML templates, default MDX components, and theme-specific directories for components, pages, and public assets.
```javascript
theme: {
root: 'path/to/theme',
template: ({ ctx, page, PageContent, ExtraHead, withBase, HTMLRenderer, components }) => {
// custom template logic
},
components: {
// default MDX components
},
componentsDir: 'theme/components',
pagesDir: 'theme/pages',
publicDir: 'theme/public',
sources: { /* virtual file map */ },
mdx: { /* default MDX options */ },
vite: { /* default Vite config */ }
}
```
--------------------------------
### Preview Production Output Locally with Methanol CLI
Source: https://methanol.sudomaker.com/guide/publishing
This command serves the production-ready static site locally, allowing verification of the output generated by `npx methanol build`. It's useful for testing before deployment.
```bash
npx methanol serve
```
--------------------------------
### Build Pipeline Hooks Example
Source: https://methanol.sudomaker.com/reference/configuration
Demonstrates the implementation of preBuild, preBundle, and postBuild hooks to customize build processes. These hooks allow for custom logic before building, before bundling, and after building, respectively. They can access shared data and information about the site and pages.
```javascript
export default () => ({
preBuild: ({ data, site }) => {
data.startedAt = Date.now()
console.log('Building', site.name)
},
preBundle: ({ pages }) => {
console.log('Rendered pages:', pages.length)
},
postBuild: ({ data, pages }) => {
console.log('Duration (ms):', Date.now() - data.startedAt)
console.log('Pages:', pages.length)
}
})
```
--------------------------------
### Methanol CLI Dev with Input Directory Argument
Source: https://methanol.sudomaker.com/reference/cli
Illustrates how to specify only the input directory for the Methanol dev command. In development mode, only the input directory is used to start the dev server.
```bash
npx methanol dev ./pages
```
--------------------------------
### Example Frontmatter for Language Root
Source: https://methanol.sudomaker.com/reference/languages
Illustrates the frontmatter configuration for a language root index page. Key fields like 'lang', 'langCode', and 'isRoot' are used to define language properties.
```yaml
---
title: 简体中文
lang: 简体中文
langCode: zh
isRoot: true
hidden: true
---
```
--------------------------------
### rEFui Reactive Component Demo (Browser)
Source: https://methanol.sudomaker.com/advanced/refui
Demonstrates a small, reactive component using rEFui in a browser environment. It utilizes signals for state management and DOM rendering. This example is useful for experimenting with rEFui outside of Methanol.
```javascript
import { signal } from 'refui'
import { createDOMRenderer } from 'refui/dom'
import { defaults } from 'refui/browser'
const DOMRenderer = createDOMRenderer(defaults)
const App = () => {
const count = signal(0)
const increment = () => {
count.value += 1
}
return (R) => (
<>
Hello, rEFui
Double: {$(() => count.value * 2)}
>
)
}
DOMRenderer.render(document.body, App)
```
--------------------------------
### Local Image Usage in MDX
Source: https://methanol.sudomaker.com/guide/assets
Illustrates how to link to an image file that is located in the same directory as an MDX page using a relative path. This is useful for page-specific diagrams or assets.
```markdown

```
--------------------------------
### Running Methanol CLI Directly with Node
Source: https://methanol.sudomaker.com/reference/cli
Provides an alternative method to run the Methanol CLI commands by directly executing the script using Node. This approach bypasses npx and global installations.
```bash
node node_modules/methanol/bin/methanol.js dev
```
--------------------------------
### Install vite-plugin-pwa
Source: https://methanol.sudomaker.com/guide/offline
Install the vite-plugin-pwa package as a development dependency using npm. This command adds the necessary plugin to your project's development environment.
```bash
npm install vite-plugin-pwa -D
```
--------------------------------
### Configuration with Vite Plugins and Theme
Source: https://methanol.sudomaker.com/reference/configuration
An example configuration object for Methanol Sudomaker, showcasing site configuration, theme customization including HTML template and components, MDX options, and Vite integration with plugins like tailwindcss. This configuration demonstrates how to set up the development server port and customize the build output.
```javascript
import tailwindcss from '@tailwindcss/vite'
export default ({ mode }) => ({
site: { name: 'My Docs' },
theme: {
root: '.',
template: ({ PageContent, ExtraHead, ctx, withBase, HTMLRenderer, components }) => (
<>
{HTMLRenderer.rawHTML``}
{ctx.page.title || ctx.site.name}
{components.Callout ? Hi : null}
>
),
components: {
Callout: ({ children }) =>
{children}
}
},
mdx: () => ({
development: mode !== 'production'
}),
vite: () => ({
server: { port: 5173 },
plugins: [tailwindcss()]
})
})
```
--------------------------------
### Add Extra Languages to Starry Night (JavaScript)
Source: https://methanol.sudomaker.com/reference/code-highlighting
Extend Starry Night's language support by importing additional grammars, such as `sourceMDX`, and including them in the `starryNight.grammars` configuration. Ensure `@wooorm/starry-night` is installed.
```javascript
import { common } from '@wooorm/starry-night'
import sourceMDX from '@wooorm/starry-night/source.mdx'
export default () => ({
starryNight: {
grammars: [...common, sourceMDX]
}
})
```
--------------------------------
### Global Styles and Scripts Injection
Source: https://methanol.sudomaker.com/guide/assets
Shows how to include global CSS and JavaScript files by placing them in the `pages/` directory. Methanol automatically injects these into every page. The provided HTML snippet represents how these would be linked.
```html
```
--------------------------------
### Component File Structure Example
Source: https://methanol.sudomaker.com/advanced/components
Illustrates the directory structure for custom components in Methanol. Files with .jsx, .tsx, .js, or .ts extensions in the 'components/' directory are automatically discovered. The file name determines the component name, and user components override theme components with the same name.
```directory
components/
Callout.jsx
Hero.client.jsx
Toc.static.jsx
```
--------------------------------
### Injecting Unescaped HTML using rawHTML
Source: https://methanol.sudomaker.com/advanced/mdx-runtime
This snippet demonstrates the use of the `rawHTML` helper function to inject HTML content directly into the MDX output without escaping. It shows examples using both string literals and template literals.
```mdx
{rawHTML('Beta')}
{rawHTML(`
Banner
`)}
```
--------------------------------
### Get Previous/Next Links with Methanol
Source: https://methanol.sudomaker.com/advanced/theme-guide/prev-next
Retrieves previous and next sibling page objects from the context. Returns an object with `prev` and `next` properties, or null if no siblings are found. Useful for implementing navigation.
```javascript
const { prev, next } = ctx.getSiblings() || {}
```
--------------------------------
### Public Folder Asset Usage in MDX
Source: https://methanol.sudomaker.com/guide/assets
Demonstrates how to reference assets placed in the `public/` directory within MDX files using Markdown image syntax and standard link syntax. These assets are served from the site root.
```markdown

[Download the brochure](/downloads/brochure.pdf)
```
--------------------------------
### Exporting a Custom Component (JSX)
Source: https://methanol.sudomaker.com/advanced/components
Provides an example of how to export a default function component named 'Callout' using JSX. This component accepts a 'title' prop and renders a div with the title and children.
```jsx
// components/Callout.jsx
export default function Callout({ title }, ...children) {
return (
{title}
{...children}
)
}
```
--------------------------------
### Configure Methanol for Subpath Deployment
Source: https://methanol.sudomaker.com/guide/publishing
This configuration snippet allows setting a base subpath for the deployed static site. It's used in `methanol.config.*` files to specify the site's base URL if it's not served from the root.
```javascript
export default () => ({
site: {
base: '/docs/'
}
})
```
--------------------------------
### Methanol Theme Object Export with Multiple Directories
Source: https://methanol.sudomaker.com/advanced/theme-guide
Defines the theme object in index.js, specifying the root directory, template component, and directories for components, pages, public assets, and sources. It also includes configuration for asset sources.
```javascript
import PageTemplate from './page.jsx'
export default () => ({
ttheme: {
root: '.',
template: PageTemplate,
componentsDir: './components',
pagesDir: './pages',
publicDir: './public',
sources: {
'/theme': './sources'
}
}
})
```
--------------------------------
### Fenced Code Block with Language Identifier
Source: https://methanol.sudomaker.com/guide/writing
Shows how to embed code blocks in Markdown using fenced code blocks with a language identifier for syntax highlighting. This is useful for showcasing code examples.
```markdown
```js
export const title = 'Hello'
```
```
--------------------------------
### Integrate Custom Highlighter (JavaScript)
Source: https://methanol.sudomaker.com/reference/code-highlighting
Replace Starry Night with a custom highlighter by disabling Starry Night (`starryNight: false`) and adding your plugin to the `mdx.rehypePlugins` configuration. This requires installing and configuring your chosen highlighter.
```javascript
export default () => ({
starryNight: false,
mdx: () => ({
rehypePlugins: [/* your highlighter */]
})
})
```
--------------------------------
### Minimal Methanol Theme Template Component
Source: https://methanol.sudomaker.com/advanced/theme-guide
A basic implementation of a Methanol theme template component. It renders the essential HTML structure, including head content and the main page body, using provided props like PageContent and ExtraHead.
```javascript
import { HTMLRenderer } from 'methanol'
export default ({ PageContent, ExtraHead, ctx }) => (
<>
{HTMLRenderer.rawHTML``}
{ctx.page.title || ctx.site.name}
>
)
```
--------------------------------
### Define a Basic JSX Component with rEFui
Source: https://methanol.sudomaker.com/advanced/refui
Example of defining a simple functional component using JSX syntax, compatible with Methanol's rEFui integration. This component takes props and children to render a div with a 'callout' class.
```javascript
export default function Callout(props, ...children) {
return
{...children}
}
```
--------------------------------
### React Simple Select Language Switcher
Source: https://methanol.sudomaker.com/advanced/theme-guide/language-switch
A React component that renders a `