markdown support

This commit is contained in:
deadvey 2025-07-11 02:58:13 +01:00
parent b2c649d001
commit 78b836bef5
3 changed files with 53 additions and 38 deletions

View File

@ -11,10 +11,10 @@ In action on my website: [deadvey.com](https://deadvey.com)<br/>
* edit/delete posts
* probably insecure as hell
* hitcount
* Markdown syntax in posts
# planned features
* atom
* federation
* sign up
* Markdown syntax in posts
* All strings (including in edit and post page) customisable

87
app.js
View File

@ -53,7 +53,7 @@ function replace_format_indicators(input_string, post_index=0, tag_name="tag") {
.replaceAll("%%", "&#37;")
.replaceAll("%A", (post_object["tags"]))
.replaceAll("%B", (hyperlink_tags(post_object["tags"])))
.replaceAll("%C", post_object["content"].replaceAll("\n","<br/>"))
.replaceAll("%C", markdown_to_html(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 +65,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", post_object["title"])
.replaceAll("%T", markdown_to_html(post_object["title"]))
.replaceAll("%U", `/user/${users.users[post_object["userID"]]['username']}`)
.replaceAll("%Y", config.site_name)
.replaceAll("%W", config.site_description)
@ -89,6 +89,22 @@ 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!")
@ -142,40 +158,6 @@ app.get("/", (req,res) => {
}
res.send(`<html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body><footer>${footer_div}</footer></html>`);
});
app.get("/post", (req,res) => {
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head><form action="/submit_post" method="POST" onsubmit="sha512password()">
<label>Username: </label><input required name="username"><br/>
<label>Password: </label><input type="password" required id="password" name="password"><br/>
<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">
</form></html>`);
});
app.get("/edit/:post_id", (req,res) => {
const post_id = req.params.post_id
const post = posts.posts[post_id]
const user = users.users[post['userID']]
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head>
<form action="/submit_edit" method="POST" onsubmit="sha512password()">
<input name="userID" type="hidden" value="${post['userID']}">
<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>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">
</form></html>`);
});
app.get("/user/:username", (req, res) => {
header_div = config.user_page_header
header_div = replace_format_indicators(header_div)
@ -208,6 +190,39 @@ app.get("/tag/:tag", (req,res) => {
res.send(`<html><style>${config.css}</style><body><div id="header">${header_div}</div><div id="posts">${page_content}</div></body><footer>${footer_div}</footer></html>`);
});
app.get("/post", (req,res) => {
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head><form action="/submit_post" method="POST">
<label>Username: </label><input required name="username"><br/>
<label>Password: </label><input type="password" required id="password" name="password"><br/>
<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>
</form></html>`);
});
app.get("/edit/:post_id", (req,res) => {
const post_id = req.params.post_id
const post = posts.posts[post_id]
const user = users.users[post['userID']]
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head>
<form action="/submit_edit" method="POST" onsubmit="sha512password()">
<input name="userID" type="hidden" value="${post['userID']}">
<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>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">
</form></html>`);
});
app.post("/submit_edit", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const postID = req.body.postID

View File

@ -1 +1 @@
45
63