hablo/share/js/DomRenderer.js

111 lines
3.1 KiB
JavaScript
Raw Normal View History

import blog from Hablo.Config;
import Metadata;
import Remarkable;
import Template;
import * as Dom from UnitJS.Dom;
2020-05-09 16:29:26 +02:00
import {defined} from UnitJS.Fun;
return {
article: article,
articlesList: articlesList,
replaceMarkdown: replaceMarkdown
};
function replaceMarkdown() {
var div = document.getElementById('contents');
if(div.children[0] && div.children[0].tagName.toLowerCase() == 'article') {
convertArticle(div.children[0], true);
} 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');
}
}
}
function convertArticle(article, comments) {
var header = article.getElementsByTagName('header')[0];
header.appendChild(Metadata.get(article.id));
var text = article.getElementsByTagName('pre')[0];
if(text != undefined) {
article.replaceChild(getDiv(text.innerText), text);
if(comments) {
Metadata.getComments(article.id)
.forEach(article.appendChild.bind(article));
}
} else {
console.log('No content found for this article');
}
}
function getDiv(markdown) {
var d= Dom.make('div', {
innerHTML: Remarkable.md.render(markdown)
});
var scripts = d.getElementsByTagName('script');
for(var i = 0; i < scripts.length; i++) {
var run = Dom.make('script',
{type: 'text/javascript', src: scripts[i].src, textContent: scripts[i].textContent}
);
scripts[i].parentNode.replaceChild(run, scripts[i]);
}
return d;
}
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);
var div = getDiv(lines.slice(0, limit).join('\n'));
return Dom.make('article', {}, [
Dom.make('header', {}, [
Dom.make('a', {href: url}, [
Dom.make('h1', {innerText: blog.articles[key].title})
]),
Metadata.get(key)
]),
div
].concat(limit != undefined ? [] : Metadata.getComments(key)));
}
function pageTitle(tag, all) {
2020-05-09 16:29:26 +02:00
return Template.render(all ? 'allPage' : 'latestPage', {tag: tag});
}
function otherUrl(tag, all) {
2020-05-09 16:29:26 +02:00
var path = [tag, all ? '' : 'all.html'];
return '/' + path.filter(defined).join('/');
}
function articlesList(tag, all) {
return function(articlePreviews) {
return [
Dom.make('h2', {innerText: pageTitle(tag, all)}),
2020-05-09 16:29:26 +02:00
Dom.make('ul', {}, articlesListLinks(tag, all)),
Dom.make('div', {class: 'articles'}, articlePreviews.filter(defined))
];
};
}
2020-05-09 16:29:26 +02:00
function articlesListLinks(tag, all) {
var links = [
Dom.make('a', {
innerText: all ? blog.wording.latestLink : blog.wording.allLink,
href: otherUrl(tag, all),
class: 'other'
})
];
if(blog.hasRSS) {
links.unshift(Dom.make('a', {
innerText: blog.wording.rssLink,
href: 'rss.xml',
class: 'RSS',
title: Template.render('rssTitle', {tag: tag})
}));
}
return links.map(function(e) {return Dom.make('li', {}, [e]);});
}