better markdown

This commit is contained in:
deadvey 2025-07-11 12:09:09 +01:00
parent 78b836bef5
commit b538d495db
3 changed files with 21 additions and 32 deletions

44
app.js
View File

@ -1,12 +1,13 @@
const express = require('express');
const showdown = require('showdown')
const crypto = require('crypto'); // For encrypting passwords
const { fromUnixTime, format, getUnixTime } = require("date-fns")
const fs = require('fs');
const users = require('./users.js');
const posts = require('./posts.js');
const config = require('./config.js');
let converter = new showdown.Converter({simpleLineBreaks: true, tables: true, strikethrough: true, tasklists: true, encodeEmails: true})
const app = express();
const port = 8080;
let footer_div = config.site_wide_footer
footer_div = replace_format_indicators(footer_div)
@ -53,7 +54,7 @@ function replace_format_indicators(input_string, post_index=0, tag_name="tag") {
.replaceAll("%%", "%")
.replaceAll("%A", (post_object["tags"]))
.replaceAll("%B", (hyperlink_tags(post_object["tags"])))
.replaceAll("%C", markdown_to_html(post_object["content"]))
.replaceAll("%C", converter.makeHtml(post_object["content"]))
.replaceAll("%D", unix_time_to_date_format(post_object["pubdate"]))
.replaceAll("%E", unix_time_to_date_format(post_object["editdate"]))
.replaceAll("%F", users.users[post_object["userID"]]['prettyname'])
@ -65,7 +66,7 @@ function replace_format_indicators(input_string, post_index=0, tag_name="tag") {
.replaceAll("%O", `/edit/${post_index}`)
.replaceAll("%R", "/rss")
.replaceAll("%S", config.seperator)
.replaceAll("%T", markdown_to_html(post_object["title"]))
.replaceAll("%T", post_object["title"])
.replaceAll("%U", `/user/${users.users[post_object["userID"]]['username']}`)
.replaceAll("%Y", config.site_name)
.replaceAll("%W", config.site_description)
@ -89,22 +90,6 @@ function escape_input(input) {
return output
}
// Markdown supported:
// *text* = bold
// _text_ = italic
// [page](url) = hyperlink
// ```code``` = code
function markdown_to_html(input) {
// FIXME backticks before these should negate stuff
let output = input
.replaceAll(/\[(.*)\]\((https:\/\/|http:\/\/)(.*)\)/g, "<a href='$2$3'>$1</a>")
.replaceAll(/\*(.*)\*/g,"<b>$1</b>")
.replaceAll(/_(.*)_/g,"<i>$1</i>")
.replaceAll(/```(.*)```/gms,"<code>$1</code>")
.replaceAll("\n","<br/>")
return output
}
app.get(config.rss_path, (req,res) => {
if (config.rss == false) {
res.send("Sorry, RSS is disabled!")
@ -122,7 +107,7 @@ app.get(config.rss_path, (req,res) => {
<item>
<title>${posts.posts[i]["title"]}</title>
<link>${config.site_url}/post/${i}.${config.file_extension}</link>
<description><![CDATA[${posts.posts[i]["content"].replaceAll("\n","<br/>")}]]></description>
<description><![CDATA[${converter.makeHtml(posts.posts[i]["content"])}]]></description>
<guid isPermaLink="true">${config.site_url}/post/${i}</guid>
<pubDate>${unix_time_to_rss_date(posts.posts[i]['pubdate'])}</pubDate>`
for (let j = 0; j < posts.posts[i]['tags'].length; j++) {
@ -197,8 +182,8 @@ app.get("/post", (req,res) => {
<label>Title: </label><input required name="title"><br/>
<label>Content*: </label><textarea required name="content"></textarea><br/>
<label>Tags (comma seperated): </label><input name="tags"><br/>
<input type="submit" value="Submit">
<small>* Markdown supported: *bold*, _italic_, (link)[url] and \`\`\`code block\`\`\`</small>
<input type="submit" value="Submit"><br/>
<small>* Markdown supported</small>
</form></html>`);
});
app.get("/edit/:post_id", (req,res) => {
@ -211,15 +196,12 @@ app.get("/edit/:post_id", (req,res) => {
<input name="postID" type="hidden" value="${post_id}">
<label>${user.prettyname}'s Password: </label><input type="password" required id="password" name="password"><br/>
<label>Title: </label><input value="${post['title']}" required name="title"><br/>
<label>Content: </label>
<textarea required name="content">${post['content']
.replaceAll('"', "&#34;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll("\\", "&#92;")}</textarea><br/>
<label>Content*: </label>
<textarea required name="content">${post['content']}</textarea><br/>
<label>Tags (comma seperated): </label><input value="${post['tags']}" name="tags"><br/>
<label>Delete forever (no undo): </label><input name="delete" type="checkbox"><br/>
<input type="submit" value="Submit">
<input type="submit" value="Submit"><br/>
<small>* Markdown supported</small>
</form></html>`);
});
@ -281,6 +263,6 @@ app.post("/submit_post", (req,res) => {
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port} in ${config.root_path}`);
app.listen(config.port, () => {
console.log(`Server is running at http://localhost:${config.port} in ${config.root_path}`);
});

View File

@ -1,6 +1,7 @@
export const seperator = "<hr/>"
export const site_name = "Deadvey's Blog"
export const site_url = "https://deadvey.com"
export const port = 8080
export const site_description = "Films, tech, random shit"
export const timeline_length = 20
export const enable_hitcount = true // Can slow down page loading a bit
@ -103,6 +104,9 @@ export const css = `
color: #282828;
border: 1px solid #282828;
}
table {
border: solid 1px #282828;
}
}
@media (prefers-color-scheme: dark) {
body {
@ -129,6 +133,9 @@ export const css = `
color: #ebdbb2;
border: 1px solid #ebdbb2;
}
table, td, th {
border: solid 1px #ebdbb2;
}
}
`

View File

@ -1 +1 @@
63
124