diff --git a/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.test.ts b/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.test.ts index de3b95d1be..64550b5bfa 100644 --- a/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.test.ts +++ b/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.test.ts @@ -1,6 +1,7 @@ import type { EntryModel } from "@follow/store/entry/types" import { describe, expect, it } from "vitest" +import type { PlaceholderContext } from "./custom-integration-manager" import { CustomIntegrationManager } from "./custom-integration-manager" describe("CustomIntegrationManager", () => { @@ -23,3 +24,52 @@ describe("CustomIntegrationManager", () => { `) }) }) + +const context: PlaceholderContext = { + title: "Costs $$5 & $& more", + url: "https://example.com/", + contentHtml: "

$` and $'

", + contentMarkdown: "$$\nE = mc^2\n$$", + summary: "", + author: "a", + publishedAt: "2026-01-01", + description: "see [url] and [author]", +} + +describe("CustomIntegrationManager.replacePlaceholders", () => { + it("keeps dollar signs in the values", () => { + expect( + CustomIntegrationManager.replacePlaceholders( + "[title] | [content_html] | [content_markdown]", + context, + ), + ).toBe("Costs $$5 & $& more |

$` and $'

| $$\nE = mc^2\n$$") + }) + + it("keeps dollar signs with JSON escaping", () => { + const body = CustomIntegrationManager.replacePlaceholders( + '{"title":"[title]","content":"[content_markdown]"}', + context, + { jsonEscape: true }, + ) + expect(JSON.parse(body)).toEqual({ + title: context.title, + content: context.contentMarkdown, + }) + }) + + it("does not replace placeholders inside the values", () => { + expect(CustomIntegrationManager.replacePlaceholders("[description]: [url]", context)).toBe( + "see [url] and [author]: https://example.com/", + ) + expect( + CustomIntegrationManager.replacePlaceholders("[title]", { ...context, title: "[url]" }), + ).toBe("[url]") + }) + + it("replaces every placeholder and keeps unknown ones", () => { + expect( + CustomIntegrationManager.replacePlaceholders("[url] [url] [summary][unknown]", context), + ).toBe("https://example.com/ https://example.com/ [unknown]") + }) +}) diff --git a/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.ts b/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.ts index 04f6e475b4..0cd81c3669 100644 --- a/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.ts +++ b/apps/desktop/layer/renderer/src/modules/integration/custom-integration-manager.ts @@ -141,8 +141,6 @@ export class CustomIntegrationManager { jsonEscape?: boolean } = {}, ): string { - let result = template - // Define placeholder mapping const placeholders: Record = { "[title]": context.title, @@ -155,9 +153,14 @@ export class CustomIntegrationManager { "[description]": context.description, } - // Replace each placeholder - Object.entries(placeholders).forEach(([placeholder, value]) => { - let processedValue = value + // Replace all placeholders in one pass, so that a placeholder-like text in a value is kept. + // The replacer function also keeps `$` in a value from being read as a replacement pattern. + return template.replaceAll(/\[[a-z_]+\]/g, (placeholder) => { + let processedValue = placeholders[placeholder] + + if (processedValue === undefined) { + return placeholder + } if (options.urlEncode) { processedValue = encodeURIComponent(processedValue) @@ -177,10 +180,8 @@ export class CustomIntegrationManager { processedValue = JSON.stringify(processedValue).slice(1, -1) } - result = result.replaceAll(placeholder, processedValue) + return processedValue }) - - return result } /**