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