1
0
Fork 0
mirror of https://github.com/dilllxd/gitfolio.git synced 2024-08-14 22:28:09 +00:00

Merge pull request #5 from BenTearzz/patch-1

Extra functionality to blog creation
This commit is contained in:
imfunny 2019-05-10 09:34:28 +05:30 committed by GitHub
commit 22aa1292f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

41
blog.js
View file

@ -8,26 +8,34 @@ options = {
program program
.version('0.1.1') .version('0.1.1')
.option('-t, --title [title]', 'give blog a title') .option('-t, --title [title]', 'give blog a title')
.option('-s, --subtitle [subtitle]', 'give blog a subtitle', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
.option('-p, --pagetitle [pagetitle]', 'give page a title')
.option('-f, --folder [folder]', 'give folder a title')
.parse(process.argv); .parse(process.argv);
function createBlog(title) { function createBlog(title, subtitle, pagetitle, folder) {
if (!fs.existsSync(`./blog/${title}`)){ if (!fs.existsSync(`./blog/${folder}`)){
fs.mkdirSync(`./blog/${title}`); fs.mkdirSync(`./blog/${folder}`);
} }
fs.copyFile('./blog/blog_template.html', `./blog/${title}/index.html`, (err) => { fs.copyFile('./blog/blog_template.html', `./blog/${folder}/index.html`, (err) => {
if (err) throw err; if (err) throw err;
jsdom.fromFile(`./blog/${title}/index.html`, options).then(function (dom) { jsdom.fromFile(`./blog/${folder}/index.html`, options).then(function (dom) {
let window = dom.window, document = window.document; let window = dom.window, document = window.document;
var style = document.createElement("link"); var style = document.createElement("link");
style.setAttribute("rel","stylesheet") style.setAttribute("rel","stylesheet")
style.setAttribute("href","../../index.css"); style.setAttribute("href","../../index.css");
document.getElementsByTagName("head")[0].appendChild(style); document.getElementsByTagName("head")[0].appendChild(style);
fs.writeFile(`./blog/${title}/index.html`, '<!DOCTYPE html>'+window.document.documentElement.outerHTML, function (error){
document.getElementsByTagName("title")[0].textContent = pagetitle;
document.getElementById("blog_title").textContent = title;
document.getElementById("blog_sub_title").textContent = subtitle;
fs.writeFile(`./blog/${folder}/index.html`, '<!DOCTYPE html>'+window.document.documentElement.outerHTML, function (error){
if (error) throw error; if (error) throw error;
var blog_data = { var blog_data = {
"url_title": title, "url_title": pagetitle,
"title": "Lorem ipsum dolor sit amet", "title": title,
"sub_title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "sub_title": subtitle,
"top_image": "https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1450", "top_image": "https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1450",
"visible": true } "visible": true }
fs.readFile("./blog/blog.json", function (err , data) { fs.readFile("./blog/blog.json", function (err , data) {
@ -47,7 +55,18 @@ function createBlog(title) {
} }
if (program.title) { if (program.title) {
createBlog(('%s', program.title)); if (!program.pagetitle) {
program.pagetitle = program.title;
}
if (!program.folder) {
program.folder = program.title;
}
program.folder = program.folder.replace(/[^A-Za-z0-9_-\s]/g, "").trim();
program.folder = program.folder.replace(/\s/g, "-");
createBlog(program.title, program.subtitle, program.pagetitle, program.folder);
} else { } else {
console.log("provide a title to create a new blog"); console.log("provide a title to create a new blog");
} }