|
| 1 | +# Blog Management Guide - Astro DB |
| 2 | + |
| 3 | +This guide explains how to add and manage blog articles in your Astro DB-powered blog. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Your blog now uses **Astro DB** instead of file-based Content Collections. Articles are stored in a SQLite database, which provides more flexibility for querying and managing content. |
| 8 | + |
| 9 | +## Adding New Articles |
| 10 | + |
| 11 | +There are **two main ways** to add articles to your database: |
| 12 | + |
| 13 | +### Method 1: Using Markdown Files + Seed Script (Recommended for Content Writers) |
| 14 | + |
| 15 | +This method is best if you prefer writing articles in markdown files. |
| 16 | + |
| 17 | +#### Steps: |
| 18 | + |
| 19 | +1. **Create a new markdown file** in `src/content/blog/`: |
| 20 | + ```bash |
| 21 | + # Example: src/content/blog/my-new-article.md |
| 22 | + ``` |
| 23 | + |
| 24 | +2. **Add frontmatter and content**: |
| 25 | + ```markdown |
| 26 | + --- |
| 27 | + title: My New Article |
| 28 | + description: A brief description of the article |
| 29 | + pubDate: 2024-01-25 |
| 30 | + updatedDate: 2024-01-26 # Optional |
| 31 | + heroImage: /path/to/image.jpg # Optional |
| 32 | + tags: ['tutorial', 'astro'] |
| 33 | + externalLink: https://example.com/original-article # Optional - link to original article |
| 34 | + --- |
| 35 | + |
| 36 | + # My New Article |
| 37 | + |
| 38 | + Your markdown content here... |
| 39 | + ``` |
| 40 | + |
| 41 | +3. **Run the seed script** to add it to the database: |
| 42 | + ```bash |
| 43 | + npx astro db execute scripts/seed.ts |
| 44 | + ``` |
| 45 | + |
| 46 | + The seed script will: |
| 47 | + - Read all `.md` files from `src/content/blog/` |
| 48 | + - Parse frontmatter and content |
| 49 | + - Insert new articles into the database |
| 50 | + - Update existing articles if they already exist (based on slug) |
| 51 | + - Automatically set `updatedDate` to current date when updating existing articles |
| 52 | + |
| 53 | +#### Advantages: |
| 54 | +- ✅ Familiar markdown workflow |
| 55 | +- ✅ Easy to version control with Git |
| 56 | +- ✅ Can edit with any markdown editor |
| 57 | +- ✅ Batch import multiple articles at once |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +### Method 2: Direct Database Insert (For Programmatic Addition) |
| 62 | + |
| 63 | +This method is best for programmatically adding articles or when you want to add articles directly without markdown files. |
| 64 | + |
| 65 | +#### Steps: |
| 66 | + |
| 67 | +1. **Edit the helper script** `db/add-article.ts`: |
| 68 | + - Modify the `articleData` object with your article information |
| 69 | + - Set the `slug`, `title`, `description`, `pubDate`, `tags`, and `body` |
| 70 | + |
| 71 | +2. **Run the script**: |
| 72 | + ```bash |
| 73 | + npx astro db execute db/add-article.ts |
| 74 | + ``` |
| 75 | + |
| 76 | +#### Example `articleData`: |
| 77 | +```typescript |
| 78 | +const articleData = { |
| 79 | + slug: 'my-new-post', |
| 80 | + title: 'My New Post', |
| 81 | + description: 'Description here', |
| 82 | + pubDate: new Date('2024-01-25'), |
| 83 | + updatedDate: null, // or new Date('2024-01-26') |
| 84 | + heroImage: null, // or '/path/to/image.jpg' |
| 85 | + tags: ['tutorial', 'astro'], |
| 86 | + body: '# My New Post\n\nContent here...', |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +#### Advantages: |
| 91 | +- ✅ Direct database insertion |
| 92 | +- ✅ Good for automated content generation |
| 93 | +- ✅ No markdown files needed |
| 94 | +- ✅ Can be integrated into CMS or admin panel |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## Article Schema |
| 99 | + |
| 100 | +Each article in the database has the following fields: |
| 101 | + |
| 102 | +| Field | Type | Required | Description | |
| 103 | +|-------|------|----------|-------------| |
| 104 | +| `slug` | text | ✅ Yes | URL-friendly identifier (primary key) | |
| 105 | +| `title` | text | ✅ Yes | Article title | |
| 106 | +| `description` | text | ✅ Yes | Brief description for SEO | |
| 107 | +| `pubDate` | date | ✅ Yes | Publication date | |
| 108 | +| `updatedDate` | date | ❌ No | Last update date (optional) | |
| 109 | +| `heroImage` | text | ❌ No | Path to hero image (optional) | |
| 110 | +| `tags` | json | ❌ No | Array of tags (optional) | |
| 111 | +| `body` | text | ✅ Yes | Markdown content | |
| 112 | +| `externalLink` | text | ❌ No | URL to original/external article (optional) | |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## Updating Existing Articles |
| 117 | + |
| 118 | +### Option 1: Update Markdown File + Re-seed (Recommended) |
| 119 | + |
| 120 | +1. Edit the markdown file in `src/content/blog/` |
| 121 | +2. (Optional) Update the `updatedDate` in frontmatter - if not provided, the seed script will automatically set it to the current date |
| 122 | +3. Run the seed script: |
| 123 | + ```bash |
| 124 | + npx astro db execute scripts/seed.ts |
| 125 | + ``` |
| 126 | + |
| 127 | +The seed script will automatically detect that the article already exists and update it with the new content from your markdown file. |
| 128 | + |
| 129 | +**Note**: The seed script now automatically updates existing articles instead of skipping them. All fields (title, description, body, tags, etc.) will be updated from the markdown file. |
| 130 | + |
| 131 | +### Option 2: Create an Update Script (For Programmatic Updates) |
| 132 | + |
| 133 | +Create `db/update-article.ts`: |
| 134 | +```typescript |
| 135 | +import { db, BlogPosts } from 'astro:db'; |
| 136 | +import { eq } from 'astro:db'; |
| 137 | + |
| 138 | +export default async function updateArticle() { |
| 139 | + await db |
| 140 | + .update(BlogPosts) |
| 141 | + .set({ |
| 142 | + title: 'Updated Title', |
| 143 | + body: 'Updated content...', |
| 144 | + updatedDate: new Date(), |
| 145 | + }) |
| 146 | + .where(eq(BlogPosts.slug, 'article-slug')); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +Run with: `npx astro db execute db/update-article.ts` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Viewing Articles |
| 155 | + |
| 156 | +Articles are automatically displayed on: |
| 157 | +- **Blog Index**: `/blog` - Lists all articles |
| 158 | +- **Individual Posts**: `/blog/[slug]` - Shows full article |
| 159 | + |
| 160 | +The pages query the database using: |
| 161 | +```typescript |
| 162 | +// Get all articles |
| 163 | +const posts = await db.select().from(BlogPosts).orderBy(desc(BlogPosts.pubDate)); |
| 164 | + |
| 165 | +// Get single article |
| 166 | +const post = await db.select().from(BlogPosts).where(eq(BlogPosts.slug, slug)); |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Database Location |
| 172 | + |
| 173 | +Astro DB uses SQLite by default. The database file is located at: |
| 174 | +- `.astro/db.sqlite` (local development) |
| 175 | +- Or configured remote database (production) |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +## Tips & Best Practices |
| 180 | + |
| 181 | +1. **Slugs**: Use URL-friendly slugs (lowercase, hyphens, no spaces) |
| 182 | + - ✅ Good: `my-awesome-post` |
| 183 | + - ❌ Bad: `My Awesome Post!` |
| 184 | + |
| 185 | +2. **Dates**: Always use proper date format in frontmatter: |
| 186 | + - ✅ Good: `pubDate: 2024-01-25` |
| 187 | + - ✅ Good: `pubDate: 2024-01-25T10:00:00Z` |
| 188 | + |
| 189 | +3. **Tags**: Use consistent tag naming (lowercase recommended) |
| 190 | + - ✅ Good: `['tutorial', 'astro', 'database']` |
| 191 | + - ❌ Bad: `['Tutorial', 'Astro', 'Database']` |
| 192 | + |
| 193 | +4. **Body Content**: Write in markdown - it will be converted to HTML automatically |
| 194 | + |
| 195 | +5. **Hero Images**: Use paths relative to `public/` folder |
| 196 | + - ✅ Good: `/images/hero.jpg` |
| 197 | + - ❌ Bad: `../images/hero.jpg` |
| 198 | + |
| 199 | +6. **External Links**: If your article is a summary or repost of an external article, include the original URL |
| 200 | + - ✅ Good: `externalLink: https://medium.com/@author/article` |
| 201 | + - ✅ Good: `externalLink: https://example.com/original-post` |
| 202 | + - The external link will be displayed as a button on both the blog listing and individual post pages |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +## Troubleshooting |
| 207 | + |
| 208 | +### Article not appearing? |
| 209 | +- Check if seed script ran successfully |
| 210 | +- Verify slug doesn't already exist |
| 211 | +- Check browser console for errors |
| 212 | +- Restart dev server: `npm run dev` |
| 213 | + |
| 214 | +### Duplicate slug error? |
| 215 | +- Each slug must be unique (it's the primary key) |
| 216 | +- Change the slug in your markdown filename or articleData |
| 217 | + |
| 218 | +### Content not rendering? |
| 219 | +- Verify markdown syntax is correct |
| 220 | +- Check that `marked` package is installed |
| 221 | +- Ensure `body` field contains valid markdown |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +## Example: Complete Workflow |
| 226 | + |
| 227 | +Here's a complete example of adding a new article: |
| 228 | + |
| 229 | +1. **Create markdown file**: `src/content/blog/my-tutorial.md` |
| 230 | +2. **Add frontmatter and content**: |
| 231 | + ```markdown |
| 232 | + --- |
| 233 | + title: My Tutorial |
| 234 | + description: Learn something new |
| 235 | + pubDate: 2024-01-25 |
| 236 | + tags: ['tutorial'] |
| 237 | + --- |
| 238 | + |
| 239 | + # My Tutorial |
| 240 | + |
| 241 | + Content here... |
| 242 | + ``` |
| 243 | +3. **Run seed script** to sync to database: |
| 244 | + ```bash |
| 245 | + npx astro db execute scripts/seed.ts |
| 246 | + ``` |
| 247 | + You should see output like: |
| 248 | + ``` |
| 249 | + Found 13 blog post(s) to sync... |
| 250 | + ✓ Added: my-tutorial |
| 251 | + Sync complete! |
| 252 | + ``` |
| 253 | +4. **Verify**: Visit `http://localhost:4321/blog/my-tutorial` |
| 254 | + |
| 255 | +That's it! Your article is now in the database and visible on your site. |
| 256 | + |
| 257 | +## Updating an Existing Article |
| 258 | + |
| 259 | +To update an article you've already added: |
| 260 | + |
| 261 | +1. **Edit the markdown file** in `src/content/blog/` |
| 262 | +2. **Make your changes** to the content |
| 263 | +3. **Optionally update `updatedDate`** in frontmatter (if omitted, it will be set automatically) |
| 264 | +4. **Run the seed script again**: |
| 265 | + ```bash |
| 266 | + npx astro db execute scripts/seed.ts |
| 267 | + ``` |
| 268 | + You should see: |
| 269 | + ``` |
| 270 | + Found 13 blog post(s) to sync... |
| 271 | + ✓ Updated: my-tutorial |
| 272 | + Sync complete! |
| 273 | + ``` |
| 274 | +5. **Refresh your browser** to see the changes |
| 275 | + |
| 276 | +The seed script automatically detects existing articles and updates them with the latest content from your markdown files. |
0 commit comments