Browse Source

Unified how blog posts were rendered between the preview post and full render.

pull/5/head
jrtechs 6 years ago
parent
commit
a35cdaadd8
3 changed files with 114 additions and 153 deletions
  1. BIN
      blogContent/posts/programming/media/fibonacci/ConstantTimeComplexity.png
  2. +3
    -146
      posts/singlePost.js
  3. +111
    -7
      utils/markdownToHTML.js

BIN
blogContent/posts/programming/media/fibonacci/ConstantTimeComplexity.png View File

Before After
Width: 860  |  Height: 480  |  Size: 38 KiB

+ 3
- 146
posts/singlePost.js View File

@ -1,36 +1,5 @@
const utils = require('../utils/utils.js');
const postGenerator = require('../utils/markdownToHTML.js');
const sql = require('../utils/sql');
const Remarkable = require('remarkable');
const hljs = require('highlight.js');
const pandoc = require('../utils/markdownToHTML.js');
const md = new Remarkable(
{
html: true,
highlight: function (str, lang)
{
if (lang && hljs.getLanguage(lang))
{
try
{
return hljs.highlight(lang, str).value;
}
catch (err) {}
}
try
{
return hljs.highlightAuto(str).value;
}
catch (err) {}
return ''; // use external default escaping
}
});
module.exports= module.exports=
@ -43,69 +12,7 @@ module.exports=
*/ */
renderPreview: function(post) renderPreview: function(post)
{ {
return new Promise(function(resolve, reject)
{
//var html = "<div class=\"w3-card-4 w3-margin w3-white\">";
var html = "<div class=\"blogPost\">";
//image
if(!(post.picture_url === "n/a"))
{
html +="<img src=\"/blogContent/headerImages/" + post.picture_url +
"\" alt=\"\" style=\"width:100%; height:10%\">";
}
html += "<div class=\"p-4\"><div class=\"\">";
//title
html += "<h3><b>" + post.name + "</b></h3>";
//date
html += "<h5><span class=\"w3-opacity\">" +
post.published.toDateString() + "</span></h5>";
html +="</div>";
html += "<div class=\"\">";
try
{
sql.getCategory(post.category_id).then(function(category)
{
var pathName = "blogContent/posts/" + category[0].url + "/"
+ post.url + ".md";
var markDown = utils.getFileContents(pathName).toString();
markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
+ category[0].url + "/media/");
var htmlPost = md.render(markDown).split("<p>");
for(var i = 0; i < 3; i++)
{
html+= "<p>" + htmlPost[i];
}
html = html.split("<img").join("<img style=\"width: 100%; height:10%\" ");
html = html.split("<code>").join("<code class='hljs cpp'>");
html += " <div class=\"\">\n" +
" <p class='text-center'><button class=\"btn btn-secondary btn-lg " +
"w3-padding-large w3-white w3-border\" onclick=\"location.href='" +
"http://jrtechs.net/" + category[0].url + "/" + post.url +
"'\"><b>READ MORE &raquo;</b></button></p>\n" +
" </div>\n";
html += "</div></div></div><br><br>";
resolve(html)
}).catch(function(error)
{
reject(error);
});
}
catch(ex)
{
reject(ex);
}
});
return postGenerator.generateBlogPost(post, 3);
}, },
/** /**
@ -117,56 +24,6 @@ module.exports=
*/ */
renderPost: function(post) renderPost: function(post)
{ {
return new Promise(function (resolve, reject)
{
var htmlHead = "<div class=\"blogPost\">";
//image
if(!(post.picture_url === "n/a"))
{
htmlHead +="<img src=\"/blogContent/headerImages/" + post.picture_url +
"\" alt=\"\" style=\"width:100%; height:10%\">";
}
htmlHead += "<div class=\"p-4\"><div class=\"\">";
//title
htmlHead += "<h3><b>" + post.name + "</b></h3>";
//date
htmlHead += "<h5><span class=\"w3-opacity\">" +
post.published.toDateString() + "</span></h5>";
htmlHead +="</div>";
var html = "<div class=\"\">";
try
{
sql.getCategory(post.category_id).then(function(category)
{
const pathName = "blogContent/posts/" + category[0].url + "/"
+ post.url + ".md";
var markDown = utils.getFileContents(pathName).toString();
markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
+ category[0].url + "/media/");
//html += md.render(markDown);
pandoc.convertToHTML(markDown).then(function(result)
{
html +=result;
html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
html = html.split("<code>").join("<code class='hljs cpp'>");
html += "</div></div></div><br><br>";
resolve(htmlHead + html);
}).catch(function(error)
{
reject(error);
})
});
}
catch(ex)
{
reject(ex);
}
});
return postGenerator.generateBlogPost(post, -1);
} }
}; };

