Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -23,3 +24,52 @@ describe("CustomIntegrationManager", () => {
`)
})
})

const context: PlaceholderContext = {
title: "Costs $$5 & $& more",
url: "https://example.com/",
contentHtml: "<p>$` and $'</p>",
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 | <p>$` and $'</p> | $$\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]")
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ export class CustomIntegrationManager {
jsonEscape?: boolean
} = {},
): string {
let result = template

// Define placeholder mapping
const placeholders: Record<string, string> = {
"[title]": context.title,
Expand All @@ -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)
Expand All @@ -177,10 +180,8 @@ export class CustomIntegrationManager {
processedValue = JSON.stringify(processedValue).slice(1, -1)
}

result = result.replaceAll(placeholder, processedValue)
return processedValue
})

return result
}

/**
Expand Down