extract some stuff to utility class
All checks were successful
Web/pipeline/head This commit looks good

This commit is contained in:
Ell 2021-03-31 19:56:33 +02:00
parent 798e8cd8e3
commit a33d718416
4 changed files with 39 additions and 40 deletions

View file

@ -2,7 +2,8 @@ const {
JSDOM JSDOM
} = require("jsdom"); } = require("jsdom");
const fs = require("fs"); const fs = require("fs");
const converter = require("./showdown")(2); const util = require("./util");
const converter = util.showdown(2);
const folder = `${__dirname}/../`; const folder = `${__dirname}/../`;
console.log("Refreshing blog sub-sites..."); console.log("Refreshing blog sub-sites...");
@ -17,10 +18,8 @@ fs.readFile(`${folder}index.html`, function (_, html) {
let elements = templateDom.window.document.getElementsByTagName("*"); let elements = templateDom.window.document.getElementsByTagName("*");
for (let i = 0; i < elements.length; i++) { for (let i = 0; i < elements.length; i++) {
let element = elements[i]; let element = elements[i];
if (element.src && element.src.startsWith("./")) util.replaceRelativeLinks(element, "src");
element.src = `../${element.src.substring(2)}`; util.replaceRelativeLinks(element, "href");
if (element.href && element.href.startsWith("./"))
element.href = `../${element.href.substring(2)}`;
} }
const template = templateDom.serialize(); const template = templateDom.serialize();
@ -51,16 +50,7 @@ fs.readFile(`${folder}index.html`, function (_, html) {
let content; let content;
if (post.book) { if (post.book) {
document.head.innerHTML += /*html*/ `<link rel="stylesheet" href="../style/book.css">`; document.head.innerHTML += /*html*/ `<link rel="stylesheet" href="../style/book.css">`;
// pull the actual content from the crowbook export content = util.extractBookData(file, post);
let html = new JSDOM(file);
let page = html.window.document.getElementById("page");
let header = page.getElementsByTagName("header");
while (header.length > 0)
header[0].parentNode.removeChild(header[0]);
content = /*html*/ `
${post.prompt ? /*html*/ `<p class="meta-info"><i>This story was inspired by a Reedsy Prompt and submitted to their competition. As such, it has also been published on <a href="${post.prompt}">their website</a>.</i></p>` : ""}
${page.outerHTML}
`;
} else { } else {
content = converter.makeHtml(file.toString()); content = converter.makeHtml(file.toString());
} }

View file

@ -1,11 +1,9 @@
const { const {
Feed Feed
} = require("feed"); } = require("feed");
const {
JSDOM
} = require("jsdom");
const fs = require("fs"); const fs = require("fs");
const converter = require("./showdown")(1); const util = require("./util");
const converter = util.showdown(1);
let folder = `${__dirname}/../`; let folder = `${__dirname}/../`;
console.log("Refreshing feeds..."); console.log("Refreshing feeds...");
@ -45,17 +43,7 @@ function createFeed(callback) {
const index = i; const index = i;
let extension = post.book ? "html" : "md"; let extension = post.book ? "html" : "md";
fs.readFile(`${__dirname}/../blog/src/${post.id}.${extension}`, function (_, file) { fs.readFile(`${__dirname}/../blog/src/${post.id}.${extension}`, function (_, file) {
let content; let content = post.book ? util.extractBookData(file, post) : converter.makeHtml(file.toString());
if (post.book) {
let html = new JSDOM(file);
let page = html.window.document.getElementById("page");
let header = page.getElementsByTagName("header");
while (header.length > 0)
header[0].parentNode.removeChild(header[0]);
content = page.innerHTML;
} else {
content = converter.makeHtml(file.toString());
}
feed.addItem({ feed.addItem({
title: `${post.name}${post.archived ? " (Archived)" : ""}`, title: `${post.name}${post.archived ? " (Archived)" : ""}`,
link: `https://ellpeck.de/blog/${post.id}`, link: `https://ellpeck.de/blog/${post.id}`,

View file

@ -1,10 +0,0 @@
const showdown = require("showdown");
require("./lib/showdown-prettify");
require("./lib/showdown-footnotes");
module.exports = function (headerLevel) {
return new showdown.Converter({
parseImgDimensions: true,
headerLevelStart: headerLevel,
extensions: ["prettify", "footnotes"]
});
};

31
node/util.js Normal file
View file

@ -0,0 +1,31 @@
const {
JSDOM
} = require("jsdom");
const showdown = require("showdown");
require("./lib/showdown-prettify");
require("./lib/showdown-footnotes");
module.exports = {
extractBookData: function (file, post) {
let html = new JSDOM(file);
let page = html.window.document.getElementById("page");
let header = page.getElementsByTagName("header");
while (header.length > 0)
header[0].parentNode.removeChild(header[0]);
return /*html*/ `
${post.prompt ? /*html*/ `<p class="meta-info"><i>This story was inspired by a Reedsy Prompt and submitted to their competition. As such, it has also been published on <a href="${post.prompt}">their website</a>.</i></p>` : ""}
${page.outerHTML}
`;
},
replaceRelativeLinks: function (element, tag) {
if (element[tag] && element[tag].startsWith("./"))
element[tag] = `../${element[tag].substring(2)}`;
},
showdown: function (headerLevel) {
return new showdown.Converter({
parseImgDimensions: true,
headerLevelStart: headerLevel,
extensions: ["prettify", "footnotes"]
});
}
};