hablo/share/js/DomRenderer.js

144 lines
4.3 KiB
JavaScript
Raw Permalink 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 {
articlesList: articlesList,
2021-01-20 13:18:45 +01:00
getResource: getResource,
render: render,
replaceMarkdown: replaceMarkdown
};
2021-01-20 13:18:45 +01:00
function getResource(url) {
var i = url.lastIndexOf('/');
var path = url.slice(1, i);
if(path == blog.path.articlesPath) {
return {type: 'article', key: url.slice(i+1).replace(/\.html/, '')};
} else if(path == blog.path.pagesPath) {
return {type: 'page', key: url.slice(i+1).replace(/\.html/, '')};
} else if(path == '' || blog.tags[path] != undefined) {
var tag = path.length > 0 ? path : undefined;
return {type: 'list', tag: tag, all: url.slice(i+1) == 'all.html'};
} else {
return {type: 'unknown'};
}
}
function resourceUrl(resource, limit) {
var directory = blog.path[resource.type + 'sPath'];
var extension = limit != undefined ? '.html' : '.md';
return ["", directory, resource.key + extension].join('/');
}
function replaceMarkdown() {
var div = document.getElementById('contents');
if(div.children[0] && div.children[0].tagName.toLowerCase() == 'article') {
2021-01-20 13:18:45 +01:00
var resourceType = getResource(window.location.pathname).type;
convertContent(resourceType, div.children[0], true);
} else {
var articles = div.getElementsByClassName('articles')[0];
if(articles != undefined) {
for(var i = 0; i < articles.children.length; i++) {
convertContent('article', articles.children[i]);
}
} else {
console.log('No articles found for this page');
}
}
}
2021-01-20 13:18:45 +01:00
function convertContent(resourceType, article, comments) {
var header = article.getElementsByTagName('header')[0];
2021-01-20 13:18:45 +01:00
if(resourceType == 'article') {
header.appendChild(Metadata.get(article.id));
}
var text = article.getElementsByTagName('pre')[0];
if(text != undefined) {
article.replaceChild(getDiv(text.innerText), text);
2021-01-20 13:18:45 +01:00
if(resourceType == 'article' && 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;
}
2021-01-20 13:18:45 +01:00
function commentsSection(resource, limit) {
if(resource.type != 'article' || limit != undefined) {
return [];
} else {
2021-01-20 13:18:45 +01:00
return Metadata.getComments(resource.key);
}
}
2021-01-20 13:18:45 +01:00
function render(resource, markdown, limit) {
var url = resourceUrl(resource, limit);
var content = blog[resource.type + 's'][resource.key];
var lines = markdown.split(/\n/).slice(content.bodyOffset);
var div = getDiv(lines.slice(0, limit).join('\n'));
return Dom.make('article', {}, [
Dom.make('header', {}, [
Dom.make('h1', {}, [
2021-01-20 13:18:45 +01:00
Dom.make('a', {href: url, innerText: content.title})
])].concat(resource.type == 'article' ? Metadata.get(resource.key) : [])
),
div
2021-01-20 13:18:45 +01:00
].concat(commentsSection(resource, limit)));
}
2021-01-20 13:18:45 +01:00
function pageTitle(resource) {
return Template.render(resource.all ? 'allPage' : 'latestPage', {tag: resource.tag});
}
2021-01-20 13:18:45 +01:00
function otherUrl(resource) {
var path = [resource.tag, resource.all ? '' : 'all.html'];
2020-05-09 16:29:26 +02:00
return '/' + path.filter(defined).join('/');
}
2021-01-20 13:18:45 +01:00
function articlesList(resource) {
return function(articlePreviews) {
return [
2021-01-20 13:18:45 +01:00
Dom.make('h2', {innerText: pageTitle(resource)}),
Dom.make('ul', {}, articlesListLinks(resource)),
2020-05-09 16:29:26 +02:00
Dom.make('div', {class: 'articles'}, articlePreviews.filter(defined))
];
};
}
2020-05-09 16:29:26 +02:00
2021-01-20 13:18:45 +01:00
function articlesListLinks(resource) {
2020-05-09 16:29:26 +02:00
var links = [
Dom.make('a', {
2021-01-20 13:18:45 +01:00
innerText: resource.all ? blog.wording.latestLink : blog.wording.allLink,
href: otherUrl(resource),
2020-05-09 16:29:26 +02:00
class: 'other'
})
];
if(blog.hasRSS) {
links.unshift(Dom.make('a', {
innerText: blog.wording.rssLink,
href: 'rss.xml',
class: 'RSS',
2021-01-20 13:18:45 +01:00
title: Template.render('rssTitle', {tag: resource.tag})
2020-05-09 16:29:26 +02:00
}));
}
return links.map(function(e) {return Dom.make('li', {}, [e]);});
}