mirror of
https://github.com/jackyzha0/quartz.git
synced 2026-03-22 14:05:43 -05:00
* feat(plugins): v5 plugin system * feat(plugins): explorer as community plugin * feat(plugins): graph as community plugin * chore: update package-lock.json * chore: update package-lock.json * docs: updated plugin-specific docs * chore: update package-lock.json * chore: update package-lock.json * chore: update package-lock.json * Implement Git-based plugin system with dogfooding for community plugins - Remove npm dependencies for @quartz-community/* plugins - Add gitLoader.ts for installing plugins from GitHub - Update quartz.layout.ts to import from .quartz/plugins/ - Add install-plugins.ts script for prebuild hook - Add .quartz/ to .gitignore * Add comprehensive Git-based plugin CLI with lockfile support - Create quartz.lock.json format for tracking exact plugin commits - Add 'npx quartz plugin' commands: install, add, remove, update, list, restore - Plugin state is fully reproducible via lockfile - No npm dependencies required for community plugins * Fix TypeScript errors in git-installed plugins - Install @quartz-community/types as devDependency - Fix plugin imports to define types locally - Fix search inline script fetchData bug - Format code with prettier * fix(types): install types from github * docs: updated plugin-specific docs * Update Dockerfile and add CI/CD documentation - Add plugin install step to Dockerfile - Create docs/ci-cd.md with pipeline configuration guide * Update GitHub Actions workflows for v5 branch and Git-based plugins - Change branch references from v4 to v5 - Add plugin caching to speed up builds - Use 'npx quartz plugin install' instead of 'restore' - Update Docker workflow branch trigger * Update quartz.lock.json with fixed plugin versions * fix(docker): install command * docs: add plugin migration analysis document Comprehensive analysis of which Quartz v4 components and plugins can be migrated to separate repositories, including: - Component analysis (25 components) - Plugin analysis (transformers, emitters, filters) - Migration strategies for different plugin types - Lessons learned from Explorer/Graph/Search migrations - Recommended migration order * chore: updated plugins * chore: updated plugins * chore: updated dependencies * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: tsconfig * feat: build installed plugins * chore: updated plugins * chore: updated plugins * chore: update explorer plugin with duplication fix * docs: Quartz v5 * chore: update graph plugin with navigation fix * fix: update explorer plugin with toggle fix * fix: update explorer plugin - ensure toggle buttons always work * fix: create plugin components once to prevent duplicate script registration * chore: updated plugins * chore: updated plugins * feat: migrate 7 feature components to community plugins (Phase B) Migrate ArticleTitle, TagList, PageTitle, Darkmode, ReaderMode, ContentMeta, and Footer from internal components to community plugins. Update layout to use Plugin.X() pattern, remove internal component files and their styles/scripts. Add MIGRATION_TASKS.md documenting the full migration roadmap. * chore: updated plugins * refactor: delete 6 internal component duplicates (Phase A) Remove Backlinks, Breadcrumbs, RecentNotes, Search, TableOfContents, Comments, and OverflowList — all replaced by community plugins. Delete associated styles (6) and scripts (3). Switch layout to use Plugin.Breadcrumbs() instead of Component.Breadcrumbs(). * refactor: unify QuartzComponent type to structural interface (Phase C) - Changed QuartzComponent from ComponentType<QuartzComponentProps> to callable type ((props: QuartzComponentProps) => any) - Added optional displayName property for better debugging - Removed ComponentType import from preact - Removed all 13 'as QuartzComponent' type casts from quartz.layout.ts - Community plugin components now directly assignable without casts * feat: add PageType plugin infrastructure (Phase D Step 4) * feat: add PageTypePluginEntry for cross-boundary type compatibility Introduce PageTypePluginEntry with never[] parameter types to accept both internal and community PageType plugins in config arrays without casts, working around branded FullSlug contravariance mismatch. * refactor: update dispatcher to cast PageTypePluginEntry at boundary Add getPageTypes() helper that casts config's PageTypePluginEntry[] to QuartzPageTypePluginInstance[] in one place. Cast VirtualPage.slug to FullSlug at emitPage/defaultProcessedContent call sites. * feat: integrate community PageType plugins (Phase D Step 6) Replace old page-rendering emitters with PageTypeDispatcher emitter and pageTypes array. Restructure quartz.layout.ts from three separate exports to unified layout object with defaults and byPageType record. Install content-page, folder-page, tag-page community plugins. * refactor: delete old page-rendering emitters Remove ContentPage, FolderPage, TagPage, and NotFoundPage emitters now replaced by community PageType plugins and the PageTypeDispatcher. * refactor: remove migrated page body components Delete Content, FolderContent, TagContent page components now provided by community PageType plugins. Update components barrel export. * fix: update lockfile to fixed folder-page and tag-page commits Points to commits that remove duplicate PageList/SortFn re-exports, fixing TS2300 duplicate identifier errors in generated plugin index. * chore: updated plugins * fix: populate ctx.trie in PageTypeDispatcher before rendering Components like FolderContent depend on ctx.trie for folder hierarchy. The dispatcher now lazily initializes it via trieFromAllFiles in emit and force-rebuilds it in partialEmit to reflect file changes. * chore: update lockfile to fixed folder-page commit * chore: updated plugins * chore: update explorer plugin to fix SPA folder navigation * feat: extract transformers to community plugins and fix type compatibility - Delete 12 internal transformer files (keep FrontMatter as internal) - Switch quartz.config.ts to use ExternalPlugin.* for all transformers - Align branded types with @quartz-community/types (_brand, FullSlug etc.) - Add vfile DataMap augmentations for fields from extracted transformers - Update all 29 plugins to @quartz-community/types v0.2.1 * Migrate filters to external plugins (remove-draft, explicit-publish) Delete internal RemoveDrafts and ExplicitPublish filter implementations, install them as community plugins, and update quartz.config.ts to use ExternalPlugin.RemoveDrafts(). * Migrate emitters to external plugins (alias-redirects, cname, favicon, content-index, og-image) * refactor: remove inline scripts/styles migrated to plugins Delete dead code: callout, checkbox, mermaid inline scripts and styles are now bundled by the obsidian-flavored-markdown plugin. Clipboard script and styles moved to the syntax-highlighting plugin. listPage.scss was unreferenced. Body.tsx simplified to a pure layout wrapper. * refactor: consolidate utils to re-export from @quartz-community/utils * fix: use dangerouslySetInnerHTML for inline CSS to prevent HTML-escaping Preact was escaping & characters in SCSS-compiled CSS (e.g. & nesting) into &, breaking CSS rules. Using dangerouslySetInnerHTML bypasses the escaping, matching how browsers expect style element content. * chore: update plugins with inline script transpilation fix * chore: updated plugins * docs: update plugin API sections for v5 community plugins * docs: rewrite documentation for v5 plugin system Update feature docs, hosting, CI/CD, getting started, configuration, layout, architecture, creating components, making plugins, and migration guide to reflect the v5 community plugin architecture. * docs: fix outdated v4 references in documentation * chore: remove completed migration planning docs * chore: updated plugins * chore: cleanup * chore: cleanup * chore: bump version to 5.0.0 * chore: updated dependencies * feat: integrate CanvasPage plugin with types, assets, config, layout, and documentation * chore: updated dependencies * chore: updated dependencies * chore: updated linter * chore: update canvas-page plugin to c942fcb * chore: updated plugins * chore: update canvas-page plugin to f88f1b9 * chore: updated plugins * chore: update canvas-page plugin to 079304c * chore: updated plugins * chore: canvas layout * chore: update canvas-page plugin to 38d49e1 * chore: updated plugins * chore: update canvas-page plugin to 505c099 * chore: updated plugins * chore: updated plugins * fix: Obsidian flavored markdown * fix: Obsidian flavored markdown * fix: Obsidian flavored markdown * chore: cleanup * chore: updated plugins * feat: configuration files * feat: Quartz TUI * feat(tui): YAML configuration * chore: tsup * chore: tsup * feat: support array categories in plugin manifests Plugins like note-properties export both transformer and component functionality. Allow PluginManifest.category to be a single value or an array, with config-loader resolving to the first processing category (transformer/filter/emitter/pageType) for dispatch. * refactor: remove built-in FrontMatter transformer Frontmatter processing is now handled by the note-properties plugin, which provides the same YAML/TOML parsing plus link extraction and a visual properties panel. The built-in transformer is no longer needed. * feat: add note-properties plugin to default configuration Register note-properties as the first plugin (order 5) in both the user config and the default config. Placed in beforeBody layout zone with priority 15 (between article-title at 10 and content-meta at 20). * docs: add plugin management strategy and syncer v5 notes Document the plugin management system design decisions and provide implementation guidance for the Quartz Syncer v5 integration. * feat: add bases-page plugin to default configuration Enable Obsidian Bases (.base) file support with bases page type and layout entry in both user and default config. * docs: update syncer notes with bases-page, note-properties, and spacer Add all three new plugins to the quick reference table (40 total). Add content, canvas, and bases page types to byPageType documentation. * chore: updated plugins * fix: update CI to Node 24 and regenerate lockfiles for clean install * fix: resolve type errors for CI checks * chore: updated plugins * chore: updated plugins * fix: plugin mapping from configuration * fix: CI * fix: CI * docs: rewrite Frontmatter documentation for note-properties plugin * chore: updated plugins * docs: Quartz v5 * chore: updated plugins * chore: updated plugins * refactor: extract TUI to standalone plugin repository * chore: linting * docs: Quartz v5 * feat: update and upgrade commands * chore: updated plugins * chore: updated plugins * chore: cleanup * chore: cleanup * chore: cleanup * chore: cleanup * chore: cleanup * fix: layout group priority * fix: view classes * fix: include virtual pages in content index for explorer visibility * docs: add board, gallery, and cards view examples to navigation page * chore: updated plugins * fix: include virtualPages in worker serializable build context * fix: set relativePath on virtual pages to prevent explorer crash * fix: exclude 404 * fix(links): virtual page links * fix(links): virtual page transclusion * docs: architecture overview * fix: only call scripts one per page * fix: type error in component registry instantiate method * fix: left layout order * fix(layout): remove tag-list by default * docs(plugins): updated plugin list defaults * fix(layout): priorities * feat: add PageFrame system for custom page layouts * feat: integrate PageFrame into rendering pipeline * feat: add frame resolution to page type dispatcher and config loader * style: add CSS grid overrides for full-width and minimal page frames * feat: set minimal frame for 404 and update canvas-page plugin * docs: add PageFrame system to architecture overview * fix: wrap frame.render() in array to satisfy Body children type * chore: format * fix: use absolute asset paths for 404 page so it works in subdirectories * fix(layout): priorities * docs: page frames * feat: add FrameRegistry for plugin-provided page frames Plugins can now register custom page frames via their manifest's 'frames' field. Frames are loaded alongside components during plugin initialization and resolved by name at render time with fallback to built-in frames. * feat(layout): page frames * fix(layout): linting * fix: inject frame CSS into page so plugin-provided frames render correctly * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * docs: canvas * chore: updated plugins * chore: updated plugins * chore: updated plugins * feat: add TreeTransform hook, fix multi-category plugins, and resolve cross-plugin dependencies - Add TreeTransform type and treeTransforms hook to pageType plugins, enabling render-time HAST tree mutations (e.g. bases-page inline codeblock resolution) - Fix config-loader to push multi-category plugins into ALL matching processing buckets instead of only the first match - Add side-effect import for component-only plugins so view registrations (e.g. leaflet-map via globalThis ViewRegistry) execute at load time - Add npm prune --omit=dev and cross-plugin peer dependency symlinking to buildPlugin() to prevent duplicate-singleton issues from nested node_modules * chore: format * chore: test docs * chore: updated plugins * fix: prevent HTML-escaping of inline style and script content in htmlToJsx Add dangerouslySetInnerHTML overrides for <style> and <script> elements so that CSS/JS injected by tree transforms is not HTML-escaped during preact-render-to-string serialization. * chore: update plugin lockfile for htmlToJsx migration * chore: update leaflet-map plugin (fix deferred L.Control) * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: updated plugins * chore: test npx quartz upgrade * feat(templates): add obsidian, ttrpg, blog templates * docs: move bases * docs: removed leaflet demo * feat(cli): configure baseUrl during create * docs: updated cli commands * docs: updated documentation for v5 * feat(cli): prune and resolve * chore: rebuild lockfile * docs: cli documentation * docs: plugin development and setup guide * chore: deleted redundant files * fix(build): fallback config * chore: updated lockfile * docs: removed outdated v3 setup * feat(cli): allow non-default branch plugins * docs: install branch commands * feat(cli): allow local plugins * docs: install local commands * feat: add render event type and listener for in-place DOM re-initialization * docs: add EncryptedPages plugin documentation * docs: add encrypted pages live demo page - New password-protected demo page (password: quartz) showing the plugin in action - Link to demo from EncryptedPages plugin page with password hint callout * feat: add encrypted-pages plugin to all templates - Enabled by default in default, obsidian, and ttrpg templates - Disabled by default in blog template * chore: updated plugins * chore: updated layouts * chore: updated plugins * feat: stacked pages * feat: added stacked page panes * docs: touch-ups
539 lines
20 KiB
Markdown
539 lines
20 KiB
Markdown
---
|
|
title: Making your own plugins
|
|
---
|
|
|
|
> [!warning]
|
|
> This part of the documentation will assume you have working knowledge in TypeScript and will include code snippets that describe the interface of what Quartz plugins should look like.
|
|
|
|
Quartz's plugins are a series of transformations over content. This is illustrated in the diagram of the processing pipeline below:
|
|
|
|
![[quartz transform pipeline.png]]
|
|
|
|
All plugins are defined as a function that takes in a single parameter for options `type OptionType = object | undefined` and return an object that corresponds to the type of plugin it is.
|
|
|
|
```ts
|
|
type OptionType = object | undefined
|
|
type QuartzPlugin<Options extends OptionType = undefined> = (opts?: Options) => QuartzPluginInstance
|
|
type QuartzPluginInstance =
|
|
| QuartzTransformerPluginInstance
|
|
| QuartzFilterPluginInstance
|
|
| QuartzEmitterPluginInstance
|
|
| QuartzPageTypePluginInstance
|
|
```
|
|
|
|
The following sections will go into detail for what methods can be implemented for each plugin type. Before we do that, let's clarify a few more ambiguous types:
|
|
|
|
- `BuildCtx` is defined in `@quartz-community/types`. It consists of
|
|
- `argv`: The command line arguments passed to the Quartz [[build]] command
|
|
- `cfg`: The full Quartz [[configuration]]
|
|
- `allSlugs`: a list of all the valid content slugs (see [[paths]] for more information on what a slug is)
|
|
- `StaticResources` is defined in `@quartz-community/types`. It consists of
|
|
- `css`: a list of CSS style definitions that should be loaded. A CSS style is described with the `CSSResource` type. It accepts either a source URL or the inline content of the stylesheet.
|
|
- `js`: a list of scripts that should be loaded. A script is described with the `JSResource` type. It allows you to define a load time (either before or after the DOM has been loaded), whether it should be a module, and either the source URL or the inline content of the script.
|
|
- `additionalHead`: a list of JSX elements or functions that return JSX elements to be added to the `<head>` tag of the page. Functions receive the page's data as an argument and can conditionally render elements.
|
|
|
|
## Getting Started
|
|
|
|
In v5, plugins are standalone repositories. The easiest way to create one is using the plugin template:
|
|
|
|
```shell
|
|
# Use the plugin template to create a new repository on GitHub
|
|
# Then clone it locally
|
|
git clone https://github.com/your-username/my-plugin.git
|
|
cd my-plugin
|
|
npm install
|
|
```
|
|
|
|
The template provides the build configuration (`tsup.config.ts`), TypeScript setup, and correct package structure.
|
|
|
|
## Plugin Structure
|
|
|
|
The basic file structure of a plugin is as follows:
|
|
|
|
```
|
|
my-plugin/
|
|
├── src/
|
|
│ └── index.ts # Plugin entry point
|
|
├── tsup.config.ts # Build configuration
|
|
├── package.json # Dependencies and exports
|
|
└── tsconfig.json # TypeScript configuration
|
|
```
|
|
|
|
The plugin's `package.json` should declare dependencies on `@quartz-community/types` (for type definitions) and optionally `@quartz-community/utils` (for shared utilities).
|
|
|
|
## Plugin Types
|
|
|
|
### Transformers
|
|
|
|
Transformers **map** over content, taking a Markdown file and outputting modified content or adding metadata to the file itself.
|
|
|
|
```ts
|
|
export type QuartzTransformerPluginInstance = {
|
|
name: string
|
|
textTransform?: (ctx: BuildCtx, src: string) => string
|
|
markdownPlugins?: (ctx: BuildCtx) => PluggableList
|
|
htmlPlugins?: (ctx: BuildCtx) => PluggableList
|
|
externalResources?: (ctx: BuildCtx) => Partial<StaticResources>
|
|
}
|
|
```
|
|
|
|
All transformer plugins must define at least a `name` field to register the plugin and a few optional functions that allow you to hook into various parts of transforming a single Markdown file.
|
|
|
|
- `textTransform` performs a text-to-text transformation _before_ a file is parsed into the [Markdown AST](https://github.com/syntax-tree/mdast).
|
|
- `markdownPlugins` defines a list of [remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md). `remark` is a tool that transforms Markdown to Markdown in a structured way.
|
|
- `htmlPlugins` defines a list of [rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md). Similar to how `remark` works, `rehype` is a tool that transforms HTML to HTML in a structured way.
|
|
- `externalResources` defines any external resources the plugin may need to load on the client-side for it to work properly.
|
|
|
|
Normally for both `remark` and `rehype`, you can find existing plugins that you can use. If you'd like to create your own `remark` or `rehype` plugin, checkout the [guide to creating a plugin](https://unifiedjs.com/learn/guide/create-a-plugin/) using `unified` (the underlying AST parser and transformer library).
|
|
|
|
A good example of a transformer plugin that borrows from the `remark` and `rehype` ecosystems is the [[plugins/Latex|Latex]] plugin:
|
|
|
|
```ts
|
|
import remarkMath from "remark-math"
|
|
import rehypeKatex from "rehype-katex"
|
|
import rehypeMathjax from "rehype-mathjax/svg"
|
|
import { QuartzTransformerPlugin } from "@quartz-community/types"
|
|
|
|
interface Options {
|
|
renderEngine: "katex" | "mathjax"
|
|
}
|
|
|
|
export const Latex: QuartzTransformerPlugin<Options> = (opts?: Options) => {
|
|
const engine = opts?.renderEngine ?? "katex"
|
|
return {
|
|
name: "Latex",
|
|
markdownPlugins() {
|
|
return [remarkMath]
|
|
},
|
|
htmlPlugins() {
|
|
if (engine === "katex") {
|
|
// if you need to pass options into a plugin, you
|
|
// can use a tuple of [plugin, options]
|
|
return [[rehypeKatex, { output: "html" }]]
|
|
} else {
|
|
return [rehypeMathjax]
|
|
}
|
|
},
|
|
externalResources() {
|
|
if (engine === "katex") {
|
|
return {
|
|
css: [
|
|
{
|
|
// base css
|
|
content: "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css",
|
|
},
|
|
],
|
|
js: [
|
|
{
|
|
// fix copy behaviour: https://github.com/KaTeX/KaTeX/blob/main/contrib/copy-tex/README.md
|
|
src: "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js",
|
|
loadTime: "afterDOMReady",
|
|
contentType: "external",
|
|
},
|
|
],
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
```
|
|
|
|
Another common thing that transformer plugins will do is parse a file and add extra data for that file:
|
|
|
|
```ts
|
|
import { QuartzTransformerPlugin } from "@quartz-community/types"
|
|
|
|
export const AddWordCount: QuartzTransformerPlugin = () => {
|
|
return {
|
|
name: "AddWordCount",
|
|
markdownPlugins() {
|
|
return [
|
|
() => {
|
|
return (tree, file) => {
|
|
// tree is an `mdast` root element
|
|
// file is a `vfile`
|
|
const text = file.value
|
|
const words = text.split(" ").length
|
|
file.data.wordcount = words
|
|
}
|
|
},
|
|
]
|
|
},
|
|
}
|
|
}
|
|
|
|
// tell typescript about our custom data fields we are adding
|
|
// other plugins will then also be aware of this data field
|
|
declare module "vfile" {
|
|
interface DataMap {
|
|
wordcount: number
|
|
}
|
|
}
|
|
```
|
|
|
|
Finally, you can also perform transformations over Markdown or HTML ASTs using the `visit` function from the `unist-util-visit` package or the `findAndReplace` function from the `mdast-util-find-and-replace` package.
|
|
|
|
```ts
|
|
import { visit } from "unist-util-visit"
|
|
import { findAndReplace } from "mdast-util-find-and-replace"
|
|
import { QuartzTransformerPlugin } from "@quartz-community/types"
|
|
import { Link } from "mdast"
|
|
|
|
export const TextTransforms: QuartzTransformerPlugin = () => {
|
|
return {
|
|
name: "TextTransforms",
|
|
markdownPlugins() {
|
|
return [
|
|
() => {
|
|
return (tree, file) => {
|
|
// replace _text_ with the italics version
|
|
findAndReplace(tree, /_(.+)_/, (_value: string, ...capture: string[]) => {
|
|
// inner is the text inside of the () of the regex
|
|
const [inner] = capture
|
|
// return an mdast node
|
|
// https://github.com/syntax-tree/mdast
|
|
return {
|
|
type: "emphasis",
|
|
children: [{ type: "text", value: inner }],
|
|
}
|
|
})
|
|
|
|
// remove all links (replace with just the link content)
|
|
// match by 'type' field on an mdast node
|
|
// https://github.com/syntax-tree/mdast#link in this example
|
|
visit(tree, "link", (link: Link) => {
|
|
return {
|
|
type: "paragraph",
|
|
children: [{ type: "text", value: link.title }],
|
|
}
|
|
})
|
|
}
|
|
},
|
|
]
|
|
},
|
|
}
|
|
}
|
|
```
|
|
|
|
A parting word: transformer plugins are quite complex so don't worry if you don't get them right away. Take a look at the built in transformers and see how they operate over content to get a better sense for how to accomplish what you are trying to do.
|
|
|
|
### Filters
|
|
|
|
Filters **filter** content, taking the output of all the transformers and determining what files to actually keep and what to discard.
|
|
|
|
```ts
|
|
export type QuartzFilterPlugin<Options extends OptionType = undefined> = (
|
|
opts?: Options,
|
|
) => QuartzFilterPluginInstance
|
|
|
|
export type QuartzFilterPluginInstance = {
|
|
name: string
|
|
shouldPublish(ctx: BuildCtx, content: ProcessedContent): boolean
|
|
}
|
|
```
|
|
|
|
A filter plugin must define a `name` field and a `shouldPublish` function that takes in a piece of content that has been processed by all the transformers and returns a `true` or `false` depending on whether it should be passed to the emitter plugins or not.
|
|
|
|
For example, here is the built-in plugin for removing drafts:
|
|
|
|
```ts
|
|
import { QuartzFilterPlugin } from "@quartz-community/types"
|
|
|
|
export const RemoveDrafts: QuartzFilterPlugin<{}> = () => ({
|
|
name: "RemoveDrafts",
|
|
shouldPublish(_ctx, [_tree, vfile]) {
|
|
// uses frontmatter parsed from transformers
|
|
const draftFlag: boolean = vfile.data?.frontmatter?.draft ?? false
|
|
return !draftFlag
|
|
},
|
|
})
|
|
```
|
|
|
|
### Emitters
|
|
|
|
Emitters **reduce** over content, taking in a list of all the transformed and filtered content and creating output files.
|
|
|
|
```ts
|
|
export type QuartzEmitterPlugin<Options extends OptionType = undefined> = (
|
|
opts?: Options,
|
|
) => QuartzEmitterPluginInstance
|
|
|
|
export type QuartzEmitterPluginInstance = {
|
|
name: string
|
|
emit(
|
|
ctx: BuildCtx,
|
|
content: ProcessedContent[],
|
|
resources: StaticResources,
|
|
): Promise<FilePath[]> | AsyncGenerator<FilePath>
|
|
partialEmit?(
|
|
ctx: BuildCtx,
|
|
content: ProcessedContent[],
|
|
resources: StaticResources,
|
|
changeEvents: ChangeEvent[],
|
|
): Promise<FilePath[]> | AsyncGenerator<FilePath> | null
|
|
getQuartzComponents(ctx: BuildCtx): QuartzComponent[]
|
|
}
|
|
```
|
|
|
|
An emitter plugin must define a `name` field, an `emit` function, and a `getQuartzComponents` function. It can optionally implement a `partialEmit` function for incremental builds.
|
|
|
|
- `emit` is responsible for looking at all the parsed and filtered content and then appropriately creating files and returning a list of paths to files the plugin created.
|
|
- `partialEmit` is an optional function that enables incremental builds. It receives information about which files have changed (`changeEvents`) and can selectively rebuild only the necessary files. This is useful for optimizing build times in development mode. If `partialEmit` is undefined, it will default to the `emit` function.
|
|
- `getQuartzComponents` declares which Quartz components the emitter uses to construct its pages.
|
|
|
|
Creating new files can be done via regular Node [fs module](https://nodejs.org/api/fs.html) (i.e. `fs.cp` or `fs.writeFile`) or via the `write` function in `@quartz-community/utils` if you are creating files that contain text. `write` has the following signature:
|
|
|
|
```ts
|
|
export type WriteOptions = (data: {
|
|
// the build context
|
|
ctx: BuildCtx
|
|
// the name of the file to emit (not including the file extension)
|
|
slug: FullSlug
|
|
// the file extension
|
|
ext: `.${string}` | ""
|
|
// the file content to add
|
|
content: string
|
|
}) => Promise<FilePath>
|
|
```
|
|
|
|
This is a thin wrapper around writing to the appropriate output folder and ensuring that intermediate directories exist. If you choose to use the native Node `fs` APIs, ensure you emit to the `argv.output` folder as well.
|
|
|
|
If you are creating an emitter plugin that needs to render components, there are three more things to be aware of:
|
|
|
|
- Your component should use `getQuartzComponents` to declare a list of `QuartzComponents` that it uses to construct the page. See the page on [[creating components]] for more information.
|
|
- You can use the `renderPage` function defined in `@quartz-community/utils` to render Quartz components into HTML.
|
|
- If you need to render an HTML AST to JSX, you can use the `htmlToJsx` function from `@quartz-community/utils`.
|
|
|
|
For example, the following is a simplified version of the content page plugin that renders every single page.
|
|
|
|
```tsx
|
|
import { QuartzEmitterPlugin, FullPageLayout, QuartzComponentProps } from "@quartz-community/types"
|
|
import { renderPage, canonicalizeServer, pageResources, write } from "@quartz-community/utils"
|
|
|
|
export const ContentPage: QuartzEmitterPlugin = () => {
|
|
return {
|
|
name: "ContentPage",
|
|
getQuartzComponents(ctx) {
|
|
const { head, header, beforeBody, pageBody, afterBody, left, right, footer } = ctx.cfg.layout
|
|
return [head, ...header, ...beforeBody, pageBody, ...afterBody, ...left, ...right, footer]
|
|
},
|
|
async emit(ctx, content, resources): Promise<FilePath[]> {
|
|
const cfg = ctx.cfg.configuration
|
|
const fps: FilePath[] = []
|
|
const allFiles = content.map((c) => c[1].data)
|
|
for (const [tree, file] of content) {
|
|
const slug = canonicalizeServer(file.data.slug!)
|
|
const externalResources = pageResources(slug, file.data, resources)
|
|
const componentData: QuartzComponentProps = {
|
|
fileData: file.data,
|
|
externalResources,
|
|
cfg,
|
|
children: [],
|
|
tree,
|
|
allFiles,
|
|
}
|
|
|
|
const content = renderPage(cfg, slug, componentData, {}, externalResources)
|
|
const fp = await write({
|
|
ctx,
|
|
content,
|
|
slug: file.data.slug!,
|
|
ext: ".html",
|
|
})
|
|
|
|
fps.push(fp)
|
|
}
|
|
return fps
|
|
},
|
|
}
|
|
}
|
|
```
|
|
|
|
Page types define how a category of pages is rendered. They are the primary way to add support for new file types or virtual pages in Quartz.
|
|
|
|
```ts
|
|
export interface QuartzPageTypePluginInstance {
|
|
name: string
|
|
priority?: number
|
|
fileExtensions?: string[]
|
|
match: PageMatcher
|
|
generate?: PageGenerator
|
|
layout: string
|
|
frame?: string
|
|
body: QuartzComponentConstructor
|
|
}
|
|
```
|
|
|
|
- `name`: A unique identifier for this page type.
|
|
- `priority`: Controls matching order when multiple page types could match a slug. Higher priority page types are checked first. Default: `0`.
|
|
- `fileExtensions`: Array of file extensions this page type handles (e.g. `[".canvas"]`, `[".base"]`). Content files (`.md`) are handled by the default content page type.
|
|
- `match`: A function that determines whether a given slug/file should be rendered by this page type.
|
|
- `generate`: An optional function that produces virtual pages (pages not backed by files on disk, such as folder listings or tag indices).
|
|
- `layout`: The layout configuration key (e.g. `"content"`, `"folder"`, `"tag"`). This determines which `byPageType` entry in `quartz.config.yaml` provides the layout overrides for this page type.
|
|
- `frame`: The [[layout#Page Frames|page frame]] to use for this page type. Controls the overall HTML structure (e.g. `"default"`, `"full-width"`, `"minimal"`, or a custom frame provided by your plugin). If not set, defaults to `"default"`. Can be overridden per-page-type via `layout.byPageType.<name>.template` in `quartz.config.yaml`.
|
|
- `body`: The Quartz component constructor that renders the page body content.
|
|
|
|
### Providing Custom Frames
|
|
|
|
Plugins can ship their own [[layout#Page Frames|page frames]] — custom page layouts that control how the HTML structure (sidebars, header, content area, footer) is arranged. This is useful for page types that need fundamentally different layouts (e.g. a fullscreen canvas, a presentation mode, a dashboard).
|
|
|
|
To provide a custom frame:
|
|
|
|
**1. Create the frame file:**
|
|
|
|
```tsx title="src/frames/MyFrame.tsx"
|
|
import type { PageFrame, PageFrameProps } from "@quartz-community/types"
|
|
import type { ComponentChildren } from "preact"
|
|
|
|
export const MyFrame: PageFrame = {
|
|
name: "my-frame",
|
|
css: `
|
|
.page[data-frame="my-frame"] > #quartz-body {
|
|
grid-template-columns: 1fr;
|
|
grid-template-areas: "center";
|
|
}
|
|
`,
|
|
render({ componentData, pageBody: Content, footer: Footer }: PageFrameProps): unknown {
|
|
const renderSlot = (C: (props: typeof componentData) => unknown): ComponentChildren =>
|
|
C(componentData) as ComponentChildren
|
|
return (
|
|
<div class="center">
|
|
{(Content as any)(componentData)}
|
|
{(Footer as any)(componentData)}
|
|
</div>
|
|
)
|
|
},
|
|
}
|
|
```
|
|
|
|
Key requirements:
|
|
|
|
- `name`: A unique string identifier. This is what page types and YAML config reference.
|
|
- `render()`: Receives all layout slots (header, sidebars, content, footer) and returns JSX for the inner page structure.
|
|
- `css` (optional): Frame-specific CSS. Scope it with `.page[data-frame="my-frame"]` selectors to avoid conflicts.
|
|
|
|
**2. Re-export the frame:**
|
|
|
|
```ts title="src/frames/index.ts"
|
|
export { MyFrame } from "./MyFrame"
|
|
```
|
|
|
|
**3. Declare the frame in `package.json`:**
|
|
|
|
```json title="package.json"
|
|
{
|
|
"exports": {
|
|
".": {
|
|
"import": "./dist/index.js",
|
|
"types": "./dist/index.d.ts"
|
|
},
|
|
"./frames": {
|
|
"import": "./dist/frames/index.js",
|
|
"types": "./dist/frames/index.d.ts"
|
|
}
|
|
},
|
|
"quartz": {
|
|
"frames": {
|
|
"MyFrame": { "exportName": "MyFrame" }
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
The `"frames"` field in the `"quartz"` manifest maps export names to frame metadata. The key (e.g. `"MyFrame"`) must match the export name in `src/frames/index.ts`.
|
|
|
|
**4. Add the frame entry point to your build config:**
|
|
|
|
```ts title="tsup.config.ts"
|
|
export default defineConfig({
|
|
entry: ["src/index.ts", "src/frames/index.ts"],
|
|
// ...
|
|
})
|
|
```
|
|
|
|
**5. Reference the frame in your page type:**
|
|
|
|
```ts
|
|
export const MyPageType: QuartzPageTypePlugin = () => ({
|
|
name: "MyPageType",
|
|
frame: "my-frame", // References the frame by its name property
|
|
// ...
|
|
})
|
|
```
|
|
|
|
When a user installs your plugin, Quartz automatically loads the frame from the `./frames` export and registers it in the Frame Registry. The frame is then available by name in any page type or YAML config override.
|
|
|
|
> [!tip]
|
|
> See the [`canvas-page`](https://github.com/quartz-community/canvas-page) plugin for a complete real-world example of a plugin-provided frame.
|
|
|
|
## Building and Testing
|
|
|
|
```shell
|
|
# Build the plugin
|
|
npm run build
|
|
# or
|
|
npx tsup
|
|
```
|
|
|
|
## Installing Your Plugin
|
|
|
|
```shell
|
|
# In your Quartz project
|
|
npx quartz plugin add github:your-username/my-plugin
|
|
```
|
|
|
|
This clones the plugin, builds it, and adds it to both `quartz.config.yaml` and `quartz.lock.json`. You can then configure it in your config:
|
|
|
|
```yaml title="quartz.config.yaml"
|
|
plugins:
|
|
- source: github:your-username/my-plugin
|
|
enabled: true
|
|
```
|
|
|
|
Or via TS override in `quartz.ts`:
|
|
|
|
```ts title="quartz.ts (override)"
|
|
import * as ExternalPlugin from "./.quartz/plugins"
|
|
// ...
|
|
transformers: [ExternalPlugin.MyPlugin()]
|
|
```
|
|
|
|
### Development Workflow
|
|
|
|
During plugin development, you'll frequently install and uninstall your plugin to test changes. The following commands help manage this cycle:
|
|
|
|
```shell
|
|
# Remove your plugin and clean up
|
|
npx quartz plugin remove my-plugin
|
|
|
|
# Re-add after making changes
|
|
npx quartz plugin add github:your-username/my-plugin
|
|
```
|
|
|
|
If you've updated your `quartz.config.yaml` to reference a plugin that isn't installed yet, you can install it without manually running `add`:
|
|
|
|
```shell
|
|
# Install all config-referenced plugins missing from the lockfile
|
|
npx quartz plugin resolve
|
|
|
|
# Preview first without making changes
|
|
npx quartz plugin resolve --dry-run
|
|
```
|
|
|
|
To clean up plugins that are installed but no longer referenced in your config:
|
|
|
|
```shell
|
|
# Remove orphaned plugins
|
|
npx quartz plugin prune
|
|
|
|
# Preview first without making changes
|
|
npx quartz plugin prune --dry-run
|
|
```
|
|
|
|
> [!tip]
|
|
> Both `resolve` and `prune` fall back to `quartz.config.default.yaml` if no `quartz.config.yaml` is present. This is useful for CI environments where the default config is the source of truth. See [[cli/plugin#prune|prune]] and [[cli/plugin#resolve|resolve]] for full details.
|
|
|
|
## Component Plugins
|
|
|
|
For plugins that provide visual components (like Explorer, Graph, Search), see the [[creating components|creating component plugins]] guide.
|