-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-new-post.js
146 lines (128 loc) · 3.48 KB
/
create-new-post.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
146
const inquirer = require('inquirer');
const config = require('./gatsby-config');
const fs = require('fs');
const today = new Date();
const outputFolder = 'src/pages/';
inquirer
.prompt([
{
type: 'input',
name: 'title',
message: 'Article title:'
},
{
type: 'number',
name: 'year',
message: 'Year:',
default: today.getFullYear(),
},
{
type: 'number',
name: 'month',
message: 'Month:',
default: today.getMonth() + 1,
},
{
type: 'number',
name: 'day',
message: 'Day:',
default: today.getDate(),
},
{
type: 'list',
name: 'author',
message: 'Select author:',
choices: config.siteMetadata.authors.map(d => ({name: d.name, value: d.id})),
},
{
type: 'list',
name: 'category',
message: 'Select category:',
choices: config.siteMetadata.categories.map(d => ({name: d.name, value: d.id})),
},
{
type: 'input',
name: 'featuredImage',
message: 'Cover Photo Filename:'
},
])
.then((answers) => {
const {title, author, category, featuredImage, year} = answers;
const slug = title.replace(/\s+/g, '-').toLowerCase().replace(/[^a-z0-9-]/gi,'');
let month = answers.month;
if (month < 10) {
month = `0${month}`;
}
let day = answers.day;
if (day < 10) {
day = `0${day}`;
}
if (!fs.existsSync(`${outputFolder}${year}/`)){
fs.mkdirSync(`${outputFolder}${year}/`);
}
if (!fs.existsSync(`${outputFolder}${year}/${month}/`)){
fs.mkdirSync(`${outputFolder}${year}/${month}/`);
}
if (!fs.existsSync(`${outputFolder}${year}/${month}/${day}/`)){
fs.mkdirSync(`${outputFolder}${year}/${month}/${day}/`);
}
const dir = `${outputFolder}${year}/${month}/${day}/`;
fs.writeFileSync(`${dir}${slug}.mdx`,
`---
title: "${title}"
path: "/${year}/${month}/${day}/${slug}/"
author: "${author}"
category: "${category}"
featuredImage: "${featuredImage}"
date: ${year}-${month}-${day}
---
import { graphql } from "gatsby"
[comment]: # ( ARTICLE CONTENT BEGINS HERE )
# Hello, world!
Write the content of the article here using markdown. Do not remove anything above or below the comments. For a syntax guide, [see this page here](https://daringfireball.net/projects/markdown/syntax).
If changing the metadata, make sure the author and category match what is found in the ${"`"}gatsby-config.js${"`"} file. If changing the date, only change the file structure if it is okay if the url changes. The url is based on the file structure and file name. ${"`"}path${"`"} should reflect that.
[comment]: # ( ARTICLE CONTENT ENDS HERE )
export const pageQuery = graphql${"`"}
query {
site {
siteMetadata {
authors {
id
name
}
categories {
id
name
}
}
}
allMdx(
sort: {fields: [frontmatter___date], order: DESC}
limit: 6
filter: {frontmatter: {category: {eq: "${category}" }}}
) {
edges {
node {
frontmatter {
author
date
path
title
featuredImage
}
slug
timeToRead
excerpt(truncate: false, pruneLength: 200)
}
}
}
}
${"`"}
`);
console.log('Post created! It can be found at ');
console.log(`${dir}${slug}.mdx`);
})
.catch((error) => {
console.log(error);
console.log("Unable to run post generator");
});