Skip to content

Commit

Permalink
feat: allow to define format for page title
Browse files Browse the repository at this point in the history
respect this config option also when generating static content.

closes AOEpeople#376
  • Loading branch information
d-koppenhagen authored and srotsch committed Jun 28, 2023
1 parent 8876d3b commit 810db6a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ You can customize the following parts of the tech radar.
### Change title, description and headline
Set the environment variable `REACT_APP_RADAR_NAME`. The default is "AOE Technology Radar".

Set the environment variable `REACT_APP_RADAR_TITLE_FORMAT` to define the title format for each technology page.
You can use two placeholders here:

- `%TECHNOLOGY_NAME%`: The name of the technology will be inserted
- `%APP_TITLE%`: The base app name (from `REACT_APP_RADAR_NAME`) will be inserted

For example: `%TECHNOLOGY_NAME% | %APP_TITLE%`

### Host the application under a sub path
To host the application under a sub path, set the environment variable `PUBLIC_URL`, e.g. "/techradar". The default is "/".

Expand Down
2 changes: 1 addition & 1 deletion dist_scripts/scripts/buildRadar.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var runCommand = function (command) {
var executedCommand = (0, child_process_1.spawn)(command, {
stdio: "inherit",
shell: true,
env: __assign({ REACT_APP_RADAR_NAME: "AOE Technology Radar", REACT_APP_BUILDHASH: (0, crypto_1.randomBytes)(10).toString("hex"), GENERATE_SOURCEMAP: "false" }, process.env),
env: __assign({ REACT_APP_RADAR_NAME: "AOE Technology Radar", REACT_APP_RADAR_TITLE_FORMAT: "%TECHNOLOGY_NAME% | %APP_TITLE%", REACT_APP_BUILDHASH: (0, crypto_1.randomBytes)(10).toString("hex"), GENERATE_SOURCEMAP: "false" }, process.env),
});
executedCommand.on("error", function (error) {
reject(error);
Expand Down
2 changes: 2 additions & 0 deletions dist_scripts/scripts/createStaticFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ var createStaticFiles = function () { return __awaiter(void 0, void 0, void 0, f
jsdom_1.JSDOM.fromFile(targetPath).then(function (dom) {
var document = dom.window.document;
var rootEl = document.getElementById("root");
document.title = "test";
(0, config_1.setTitle)(document, item.title);
if (rootEl) {
var textNode = document.createElement("div");
var bodyFragment = jsdom_1.JSDOM.fragment(item.body);
Expand Down
9 changes: 8 additions & 1 deletion dist_scripts/src/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.translate = exports.assetUrl = exports.publicUrl = exports.isMobileViewport = exports.getItemPageNames = exports.radarNameShort = exports.radarName = void 0;
exports.translate = exports.assetUrl = exports.publicUrl = exports.isMobileViewport = exports.getItemPageNames = exports.setTitle = exports.titleFormat = exports.radarNameShort = exports.radarName = void 0;
exports.radarName = process.env.REACT_APP_RADAR_NAME || "AOE Technology Radar";
exports.radarNameShort = exports.radarName;
exports.titleFormat = process.env.REACT_APP_RADAR_TITLE_FORMAT || "%TECHNOLOGY_NAME% | %APP_TITLE%";
function setTitle(document, title) {
document.title = exports.titleFormat
.replace("%TECHNOLOGY_NAME%", title)
.replace("%APP_TITLE%", exports.radarName);
}
exports.setTitle = setTitle;
var getItemPageNames = function (items) {
return items.map(function (item) { return "".concat(item.quadrant, "/").concat(item.name); });
};
Expand Down
1 change: 1 addition & 0 deletions scripts/buildRadar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const runCommand = (command: string) =>
shell: true,
env: {
REACT_APP_RADAR_NAME: "AOE Technology Radar",
REACT_APP_RADAR_TITLE_FORMAT: "%TECHNOLOGY_NAME% | %APP_TITLE%",
REACT_APP_BUILDHASH: randomBytes(10).toString("hex"),
GENERATE_SOURCEMAP: "false",
...process.env,
Expand Down
6 changes: 5 additions & 1 deletion scripts/createStaticFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { JSDOM } from "jsdom";
import XmlSitemap from "xml-sitemap";

import { publicUrl } from "../src/config";
import { publicUrl, setTitle } from "../src/config";
import { createRadar } from "./generateJson/radar";

// Do this as the first thing so that any code reading it knows the right env.
Expand Down Expand Up @@ -56,6 +56,10 @@ const createStaticFiles = async () => {
const document = dom.window.document;
const rootEl = document.getElementById("root");

document.title = 'test'

setTitle(document, item.title)

if (rootEl) {
const textNode = document.createElement("div");
const bodyFragment = JSDOM.fragment(item.body);
Expand Down
4 changes: 2 additions & 2 deletions src/components/SetTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useEffect } from "react";

import { radarName } from "../config";
import { setTitle } from "../config";

type SetTitleProps = {
title: string;
};

export default function SetTitle({ title }: SetTitleProps) {
useEffect(() => {
document.title = `${title} | ${radarName}`;
setTitle(document, title)
}, [title]);

return null;
Expand Down
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export interface ConfigData {
export const radarName =
process.env.REACT_APP_RADAR_NAME || "AOE Technology Radar";
export const radarNameShort = radarName;
export const titleFormat = process.env.REACT_APP_RADAR_TITLE_FORMAT || "%TECHNOLOGY_NAME% | %APP_TITLE%"

export function setTitle(document: Document, title: string) {
document.title = titleFormat
.replace('%TECHNOLOGY_NAME%', title)
.replace('%APP_TITLE%', radarName)
}

export const getItemPageNames = (items: Item[]) =>
items.map((item) => `${item.quadrant}/${item.name}`);
Expand Down

0 comments on commit 810db6a

Please sign in to comment.