fix: generate static files content

closes #327
This commit is contained in:
Danny Koppenhagen
2023-04-28 21:05:44 +02:00
committed by Stefan Rotsch
parent a81221573a
commit 71e397459f
4 changed files with 888 additions and 340 deletions

View File

@@ -42,6 +42,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var xml_sitemap_1 = __importDefault(require("xml-sitemap"));
var jsdom_1 = require("jsdom");
var config_1 = require("../src/config");
var radar_1 = require("./generateJson/radar");
// Do this as the first thing so that any code reading it knows the right env.
@@ -80,7 +81,26 @@ var createStaticFiles = function () { return __awaiter(void 0, void 0, void 0, f
};
sitemap.add("".concat(config_1.publicUrl, "index.html"), sitemapOptions);
radar.items.forEach(function (item) {
(0, fs_1.copyFileSync)("build/index.html", "build/".concat(item.quadrant, "/").concat(item.name, ".html"));
var targetPath = "build/".concat(item.quadrant, "/").concat(item.name, ".html");
(0, fs_1.copyFileSync)("build/index.html", targetPath);
jsdom_1.JSDOM.fromFile(targetPath).then(function (dom) {
var document = dom.window.document;
var rootEl = document.getElementById("root");
if (rootEl) {
var textNode = document.createElement("div");
var bodyFragment = jsdom_1.JSDOM.fragment(item.body);
textNode.appendChild(bodyFragment);
var headlineNode = document.createElement("h1");
var titleText = document.createTextNode(item.title);
headlineNode.appendChild(titleText);
rootEl.appendChild(headlineNode);
rootEl.appendChild(textNode);
}
else {
console.warn('Element with ID "root" not found. Static site content will be empty.');
}
(0, fs_1.writeFileSync)(targetPath, dom.serialize());
});
sitemap.add("".concat(config_1.publicUrl).concat(item.quadrant, "/").concat(item.name, ".html"), sitemapOptions);
});
(0, fs_1.writeFileSync)("build/sitemap.xml", sitemap.xml);

1175
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -42,6 +42,7 @@
"fs-extra": "11.1.1",
"highlight.js": "11.7.0",
"husky": "8.0.3",
"jsdom": "21.1.1",
"marked": "4.3.0",
"moment": "2.29.4",
"postcss-normalize": "10.0.1",
@@ -62,6 +63,7 @@
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "4.1.1",
"@types/jsdom": "21.1.1",
"@types/marked": "4.0.8",
"@types/node": "18.15.11",
"@typescript-eslint/parser": "5.58.0",

View File

@@ -7,6 +7,7 @@ import {
writeFileSync,
} from "fs";
import XmlSitemap from "xml-sitemap";
import { JSDOM } from 'jsdom';
import { publicUrl } from "../src/config";
import { createRadar } from "./generateJson/radar";
@@ -48,11 +49,37 @@ const createStaticFiles = async () => {
sitemap.add(`${publicUrl}index.html`, sitemapOptions);
radar.items.forEach((item) => {
const targetPath = `build/${item.quadrant}/${item.name}.html`
copyFileSync(
"build/index.html",
`build/${item.quadrant}/${item.name}.html`
targetPath
);
JSDOM.fromFile(targetPath).then(dom => {
const document = dom.window.document;
const rootEl = document.getElementById("root")
if (rootEl) {
const textNode = document.createElement("div")
const bodyFragment = JSDOM.fragment(item.body);
textNode.appendChild(bodyFragment)
const headlineNode = document.createElement("h1")
const titleText = document.createTextNode(item.title);
headlineNode.appendChild(titleText)
rootEl.appendChild(headlineNode)
rootEl.appendChild(textNode)
} else {
console.warn('Element with ID "root" not found. Static site content will be empty.')
}
writeFileSync(
targetPath,
dom.serialize()
);
});
sitemap.add(`${publicUrl}${item.quadrant}/${item.name}.html`, sitemapOptions);
});