add markdown post support
This commit is contained in:
13
package-lock.json
generated
13
package-lock.json
generated
@@ -10,6 +10,7 @@
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"marked": "^14.1.3",
|
||||
"nodemailer": "^6.9.15"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -464,6 +465,18 @@
|
||||
"node": ">= 0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/marked": {
|
||||
"version": "14.1.3",
|
||||
"resolved": "https://registry.npmjs.org/marked/-/marked-14.1.3.tgz",
|
||||
"integrity": "sha512-ZibJqTULGlt9g5k4VMARAktMAjXoVnnr+Y3aCqW1oDftcV4BA3UmrBifzXoZyenHRk75csiPu9iwsTj4VNBT0g==",
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"marked": "bin/marked.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 18"
|
||||
}
|
||||
},
|
||||
"node_modules/media-typer": {
|
||||
"version": "0.3.0",
|
||||
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"marked": "^14.1.3",
|
||||
"nodemailer": "^6.9.15"
|
||||
},
|
||||
"name": "enstrayedapi",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { app, globalConfig, fs, globalVersion } from "../index.js" // Get globals from index
|
||||
import { marked } from "marked"
|
||||
|
||||
var timeSinceLastQuery = Date.now()-10000
|
||||
var cachedResult = ""
|
||||
@@ -8,7 +9,17 @@ app.get("/static/*", (rreq,rres) => {
|
||||
})
|
||||
|
||||
app.get("/posts/*", (rreq,rres) => {
|
||||
|
||||
if (rreq.url.endsWith(".md")) {
|
||||
let file = fs.readFileSync("./static/markdownposttemplate.html","utf-8")
|
||||
file = file.replace("<!--SSR_REPLACE_URL-->",`https://enstrayed.com${rreq.url}`)
|
||||
file = file.replaceAll("<!--SSR_REPLACE_TITLE-->",rreq.url.replace("/posts/","").slice(9).replace(/-/g," ").replace(".md",""))
|
||||
file = file.replace("<!--SSR_REPLACE_BODY-->",marked.parse(fs.readFileSync(globalConfig.frontpage.directory+"posts/"+rreq.url.replace("/posts/",""),"utf-8")))
|
||||
rres.send(file)
|
||||
} else {
|
||||
rres.sendFile(globalConfig.frontpage.directory+"posts/"+rreq.url.replace("/posts/",""))
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
app.get("/", (rreq, rres) => {
|
||||
@@ -26,14 +37,14 @@ function parseFiles() {
|
||||
let result = ""
|
||||
|
||||
for (let x in files) {
|
||||
if (files[x].endsWith(".html") === false) { break } // If file/dir is not .html then ignore
|
||||
if (files[x].endsWith(".html") === false && files[x].endsWith(".md") === false ) { break } // If file/dir is not .html or .md then ignore
|
||||
|
||||
let date = files[x].split("-")[0]
|
||||
if (date < 10000000 || date > 99999999) { break } // If date does not fit ISO8601 format then ignore
|
||||
|
||||
date = date.replace(/.{2}/g,"$&-").replace("-","").slice(0,-1) // Insert a dash every 2 characters, remove the first dash, remove the last character
|
||||
|
||||
let name = files[x].slice(9).replace(/-/g," ").replace(".html","") // Strip Date, replace seperator with space & remove file extension
|
||||
let name = files[x].slice(9).replace(/-/g," ").replace(".html","").replace(".md","") // Strip Date, replace seperator with space & remove file extension
|
||||
|
||||
result = `<span>${date} <a href="/posts/${files[x]}">${name}</a></span>`+result
|
||||
}
|
||||
|
||||
21
static/markdownposttemplate.html
Normal file
21
static/markdownposttemplate.html
Normal file
@@ -0,0 +1,21 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="/static/posts.css">
|
||||
<meta property="og:site_name" content="enstrayed.com">
|
||||
|
||||
<meta property="og:url" content="<!--SSR_REPLACE_URL-->>">
|
||||
|
||||
<title><!--SSR_REPLACE_TITLE--></title>
|
||||
<meta property="og:title" content="<!--SSR_REPLACE_TITLE-->">
|
||||
|
||||
<meta name="description" content="No description available.">
|
||||
<meta property="og:description" content="No description available.">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<a href="/">Return to enstrayed.com</a>
|
||||
<!--SSR_REPLACE_BODY-->
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user