https://github.com/johnzanussi/astro-markdown-prerender
Astro integration to set route prerender based on markdown frontmatter
https://github.com/johnzanussi/astro-markdown-prerender
astro integration markdown prerender
Last synced: about 1 month ago
JSON representation
Astro integration to set route prerender based on markdown frontmatter
- Host: GitHub
- URL: https://github.com/johnzanussi/astro-markdown-prerender
- Owner: johnzanussi
- License: mit
- Created: 2025-06-25T22:34:52.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-25T22:40:48.000Z (12 months ago)
- Last Synced: 2025-06-25T22:45:08.809Z (12 months ago)
- Topics: astro, integration, markdown, prerender
- Language: TypeScript
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# astro-markdown-prerender
An Astro integration that automatically sets `route.prerender` based on frontmatter properties in your Markdown files.
## Installation
```bash
npm install astro-markdown-prerender
```
## Usage
Add the integration to your `astro.config.ts`:
```typescript
import { defineConfig } from 'astro/config';
import markdownPrerender from 'astro-markdown-prerender';
export default defineConfig({
integrations: [
markdownPrerender()
],
});
```
Then in your Markdown files, add the `prerender` property to your frontmatter:
```markdown
---
title: "My Blog Post"
prerender: true
---
# My Blog Post
This page will be prerendered at build time.
```
```markdown
---
title: "Dynamic Page"
prerender: false
---
# Dynamic Page
This page will be rendered on-demand.
```
## Configuration
The integration accepts the following options:
```typescript
export interface MarkdownPrerenderOptions {
/**
* Whether to log debug information about processed files
* @default false
*/
verbose?: boolean;
/**
* Custom property name to look for in frontmatter
* @default 'prerender'
*/
frontmatterProperty?: string;
/**
* Default prerender value when property is not found
* @default undefined (no change to route.prerender)
*/
defaultValue?: boolean;
/**
* Whether to prerender all markdown files regardless of frontmatter
* @default false
*/
prerenderAll?: boolean;
}
```
### Examples
#### Enable verbose logging
```typescript
import markdownPrerender from 'astro-markdown-prerender';
export default defineConfig({
integrations: [
markdownPrerender({
verbose: true
})
],
});
```
#### Use a custom frontmatter property
```typescript
import markdownPrerender from 'astro-markdown-prerender';
export default defineConfig({
integrations: [
markdownPrerender({
frontmatterProperty: 'static'
})
],
});
```
Then in your Markdown:
```markdown
---
title: "My Page"
static: true
---
# My Page
```
#### Set a default value
```typescript
import markdownPrerender from 'astro-markdown-prerender';
export default defineConfig({
integrations: [
markdownPrerender({
defaultValue: true // All markdown files will be prerendered by default
})
],
});
```
#### Force prerender all markdown files
```typescript
import markdownPrerender from 'astro-markdown-prerender';
export default defineConfig({
integrations: [
markdownPrerender({
prerenderAll: true // All .md files will be prerendered, ignoring frontmatter
})
],
});
```
When `prerenderAll` is enabled, all markdown files will be prerendered regardless of their frontmatter values. This is useful when you want to ensure all markdown content is static without having to modify individual files.
## How it works
This integration uses the `astro:route:setup` hook to:
1. Check if the route component is a Markdown file (`.md` extension)
2. If `prerenderAll` is enabled, immediately set `route.prerender = true` and skip frontmatter parsing
3. Otherwise, parse the frontmatter using `gray-matter`
4. Look for the specified property (default: `prerender`)
5. Set `route.prerender` to the value found in frontmatter, or use `defaultValue` if specified
## Use Cases
- **Mixed rendering strategies**: Some blog posts static, others dynamic
- **Content management**: Let content creators control rendering per page
- **Performance optimization**: Prerender important pages, keep others dynamic
- **A/B testing**: Dynamically render test pages while keeping others static
- **Force static site**: Use `prerenderAll: true` to ensure all markdown content is prerendered
- **Migration scenarios**: Quickly convert all markdown to static without editing individual files
## Requirements
- Astro 4.0+ or 5.0+
- Node.js 18+
## License
MIT
## Contributing
Issues and pull requests are welcome on [GitHub](https://github.com/yourusername/astro-markdown-prerender).