(text: string, options?: RemendOptions)
| 272 | |
| 273 | // Parses markdown text and removes incomplete tokens to prevent partial rendering |
| 274 | const 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 | |
| 311 | export default remend; |
no test coverage detected