Markdown repair
Closes unterminated emphasis and code runs in a streaming chunk. Without it every streamed message flashes literal markdown syntax mid-token.
- v0.2.1
- lib
- nativewind
- uniwind
- runs in Expo Go
Install
npx @react-native-reusables/cli@latest add https://rnraielements.com/r/nativewind/markdown.jsonnpx @react-native-reusables/cli@latest add https://rnraielements.com/r/uniwind/markdown.jsonDependencies
No required npm packages — this item installs through the registry dependencies below.
AI SDK peers:ai@^7.0.89@ai-sdk/react@^4.0.92
Source
/**
* Streaming-markdown repair. Pure — no React, no React Native, no engine import — so it
* is unit-testable, which is what the architecture lens scoped Vitest to own.
*
* The web original does this inside its HTML-producing parser. The behaviour is
* renderer-agnostic and MUST survive the port: without it every streamed message flashes
* literal `**` and backticks between tokens. That is the documented regression.
*/
const PAIRED_TOKENS = ['```', '**', '__', '*', '_', '`'] as const;
/**
* Closes an unterminated emphasis or code run at the end of a streaming chunk.
* Longest tokens first, so ``` is consumed before `.
*/
export function repairIncompleteMarkdown(input: string): string {
let out = input;
for (const token of PAIRED_TOKENS) {
const count = out.split(token).length - 1;
if (count % 2 === 1) out += token;
}
return out;
}
Theming
No semantic theme classes in source — appearance follows the consumer theme and component defaults.
Usage
() => (
<MarkdownDemo chunk={'**bold and `code` repair: closed** — repaired OK'} />
)