Skip to content

Commit b5d9bd4

Browse files
marc0oloclaude
andauthored
docs: replace prepare-docs.sh with rehype plugin and frontmatter (#435)
* docs: replace prepare-docs.sh with rehype plugin and frontmatter Eliminate the .docs-temp preprocessing pipeline by: - Adding a rehype plugin (rehype-rewrite-links.mjs) that rewrites .md links at build time, replacing the fragile sed-based approach - Adding minimal frontmatter (title + description) to all 31 doc files and removing H1 headings (Starlight renders title as H1) - Pointing the content loader directly at ../docs/ with glob exclusions - Adding a global Banner component override for the feedback banner - Removing prepare-docs.sh and all references to .docs-temp Closes #423 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: preserve frontmatter in generate-cli-docs.sh The CLI docs generation script overwrites cli.md from scratch. Update it to prepend the required YAML frontmatter (title + description) before the auto-generated content. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: strip H1 from generated CLI docs to match frontmatter title The --markdown-help output includes a `# Command-Line Help` H1 heading. Since we now use frontmatter title (which Starlight renders as H1), strip the generated H1 to avoid duplication and match the committed file. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent aff6026 commit b5d9bd4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+490
-403
lines changed

.claude/docs-guidelines.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Documentation follows the Diátaxis framework:
4242
Source documentation in `docs/` must work in two contexts:
4343

4444
1. **GitHub**: Renders Markdown directly with `.md` extensions
45-
2. **Starlight docs site**: `scripts/prepare-docs.sh` transforms links to clean URLs
45+
2. **Starlight docs site**: A rehype plugin (`docs-site/plugins/rehype-rewrite-links.mjs`) transforms links to clean URLs at build time
4646

4747
**Link format rules:**
4848

@@ -62,4 +62,4 @@ Source documentation in `docs/` must work in two contexts:
6262
[root-level link](../tutorial.md)
6363
```
6464

65-
The `prepare-docs.sh` script handles the transformation to Starlight's URL structure. If you add new link patterns, verify they work by building the docs site locally with `cd docs-site && npm run build`.
65+
The rehype plugin handles the transformation to Starlight's URL structure at build time. If you add new link patterns, verify they work by building the docs site locally with `cd docs-site && npm run build`.

.github/CONTRIBUTING.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,12 @@ Documentation follows the [Diátaxis framework](https://diataxis.fr/):
9494

9595
The documentation site uses [Astro](https://astro.build/) with [Starlight](https://starlight.astro.build/):
9696

97-
1. **Source files** (`docs/`) are plain Markdown without frontmatter
98-
2. **Build script** (`scripts/prepare-docs.sh`) runs before each build and:
99-
- Copies docs to `.docs-temp/` directory (excluding schemas and READMEs)
100-
- Adjusts relative paths and strips `.md` extensions for Starlight's clean URLs
101-
- Extracts titles from H1 headings and adds frontmatter
102-
3. **Starlight** reads from `.docs-temp/` and builds the site
97+
1. **Source files** (`docs/`) are Markdown with minimal YAML frontmatter (title + description)
98+
2. **Starlight** reads directly from `docs/` via the glob content loader
99+
3. **Rehype plugin** (`docs-site/plugins/rehype-rewrite-links.mjs`) rewrites `.md` links at build time for Starlight's clean URLs
103100
4. **GitHub Actions** automatically deploys to GitHub Pages on push to main
104101

105-
This architecture keeps source docs clean and GitHub-friendly while providing a polished documentation site.
102+
This architecture keeps source docs GitHub-friendly (`.md` links work on GitHub) while producing clean URLs on the documentation site.
106103

107104
### Versioned Documentation Deployment
108105

@@ -194,7 +191,7 @@ When submitting a documentation PR:
194191
- Check that all links work (both in GitHub and on the site)
195192
- Follow the Diátaxis framework for placing content in the right section
196193
- Verify your examples work by testing them
197-
- Run `./scripts/prepare-docs.sh` locally to check for build errors
194+
- Run `npm run build` in `docs-site/` locally to check for build errors
198195

199196
For more details on the documentation system, see:
200197
- [docs/README.md](../docs/README.md) - Documentation writing guide

.github/workflows/docs.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ on:
1010
paths:
1111
- 'docs/**'
1212
- 'docs-site/**'
13-
- 'scripts/prepare-docs.sh'
1413
- '.github/workflows/docs.yml'
1514
pull_request:
1615
paths:
1716
- 'docs/**'
1817
- 'docs-site/**'
19-
- 'scripts/prepare-docs.sh'
2018
- '.github/workflows/docs.yml'
2119
workflow_dispatch:
2220

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ docs-site/.astro/
1919
docs-site/dist/
2020
docs-site/dist-test/
2121
docs-site/node_modules/
22-
docs-site/.docs-temp/

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ cargo fmt && cargo clippy
107107
# Preview documentation site locally
108108
cd docs-site && npm install && npm run dev
109109
# Opens at http://localhost:4321
110-
111-
# Prepare docs for build (runs automatically during build)
112-
./scripts/prepare-docs.sh
113110
```
114111

115112
Documentation structure follows the [Diátaxis framework](https://diataxis.fr/):

docs-site/README.md

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,42 @@ This directory contains the Starlight-based documentation website for ICP CLI.
44

55
## Overview
66

7-
The documentation site is built with [Astro](https://astro.build/) and [Starlight](https://starlight.astro.build/), reading markdown files from the `../docs/` directory.
7+
The documentation site is built with [Astro](https://astro.build/) and [Starlight](https://starlight.astro.build/), reading markdown files directly from the `../docs/` directory.
88

99
## Architecture
1010

1111
```
1212
docs-site/
13-
├── astro.config.mjs # Starlight configuration (sidebar, theme)
13+
├── astro.config.mjs # Starlight configuration (sidebar, theme)
14+
├── plugins/
15+
│ └── rehype-rewrite-links.mjs # Rewrites .md links for Starlight's clean URLs
1416
├── src/
1517
│ ├── content.config.ts # Content loader configuration
18+
│ ├── components/ # Custom Starlight component overrides
1619
│ ├── assets/ # Logo and static assets
1720
│ └── styles/ # DFINITY theme CSS
18-
├── public/ # Static files (favicon, etc.)
19-
└── package.json # Dependencies and scripts
21+
├── public/ # Static files (favicon, etc.)
22+
└── package.json # Dependencies and scripts
2023
```
2124

2225
## Key Features
2326

2427
### Content Loading
25-
- Uses Astro's `glob` loader to read from `../docs/` via a temporary `.docs-temp/` directory
26-
- Source docs remain plain Markdown (GitHub-friendly)
27-
- Build process adds frontmatter and processes links automatically
28+
- Uses Astro's `glob` loader to read directly from `../docs/` (excluding `schemas/` and README files)
29+
- Source docs use minimal YAML frontmatter (`title` + `description`)
30+
- A rehype plugin rewrites `.md` links at build time for Starlight's clean URLs
2831

2932
### Build Pipeline
3033

31-
The build pipeline ensures source documentation remains clean while producing a polished site:
34+
1. **Starlight** reads content directly from `../docs/` via the glob content loader
35+
2. **Rehype plugin** (`plugins/rehype-rewrite-links.mjs`) strips `.md` extensions from relative links and adjusts paths for Astro's directory-based output
36+
3. **DFINITY theme CSS** is applied for consistent branding
37+
4. **Static HTML** is produced in `dist/`
3238

33-
**Step 1: Prepare Docs** (`../scripts/prepare-docs.sh`)
34-
- Copies `../docs/` to `.docs-temp/` (excluding `schemas/` directory and README files)
35-
- Adjusts relative paths and strips `.md` extensions for Starlight's clean URLs
36-
- Source files use `.md` extensions (work on GitHub)
37-
- Build transforms to clean URLs without `.md` (work on site)
38-
- Extracts page title from first H1 heading
39-
- Adds YAML frontmatter with the title
40-
- Removes the H1 heading from content (prevents duplicate titles)
41-
42-
**Step 2: Starlight Build**
43-
- Reads content from `.docs-temp/` via glob loader
44-
- Applies DFINITY theme CSS
45-
- Generates navigation from manual sidebar configuration
46-
- Produces static HTML in `dist/`
47-
48-
**Why this approach?**
49-
- Source docs stay plain Markdown (GitHub-friendly, no framework lock-in)
50-
- Build-time transformations keep things DRY (single source of truth)
51-
- Cross-platform compatibility (works on macOS and Linux)
39+
Source docs use `.md` extensions in links (GitHub-friendly), and the rehype plugin transforms them to clean URLs at build time.
5240

5341
### Styling
54-
- Custom CSS copied from `icp-js-sdk-docs` for DFINITY branding
42+
- Custom CSS for DFINITY branding
5543
- Files: `layers.css`, `theme.css`, `overrides.css`, `elements.css`
5644
- Maintains consistent look with other DFINITY documentation sites
5745

@@ -60,6 +48,10 @@ The build pipeline ensures source documentation remains clean while producing a
6048
- Implemented via `rehype-external-links` plugin for content links
6149
- Custom script in `astro.config.mjs` handles social/header links
6250

51+
### Global Banner
52+
- A feedback banner is shown on every page via a custom `Banner` component override (`src/components/Banner.astro`)
53+
- No per-page frontmatter needed — the component renders the same banner globally
54+
6355
### Navigation
6456
- Sidebar is **manually configured** in `astro.config.mjs`
6557
- This is required because Starlight's autogenerate doesn't work with glob loaders
@@ -93,15 +85,14 @@ npm run preview
9385
```bash
9486
npm run clean
9587
```
96-
Removes `.docs-temp/`, `dist/`, and `.astro/` directories
88+
Removes `dist/` and `.astro/` directories
9789

9890
## Scripts
9991

100-
- `prepare-docs` - Runs `../scripts/prepare-docs.sh` to prepare documentation files
101-
- `dev` - Cleans artifacts, prepares docs, and starts development server
102-
- `build` - Prepares docs and builds for production
92+
- `dev` - Cleans artifacts and starts development server
93+
- `build` - Builds for production
10394
- `preview` - Previews production build locally
104-
- `clean` - Removes build artifacts (`.docs-temp/`, `dist/`, `.astro/`)
95+
- `clean` - Removes build artifacts (`dist/`, `.astro/`)
10596

10697
## Deployment
10798

@@ -112,7 +103,7 @@ The site is automatically deployed to GitHub Pages:
112103

113104
The workflow:
114105
1. Installs dependencies
115-
2. Runs `npm run build` (which runs `prepare-docs.sh`)
106+
2. Runs `npm run build`
116107
3. Uploads the `dist/` directory as a GitHub Pages artifact
117108
4. Deploys to GitHub Pages
118109

@@ -126,7 +117,7 @@ In `astro.config.mjs`:
126117
- `logo`: ICP logo configuration
127118
- `favicon`: Site favicon
128119
- `customCss`: DFINITY theme files
129-
- `markdown.rehypePlugins`: External link handling with `rehype-external-links`
120+
- `markdown.rehypePlugins`: Link rewriting and external link handling
130121

131122
### Sidebar Configuration
132123
Manual sidebar definition in `astro.config.mjs`:
@@ -148,8 +139,8 @@ The `slug` should match the file path relative to `docs/` without the `.md` exte
148139
## Adding New Pages
149140

150141
1. Create a `.md` file in `../docs/` in the appropriate directory
151-
2. Start with a `# Heading` (used as page title)
152-
3. Write standard Markdown content
142+
2. Add YAML frontmatter with `title` and `description`
143+
3. Write standard Markdown content (no H1 heading — Starlight renders the title)
153144
4. Add the page to the sidebar in `astro.config.mjs`:
154145
```js
155146
{
@@ -166,21 +157,20 @@ The `slug` should match the file path relative to `docs/` without the `.md` exte
166157
### Sidebar shows no pages
167158
Check that:
168159
- The file exists in `../docs/` with correct path
160+
- The file has YAML frontmatter with at least a `title` field
169161
- The slug in `astro.config.mjs` matches the file path (without `.md`)
170162
- You ran `npm run dev` to trigger the build process
171163

172164
### Duplicate page titles
173165
Check that:
174-
- The source file in `../docs/` has only one H1 heading
175-
- The `prepare-docs.sh` script is running correctly
166+
- The source file in `../docs/` does not have an H1 heading (the `title` frontmatter is rendered as H1 by Starlight)
176167

177168
### Broken links
178169
- Use relative links with `.md` extension in source docs: `[text](./file.md)`
179-
- The build process (`prepare-docs.sh`) adjusts paths and strips `.md` extensions
170+
- The rehype plugin (`plugins/rehype-rewrite-links.mjs`) strips `.md` extensions and adjusts paths at build time
180171
- External links should use full URLs
181172

182173
## Notes
183174

184-
- The `.docs-temp/` directory is generated at build time and should not be committed
185-
- Source documentation in `../docs/` should never have frontmatter
175+
- Source documentation in `../docs/` uses minimal YAML frontmatter (`title` + `description`)
186176
- The `schemas/` directory is excluded from the docs site (served via GitHub raw URLs)

docs-site/astro.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from 'astro/config';
22
import starlight from '@astrojs/starlight';
33
import rehypeExternalLinks from 'rehype-external-links';
4+
import rehypeRewriteLinks from './plugins/rehype-rewrite-links.mjs';
45

56
// https://astro.build/config
67
export default defineConfig({
@@ -11,6 +12,8 @@ export default defineConfig({
1112
base: process.env.PUBLIC_BASE_PATH || (process.env.NODE_ENV === 'production' ? process.env.PUBLIC_BASE_PREFIX + '/' : '/'),
1213
markdown: {
1314
rehypePlugins: [
15+
// Rewrite relative .md links for Astro's directory-based output
16+
rehypeRewriteLinks,
1417
// Open external links in new tab
1518
[rehypeExternalLinks, { target: '_blank', rel: ['noopener', 'noreferrer'] }],
1619
],
@@ -22,6 +25,7 @@ export default defineConfig({
2225
favicon: '/favicon.png',
2326
components: {
2427
SiteTitle: './src/components/SiteTitle.astro',
28+
Banner: './src/components/Banner.astro',
2529
},
2630
head: [
2731
{

0 commit comments

Comments
 (0)