2020-01-11 09:05:32 +01:00
|
|
|
import blog from Hablo.Config;
|
2020-01-10 18:56:59 +01:00
|
|
|
import Metadata;
|
|
|
|
import Remarkable;
|
2020-01-11 09:05:32 +01:00
|
|
|
import Template;
|
|
|
|
import * as Dom from UnitJS.Dom;
|
2020-05-09 16:29:26 +02:00
|
|
|
import {defined} from UnitJS.Fun;
|
2020-01-10 18:56:59 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
articlesList: articlesList,
|
2020-06-20 22:59:39 +02:00
|
|
|
render: render,
|
2020-01-10 18:56:59 +01:00
|
|
|
replaceMarkdown: replaceMarkdown
|
|
|
|
};
|
|
|
|
|
|
|
|
function replaceMarkdown() {
|
|
|
|
var div = document.getElementById('contents');
|
|
|
|
if(div.children[0] && div.children[0].tagName.toLowerCase() == 'article') {
|
2020-06-20 22:59:39 +02:00
|
|
|
var contentType = window.location.pathname.slice(1).replace(/\/.*/, '');
|
|
|
|
convertContent(contentType, div.children[0], true);
|
2020-01-10 18:56:59 +01:00
|
|
|
} else {
|
|
|
|
var articles = div.getElementsByClassName('articles')[0];
|
|
|
|
if(articles != undefined) {
|
|
|
|
for(var i = 0; i < articles.children.length; i++) {
|
2020-06-20 22:59:39 +02:00
|
|
|
convertContent('article', articles.children[i]);
|
2020-01-10 18:56:59 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('No articles found for this page');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 22:59:39 +02:00
|
|
|
function convertContent(contentType, article, comments) {
|
2020-01-10 18:56:59 +01:00
|
|
|
var header = article.getElementsByTagName('header')[0];
|
2020-06-20 22:59:39 +02:00
|
|
|
if(contentType == 'article') {
|
|
|
|
header.appendChild(Metadata.get(article.id));
|
|
|
|
}
|
2020-01-10 18:56:59 +01:00
|
|
|
var text = article.getElementsByTagName('pre')[0];
|
|
|
|
if(text != undefined) {
|
|
|
|
article.replaceChild(getDiv(text.innerText), text);
|
2020-06-20 22:59:39 +02:00
|
|
|
if(contentType == 'article' && comments) {
|
2020-01-10 18:56:59 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-20 22:59:39 +02:00
|
|
|
function contentUrl(contentType, key, limit) {
|
|
|
|
var directory = blog.path[contentType + 'sPath'];
|
|
|
|
var extension = limit != undefined ? '.html' : '.md';
|
|
|
|
return ["", directory, key + extension].join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
function commentsSection(contentType, key, limit) {
|
|
|
|
if(contentType != 'article' || limit != undefined) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
return Metadata.getComments(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function render(contentType, key, markdown, limit) {
|
|
|
|
var url = contentUrl(contentType, key, limit);
|
|
|
|
var resource = blog[contentType + 's'][key];
|
|
|
|
var lines = markdown.split(/\n/).slice(resource.bodyOffset);
|
2020-01-10 18:56:59 +01:00
|
|
|
var div = getDiv(lines.slice(0, limit).join('\n'));
|
|
|
|
return Dom.make('article', {}, [
|
|
|
|
Dom.make('header', {}, [
|
2020-06-20 22:59:39 +02:00
|
|
|
Dom.make('h1', {}, [
|
|
|
|
Dom.make('a', {href: url, innerText: resource.title})
|
|
|
|
])].concat(contentType == 'article' ? Metadata.get(key) : [])
|
|
|
|
),
|
2020-01-10 18:56:59 +01:00
|
|
|
div
|
2020-06-20 22:59:39 +02:00
|
|
|
].concat(commentsSection(contentType, key, limit)));
|
2020-01-10 18:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function pageTitle(tag, all) {
|
2020-05-09 16:29:26 +02:00
|
|
|
return Template.render(all ? 'allPage' : 'latestPage', {tag: tag});
|
2020-01-10 18:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function otherUrl(tag, all) {
|
2020-05-09 16:29:26 +02:00
|
|
|
var path = [tag, all ? '' : 'all.html'];
|
|
|
|
return '/' + path.filter(defined).join('/');
|
2020-01-10 18:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-01-10 18:56:59 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
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]);});
|
|
|
|
}
|