diff --git a/docs/CONFIG.md b/docs/CONFIG.md index af7ffb7..aaad9bc 100755 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -14,7 +14,8 @@ All options show an example configuartion value and the variable type + an expla | enable_hitcount | true | Boolean | Enabling the hitcount (a number that represents the amount of front page loads (stored in hitcount.txt)) can slightly slow down loading of the front page. | | charset | "UTF-8" | String | This is the value in the tag in the html of all pages, you should not change this unless you know why. | | root_path | "/path/to/root/of/website" | String | Anything in this directory will be in the webroot, so put favicon.ico and anything else here. | - +| data_storage | "json" | String | JSON is currently the only supported format, but SQL is going to be added/is a work in progress | +| cache_data | true | String | Not caching data means you can edit the posts, users, comments etc, maunally and not have to restart the server, however, for large instances this is not reccomended as it takes longer to load the required data. Note: config.json always needs a restart | ## Basic Customisation | name | example value | variable type | explanation | |-----------------|----------------------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------| diff --git a/example-config.json b/example-config.json index eb1e4da..822d5c6 100755 --- a/example-config.json +++ b/example-config.json @@ -5,6 +5,7 @@ "site_url": "https://example.com", "language": "en-US", "port": 8080, + "cache_data": false, "allow_signup": true, "site_description": "Read my blogs!", "timeline_length": 20, diff --git a/src/data.js b/src/data.js index bd8b03e..ed5c7e2 100644 --- a/src/data.js +++ b/src/data.js @@ -1,6 +1,7 @@ import { createRequire } from "module"; const require = createRequire(import.meta.url); +const func = require('./functions.js') const config = require("../config.json") const fs = require("fs") @@ -8,14 +9,14 @@ export function getdata(data, index=-1) { if (config["data_storage"] == "json") { if (data == "posts" || data == 'users' || data == 'comments') { - let result = require(`../data/${data}.json`) + let result = func.require_module(`../data/${data}.json`) if (index != -1) { return result[index] } return result } else if (data == "hitcount") { - let result = require('../data/data.json') // This file is actually called data.json + let result = func.require_module('../data/data.json') // This file is actually called data.json return result["hitcount"] } else { diff --git a/src/functions.js b/src/functions.js index e1a0258..7fa8b70 100644 --- a/src/functions.js +++ b/src/functions.js @@ -4,6 +4,19 @@ const config = require("../config.json") const fs = require('fs') const locale = require(`../locales/${config.locale}.json`) +// This function requires a module without caching it +// So the server doesn't need to be restarted, though this can slow it down a bit. +// https://stackoverflow.com/a/16060619 +export function require_module(module) { + if (config.cache_data == false) { + delete require.cache[require.resolve(module)]; + return require(module); + } + else { + return require(module); + } +} + // The configuration defines a date format using the date-fns (a datetime library) syntax // eg "yyyy-MM-dd" // this converts unix time (an integer) into a string that is formatted according to config.js @@ -86,6 +99,7 @@ export function escape_input(input) { } // Render comment content by replacing the >> int with a url link to that comment +// Syntax: ">> postID-commentID" export function render_comment(comment_content) { return comment_content .replaceAll(/>> ([0-9]*)-([0-9]*)/g, ">> $1-$2") @@ -95,9 +109,11 @@ export function render_comment(comment_content) { .replaceAll("\n", "
") }; + +// Renders a string into markdown using markdown-it library export function render_md(content) { const markdownit = require("markdown-it") - const md = markdownit({ + const md = markdownit({ // this is just defining some options for markdown-it, should I add this to config.json? html: false, xhtmlOut: false, breaks: true,