-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.js
More file actions
68 lines (57 loc) · 1.93 KB
/
render.js
File metadata and controls
68 lines (57 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// render.js
import { renderTemplate } from './templates.js';
import { escapeHtml } from './utils.js';
import { marked } from 'marked';
import { filterXSS } from 'xss';
function getExcerpt(content, maxLength = 300) {
// Strip markdown formatting for clean excerpt
const plainText = content
.replace(/[#*`_~()]/g, '')
.replace(/\n+/g, ' ')
.trim();
if (plainText.length <= maxLength) return content; // Return full content if short
// Find the last space before maxLength to avoid cutting words
const lastSpace = plainText.lastIndexOf(' ', maxLength);
const cutoff = lastSpace > -1 ? lastSpace : maxLength;
return plainText.substring(0, cutoff) + '...';
}
// Function to render individual posts
export function renderPost(post, user) {
const timestamp = post.created_at
? new Date(post.created_at).toLocaleString()
: 'Unknown date';
let adminLinks = '';
if (user) {
adminLinks = `
<button onclick="window.location.href='/admin/edit/${post.id}'">Edit</button>
<form action="/admin/delete/${post.id}" method="POST" style="display:inline;" class="delete-link">
<button type="submit" onclick="return confirm('Are you sure you want to delete this post?');">Delete</button>
</form>
`;
}
// Convert Markdown content to HTML
const htmlContent = marked.parse(post.content);
// Sanitize the HTML content
const sanitizedContent = filterXSS(htmlContent);
return `
<article>
<h2>${escapeHtml(post.title)}</h2>
<time datetime="${post.created_at}" class="timestamp">Posted on: ${timestamp}</time>
<div class="post-content">${sanitizedContent}</div>
${adminLinks}
</article>
<hr>
`;
}
// Function to render the main blog page
export function renderBlogPage(postsHtml, authLink) {
return renderTemplate('deadlight.boo', `
<header>
<h1>deadlight.boo</h1>
<nav>${authLink}</nav>
</header>
<main>
${postsHtml}
</main>
`);
}