+ 111
- 7
utils/markdownToHTML.js View File

@ -1,25 +1,129 @@
const pandoc = require('node-pandoc'); const pandoc = require('node-pandoc');
// const args = '-t html5';
const args = '-S --base-header-level=1 --toc --toc-depth=6 -N --normalize -s --mathjax -t html5';
const utils = require('../utils/utils.js');
const sql = require('../utils/sql');
const argsFull = '-S --base-header-level=1 --toc --toc-depth=3 -N --normalize -s --mathjax -t html5';
const argsPreview = '-S --normalize -s --mathjax -t html5';
console.log("");
module.exports= module.exports=
{ {
convertToHTML: function(markdownContents)
generateBlogPost: function(post, blocks)
{
return new Promise(function(resolve, reject)
{
Promise.all([module.exports.generateBlogPostHeader(post),
module.exports.generateBlogPostBody(post, blocks),
module.exports.generateBlogPostFooter()]).then(function(content)
{
resolve(content.join(''));
})
});
},
generateBlogPostHeader: function(post)
{
var htmlHead = "<div class=\"blogPost\">";
//image
if(!(post.picture_url === "n/a"))
{
htmlHead +="<img src=\"/blogContent/headerImages/" + post.picture_url +
"\" alt=\"\" style=\"width:100%; height:10%\">";
}
htmlHead += "<div class=\"p-4\"><div class=\"\">";
//title
htmlHead += "<h3><b>" + post.name + "</b></h3>";
//date
htmlHead += "<h5><span class=\"w3-opacity\">" +
post.published.toDateString() + "</span></h5>";
htmlHead +="</div>" + "<div class=\"\">";
return htmlHead;
},
generateBlogPostBody: function(post, blocks)
{
return new Promise(function(resolve, reject)
{
sql.getCategory(post.category_id).then(function(category)
{
const pathName = "blogContent/posts/" + category[0].url + "/"
+ post.url + ".md";
var markDown = utils.getFileContents(pathName).toString();
markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
+ category[0].url + "/media/");
module.exports.convertToHTML(markDown, 1).then(function(result)
{
result = result.split("<figcaption>").join("<figcaption style=\"visibility: hidden;\">");
if(blocks == -1)
resolve(result);
var htmlBlocks = result.split("<p>");
var html = "";
for(var i = 0; i < blocks; i++)
{
html += "<p>" + htmlBlocks[i];
}
html += " <div class=\"\">\n" +
" <p class='text-center'><button class=\"btn btn-secondary btn-lg "
+ "onclick=\"location.href='" +
"http://jrtechs.net/" + category[0].url + "/" + post.url +
"'\"><b>READ MORE &raquo;</b></button></p>\n" +
" </div>\n";
resolve(html);
}).catch(function(error)
{
reject(error);
})
});
})
},
generateBlogPostFooter: function()
{
return "</div></div></div><br><br>";
},
convertToHTML: function(markdownContents, type)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
// Set your callback function // Set your callback function
callback = function (err, result)
callback = function (err, html)
{ {
if (err) if (err)
{ {
reject(err); reject(err);
} }
resolve(result);
html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
html = html.split("<code>").join("<code class='hljs cpp'>");
resolve(html);
}; };
pandoc(markdownContents, args, callback);
if(type == -1)
{
pandoc(markdownContents, argsFull, callback);
}
else
{
pandoc(markdownContents, argsPreview, callback);
}
}); });
}, },
} }

Loading…
Cancel
Save