Skip to content

Commit

Permalink
move post to correct folder, update new script
Browse files Browse the repository at this point in the history
  • Loading branch information
taddison committed Apr 29, 2023
1 parent a35d5ce commit bb874d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions scripts/newpost.mjs
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
import { existsSync, mkdirSync, writeFileSync } from "fs"
import { exit } from "process"
import { createInterface } from "readline"
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { exit } from "process";
import { createInterface } from "readline";

const BASE_PATH = "./content/blog"
const BASE_PATH = "./site/content/blog";

let title, slug
let date = new Date().toISOString().slice(0, 10)
let format = `md`
let title, slug;
let date = new Date().toISOString().slice(0, 10);
let format = `md`;

const question = (prompt) => {
return new Promise((res) => {
rl.question(prompt, (answer) => {
res(answer)
})
})
}
res(answer);
});
});
};

const slugify = (inputString) => {
// spaces to hyphen, remove non-alphanumeric
inputString = inputString
.toLowerCase()
.replace(/\s/g, "-")
.replace(/[^\w-]/g, "")
return inputString
}
.replace(/[^\w-]/g, "");
return inputString;
};

const rl = createInterface({
input: process.stdin,
output: process.stdout,
})
});

title = await question("Post title > ")
title = await question("Post title > ");
if (!title.length || !title.trim().length) {
throw "Title must be provided"
throw "Title must be provided";
}
title = title.trim()
slug = slugify(title)
title = title.trim();
slug = slugify(title);

const inputDate = await question(`Post date [yyyy-MM-dd] (${date}) > `)
const inputDate = await question(`Post date [yyyy-MM-dd] (${date}) > `);
if (inputDate.length > 0 && inputDate.length !== 10) {
throw "Post date must be yyyy-MM-dd"
throw "Post date must be yyyy-MM-dd";
} else if (inputDate.length) {
const parsedDate = new Date(inputDate)
const parsedDate = new Date(inputDate);
if (isNaN(parsedDate)) {
throw "Invalid date provided"
throw "Invalid date provided";
}
date = parsedDate.toISOString().slice(0, 10)
date = parsedDate.toISOString().slice(0, 10);
}

const inputFormat = await question(`Post format [md|mdx] (${format}) > `)
const inputFormat = await question(`Post format [md|mdx] (${format}) > `);
if (inputFormat) {
if (inputFormat !== "md" && inputFormat !== "mdx") {
throw "Invalid input format"
throw "Invalid input format";
}
format = inputFormat
format = inputFormat;
}

const year = date.slice(0, 4)
const month = date.slice(5, 7)
const path = `${BASE_PATH}/${year}/${month}/${slug}`
const year = date.slice(0, 4);
const month = date.slice(5, 7);
const path = `${BASE_PATH}/${year}/${month}/${slug}`;

console.log({ title, slug, date, format, path })
const confirm = await question("Create post (yes) > ")
console.log({ title, slug, date, format, path });
const confirm = await question("Create post (yes) > ");
if (confirm.length && confirm !== "yes") {
exit(0)
exit(0);
}

if (!existsSync(path)) {
mkdirSync(path, { recursive: true })
mkdirSync(path, { recursive: true });
}

writeFileSync(
Expand All @@ -82,10 +82,10 @@ tags: []
---`,
(err) => {
if (err) {
return console.log(err)
return console.log(err);
}
console.log(`${title} was created!`)
console.log(`${title} was created!`);
}
)
);

rl.close()
rl.close();

0 comments on commit bb874d3

Please sign in to comment.