49 lines
No EOL
1.7 KiB
JavaScript
49 lines
No EOL
1.7 KiB
JavaScript
const {
|
|
JSDOM
|
|
} = require("jsdom");
|
|
const showdown = require("showdown");
|
|
require("./lib/showdown-prettify");
|
|
require("./lib/showdown-footnotes");
|
|
|
|
exports.showdown = function (headerLevel) {
|
|
return new showdown.Converter({
|
|
parseImgDimensions: true,
|
|
headerLevelStart: headerLevel,
|
|
extensions: ["prettify", "footnotes"]
|
|
});
|
|
};
|
|
|
|
exports.extractBookData = function (file, post, reduceHeadings) {
|
|
let html = new JSDOM(file);
|
|
let page = html.window.document.getElementById("page");
|
|
// remove header section
|
|
let header = page.getElementsByTagName("header");
|
|
while (header.length > 0)
|
|
header[0].parentNode.removeChild(header[0]);
|
|
if (reduceHeadings) {
|
|
// reduce all headings by 1
|
|
for (let h = 5; h >= 1; h--) {
|
|
let heading = page.getElementsByTagName(`h${h}`);
|
|
while (heading.length > 0)
|
|
this.changeTag(heading[0], `h${h+1}`);
|
|
}
|
|
}
|
|
|
|
return /*html*/ `
|
|
${post.prompt ? /*html*/ `<p><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}
|
|
`;
|
|
};
|
|
|
|
exports.replaceRelativeLinks = function (element, tag) {
|
|
if (element[tag] && element[tag].startsWith("./"))
|
|
element[tag] = `../${element[tag].substring(2)}`;
|
|
};
|
|
|
|
exports.changeTag = function (element, newTag) {
|
|
const newElement = element.ownerDocument.createElement(newTag);
|
|
newElement.innerHTML = element.innerHTML;
|
|
for (let a of element.attributes)
|
|
newElement.setAttribute(a.nodeName, a.nodeValue);
|
|
element.parentNode.replaceChild(newElement, element);
|
|
}; |