forked from kentcdodds/old-kentcdodds.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogpost.js
145 lines (127 loc) · 3.67 KB
/
blogpost.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const path = require('path')
const fs = require('fs')
const util = require('util')
const jsToYaml = require('json-to-pretty-yaml')
const mkdirp = require('mkdirp')
const fakeUa = require('fake-useragent')
const opn = require('opn')
const axios = require('axios')
const slugify = require('@sindresorhus/slugify')
const inquirer = require('inquirer')
const prettier = require('prettier')
const tinify = require('tinify')
const ora = require('ora')
require('dotenv').config({
path: path.join(__dirname, '.env'),
})
const fromRoot = (...p) => path.join(__dirname, '..', ...p)
tinify.key = process.env.TINY_PNG_API_KEY
const padLeft0 = n => n.toString().padStart(2, '0')
const formatDate = d =>
`${d.getFullYear()}-${padLeft0(d.getMonth() + 1)}-${padLeft0(d.getDate())}`
const listify = a =>
a && a.trim().length
? a
.split(',')
.map(s => s.trim())
.filter(Boolean)
: null
async function generateBlogPost() {
const {title, description, categories, keywords} = await inquirer.prompt([
{
type: 'input',
name: 'title',
message: 'Title',
},
{
type: 'input',
name: 'description',
message: 'Description',
},
{
type: 'input',
name: 'categories',
message: 'Categories (comma separated)',
},
{
type: 'input',
name: 'keywords',
message: 'Keywords (comma separated)',
},
])
const slug = slugify(title)
const destination = fromRoot('content/blog', slug)
mkdirp.sync(destination)
const bannerCredit = await getBannerPhoto(title, destination)
const yaml = jsToYaml.stringify(
removeEmpty({
slug,
title,
date: formatDate(new Date()),
author: 'Kent C. Dodds',
description: `_${description}_`,
categories: listify(categories),
keywords: listify(keywords),
banner: './images/banner.jpg',
bannerCredit,
}),
)
const markdown = prettier.format(`---\n${yaml}\n---\n`, {
...require('../prettier.config'),
parser: 'mdx',
})
fs.writeFileSync(path.join(destination, 'index.mdx'), markdown)
console.log(`${destination.replace(process.cwd(), '')} is all ready for you`)
}
async function getBannerPhoto(title, destination) {
const imagesDestination = path.join(destination, 'images')
await opn(`https://unsplash.com/search/photos/${encodeURIComponent(title)}`, {
wait: false,
})
const {unsplashPhotoId} = await inquirer.prompt([
{
type: 'input',
name: 'unsplashPhotoId',
message: `What's the Unsplash Photo ID for the banner for this post?`,
},
])
mkdirp.sync(imagesDestination)
const source = tinify
.fromUrl(
`https://unsplash.com/photos/${unsplashPhotoId}/download?force=true`,
)
.resize({
method: 'scale',
width: 2070,
})
const spinner = ora('compressing the image with tinypng.com').start()
await util
.promisify(source.toFile)
.call(source, path.join(imagesDestination, 'banner.jpg'))
spinner.text = 'compressed the image with tinypng.com'
spinner.stop()
const bannerCredit = await getPhotoCredit(unsplashPhotoId)
return bannerCredit
}
async function getPhotoCredit(unsplashPhotoId) {
const response = await axios({
url: `https://unsplash.com/photos/${unsplashPhotoId}`,
headers: {'User-Agent': fakeUa()},
})
const {
groups: {name},
} = response.data.match(/Photo by (?<name>.*?) on Unsplash/) || {
groups: {name: 'Unknown'},
}
return `Photo by [${name}](https://unsplash.com/photos/${unsplashPhotoId})`
}
function removeEmpty(obj) {
return Object.entries(obj).reduce((o, [key, value]) => {
if (value) {
o[key] = value
}
return o
}, {})
}
generateBlogPost()
/* eslint no-console:0 */