Skip to content

Build your personal knowledge base with Trilium Notes

feat(fonts): set a bold and an italic run in the document sample

Elian Doran80e42983c400
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 478fad7bb0..f9c39e4964 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1846,7 +1846,7 @@
"search_placeholder": "Search fonts",
"no_fonts_found": "No font matches what you are looking for.",
"document_preview_heading": "Purple heart cuttings",
- "document_preview_body": "Snipped 4 stems below a node and stood them in water on the sill; the first roots showed after 9 days. It holds its colour in full sun and fades to green in the shade."
+ "document_preview_body": "Snipped 4 stems below a node and stood them in water on the sill; the first roots showed after <strong>9 days</strong>. It holds its colour in <em>full sun</em> and fades to green in the shade."
},
"max_content_width": {
"title": "Content Width",
diff --git a/apps/client/src/widgets/type_widgets/options/appearance_fonts.spec.tsx b/apps/client/src/widgets/type_widgets/options/appearance_fonts.spec.tsx
index d5aa2cf153..ba355e469f 100644
--- a/apps/client/src/widgets/type_widgets/options/appearance_fonts.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/appearance_fonts.spec.tsx
@@ -1,3 +1,6 @@
+import { readFileSync } from "node:fs";
+import { join } from "node:path";
+
import { render } from "preact";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -19,6 +22,14 @@ vi.mock("../../../services/utils", async (importOriginal) => ({
vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));
+// The real i18n is not initialized under test, so `Trans` would render the bare key and drop what
+// it wires in; the stub renders the components themselves, which is what the sample is answering for.
+vi.mock("react-i18next", () => ({
+ Trans: ({ i18nKey, components }: { i18nKey: string, components: Record<string, preact.VNode> }) => (
+ <span data-i18n-key={i18nKey}>{Object.values(components)}</span>
+ )
+}));
+
// The picker asks for the user's own fonts and registers each one it is given; only the list is of
// interest here, so the registration is stood in for.
vi.mock("../../../services/custom_fonts", () => ({
@@ -306,6 +317,23 @@ describe("the font settings", () => {
expect(document.querySelector(".font-picker-modal .font-size-description")?.textContent).toBe("fonts.size_relative_to_general");
});
+ it("sets the document sample's emphasis in the faces a family may not have", async () => {
+ await openPicker();
+ await act(async () => pickerTargets()[2].click());
+
+ // A family missing a bold or an italic has one sheared out of its upright by the browser,
+ // which is what the runs are there to show.
+ const sample = document.querySelector(".font-preview-document p");
+ expect(sample?.querySelector("strong")).not.toBeNull();
+ expect(sample?.querySelector("em")).not.toBeNull();
+
+ // The two halves have to name the same tags: the sentence carries them, the components map
+ // above answers for them, and a tag named in one alone renders as text in the paragraph.
+ const catalogue = JSON.parse(readFileSync(join(__dirname, "..", "..", "..", "translations", "en", "translation.json"), "utf-8"));
+ expect(catalogue.fonts.document_preview_body).toContain("<strong>");
+ expect(catalogue.fonts.document_preview_body).toContain("<em>");
+ });
+
it("leaves ligatures alone, since they come from the theme's own font", () => {
open();
diff --git a/apps/client/src/widgets/type_widgets/options/appearance_fonts.tsx b/apps/client/src/widgets/type_widgets/options/appearance_fonts.tsx
index 20955da9a7..bf3160f1b1 100644
--- a/apps/client/src/widgets/type_widgets/options/appearance_fonts.tsx
+++ b/apps/client/src/widgets/type_widgets/options/appearance_fonts.tsx
@@ -5,6 +5,7 @@ import clsx from "clsx";
import { ComponentChildren, Fragment } from "preact";
import { createPortal } from "preact/compat";
import { useEffect, useMemo, useRef, useState } from "preact/hooks";
+import { Trans } from "react-i18next";
import FNote from "../../../entities/fnote";
import { getCustomFonts, registerFontNote } from "../../../services/custom_fonts";
@@ -453,12 +454,24 @@ function TreePreviewRow({ note, expanded, nested }: { note: FNote; expanded?: bo
* A heading over a paragraph, which is the shape the font is being chosen for. One line says
* nothing about how a reading font behaves at length: the spacing between the lines, and the weight
* a heading takes against the text under it.
+ *
+ * The paragraph carries a bold and an italic run, which a family missing either of those faces
+ * cannot draw: the browser shears and thickens the upright one instead, and notes are written in
+ * both often enough that a font that fails at them is the wrong font to have chosen.
*/
function DocumentPreview() {
return (
<div className="font-preview-document">
<h3>{t("fonts.document_preview_heading")}</h3>
- <p>{t("fonts.document_preview_body")}</p>
+ <p>
+ <Trans
+ i18nKey="fonts.document_preview_body"
+ components={{
+ strong: <strong />,
+ em: <em />
+ }}
+ />
+ </p>
</div>
);
}