-
peter_rabbit authoredpeter_rabbit authored
articles.js 6.02 KiB
"use strict";
const { articles_url } = require("../../constants");
const { fetchjson, fetchtext } = require("../lib/fetch");
const objectHtmlRenderer = require("../lib/object-html-renderer");
const ImageCarousel = require("./image-carousel");
class Articles {
constructor() {
this.id = performance.now().toString();
this.state = {
loading: true,
articles: [],
showArticleIndex: -1,
};
this.loadArticles();
}
loadArticles() {
fetchjson(`${articles_url}/index.json`)
.then(json => {
Promise.all(
json.map(async articlePath => {
const art = await fetchjson(`${articles_url}/${articlePath}`);
const tmpSplit = articlePath.split("/");
tmpSplit.pop();
const absArtPath = `${articles_url}/${tmpSplit.join("/")}`;
return Object.assign(art, { path: absArtPath });
})
)
.then(articles => this.setArticles(articles))
.catch(e => console.log(e));
})
.catch(e => console.log(e));
}
async setArticles(articles) {
for (const article of articles) {
if (article.content_text.indexOf("<file>") !== -1) {
const txtPath = article.content_text.replace("<file>", "");
const txtValue = await fetchtext(`${article.path}/${txtPath}`);
article.content_text = txtValue;
}
article.date = new Date(article.date);
}
this.state.articles = articles.sort((a, b) => a.date - b.date);
this.state.showArticleIndex = this.state.articles.length - 1;
this.refresh();
}
refresh() {
objectHtmlRenderer.subRender(this.render(), document.getElementById(this.id), {
mode: "replace",
});
}
getArticleDate(date) {
return `${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}`;
}
getArticleBody(text) {
return text
.split(" ")
.map(word => {
if (word.includes("http://") || word.includes("https://")) {
const splitword = word.split("||");
const href = splitword[0];
const text = splitword.length > 1 ? splitword[1] : href;
return `<a href=${href} target="_blank">${text}</a>`;
} else return word;