Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/quiet-plugins-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"streamdown": patch
---

Distinguish remark and rehype plugins by function identity when caching processors, so same-name plugins with different closures do not reuse each other's behavior.
89 changes: 89 additions & 0 deletions packages/streamdown/__tests__/plugin-cache.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { render } from "@testing-library/react";
import type { Root } from "hast";
import type { PluggableList, Plugin } from "unified";
import { visit } from "unist-util-visit";
import { describe, expect, it } from "vitest";
import { Streamdown } from "../index";
import { Markdown } from "../lib/markdown";

const createPlugin = (value: string): Plugin<[{ suffix?: string }?], Root> =>
function replaceText(options) {
return (tree) => {
visit(tree, "text", (node) => {
node.value = value + (options?.suffix ?? "");
});
};
};

describe("processor plugin identity", () => {
for (const stage of ["remarkPlugins", "rehypePlugins"] as const) {
for (const tuple of [false, true]) {
it(`isolates same-name ${stage} with tuple=${tuple}`, () => {
const first = createPlugin("first");
const second = createPlugin("second");
expect(first.name).toBe(second.name);
const plugins = (plugin: typeof first): PluggableList =>
tuple ? [[plugin, { suffix: "!" }]] : [plugin];
const { container, rerender } = render(
<Markdown {...{ [stage]: plugins(first) }}>input</Markdown>
);
expect(container.textContent).toBe(tuple ? "first!" : "first");
rerender(<Markdown {...{ [stage]: plugins(second) }}>input</Markdown>);
expect(container.textContent).toBe(tuple ? "second!" : "second");
rerender(<Markdown {...{ [stage]: plugins(first) }}>input</Markdown>);
expect(container.textContent).toBe(tuple ? "first!" : "first");
});
}
}

it("reuses the same function across new plugin arrays", () => {
let setups = 0;
const plugin: Plugin = () => {
setups += 1;
};
const { rerender } = render(
<Markdown remarkPlugins={[plugin]}>first</Markdown>
);
rerender(<Markdown remarkPlugins={[plugin]}>second</Markdown>);
expect(setups).toBe(1);
});

it("keeps options distinct for the same function", () => {
const plugin = createPlugin("text");
const { container, rerender } = render(
<Markdown remarkPlugins={[[plugin, { suffix: "1" }]]}>input</Markdown>
);
expect(container.textContent).toBe("text1");
rerender(
<Markdown remarkPlugins={[[plugin, { suffix: "2" }]]}>input</Markdown>
);
expect(container.textContent).toBe("text2");
});

for (const mode of ["static", "streaming"] as const) {
it(`isolates simultaneous Streamdown instances in ${mode} mode`, () => {
const first = createPlugin("left");
const second = createPlugin("right");
const { container } = render(
<>
<section data-testid="left">
<Streamdown mode={mode} remarkPlugins={[first]}>
input
</Streamdown>
</section>
<section data-testid="right">
<Streamdown mode={mode} remarkPlugins={[second]}>
input
</Streamdown>
</section>
</>
);
expect(container.querySelector('[data-testid="left"]')?.textContent).toBe(
"left"
);
expect(
container.querySelector('[data-testid="right"]')?.textContent
).toBe("right");
});
}
});
32 changes: 17 additions & 15 deletions packages/streamdown/lib/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,22 @@ export interface Options {
const EMPTY_PLUGINS: PluggableList = [];
const DEFAULT_REMARK_REHYPE_OPTIONS = { allowDangerousHtml: true };

// Plugin name cache for faster serialization
// Function names do not distinguish closures or minified plugins.
// biome-ignore lint/complexity/noBannedTypes: "Need Function type for plugin caching"
const pluginNameCache = new WeakMap<Function, string>();
const pluginIds = new WeakMap<Function, number>();
let nextPluginId = 0;

// biome-ignore lint/complexity/noBannedTypes: "Need Function type for plugin caching"
const getPluginId = (plugin: Function): number => {
const cached = pluginIds.get(plugin);
if (cached !== undefined) {
return cached;
}
const id = nextPluginId;
nextPluginId += 1;
pluginIds.set(plugin, id);
return id;
};

// LRU Cache for unified processors
class ProcessorCache {
Expand Down Expand Up @@ -88,7 +101,6 @@ class ProcessorCache {
}

// Optimize serialization for plugins
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: "Plugin serialization requires checking multiple plugin formats"
const serializePlugins = (plugins: PluggableList | undefined): string => {
if (!plugins || plugins.length === 0) {
return "";
Expand All @@ -105,25 +117,15 @@ class ProcessorCache {
// Plugin with options: [plugin, options]
const [pluginFn, pluginOptions] = plugin;
if (typeof pluginFn === "function") {
let name = pluginNameCache.get(pluginFn);
if (!name) {
name = pluginFn.name;
pluginNameCache.set(pluginFn, name);
}
result += name;
result += getPluginId(pluginFn);
} else {
result += String(pluginFn);
}
result += ":";
result += JSON.stringify(pluginOptions);
} else if (typeof plugin === "function") {
// Plugin without options
let name = pluginNameCache.get(plugin);
if (!name) {
name = plugin.name;
pluginNameCache.set(plugin, name);
}
result += name;
result += getPluginId(plugin);
} else {
result += String(plugin);
}
Expand Down
Loading