From 4b09f526ba67ed1dfaac43014b7ba8d73f759835 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 17 Mar 2018 14:38:03 -0400 Subject: [PATCH] Added a sitemap generator --- .idea/vcs.xml | 6 ------ README.md | 6 +++--- admin/newPost.js | 2 ++ package.json | 20 ++++++++++++++------ server.js | 4 +++- sitemap.txt | 17 ----------------- utils/generateSiteMap.js | 41 +++++++++++++++++++++++++++------------- utils/sql.js | 31 ++++++++++++++++++++++++++++++ 8 files changed, 81 insertions(+), 46 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index 95d92a4..cb97673 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ Recreating my Wordpress blog in node JS. ## MYSQL Information ```SQL -create database blog_name; +create database jrtechs_blog; -use blog_name; +use jrtechs_blog; create table users( user_id mediumint unsigned not null AUTO_INCREMENT, @@ -46,7 +46,7 @@ post_id mediumint unsigned not null, primary key(popular_post_id) ); -grant all on blog_name.* to blog_user@localhost identified by "password"; +grant all on jrtechs_blog.* to blog_user@localhost identified by "password"; ``` diff --git a/admin/newPost.js b/admin/newPost.js index ab96b4e..a1c14b8 100644 --- a/admin/newPost.js +++ b/admin/newPost.js @@ -42,6 +42,8 @@ module.exports= "', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')"; sql.insert(q).then(function() { + var map = require('../utils/generateSiteMap'); + map.main(); resolve(); }) } diff --git a/package.json b/package.json index 259c0f5..51af369 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,18 @@ { - "name": "jrtechs", + "name": "jrtechs-blog", "version": "1.0.0", - "description": "Personal blog", + "description": "Personal wordpress replacement", "main": "server.js", - "dependencies": {}, + "dependencies": { + "highlight": "^0.2.4", + "markdown": "^0.5.0", + "markdown-to-html": "^0.0.13", + "mysql": "^2.15.0", + "promise": "^8.0.1", + "sanitizer": "^0.1.3", + "step": "^1.0.0", + "synchronize": "^2.0.0" + }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", @@ -14,8 +23,7 @@ "url": "git+https://github.com/jrtechs/NodeJSBlog.git" }, "keywords": [ - "nodejs", - "blog" + "nodejs" ], "author": "Jeffery Russell", "license": "ISC", @@ -23,4 +31,4 @@ "url": "https://github.com/jrtechs/NodeJSBlog/issues" }, "homepage": "https://github.com/jrtechs/NodeJSBlog#readme" -} +} \ No newline at end of file diff --git a/server.js b/server.js index 78416d7..b976797 100644 --- a/server.js +++ b/server.js @@ -19,8 +19,10 @@ const utils = require('./utils/utils.js'); // // var forceSsl = require('express-force-ssl'); -var app = express(); +var map = require('./utils/generateSiteMap'); +map.main(); +var app = express(); app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); diff --git a/sitemap.txt b/sitemap.txt index 74dcf3e..e69de29 100644 --- a/sitemap.txt +++ b/sitemap.txt @@ -1,17 +0,0 @@ -http://jrtechs.net -http://www.jrtechs.net/programming/using-english-conventions-to-write-clean-code -http://www.jrtechs.net/web-development/node.js-vs-php -http://www.jrtechs.net/other/why-do-i-blog -http://www.jrtechs.net/web-development/history-of-jrtechs -http://www.jrtechs.net/web-development/why-i-stopped-using-wordpress -http://www.jrtechs.net/projects/musical-floppy-drives -http://www.jrtechs.net/projects/java-fibonacci-solver -http://www.jrtechs.net/projects/batch-minecraft-launcher-with-auto-restart -http://www.jrtechs.net/projects/ackermann-function-written-in-java -http://www.jrtechs.net/projects/time-lapse-programming-zombie-game -http://www.jrtechs.net/category/java -http://www.jrtechs.net/category/other -http://www.jrtechs.net/category/projects -http://www.jrtechs.net/category/web-development -http://www.jrtechs.net/category/hardware -http://www.jrtechs.net/category/programming \ No newline at end of file diff --git a/utils/generateSiteMap.js b/utils/generateSiteMap.js index c7d9533..f98c80c 100644 --- a/utils/generateSiteMap.js +++ b/utils/generateSiteMap.js @@ -2,18 +2,33 @@ const fs = require('fs'); const sql = require('../utils/sql'); -var stream = fs.createWriteStream("../sitemap.txt"); - -stream.once('open', function(fd) +module.exports= { - sql.getSiteMap().then(function(result) - { - stream.write(result); - }).then(function() - { - stream.end(); - }).catch(function(err) + + main: function() { - console.log(err); - }); -}); \ No newline at end of file + sql.getSiteMap().then(function(result) + { + var buf = Buffer.from(result, 'utf8'); + + var path = '../sitemap.txt'; + fs.open(path, 'w', function(error, fd) + { + if(error) + throw 'could not open file: ' + error; + + fs.write(fd, buf, 0, buf.length, null, function(err) + { + if(err) + throw "error writing file: " + err; + + fs.close(fd, function() + { + console.log("Updated Sitemap"); + }) + }); + }); + }); + } +} + diff --git a/utils/sql.js b/utils/sql.js index 0b169bc..83556fd 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -276,6 +276,37 @@ module.exports= { return new Promise(function(resolve, reject) { + var base = "http://jrtechs.net/"; + var sm = base + "\n"; + var promises = []; + module.exports.getCategories().then(function(categories) + { + categories.forEach(function(cat) + { + promises.push(new Promise(function(res, rej) + { + sm += base + "category/" + cat.url + "\n"; + + module.exports.getPostsFromCategory(cat.url).then(function(posts) + { + posts.forEach(function(post) + { + sm += base + cat.url + "/" + post.url + "\n"; + }); + res() + }) + })); + }); + + Promise.all(promises).then(function() + { + resolve(sm); + }).catch(function(error) + { + throw error; + }); + + }); }); }