Browse Source

Added markdown viewer for posts and implemented sql entry for posts

pull/4/head
jrtechs 6 years ago
parent
commit
8a7db301cf
6 changed files with 87 additions and 22 deletions
  1. +2
    -0
      README.md
  2. +28
    -1
      admin/newPost.js
  3. +8
    -0
      entries/testing-my-server.md
  4. +16
    -5
      posts/singlePost.js
  5. +19
    -16
      utils/sql.js
  6. +14
    -0
      utils/utils.js

+ 2
- 0
README.md View File

@ -32,6 +32,7 @@ category_id mediumint unsigned not null,
picture_url varchar(100) not null,
published datetime not null,
name varchar(100) not null,
url varchar(100) not null,
primary key(post_id)
);
@ -50,4 +51,5 @@ grant all on blog_name.* to blog_user@localhost identified by "password";
npm install mysql
npm install sanitizer
npm install promise
npm install markdown
```

+ 28
- 1
admin/newPost.js View File

@ -7,11 +7,23 @@ var Promise = require('promise');
module.exports=
{
/**
*
* @param res
* @param postData
* @return {*}
*/
main: function(res, postData)
{
utils.include(res, "./admin/newPost.html");
return this.processPost(res, postData);
},
/**
*
* @param res
* @param postData
* @return {*|Promise}
*/
processPost: function(res, postData)
{
return new Promise(function(resolve, reject)
@ -19,9 +31,24 @@ module.exports=
var post = qs.parse(postData);
if(post.add_post_name)
{
var urls = post.add_post_name;
urls = urls.split(" ").join("-");
urls =urls.toLowerCase();
var q = "insert into posts (category_id, picture_url, published, name, url) values ";
q += "('" + post.add_post_category + "', '" + post.add_post_picture +
"', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')";
sql.insert(q).then(function()
{
resolve();
})
}
else
{
resolve(postData);
}
resolve(postData);
});
}
};

+ 8
- 0
entries/testing-my-server.md View File

@ -0,0 +1,8 @@
#THIS IS A H1
This page is merely for testing -- go away
```javascript
var i = 1;
```

+ 16
- 5
posts/singlePost.js View File

@ -2,6 +2,8 @@ const utils = require('../utils/utils.js');
var Promise = require('promise');
var markdown = require( "markdown" ).markdown;
module.exports=
{
/**
@ -25,11 +27,20 @@ module.exports=
res.write("</div>");
res.write("<div class=\"w3-container\">");
//include page content
utils.include(res, "../entries/" + post.url + ".html").then(function (value) {
res.write("</div></div>");
resolve();
});
var pathName = "entries/" + post.url + ".md";
try
{
res.write(markdown.toHTML(utils.getFileContents(pathName)));
}
catch(ex)
{
console.log(ex);
//utils.include(res, "includes/404.html");
}
res.write("</div></div>");
resolve()
});
}
};

+ 19
- 16
utils/sql.js View File

@ -24,7 +24,7 @@ con.connect(function(err) {
*/
var fetch = function(sqlStatement)
{
console.log("sql fetch method called");
console.log("sql fetch method called with + " + sqlStatement);
return new Promise(function(resolve, reject)
{
con.query(sqlStatement, function (err, result)
@ -38,7 +38,6 @@ var fetch = function(sqlStatement)
resolve(result);
});
});
};
module.exports=
@ -53,15 +52,19 @@ module.exports=
*/
insert : function(sqlStatement)
{
con.query((sqlStatement), function (err, result)
return new Promise(function(resolve, reject)
{
if (err)
con.query(sanitizer.sanitize(sqlStatement), function (err, result)
{
console.log(err);
return 0;
}
return result.insertId;
});
if (err)
{
console.log(err);
resolve(0);
}
console.log(sqlStatement);
resolve(result.insertId);
});
})
},
/**
@ -75,19 +78,19 @@ module.exports=
{
return new Promise(function(resolve, reject)
{
var splitURL = requestURL.split("/");
var splitURL = requestURL.split("/")
var q = "select * from categories where url='" + splitURL[1] + "'";
fetch(q).then(function (result_category)
{
console.log(result_category);
if(result_category.length != 0)
{
q = "select * from posts where category_id='" + result_category[0].category_id + "' and url='" + splitURL[2] + "'";
console.log(q);
fetch(q).then(function (result_posts)
var q2 = "select * from posts where category_id='" + result_category[0].category_id +
"' and url='" + splitURL[2] + "'";
fetch(q2).then(function (result_posts)
{
console.log(result_posts);
if(result_posts != 0)
{
resolve(result_posts[0]);
@ -112,7 +115,7 @@ module.exports=
*
* @return {Promise<Response> | * | Array}
*/
getCategories: function()
getCategories : function()
{
var q = "select * from categories";
return fetch(q);

+ 14
- 0
utils/utils.js View File

@ -32,6 +32,20 @@ module.exports=
});
},
getFileContents: function(fileName)
{
try
{
return write(fs.readFileSync(fileName));
}
catch (e)
{
console.log("Could not find " + fileName);
}
return 0;
},
/**
* Function which is responsible for returning all post data.
*

Loading…
Cancel
Save