astrocms 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/README.md +106 -16
  2. package/backend/parsers/components.ts +4 -4
  3. package/backend/parsers/frontmatter.ts +8 -28
  4. package/backend/routes/claude.ts +3 -6
  5. package/dist/_cms/{index-9o-llXDY.js → index--BZJNKbj.js} +114 -114
  6. package/dist/_cms/{index-Ce64khPN.js → index-AcL57xZ_.js} +1 -1
  7. package/dist/_cms/{index-wHleENT0.js → index-BkfBVCds.js} +1 -1
  8. package/dist/_cms/{index-DAAZmIPw.js → index-C-FkFQiR.js} +1 -1
  9. package/dist/_cms/{index-Bvz8WTJL.js → index-CYE1F8io.js} +1 -1
  10. package/dist/_cms/{index-M4bKZFGh.js → index-Cc1XfOk5.js} +1 -1
  11. package/dist/_cms/{index-DM5GktYD.js → index-CzHG20cy.js} +1 -1
  12. package/dist/_cms/{index-DQf5bTbe.js → index-DOj3riRn.js} +1 -1
  13. package/dist/_cms/{index-7Pc6H4sv.js → index-DYLQOWH5.js} +1 -1
  14. package/dist/_cms/{index-enNvjuOI.js → index-DizxtYKi.js} +1 -1
  15. package/dist/_cms/{index-BdC39RoS.js → index-QanI40bh.js} +1 -1
  16. package/dist/_cms/{index-CoIA6zkf.js → index-T_P_b0mL.js} +1 -1
  17. package/dist/_cms/{index-893Rzaov.js → index-UyGAnl2N.js} +1 -1
  18. package/dist/_cms/{index-C2vDuCBy.js → index-ZE3ch6VY.js} +1 -1
  19. package/dist/_cms/{index-BdChuKfE.js → index-_dNFOYm2.js} +1 -1
  20. package/dist/_cms/{index-Bpfn86Of.js → index-q0t8g2gT.js} +1 -1
  21. package/dist/_cms/{index-Cdwq3Ev5.js → index-q9XyrmEJ.js} +1 -1
  22. package/dist/_cms/{index-C9zUJLja.css → index-saIztkFM.css} +1 -1
  23. package/dist/_cms/{index-NwHME3i3.js → index-xUvntAv0.js} +1 -1
  24. package/dist/index.html +2 -2
  25. package/package.json +2 -1
  26. package/shared/frontmatter.ts +78 -0
package/README.md CHANGED
@@ -6,7 +6,7 @@ AstroCMS edits your Markdown and MDX files directly, manages media assets, commi
6
6
 
7
7
  ## Why AstroCMS
8
8
 
9
- - **No database.** Your content lives in your repo as Markdown/MDX — AstroCMS just edits the files.
9
+ - **No database.** Your content lives in your repo as Markdown/MDX, and AstroCMS just edits the files.
10
10
  - **Git-native workflow.** Changes are staged, committed, and pushed to GitHub directly from the UI.
11
11
  - **Schema-aware.** Frontmatter forms are auto-generated from the Zod schemas in your `content.config.ts`.
12
12
  - **MDX-ready.** Edit rich MDX visually, including your own Astro components with typed props and slots.
@@ -16,9 +16,9 @@ AstroCMS edits your Markdown and MDX files directly, manages media assets, commi
16
16
 
17
17
  - **Visual MDX editor** with a frontmatter form generated from your Zod collection schemas
18
18
  - **File tree browser** for all Astro content collections
19
- - **Astro component discovery** — insert your own components into MDX with prop and slot editing
19
+ - **Astro component discovery** for inserting your own components into MDX with prop and slot editing
20
20
  - **Media upload** to your configured assets directory
21
- - **Git integration** — status, diff, stage, commit, and push without leaving the browser
21
+ - **Git integration** with status, diff, stage, commit, and push without leaving the browser
22
22
  - **Claude Code agent** (optional) for AI-assisted writing and editing
23
23
  - **Password protection** for deployed instances
24
24
 
@@ -31,7 +31,7 @@ AstroCMS edits your Markdown and MDX files directly, manages media assets, commi
31
31
 
32
32
  Setup takes two steps. Really.
33
33
 
34
- ### Option 1 — as a project dependency
34
+ ### Option 1, as a project dependency
35
35
 
36
36
  Install it, then add a script:
