extract some stuff to utility class
Web/pipeline/head This commit looks good Details

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
} = require("jsdom");
const fs = require("fs");
const converter = require("./showdown")(2);
const util = require("./util");
const converter = util.showdown(2);
const folder = `${__dirname}/../`;
console.log("Refreshing blog sub-sites...");
@ -17,10 +18,8 @@ fs.readFile(`${folder}index.html`, function (_, html) {
let elements = templateDom.window.document.getElementsByTagName("*");
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
if (element.src && element.src.startsWith("./"))
element.src = `../${element.src.substring(2)}`;
if (element.href && element.href.startsWith("./"))
element.href = `../${element.href.substring(2)}`;
util.replaceRelativeLinks(element, "src");
util.replaceRelativeLinks(element, "href");
}
const template = templateDom.serialize();
@ -51,16 +50,7 @@ fs.readFile(`${folder}index.html`, function (_, html) {
let content;
if (post.book) {
document.head.innerHTML += /*html*/ `<link rel="stylesheet" href="../style/book.css">`;
// pull the actual content from the crowbook export
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}
`;
content = util.extractBookData(file, post);
} else {
content = converter.makeHtml(file.toString());
}

View File

@ -1,11 +1,9 @@
const {
Feed
} = require("feed");
const {
JSDOM
} = require("jsdom");
const fs = require("fs");
const converter = require("./showdown")(1);
const util = require("./util");
const converter = util.showdown(1);
let folder = `${__dirname}/../`;
console.log("Refreshing feeds...");
@ -45,17 +43,7 @@ function createFeed(callback) {
const index = i;
let extension = post.book ? "html" : "md";
fs.readFile(`${__dirname}/../blog/src/${post.id}.${extension}`, function (_, file) {
let content;
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());
}
let content = post.book ? util.extractBookData(file, post) : converter.makeHtml(file.toString());
feed.addItem({
title: `${post.name}${post.archived ? " (Archived)" : ""}`,
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"]
});
}
};