Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/trueberryless/the-thinking-hub. The central knowledge hub for 8 key Computer Science thinking modes. the-thinking-hub.felixs.dev
Something went wrong. Try again.
1.7 kB · 55 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556/** * Create a new post with frontmatter * Usage: pnpm new <title> */
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'import { dirname, join } from 'node:path'import process from 'node:process'
// Process title from all argumentsconst titleArgs: string[] = process.argv.slice(2)const rawTitle: string = titleArgs.length > 0 ? titleArgs.join(' ') : 'new-post'
// Check if title starts with underscore (draft post)const isDraft: boolean = rawTitle.startsWith('_')const displayTitle: string = isDraft ? rawTitle.slice(1) : rawTitle
const fileName: string = rawTitle .toLowerCase() .replace(/[^a-z0-9\s-_]/g, '') // Remove special characters but keep underscore and hyphen .replace(/\s+/g, '-') // Replace spaces with hyphens .replace(/-+/g, '-') // Replace multiple hyphens with single .replace(/^-|-$/g, '') // Remove leading/trailing hyphensconst targetFile: string = `${fileName}.md`const fullPath: string = join('src/content/posts', targetFile)
// Check if the target file already existsif (existsSync(fullPath)) { console.error(`😇 File already exists: ${fullPath}`) process.exit(1)}
// Ensure the directory structure existsmkdirSync(dirname(fullPath), { recursive: true })
// Generate frontmatter with current dateconst content: string = `---title: ${displayTitle}pubDate: '${new Date().toISOString().split('T')[0]}'---
`
// Write the new post filetry { writeFileSync(fullPath, content) if (isDraft) { console.log(`📝 Draft created: ${fullPath}`) } else { console.log(`✅ Post created: ${fullPath}`) }} catch (error) { console.error('⚠️ Failed to create post:', error) process.exit(1)}