37
37
 
@@ -53,7 +53,7 @@ Run it from your project:
53
53
  npm run astrocms
54
54
  ```
55
55
 
56
- ### Option 2 — global install
56
+ ### Option 2, global install
57
57
 
58
58
  Install once, use on any Astro project:
59
59
 
@@ -120,27 +120,117 @@ Deployment-only environment variables:
120
120
 
121
121
  A `.env` file at the project root is auto-loaded when running `astrocms` locally. Shell variables take precedence over `.env` values.
122
122
 
123
+ ## Best practices
124
+
125
+ ### Pass components to `<Content />` rather than importing them in MDX
126
+
127
+ MDX lets you `import` Astro components directly inside a `.mdx` file. It works, but **AstroCMS hides import statements in the visual editor** to keep MDX authoring WYSIWYG. If a page relies on imports inside the MDX, editors will still see the components but will not be able to manage or remove the imports from the editor UI.
128
+
129
+ The recommended pattern is to keep MDX files import-free and pass components through Astro's `<Content components={...} />` prop from the consuming page, like [`example/src/pages/[lang]/blog/[slug].astro`](./example/src/pages/%5Blang%5D/blog/%5Bslug%5D.astro):
130
+
131
+ ```astro
132
+ ---
133
+ import { render } from 'astro:content'
134
+ import { mdxComponents } from '../../../components'
135
+ // ...
136
+ const { Content } = await render(post)
137
+ ---
138
+
139
+ <Content components={mdxComponents} />
140
+ ```
141
+
142
+ ### Auto-register every component in `componentsDir`
143
+
144
+ To keep the component list in sync with `ASTROCMS_COMPONENTS_DIR` (what the CMS shows in the MDX editor) without maintaining it by hand, use Vite's `import.meta.glob` to build the map automatically. See [`example/src/components/index.ts`](./example/src/components/index.ts):
145
+
146
+ ```ts
147
+ const modules = import.meta.glob('./**/*.astro', { eager: true })
148
+
149
+ export const mdxComponents: Record<string, any> = Object.fromEntries(
150
+ Object.entries(modules).map(([path, mod]) => [
151
+ path.split('/').pop()!.replace('.astro', ''),
152
+ (mod as any).default,
153
+ ])
154
+ )
155
+ ```
156
+
157
+ This way, any `.astro` file you drop into the components directory becomes available in MDX at both build time and in the CMS editor, with nothing to wire up.
158
+
123
159
  ## Deploying with Docker
124
160
 
125
- Use the provided Docker image to clone and serve a git-hosted Astro project:
161
+ AstroCMS publishes a prebuilt image to GitHub Container Registry on every release: `ghcr.io/lonestone/astrocms`. It clones a git-hosted Astro project into a persistent volume and serves the CMS.
162
+
163
+ Available tags:
164
+
165
+ - `latest`: most recent release
166
+ - `X.Y.Z`: a specific version (e.g. `0.1.2`), pinned for reproducibility
167
+ - `X.Y`: latest patch of a minor line (e.g. `0.1`)
168
+
169
+ ### Quickstart with `docker run`
126
170
 
127
171
  ```bash
128
- docker compose up
172
+ docker run -d \
173
+ --name astrocms \
174
+ -p 4001:4001 \
175
+ -e GIT_REPO_URL=https://github.com/user/repo \
176
+ -e GIT_PAT=github_pat_xxx \
177
+ -e ASTROCMS_PASSWORD=secret \
178
+ -v astrocms-data:/data \
179
+ ghcr.io/lonestone/astrocms:latest
129
180
  ```
130
181
 
131
- Configure via `.env` (see `.env.example`):
132
-
133
- ```env
134
- GIT_REPO_URL=https://github.com/user/repo
135
- GIT_BRANCH=main
136
- GIT_PAT=ghp_xxx
137
- ASTROCMS_PASSWORD=secret
182
+ ### With `docker compose`
183
+
184
+ ```yaml
185
+ services:
186
+ cms:
187
+ image: ghcr.io/lonestone/astrocms:latest
188
+ ports:
189
+ - '4001:4001'
190
+ environment:
191
+ - GIT_REPO_URL=${GIT_REPO_URL}
192
+ - GIT_PAT=${GIT_PAT}
193
+ - ASTROCMS_PASSWORD=${ASTROCMS_PASSWORD}
194
+ volumes:
195
+ - astrocms-data:/data
196
+ restart: unless-stopped
197
+
198
+ volumes:
199
+ astrocms-data:
138
200
  ```
