feat: update compose to support new frontmatter options
This commit is contained in:
@ -1,31 +1,121 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const inquirer = require('inquirer')
|
||||
const dedent = require('dedent')
|
||||
|
||||
const args = process.argv.slice(2)
|
||||
const title = args[0]
|
||||
const ext = typeof args[1] !== 'undefined' ? args[1] : 'mdx'
|
||||
// Remove special characters and replace space with -
|
||||
const fileName = title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-zA-Z0-9 ]/g, '')
|
||||
.replace(/ /g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
let d = new Date()
|
||||
const date = [
|
||||
d.getFullYear(),
|
||||
('0' + (d.getMonth() + 1)).slice(-2),
|
||||
('0' + d.getDate()).slice(-2),
|
||||
].join('-')
|
||||
const root = process.cwd()
|
||||
|
||||
const frontMatter = `---
|
||||
title: ${title}
|
||||
date: '${date}'
|
||||
tags: []
|
||||
draft: true
|
||||
summary:
|
||||
images: []
|
||||
---
|
||||
`
|
||||
const getAuthors = () => {
|
||||
const authorPath = path.join(root, 'data', 'authors')
|
||||
const authorList = fs.readdirSync(authorPath).map((filename) => path.parse(filename).name)
|
||||
return authorList
|
||||
}
|
||||
|
||||
fs.writeFile(`data/blog/${fileName}.${ext}`, frontMatter, (err) => {
|
||||
if (err) throw err
|
||||
})
|
||||
const getLayouts = () => {
|
||||
const layoutPath = path.join(root, 'layouts')
|
||||
const layoutList = fs
|
||||
.readdirSync(layoutPath)
|
||||
.map((filename) => path.parse(filename).name)
|
||||
.filter((file) => file.toLowerCase().includes('post'))
|
||||
return layoutList
|
||||
}
|
||||
|
||||
const genFrontMatter = (answers) => {
|
||||
let d = new Date()
|
||||
const date = [
|
||||
d.getFullYear(),
|
||||
('0' + (d.getMonth() + 1)).slice(-2),
|
||||
('0' + d.getDate()).slice(-2),
|
||||
].join('-')
|
||||
const tagArray = answers.tags.split(',')
|
||||
tagArray.forEach((tag, index) => (tagArray[index] = tag.trim()))
|
||||
const tags = "'" + tagArray.join("','") + "'"
|
||||
const authorArray = answers.authors.length > 0 ? "'" + answers.authors.join("','") + "'" : ''
|
||||
|
||||
let frontMatter = dedent`---
|
||||
title: ${answers.title ? answers.title : 'Untitled'}
|
||||
date: '${date}'
|
||||
tags: [${answers.tags ? tags : ''}]
|
||||
draft: ${answers.draft === 'yes' ? true : false}
|
||||
summary: ${answers.summary ? answers.summary : ' '}
|
||||
images: []
|
||||
layout: ${answers.layout}
|
||||
`
|
||||
|
||||
if (answers.authors.length > 0) {
|
||||
frontMatter = frontMatter + '\n' + `authors: [${authorArray}]`
|
||||
}
|
||||
|
||||
frontMatter = frontMatter + '\n---'
|
||||
|
||||
return frontMatter
|
||||
}
|
||||
|
||||
inquirer
|
||||
.prompt([
|
||||
{
|
||||
name: 'title',
|
||||
message: 'Enter post title:',
|
||||
type: 'input',
|
||||
},
|
||||
{
|
||||
name: 'extention',
|
||||
message: 'Choose post extension:',
|
||||
type: 'list',
|
||||
choices: ['mdx', 'md'],
|
||||
},
|
||||
{
|
||||
name: 'authors',
|
||||
message: 'Choose authors:',
|
||||
type: 'checkbox',
|
||||
choices: getAuthors,
|
||||
},
|
||||
{
|
||||
name: 'summary',
|
||||
message: 'Enter post summary:',
|
||||
type: 'input',
|
||||
},
|
||||
{
|
||||
name: 'draft',
|
||||
message: 'Set post as draft?',
|
||||
type: 'list',
|
||||
choices: ['yes', 'no'],
|
||||
},
|
||||
{
|
||||
name: 'tags',
|
||||
message: 'Any Tags? Separate them with , or leave empty if no tags.',
|
||||
type: 'input',
|
||||
},
|
||||
{
|
||||
name: 'layout',
|
||||
message: 'Select layout',
|
||||
type: 'list',
|
||||
choices: getLayouts,
|
||||
},
|
||||
])
|
||||
.then((answers) => {
|
||||
// Remove special characters and replace space with -
|
||||
const fileName = answers.title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-zA-Z0-9 ]/g, '')
|
||||
.replace(/ /g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
const frontMatter = genFrontMatter(answers)
|
||||
const filePath = `data/blog/${fileName ? fileName : 'untitled'}.${
|
||||
answers.extention ? answers.extention : 'md'
|
||||
}`
|
||||
fs.writeFile(filePath, frontMatter, { flag: 'wx' }, (err) => {
|
||||
if (err) {
|
||||
throw err
|
||||
} else {
|
||||
console.log(`Blog post generated successfully at ${filePath}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.isTtyError) {
|
||||
console.log("Prompt couldn't be rendered in the current environment")
|
||||
} else {
|
||||
console.log('Something went wrong, sorry!')
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user