Quick Start
Assuming you chose to scaffold the VitePress project in ./docs, the initial generated file structure should look like this:
.
├─ docs
│ ├─ .vitepress
│ │ └─ config.js
│ ├─ api-examples.md
│ ├─ markdown-examples.md
│ └─ index.md
└─ package.jsonInstallation
Add vitepress-templ-preview to your VitePress project along with its peer dependency css-tree, using one of the following commands:
npm add -D vitepress-templ-preview css-treepnpm add -D vitepress-templ-preview css-treeyarn add -D vitepress-templ-preview css-treeVitePress configuration
- Configure the plugin in your VitePress project by editing the VitePress config file (
.vitepress/config.jsor.vitepress/config.mts) - Register the rendering component: choose a predefined Vue component or Use a custom component; Create or edit
.vitepress/theme/index.jsor.vitepress/theme/index.ts
// .vitepress/config.mts
import { defineConfig } from "vitepress";
import viteTemplPreviewPlugin from "vitepress-templ-preview";
export default defineConfig({
/* ... */
markdown: {},
vite: {
plugins: [viteTemplPreviewPlugin()],
},
/* ... */
});//.vitepress/theme/index.ts
import type { Theme } from "vitepress";
import DefaultTheme from "vitepress/theme";
import { VTPIconTabs } from "vitepress-templ-preview/components";
import "vitepress-templ-preview/style.css";
export default {
extends: DefaultTheme,
enhanceApp({ app }) {
/* Make sure to name the tag `VTPLivePreview` */
app.component("VTPLivePreview", VTPIconTabs);
},
} satisfies Theme;INFO
Please, refer to the Plugin Options to see the available options for the plugin.
Search
Define a custom _render function to preprocess the Markdown content and remove <templ-demo> tags from the Markdown source to prevent build errors.
The vitepress-templ-preview plugin provides a sanitizeMarkdownForSearch helper to streamline this process.
import { sanitizeMarkdownForSearch } from 'vitepress-templ-preview/sanitizer';
export default defineConfig({
/* ... */
themeConfig: {
search: {
provider: 'local',
options: {
_render(src, env, md) {
sanitizeMarkdownForSearch(src, env, md)
},
},
},
/* ... */
});
/* ... */
});IMPORTANT
Additionally, renderForLocalSearch accepts a fourth parameter to specify the inputDir, if it has been set in the plugin options. For details, refer to the Plugin Options.
Templ Project Setup
Initialize a new Go project within the VitePress project root folder
docsas you would for a normaltemplproject. Refer to the official doc.bashcd docs go mod init components_demo go get github.com/a-h/templCreate a
componentsfolder within./docs.Create a
hellofolder to store both the markdown and thetemplfiles for the demos:Create an
index.mdfileCreate a
hello-demo.templfile with sometemplcode:templpackage hello templ HelloDemo() { @hello("World!!!") } templ hello(name string) { <div>Hello, { name }</div> }
Your project structure should look like this:
.
├── docs
│ ├─ .vitepress
│ │ └─ config.js
│ ├─ api-examples.md
│ ├─ markdown-examples.md
│ ├─ index.md
│ ├─ components
│ │ ├─ hello
│ │ │ ├─ hello-demo.templ
│ │ │ └─ index.md
│ ├─ go.mod
│ ├─ go.sum
└─ package.jsonEmbed in Markdown
To embed and preview a templ component in your markdown files, use the templ-demo tag.
Edit the components/hello/index.md file by adding:
<templ-demo src="hello-demo"></templ-demo>Alternatively, you can use the self-closing format:
<templ-demo src="hello-demo" />IMPORTANT
The src property is the only mandatory attribute. It must be set to the templ file name without the extension.
For more information on the available options for rendering components via the custom templ-demo tag, refer to the Rendering Components documentation.
Run the project
Based on your initial configuration, the npm scripts might already be set in the package.json file.
To start the local dev server, use one of the following commands:
npm run docs:devpnpm run docs:devyarn docs:devVisit the page for the hello component you just created.
Your project is now up and running. Enjoy! ❤️