Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(markdown): require doubled tildes for strikethrough
Elian Dorand54530fe626a
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsxindex 10ac6c9862..b9774df816 100644--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx@@ -3,6 +3,7 @@ import "./MarkdownCommons.css";import VanillaCodeMirror from "@triliumnext/codemirror";import { CustomMarkdownRenderer, renderToHtml } from "@triliumnext/commons/src/lib/markdown_renderer";+import { createLiteralTildeExtension } from "@triliumnext/commons/src/lib/marked_extensions";import DOMPurify from "dompurify";import { Marked, type Tokens } from "marked";import { createContext } from "preact";@@ -27,6 +28,9 @@ import { useSlashCommands } from "./completions";import { insertText, replaceSelection, uploadImageAndInsert } from "./editor_utils";const marked = new Marked({ breaks: true, gfm: true });+// Headings in the outline are rendered by this instance rather than by `renderToHtml`, so it needs+// the same single-tilde handling to stay consistent with the preview body.+marked.use({ extensions: [createLiteralTildeExtension()] });/*** The default {@link CustomMarkdownRenderer} falls back todiff --git a/packages/commons/src/lib/markdown_renderer.spec.ts b/packages/commons/src/lib/markdown_renderer.spec.tsindex 42306e0f0f..7744c31c18 100644--- a/packages/commons/src/lib/markdown_renderer.spec.ts+++ b/packages/commons/src/lib/markdown_renderer.spec.ts@@ -215,6 +215,18 @@ describe("renderToHtml", () => {});});+ describe("strikethrough (literal tilde)", () => {+ it("strikes through only doubled tildes, leaving lone ones literal", () => {+ expect(render("this is ~~gone~~ now")).toBe("<p>this is <del>gone</del> now</p>");+ // Two approximations in one paragraph must not pair up into a strikethrough.+ expect(render("a ~07:50 departure, supplement (~€11)"))+ .toBe("<p>a ~07:50 departure, supplement (~€11)</p>");+ // A lone tilde inside a real strikethrough stays part of the struck text.+ expect(render("~~strike with ~5 min inside~~"))+ .toBe("<p><del>strike with ~5 min inside</del></p>");+ });+ });+describe("lists", () => {it("renders a task list with checkbox inputs and labels", () => {const html = render("- [ ] a\n- [x] b");diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.tsindex ccb2dcb057..310ffa807f 100644--- a/packages/commons/src/lib/markdown_renderer.ts+++ b/packages/commons/src/lib/markdown_renderer.ts@@ -83,6 +83,7 @@ import { getMimeTypeFromMarkdownName, MIME_TYPE_AUTO, normalizeMimeTypeForCKEditimport {createCommentExtension,createHighlightExtension,+ createLiteralTildeExtension,createTransclusionExtension,createWikiLinkExtension,transclusionExtension,@@ -583,7 +584,8 @@ export function renderToHtml(content: string, title: string, options: RenderToHtconst extensions = [options.transclusion ? createTransclusionExtension(options.transclusion) : transclusionExtension,options.wikiLink ? createWikiLinkExtension(options.wikiLink) : wikiLinkExtension,- createHighlightExtension()+ createHighlightExtension(),+ createLiteralTildeExtension()];if (options.obsidian) {extensions.push(createCommentExtension());diff --git a/packages/commons/src/lib/marked_extensions.ts b/packages/commons/src/lib/marked_extensions.tsindex 97fc7f0908..281b92d75a 100644--- a/packages/commons/src/lib/marked_extensions.ts+++ b/packages/commons/src/lib/marked_extensions.ts@@ -188,6 +188,42 @@ export function createCommentExtension(): TokenizerAndRendererExtension {};}+/**+ * Creates an extension that keeps a lone `~` literal, so only `~~text~~` strikes text through.+ *+ * GFM also accepts a single tilde as a strikethrough delimiter, which is a poor fit for prose+ * where `~` overwhelmingly means "approximately". Two unrelated approximations in one paragraph+ * ("a ~07:50 departure … the supplement (~€11–12)") otherwise pair up and strike out everything+ * between them — a frequent and very visible mangling of AI-written text. Losing the single-tilde+ * shorthand is the cheaper trade: the doubled form is what editors emit and what Obsidian and+ * Notion require.+ */+export function createLiteralTildeExtension(): TokenizerAndRendererExtension {+ return {+ name: "literalTilde",+ level: "inline",++ start(src: string) {+ return src.indexOf("~");+ },++ tokenizer(src) {+ // Consume the tilde as text; a doubled one is left to marked's own `del` tokenizer.+ if (src.startsWith("~") && !src.startsWith("~~")) {+ return {+ type: "literalTilde",+ raw: "~",+ text: "~"+ };+ }+ },++ renderer() {+ return "~";+ }+ };+}+/** Pre-configured wiki-link extension for server-side (uses /noteId format) */export const wikiLinkExtension = createWikiLinkExtension();