This repository has been archived on 2021-02-16. You can view files and clone it, but cannot push or open issues or pull requests.
Nodezzarella/classes/Controllers/Articles.class.js

47 lines
2.2 KiB
JavaScript

const log = require('color-logs')(true, true, "Articles.class.js"),
hbs = require('handlebars'),
promise = require('promise'),
markdown = require('markdown').markdown,
fs = require("fs");
class Articles {
getArticle(categorie, article){
return new Promise((resolve, reject) => {
fs.readFile('./views/articles/article.hbs', 'utf-8', (error, source) => {
var paramPath = "./ressources/"+categorie+"/"+article+".json"; // path to param of article
var contentPath = "./ressources/"+categorie+"/"+article+".md"; //path to article
fs.access(paramPath, fs.constants.F_OK || fs.constants.R_OK, (error) => {
if(error)
resolve(new Error("File not exist"));
else
fs.access(contentPath, fs.constants.F_OK || fs.constants.R_OK, (error) => {
if(error)
resolve(new Error("File not exist"));
else{
fs.readFile(contentPath, 'utf-8', (error, content) => {
var content = markdown.toHTML(content);
const articleParams = require("."+paramPath);
this.title = articleParams.title;
var data = {
'title':articleParams.title,
'author': articleParams.author,
'content': content
};
hbs.registerHelper('articles', (articles) =>{
return articles;
});
var template = hbs.compile(source);
var html = template(data);
resolve(html);
});
}
});
});
});
});
}
}
module.exports = Articles;