first commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
---
|
||||
title: Example Blog Post
|
||||
excerpt: This is a brief excerpt that appears on the homepage
|
||||
---
|
||||
|
||||
This is the first paragraph of your blog post content.
|
||||
|
||||
This is the second paragraph, separated by a double newline.
|
||||
|
||||
You can write as much content as you want here, using standard markdown formatting if desired. The content will be stored as-is and displayed on the blog page.
|
||||
Executable
+101
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
* Add a new blog post from a markdown file
|
||||
* Usage: php add-blog-post.php post.md
|
||||
*
|
||||
* Markdown format:
|
||||
* ---
|
||||
* title: Your Post Title
|
||||
* excerpt: Brief excerpt for homepage
|
||||
* ---
|
||||
*
|
||||
* Your full blog post content here...
|
||||
*
|
||||
* Use double newlines for paragraphs.
|
||||
*/
|
||||
|
||||
$blogFile = __DIR__ . '/../data/posts.json';
|
||||
|
||||
if ($argc < 2) {
|
||||
echo "Usage: php add-blog-post.php post.md\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$markdownFile = $argv[1];
|
||||
|
||||
if (!file_exists($markdownFile)) {
|
||||
echo "Error: File '$markdownFile' not found\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$content = file_get_contents($markdownFile);
|
||||
|
||||
// Parse frontmatter
|
||||
if (!preg_match('/^---\s*\n(.*?)\n---\s*\n(.*)$/s', $content, $matches)) {
|
||||
echo "Error: Invalid markdown format. Expected frontmatter with --- delimiters.\n";
|
||||
echo "Format:\n---\ntitle: Your Title\nexcerpt: Your excerpt\n---\n\nContent...\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$frontmatter = $matches[1];
|
||||
$postContent = trim($matches[2]);
|
||||
|
||||
// Parse frontmatter fields
|
||||
$title = null;
|
||||
$excerpt = null;
|
||||
|
||||
foreach (explode("\n", $frontmatter) as $line) {
|
||||
if (preg_match('/^title:\s*(.+)$/i', trim($line), $m)) {
|
||||
$title = trim($m[1]);
|
||||
} elseif (preg_match('/^excerpt:\s*(.+)$/i', trim($line), $m)) {
|
||||
$excerpt = trim($m[1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$title || !$excerpt) {
|
||||
echo "Error: Missing required fields. Need 'title:' and 'excerpt:' in frontmatter.\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (empty($postContent)) {
|
||||
echo "Error: Post content is required\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Load existing posts
|
||||
$posts = [];
|
||||
if (file_exists($blogFile)) {
|
||||
$json = file_get_contents($blogFile);
|
||||
$posts = json_decode($json, true) ?: [];
|
||||
}
|
||||
|
||||
// Get next ID
|
||||
$nextId = 0;
|
||||
if (!empty($posts)) {
|
||||
$ids = array_column($posts, 'id');
|
||||
$nextId = max($ids) + 1;
|
||||
}
|
||||
|
||||
// Get today's date
|
||||
$date = date('Y-m-d');
|
||||
|
||||
// Create new post
|
||||
$newPost = [
|
||||
'id' => $nextId,
|
||||
'title' => $title,
|
||||
'date' => $date,
|
||||
'excerpt' => $excerpt,
|
||||
'content' => $postContent
|
||||
];
|
||||
|
||||
// Add to array
|
||||
$posts[] = $newPost;
|
||||
|
||||
// Write back to file
|
||||
file_put_contents($blogFile, json_encode($posts, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n");
|
||||
|
||||
echo "Blog post added successfully!\n";
|
||||
echo "ID: $nextId\n";
|
||||
echo "Date: $date\n";
|
||||
echo "Title: $title\n";
|
||||
Reference in New Issue
Block a user