MCPcopy
hub / github.com/vercel/streamdown / remend

Function remend

packages/remend/src/index.ts:274–309  ·  view source on GitHub ↗
(text: string, options?: RemendOptions)

Source from the content-addressed store, hash-verified

272
273// Parses markdown text and removes incomplete tokens to prevent partial rendering
274const remend = (text: string, options?: RemendOptions): string => {
275 if (!text || typeof text !== "string") {
276 return text;
277 }
278
279 // Remove trailing whitespace if it's not a double space
280 let result =
281 text.endsWith(" ") && !text.endsWith(" ") ? text.slice(0, -1) : text;
282
283 // Get enabled built-in handlers
284 const enabledBuiltIns = getEnabledBuiltInHandlers(options);
285
286 // Combine with custom handlers (default priority: 100)
287 const customHandlers = (options?.handlers ?? []).map((h) => ({
288 handler: { ...h, priority: h.priority ?? PRIORITY.DEFAULT },
289 earlyReturn: undefined,
290 }));
291
292 // Merge and sort by priority
293 // Priority is always set: built-ins have explicit priority, customs get default at line 252
294 const allHandlers = [...enabledBuiltIns, ...customHandlers].sort(
295 (a, b) => (a.handler.priority ?? 0) - (b.handler.priority ?? 0)
296 );
297
298 // Execute handlers in priority order
299 for (const { handler, earlyReturn } of allHandlers) {
300 result = handler.handle(result);
301
302 // Check for early return condition (e.g., incomplete link marker)
303 if (earlyReturn?.(result)) {
304 return result;
305 }
306 }
307
308 return result;
309};
310
311export default remend;

Calls 1

Tested by

no test coverage detected