139
201
 
140
- The `app-data` volume is persistent: if a clone already exists at startup, it is reused instead of re-cloned. The container always serves on port `4001` internally; change the host-side mapping in `docker-compose.yml` to expose it elsewhere.
202
+ All config variables from the table above (`ASTROCMS_CONTENT_DIR`, `ASTROCMS_DEV_CMD`, etc.) can be passed through the same way.
203
+
204
+ The container uses a single persistent volume at `/data`. Inside, `/data/app` holds the cloned project (reused and fast-forwarded on restart instead of re-cloned) and `/data/claude` persists the Claude Code login across restarts. Both paths are symlinked into `/app` and `/root/.claude` at startup. The container always serves on port `4001` internally; change the host-side mapping to expose it elsewhere.
141
205
 
142
206
  The CMS UI and its API live under `/astrocms`. Visit `http://localhost:4001/astrocms` to open it.
143
207
 
208
+ ### Stateless mode (no persistent volume)
209
+
210
+ Drop the volume mount to make the container fully disposable. Every time it starts, it clones the repo fresh and the Claude login is reset:
211
+
212
+ ```yaml
213
+ services:
214
+ cms:
215
+ image: ghcr.io/lonestone/astrocms:latest
216
+ ports:
217
+ - '4001:4001'
218
+ environment:
219
+ - GIT_REPO_URL=${GIT_REPO_URL}
220
+ - GIT_PAT=${GIT_PAT}
221
+ - ASTROCMS_PASSWORD=${ASTROCMS_PASSWORD}
222
+ restart: unless-stopped
223
+ ```
224
+
225
+ Pick this mode for Kubernetes deployments where pods are interchangeable, for preview environments that spin up per branch, or any setup where you want a clean state on every restart.
226
+
227
+ Things to know before using it:
228
+
229
+ - **Slower startup.** The repo is cloned and dependencies reinstalled on every container start (expect anywhere from 10 seconds to a few minutes depending on repo size and dependency count).
230
+ - **Local commits must be pushed before the container stops.** Anything committed in the CMS but not yet pushed is lost when the container is recreated. The "Commit and push" button in the CMS is the only safe workflow.
231
+ - **Claude Code re-login on every start.** Without the persistent `/data` volume, you have to re-authenticate Claude Code each time the container comes up.
232
+ - **Bandwidth and rate limits.** Frequent restarts mean frequent clones; on large repos this can hit GitHub's rate limits or slow cold starts noticeably.
233
+
144
234
  ### Serving the website alongside the CMS
145
235
 
146
236
  Set `ASTROCMS_DEV_CMD` to run the website's own dev server inside the container. AstroCMS then proxies every request outside `/astrocms` to the website, so both live at the same origin:
