mirror of
https://github.com/kremalicious/blog.git
synced 2025-01-03 18:35:07 +01:00
new photo script
This commit is contained in:
parent
d502dc3cec
commit
921f88eb35
@ -15,6 +15,7 @@
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [🎉 Features](#-features)
|
||||
- [🎆 EXIF extraction](#-exif-extraction)
|
||||
- [💰 Cryptocurrency donation via Web3/MetaMask](#-cryptocurrency-donation-via-web3metamask)
|
||||
@ -30,8 +31,8 @@
|
||||
- [🎈 Add a new post](#-add-a-new-post)
|
||||
- [🚚 Deployment](#-deployment)
|
||||
- [🏛 Licenses](#-licenses)
|
||||
- [Posts](#-posts)
|
||||
- [Photos & images](#-photos-images)
|
||||
- [Posts](#posts)
|
||||
- [Photos & images](#photos--images)
|
||||
|
||||
---
|
||||
|
||||
@ -169,6 +170,10 @@ npm run format:css
|
||||
npm run new "Hello World"
|
||||
```
|
||||
|
||||
```bash
|
||||
npm run new "Hello World" photo
|
||||
```
|
||||
|
||||
- [`scripts/new.js`](scripts/new.js)
|
||||
- [`scripts/new.md`](scripts/new.md)
|
||||
|
||||
|
9
scripts/new-photo.md
Normal file
9
scripts/new-photo.md
Normal file
@ -0,0 +1,9 @@
|
||||
---
|
||||
type: photo
|
||||
date: DATE_LONG
|
||||
|
||||
title: TITLE
|
||||
image: DATE_SHORT-SLUG.jpg
|
||||
---
|
||||
|
||||
Beep Boop.
|
@ -4,41 +4,56 @@ import slugify from 'slugify'
|
||||
import ora from 'ora'
|
||||
|
||||
const templatePath = path.join(__dirname, 'new.md')
|
||||
const templatePathPhoto = path.join(__dirname, 'new-photo.md')
|
||||
const template = fs.readFileSync(templatePath).toString()
|
||||
const templatePhoto = fs.readFileSync(templatePathPhoto).toString()
|
||||
|
||||
const spinner = ora('Adding new project').start()
|
||||
const spinner = ora('Adding new post').start()
|
||||
|
||||
if (!process.argv[2]) {
|
||||
spinner.fail('Use the format `npm run new "Title of post"`')
|
||||
}
|
||||
|
||||
const title = process.argv[2]
|
||||
const isPhoto = process.argv[3] === 'photo'
|
||||
|
||||
spinner.text = `Adding '${title}'.`
|
||||
|
||||
const titleSlug = slugify(title, { lower: true })
|
||||
const postsPath = path.join('.', 'content', 'posts')
|
||||
const photosPath = path.join('.', 'content', 'photos')
|
||||
const date = new Date().toISOString()
|
||||
const dateShort = date.slice(0, 10)
|
||||
|
||||
const newContents = template
|
||||
.split('TITLE')
|
||||
.join(title)
|
||||
.split('TITLE_SLUG')
|
||||
.split('SLUG')
|
||||
.join(titleSlug)
|
||||
.split('DATE')
|
||||
.join(date)
|
||||
|
||||
const datePath = date.slice(0, 10)
|
||||
const file = `${postsPath}/${datePath}-${titleSlug}/index.md`
|
||||
const newContentsPhoto = templatePhoto
|
||||
.split('TITLE')
|
||||
.join(title)
|
||||
.split('SLUG')
|
||||
.join(titleSlug)
|
||||
.split('DATE_LONG')
|
||||
.join(date)
|
||||
.split('DATE_SHORT')
|
||||
.join(dateShort)
|
||||
|
||||
fs.outputFile(file, newContents)
|
||||
.then(() => fs.readFile(file, 'utf8'))
|
||||
.then(() => {
|
||||
const file = `${postsPath}/${dateShort}-${titleSlug}/index.md`
|
||||
const filePhoto = `${photosPath}/${dateShort}-${titleSlug}.md`
|
||||
|
||||
if (isPhoto) {
|
||||
fs.appendFile(filePhoto, newContentsPhoto, err => {
|
||||
if (err) spinner.fail(`Error creating photo post: ${err}`)
|
||||
spinner.succeed(`New post '${title}' created.`)
|
||||
})
|
||||
.catch(err => {
|
||||
spinner.fail(`Error creating post: ${err}`)
|
||||
})
|
||||
|
||||
// fs.appendFile(`${postsPath}/${datePath}-${titleSlug}.md`, newContents, err => {
|
||||
// if (err) spinner.fail(`Error creating post: ${err}`)
|
||||
// spinner.succeed(`New post '${title}' created.`)
|
||||
// })
|
||||
} else {
|
||||
fs.outputFile(file, newContents)
|
||||
.then(() => fs.readFile(file, 'utf8'))
|
||||
.then(() => spinner.succeed(`New post '${title}' created.`))
|
||||
.catch(err => spinner.fail(`Error creating post: ${err}`))
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ type: post
|
||||
date: DATE
|
||||
|
||||
title: TITLE
|
||||
image: TITLE_SLUG-teaser.png
|
||||
image: SLUG-teaser.png
|
||||
|
||||
tags:
|
||||
- tag
|
||||
|
Loading…
Reference in New Issue
Block a user