diff --git a/node/blog.js b/node/blog.js index b2c66ce..22ad79e 100644 --- a/node/blog.js +++ b/node/blog.js @@ -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*/ ``; - // 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*/ `

This story was inspired by a Reedsy Prompt and submitted to their competition. As such, it has also been published on their website.

` : ""} - ${page.outerHTML} - `; + content = util.extractBookData(file, post); } else { content = converter.makeHtml(file.toString()); } diff --git a/node/rss.js b/node/rss.js index 31ac99c..d6e9efc 100644 --- a/node/rss.js +++ b/node/rss.js @@ -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}`, diff --git a/node/showdown.js b/node/showdown.js deleted file mode 100644 index 07145b6..0000000 --- a/node/showdown.js +++ /dev/null @@ -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"] - }); -}; \ No newline at end of file diff --git a/node/util.js b/node/util.js new file mode 100644 index 0000000..6afeca2 --- /dev/null +++ b/node/util.js @@ -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*/ `

This story was inspired by a Reedsy Prompt and submitted to their competition. As such, it has also been published on their website.

` : ""} + ${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"] + }); + } +}; \ No newline at end of file