mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
Codemode and write rows keep their last highlighting in a CodeHighlighter that checks theme and grammar changes itself. Markdown reuses the top-level blocks of its last render, held weakly, and assistant messages reuse their Markdown instances while streaming. Text.setText() keeps its wrapped lines for unchanged text. closes #10971
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import type { MarkdownTransformContext, MarkdownTransformer } from "../../../core/extensions/types.ts";
|
|
|
|
/** Applies extension Markdown transformers. `isStreaming` is read per transform, so a reused component sees a stream end. */
|
|
export function createMarkdownTransform(
|
|
messageType: MarkdownTransformContext["messageType"],
|
|
isStreaming: () => boolean,
|
|
transformers: readonly MarkdownTransformer[],
|
|
messageId?: string,
|
|
timestamp?: string,
|
|
): (markdown: string, availableWidth: number) => string {
|
|
return (markdown, availableWidth) =>
|
|
applyMarkdownTransformers(
|
|
markdown,
|
|
{ messageType, isStreaming: isStreaming(), availableWidth, messageId, timestamp },
|
|
transformers,
|
|
);
|
|
}
|
|
|
|
function applyMarkdownTransformers(
|
|
markdown: string,
|
|
context: MarkdownTransformContext,
|
|
transformers: readonly MarkdownTransformer[],
|
|
): string {
|
|
let transformedMarkdown = markdown;
|
|
for (const transformer of transformers) {
|
|
try {
|
|
const transformed = transformer(transformedMarkdown, context);
|
|
if (typeof transformed === "string") {
|
|
transformedMarkdown = transformed;
|
|
}
|
|
} catch {
|
|
// Keep the current Markdown and continue with the next transformer.
|
|
}
|
|
}
|
|
return transformedMarkdown;
|
|
}
|