2019-02-15 14:13:43 +01:00
|
|
|
function DomRenderer(modules) {
|
|
|
|
return {
|
|
|
|
article: article,
|
|
|
|
articlesList: articlesList,
|
|
|
|
replaceMarkdown: replaceMarkdown
|
|
|
|
};
|
|
|
|
|
|
|
|
function replaceMarkdown() {
|
|
|
|
var div = document.getElementById('contents');
|
|
|
|
if(div.children[0] && div.children[0].tagName.toLowerCase() == 'article') {
|
2019-03-02 23:44:09 +01:00
|
|
|
convertArticle(div.children[0], true);
|
2019-02-15 14:13:43 +01:00
|
|
|
} else {
|
|
|
|
var articles = div.getElementsByClassName('articles')[0];
|
|
|
|
if(articles != undefined) {
|
|
|
|
for(var i = 0; i < articles.children.length; i++) {
|
|
|
|
convertArticle(articles.children[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('No articles found for this page');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 23:44:09 +01:00
|
|
|
function convertArticle(article, comments) {
|
2019-02-15 14:13:43 +01:00
|
|
|
var header = article.getElementsByTagName('header')[0];
|
2019-03-02 23:44:09 +01:00
|
|
|
header.appendChild(modules.metadata.get(article.id));
|
2019-02-15 14:13:43 +01:00
|
|
|
var text = article.getElementsByTagName('pre')[0];
|
|
|
|
if(text != undefined) {
|
|
|
|
article.replaceChild(getDiv(text.innerText), text);
|
2019-03-02 23:44:09 +01:00
|
|
|
if(comments) {
|
|
|
|
modules.metadata.getComments(article.id)
|
|
|
|
.forEach(article.appendChild.bind(article));
|
2019-02-16 08:13:14 +01:00
|
|
|
}
|
2019-02-15 14:13:43 +01:00
|
|
|
} else {
|
|
|
|
console.log('No content found for this article');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDiv(markdown) {
|
2019-02-15 18:08:57 +01:00
|
|
|
var d= modules.dom.make('div', {
|
2019-02-15 14:13:43 +01:00
|
|
|
innerHTML: modules.md.render(markdown)
|
|
|
|
});
|
2019-02-15 18:08:57 +01:00
|
|
|
var scripts = d.getElementsByTagName('script');
|
|
|
|
for(var i = 0; i < scripts.length; i++) {
|
|
|
|
var run = modules.dom.make('script',
|
|
|
|
{type: 'text/javascript', src: scripts[i].src, textContent: scripts[i].textContent}
|
|
|
|
);
|
|
|
|
scripts[i].parentNode.replaceChild(run, scripts[i]);
|
|
|
|
}
|
|
|
|
return d;
|
2019-02-15 14:13:43 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 18:07:59 +01:00
|
|
|
function article(key, markdown, limit) {
|
|
|
|
var url = ["", blog.path.articlesPath, key + (limit != undefined ? '.html' : '.md')].join('/');
|
|
|
|
var lines = markdown.split(/\n/).slice(blog.articles[key].bodyOffset);
|
2019-02-15 14:13:43 +01:00
|
|
|
var div = getDiv(lines.slice(0, limit).join('\n'));
|
2019-02-15 18:07:59 +01:00
|
|
|
return modules.dom.make('article', {}, [
|
2019-02-15 14:13:43 +01:00
|
|
|
modules.dom.make('header', {}, [
|
2019-03-02 22:45:26 +01:00
|
|
|
modules.dom.make('a', {href: url}, [
|
2019-02-15 18:07:59 +01:00
|
|
|
modules.dom.make('h1', {innerText: blog.articles[key].title})
|
2019-03-02 23:44:09 +01:00
|
|
|
]),
|
|
|
|
modules.metadata.get(key)
|
2019-02-15 14:13:43 +01:00
|
|
|
]),
|
|
|
|
div
|
2019-03-02 23:44:09 +01:00
|
|
|
].concat(limit != undefined ? [] : modules.metadata.getComments(key)));
|
2019-02-15 14:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function pageTitle(tag, all) {
|
2019-02-17 19:52:28 +01:00
|
|
|
if(tag != undefined) {
|
2019-03-02 23:44:09 +01:00
|
|
|
var template = all ? 'allTaggedPage' : 'latestTaggedPage';
|
|
|
|
return modules.template.render(template, {tag: tag});
|
2019-02-17 19:52:28 +01:00
|
|
|
} else {
|
|
|
|
return blog.wording[all ? 'allPage' : 'latestPage'];
|
|
|
|
}
|
2019-02-15 14:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function otherUrl(tag, all) {
|
|
|
|
var path = [tag, all ? null : 'all.html'];
|
|
|
|
return '/' + path.filter(modules.fun.defined).join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
function articlesList(tag, all) {
|
|
|
|
return function(articlePreviews) {
|
|
|
|
return [
|
|
|
|
modules.dom.make('h2', {innerText: pageTitle(tag, all)}),
|
2019-02-16 08:11:58 +01:00
|
|
|
modules.dom.make('a', {
|
2019-02-17 19:52:28 +01:00
|
|
|
innerText: all ? blog.wording.latestLink : blog.wording.allLink,
|
2019-02-16 08:11:58 +01:00
|
|
|
href: otherUrl(tag, all)
|
|
|
|
}),
|
2019-02-15 14:13:43 +01:00
|
|
|
modules.dom.make('div', {class: 'articles'}, articlePreviews.filter(modules.fun.defined))
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|