@@ -161,7 +251,7 @@ With that, `http://localhost:4001/` serves the live site and `http://localhost:4
161
251
  3. **Repository permissions**:
162
252
  - **Contents**: `Read and write` (required for clone, pull, push)
163
253
  - **Metadata**: `Read-only` (selected automatically)
164
- 4. Click **Generate token** and copy the value — it's shown only once.
254
+ 4. Click **Generate token** and copy the value, which is shown only once.
165
255
  5. Paste it into `.env` as `GIT_PAT=github_pat_...`.
166
256
 
167
257
  No other permissions are needed.
@@ -3,6 +3,7 @@ import { join } from 'path'
3
3
  import * as ts from 'typescript'
4
4
  import { ROOT_DIR } from '../root.js'
5
5
  import { loadConfig } from '../config.js'
6
+ import { extractRawFrontmatter } from '../../shared/frontmatter.js'
6
7
 
7
8
  export interface PropSchema {
8
9
  name: string
@@ -115,8 +116,7 @@ function resolveType(
115
116
  // Detects <slot> tags in the template and Astro.slots.render/has calls in
116
117
  // the frontmatter. Empty string "" represents the default (unnamed) slot.
117
118
  function parseSlots(source: string): string[] {
118
- const fmMatch = source.match(/^---\n([\s\S]*?)\n---/)
119
- const frontmatter = fmMatch ? fmMatch[1] : ''
119
+ const frontmatter = extractRawFrontmatter(source) ?? ''
120
120
  const parts = source.split('---')
121
121
  const template = parts.slice(2).join('---')
122
122
 
@@ -147,8 +147,8 @@ function parseComponent(source: string): {
147
147
  props: PropSchema[]
148
148
  slots: string[]
149
149
  } {
150
- const fmMatch = source.match(/^---\n([\s\S]*?)\n---/)
151
- const props = fmMatch ? parseProps(fmMatch[1]) : []
150
+ const frontmatter = extractRawFrontmatter(source)
151
+ const props = frontmatter ? parseProps(frontmatter) : []
152
152
  const slots = parseSlots(source)
153
153
  return { props, slots }
154
154
  }
@@ -1,24 +1,17 @@
1
1
  import { readFile, stat } from 'fs/promises'
2
2
  import { extname } from 'path'
3
- import YAML from 'yaml'
3
+ import {
4
+ parseFileFrontmatter,
5
+ type FrontmatterData,
6
+ } from '../../shared/frontmatter.js'
4
7
 
5
- const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---/
6
-
7
- const cache = new Map<
8
- string,
9
- { mtimeMs: number; frontmatter: Record<string, unknown> }
10
- >()
8
+ const cache = new Map<string, { mtimeMs: number; frontmatter: FrontmatterData }>()
11
9
 
12
10
  /**
13
11
  * Read & parse frontmatter for a supported content file.
14
12
  * Cached in-memory; re-parsed only when the file's mtime changes.
15
- *
16
- * For .md/.mdx: parses the YAML frontmatter block.
17
- * For .yaml/.yml/.json: parses the whole document as frontmatter.
18
13
  */
19
- export async function getFrontmatter(
20
- absPath: string
21
- ): Promise<Record<string, unknown>> {
14
+ export async function getFrontmatter(absPath: string): Promise<FrontmatterData> {
22
15
  let mtimeMs: number
23
16
  try {
24
17
  const s = await stat(absPath)
@@ -30,23 +23,10 @@ export async function getFrontmatter(
30
23
  const cached = cache.get(absPath)
31
24
  if (cached && cached.mtimeMs === mtimeMs) return cached.frontmatter
32
25
 
33
- const ext = extname(absPath)
34
- let frontmatter: Record<string, unknown> = {}
26
+ let frontmatter: FrontmatterData = {}
35
27
  try {
36
28
  const content = await readFile(absPath, 'utf-8')
37
- if (ext === '.md' || ext === '.mdx') {
38
- const m = content.match(FRONTMATTER_RE)
39
- if (m) {
40
- const parsed = YAML.parse(m[1])
41
- if (parsed && typeof parsed === 'object') frontmatter = parsed
42
- }
43
- } else if (ext === '.yaml' || ext === '.yml') {
44
- const parsed = YAML.parse(content)
45
- if (parsed && typeof parsed === 'object') frontmatter = parsed
46
- } else if (ext === '.json') {
47
- const parsed = JSON.parse(content)
48
- if (parsed && typeof parsed === 'object') frontmatter = parsed
49
- }
29
+ frontmatter = parseFileFrontmatter(content, extname(absPath))
50
30
  } catch {
51
31
  frontmatter = {}
52
32
  }
@@ -95,10 +95,7 @@ claudeRoutes.get('/status', async (c) => {
95
95
  statusCache &&
96
96
  Date.now() - statusCache.timestamp < STATUS_CACHE_TTL
97
97
  ) {
98
- return c.json(
99
- statusCache.result,
100
- statusCache.result.authenticated ? 200 : 401
101
- )
98
+ return c.json(statusCache.result)
102
99
  }
103
100
 
104
101
  const abortController = new AbortController()
@@ -130,7 +127,7 @@ claudeRoutes.get('/status', async (c) => {
130
127
  account,
131
128
  }
132
129
  statusCache = { result, timestamp: Date.now() }
133
- return c.json(result, 401)
130
+ return c.json(result)
134
131
  }
135
132
 
136
133
  const result = { authenticated: true as const, account }
@@ -143,7 +140,7 @@ claudeRoutes.get('/status', async (c) => {
143
140
  error instanceof Error ? error.message : 'Authentication required'
144
141
  const result = { authenticated: false as const, error: message }
145
142
  statusCache = { result, timestamp: Date.now() }
146
- return c.json(result, 401)
143
+ return c.json(result)
147
144
  }
148
145
  })
149
146