Skip to content

Build your personal knowledge base with Trilium Notes

feat(client): highlight +xml/+json attachments as their base syntax

Elian Doranf3fc89967227
diff --git a/apps/client/src/widgets/type_widgets/File.spec.tsx b/apps/client/src/widgets/type_widgets/File.spec.tsx
index 6d0c2e6b8f..f8dad6054d 100644
--- a/apps/client/src/widgets/type_widgets/File.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/File.spec.tsx
@@ -51,6 +51,17 @@ describe("TextPreview", () => {
expect(applySingleBlockSyntaxHighlight.mock.calls[0][1]).toBe("text-html");
});
+ it("highlights structured-syntax suffixes (+xml, +json) as their base syntax", async () => {
+ render(<TextPreview content={"<ink/>"} mime="application/inkml+xml" />, container);
+ await vi.waitFor(() => expect(applySingleBlockSyntaxHighlight).toHaveBeenCalledTimes(1));
+ expect(applySingleBlockSyntaxHighlight.mock.calls[0][1]).toBe("text-xml");
+
+ render(null, container);
+ render(<TextPreview content={"{}"} mime="application/fhir+json" />, container);
+ await vi.waitFor(() => expect(applySingleBlockSyntaxHighlight).toHaveBeenCalledTimes(2));
+ expect(applySingleBlockSyntaxHighlight.mock.calls[1][1]).toBe("application-json");
+ });
+
it("does not highlight without a MIME type, but still renders the content", () => {
render(<TextPreview content={"plain"} />, container);
diff --git a/apps/client/src/widgets/type_widgets/File.tsx b/apps/client/src/widgets/type_widgets/File.tsx
index 30ccbfe4e9..2c742e8693 100644
--- a/apps/client/src/widgets/type_widgets/File.tsx
+++ b/apps/client/src/widgets/type_widgets/File.tsx
@@ -40,7 +40,7 @@ export function TextPreview({ content, mime }: { content: string, mime?: string
<CodeBlock
className="file-preview-content"
code={trimmedContent}
- mimeType={mime}
+ mimeType={resolveHighlightMime(mime)}
copyable={!isTooLarge}
/>
</>
@@ -54,3 +54,18 @@ function NoPreview() {
</Alert>
);
}
+
+/**
+ * Resolves a MIME type to one the highlighter has a grammar for. Concrete types carrying an
+ * RFC 6839 structured-syntax suffix (e.g. `application/inkml+xml`, OneNote's ink debug source)
+ * are unknown to the registry as such, but highlight fine as their base syntax.
+ */
+function resolveHighlightMime(mime: string | undefined): string | undefined {
+ if (mime?.endsWith("+xml")) {
+ return "text/xml";
+ }
+ if (mime?.endsWith("+json")) {
+ return "application/json";
+ }
+ return mime;
+}