Skip to content

Commit

Permalink
Enables markdown rendering for replies.
Browse files Browse the repository at this point in the history
  • Loading branch information
qubyte committed May 5, 2024
1 parent 100c90f commit 98676f2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion content/replies/1714906247110.jf2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "entry",
"in-reply-to": "https://adactio.com/journal/21104",
"content": "If you want to avoid using a nonce value, the other way to go about it is to hash the resource and put that in the CSP header. I blogged about the interaction between CSP and import maps (which must be inline), and caching and it turns out to work really well: https://qubyte.codes/blog/progressively-enhanced-caching-of-javascript-modules-without-bundling-using-import-maps",
"content": "If you want to avoid using a nonce value, the other way to go about it is to hash the resource and put that in the CSP header. [I blogged about the interaction between CSP and import maps](https://qubyte.codes/blog/progressively-enhanced-caching-of-javascript-modules-without-bundling-using-import-maps) (which must be inline), and caching and it turns out to work really well.",
"category": {},
"mp-slug": "if-you-want-to-avoid-using",
"name": "Adactio: Journal—Securing client-side JavaScript",
Expand Down
18 changes: 10 additions & 8 deletions lib/load-reply-files.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

import { readdir, readFile } from 'node:fs/promises';
import render from './render.js';
import handlebars from 'handlebars';
import getTimezone from './get-timezone.js';
import Page from './page.js';
Expand Down Expand Up @@ -46,20 +47,21 @@ class ReplyPage extends Page {

export default async function loadReplyFiles({ baseUrl, dir }) {
const filenames = await readdir(dir);
const notes = await Promise.all(filenames.filter(fn => fn.endsWith('.json')).map(async filename => {
const note = JSON.parse(await readFile(new URL(filename, dir), 'utf8'));
const timezone = getTimezone(note.location?.latitude, note.location?.longitude);
const replies = await Promise.all(filenames.filter(fn => fn.endsWith('.json')).map(async filename => {
const reply = JSON.parse(await readFile(new URL(filename, dir), 'utf8'));
const content = render(reply.content);
const timezone = getTimezone(reply.location?.latitude, reply.location?.longitude);

return new ReplyPage({
baseUrl,
content: note.content,
date: new Date(note.published),
content,
date: new Date(reply.published),
timezone,
inReplyTo: note['in-reply-to']
inReplyTo: reply['in-reply-to']
});
}));

notes.sort((a, b) => b.timestamp - a.timestamp);
replies.sort((a, b) => b.timestamp - a.timestamp);

return notes;
return replies;
}
2 changes: 1 addition & 1 deletion lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ marked.use(
* @param {Map<string, { width: number, height: number }>} imagesMap
* @returns {string}
*/
export default function render(input, imagesMap) {
export default function render(input, imagesMap = new Map()) {
return marked(input, { async: false, imagesMap });
}

4 changes: 3 additions & 1 deletion src/templates/partials/reply.html.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<header>
<a class="u-url" href="{{canonical}}"><time datetime="{{datetime}}" class="dt-published">{{humanDateTime datetime timezone}}</time></a>
</header>
<p class="e-content p-name">{{content}}</p>
<div class="e-content p-name">
{{{content}}}
</div>
<p>In reply to: <a class="u-in-reply-to" href="{{inReplyTo}}">{{inReplyTo}}</a></p>
</article>

0 comments on commit 98676f2

Please sign in to comment.