Skip to content

Build your personal knowledge base with Trilium Notes

feat(attachments): add pretty formatting to JSON

Elian Doran9e57c141300e
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index 12c8a21f47..40480e0734 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -118,8 +118,17 @@ async function renderText(note: FNote | FAttachment, $renderedContent: JQuery<HT
async function renderCode(note: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>) {
const blob = await note.getBlob();
+ let content = blob?.content || "";
+ if (note.mime === "application/json") {
+ try {
+ content = JSON.stringify(JSON.parse(content), null, 4);
+ } catch (e) {
+ // Ignore JSON parsing errors.
+ }
+ }
+
const $codeBlock = $("<code>");
- $codeBlock.text(blob?.content || "");
+ $codeBlock.text(content);
$renderedContent.append($("<pre>").append($codeBlock));
await applySingleBlockSyntaxHighlight($codeBlock, normalizeMimeTypeForCKEditor(note.mime));
}