From 0f6c06d3b37dbc277b211521a062223d96c540d0 Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Fri, 29 Mar 2024 11:20:49 +0000 Subject: Refactor instance description --- src/lib/config.ts | 26 ++++++++++++++++++++++++++ src/routes/frontend.ts | 31 ++++++++----------------------- views/home.handlebars | 12 +++++++++++- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/lib/config.ts b/src/lib/config.ts index 4bc43bd..b4086d8 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -2,6 +2,7 @@ import fs from "fs"; import toml from "toml"; import { exitWithError } from "./process.js"; import { Response } from "express"; +import { markdownToSanitizedHTML } from "../util/markdown.js"; interface StaticPage { title: string; @@ -152,6 +153,31 @@ export const instanceRules = (): InstanceRule[] => { return rules; }; +export const instanceDescription = (): string => { + const config = getConfig(); + const defaultInstanceDescription = + "**{{ siteName }}** is running on Gathio — a simple, federated, privacy-first event hosting platform."; + let instanceDescription = defaultInstanceDescription; + try { + if (fs.existsSync("./static/instance-description.md")) { + const fileBody = fs.readFileSync( + "./static/instance-description.md", + "utf-8", + ); + instanceDescription = markdownToSanitizedHTML(fileBody); + } + // Replace {{siteName}} with the instance name + instanceDescription = instanceDescription.replace( + /\{\{ ?siteName ?\}\}/g, + config?.general.site_name, + ); + return instanceDescription; + } catch (err) { + console.log(err); + return defaultInstanceDescription; + } +}; + // Attempt to load our global config. Will stop the app if the config file // cannot be read (there's no point trying to continue!) export const getConfig = (): GathioConfig => { diff --git a/src/routes/frontend.ts b/src/routes/frontend.ts index 4e6f3ed..86ad69c 100644 --- a/src/routes/frontend.ts +++ b/src/routes/frontend.ts @@ -3,7 +3,11 @@ import fs from "fs"; import moment from "moment-timezone"; import { marked } from "marked"; import { markdownToSanitizedHTML, renderPlain } from "../util/markdown.js"; -import { frontendConfig, instanceRules } from "../lib/config.js"; +import { + frontendConfig, + instanceDescription, + instanceRules, +} from "../lib/config.js"; import { addToLog, exportICal } from "../helpers.js"; import Event from "../models/Event.js"; import EventGroup, { IEventGroup } from "../models/EventGroup.js"; @@ -27,6 +31,7 @@ router.get("/", (_: Request, res: Response) => { return res.render("home", { ...frontendConfig(res), instanceRules: instanceRules(), + instanceDescription: instanceDescription(), }); }); @@ -34,6 +39,7 @@ router.get("/about", (_: Request, res: Response) => { return res.render("home", { ...frontendConfig(res), instanceRules: instanceRules(), + instanceDescription: instanceDescription(), }); }); @@ -122,33 +128,12 @@ router.get("/events", async (_: Request, res: Response) => { }; }); - // Attempt to pull the instance description from a Markdown file - const defaultInstanceDescription = - "**{{ siteName }}** is running on Gathio — a simple, federated, privacy-first event hosting platform."; - let instanceDescription = defaultInstanceDescription; - try { - if (fs.existsSync("./static/instance-description.md")) { - const fileBody = fs.readFileSync( - "./static/instance-description.md", - "utf-8", - ); - instanceDescription = markdownToSanitizedHTML(fileBody); - } - // Replace {{siteName}} with the instance name - instanceDescription = instanceDescription.replace( - /\{\{ ?siteName ?\}\}/g, - res.locals.config?.general.site_name, - ); - } catch (err) { - console.log(err); - } - res.render("publicEventList", { title: "Public events", upcomingEvents: upcomingEvents, pastEvents: pastEvents, eventGroups: updatedEventGroups, - instanceDescription, + instanceDescription: instanceDescription(), instanceRules: instanceRules(), ...frontendConfig(res), }); diff --git a/views/home.handlebars b/views/home.handlebars index aa98823..0d294d3 100755 --- a/views/home.handlebars +++ b/views/home.handlebars @@ -1,8 +1,18 @@
-

Gathio is a simple, federated, privacy-first event hosting platform.

+

About {{siteName}}

+ +{{#if instanceDescription}} +
+ {{{instanceDescription}}} +
+{{/if}} {{> instanceRules }} +

About Gathio

+ +

Gathio is a simple, federated, privacy-first event hosting platform.

+
An example event page for a picnic. The page shows the event's location, host, date and time, and description, as well as buttons to save the event to Google Calendar, export it, and open the location in OpenStreetMap and Google Maps.
-- cgit v1.2.3