-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-post.js
More file actions
executable file
·94 lines (75 loc) · 2.28 KB
/
create-post.js
File metadata and controls
executable file
·94 lines (75 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const POSTS_FILE = './posts.json';
const POSTS_DIR = './posts';
// Ensure posts directory exists
if (!fs.existsSync(POSTS_DIR)) {
fs.mkdirSync(POSTS_DIR);
}
// Load existing posts or create empty array
function loadPosts() {
if (fs.existsSync(POSTS_FILE)) {
return JSON.parse(fs.readFileSync(POSTS_FILE, 'utf8'));
}
return [];
}
// Save posts to JSON file
function savePosts(posts) {
fs.writeFileSync(POSTS_FILE, JSON.stringify(posts, null, 2));
}
// Generate URL slug from title
function generateSlug(title) {
return title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/(^-|-$)/g, '');
}
// Create new blog post
async function createPost() {
console.log('\n📝 Create New Blog Post\n');
const title = await question('Post title: ');
const excerpt = await question('Short excerpt: ');
console.log('\nEnter your post content (type "END" on a new line when finished):');
let content = '';
let line;
while ((line = await question('')) !== 'END') {
content += line + '\n';
}
const slug = generateSlug(title);
const date = new Date().toISOString().split('T')[0];
const timestamp = new Date().toISOString();
const post = {
id: Date.now(),
title,
slug,
excerpt,
content: content.trim(),
date,
timestamp
};
// Save individual post file
const postFile = path.join(POSTS_DIR, `${slug}.json`);
fs.writeFileSync(postFile, JSON.stringify(post, null, 2));
// Update posts index
const posts = loadPosts();
posts.unshift(post); // Add to beginning (newest first)
savePosts(posts);
console.log(`\n✅ Post created successfully!`);
console.log(`📄 Title: ${title}`);
console.log(`🔗 Slug: ${slug}`);
console.log(`📅 Date: ${date}`);
console.log(`📁 File: ${postFile}`);
rl.close();
}
// Helper function to promisify readline
function question(query) {
return new Promise(resolve => rl.question(query, resolve));
}
// Start the post creation process
createPost().catch(console.error);