Skip to content

About

Markdown Input & Text for React Native

Resources

Code of conduct

Contributing

Accessibility

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

627 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-enriched-markdown

OpenEvidence fork of software-mansion-labs/react-native-enriched-markdown. For feature documentation, installation details, API reference, and supported elements, see the upstream README and the docs/ directory.

Fork Changes

  • Citation chips — [[numbers|label|faviconUrl]] rendered as styled pill chips with async favicon loading
  • Verified text (iOS) — <verified-text id="…" label="…">words</verified-text> marks words that carry a check
  • Tail fade-in streaming animation — canvas overlay approach on both platforms (600ms smoothstep)
  • Display math (CommonMark) — $$...$$ block math in the single-view rendering path
  • Android touch handling — gesture arbitration between links/citations, scroll, and drawer

Architecture

Parsing (C++)

All platforms share a single C++ parser (cpp/parser/MD4CParser.cpp) built on md4c. The parser produces an AST of MarkdownASTNode structs (cpp/parser/MarkdownASTNode.hpp). Two post-processing passes run after md4c:

  1. Citation extraction — walks text nodes looking for [[...]] patterns, splits them into Text + Citation AST nodes with numbers, label, and faviconUrl attributes.
  2. Display math promotion — promotes LatexMathDisplay nodes out of wrapping paragraphs so renderers see them as block-level elements.

Where the caller turns Md4cFlags.verifiedText on (the iOS CommonMark view does), an inline <verified-text id="…">words</verified-text> tag becomes a VerifiedText node that keeps its quoted attributes. Elsewhere, and for a tag without an id, the tag is dropped like any unknown tag and its words stay. A block or span that ends closes the verified texts left open in it. bash cpp/tests/run.sh runs the parser's tests.

React Native Layer (TypeScript)

EnrichedMarkdownText (src/native/EnrichedMarkdownText.tsx) is the main component. It normalizes styles via an LRU cache (normalizeMarkdownStyle.ts), memoizes callbacks, and delegates to one of two native components depending on the flavor prop (CommonMark vs GitHub).

EnrichedMarkdownWithComponents (src/EnrichedMarkdownWithComponents.tsx) supports embedding arbitrary React components inside markdown. The host app wraps component data in ```REACTCOMPONENT code fences. splitMarkdownSegments.ts splits the markdown string on these fences and produces alternating { type: "markdown", content } and { type: "component", name, props } segments. Each markdown segment renders as its own EnrichedMarkdownText; each component segment renders via a registered component from a ComponentRegistry.

Two native component specs exist (EnrichedMarkdownNativeComponent.ts for GitHub flavor, EnrichedMarkdownTextNativeComponent.ts for CommonMark) with identical interfaces — required by React Native codegen for separate native view registrations.

iOS (Objective-C / Objective-C++)

Text view: EnrichedMarkdownText.mm is a Fabric component that owns a UITextView. Markdown is parsed on a serial dispatch queue, rendered to NSMutableAttributedString by the renderer pipeline, and applied on the main thread.

Rendering: RendererFactory.m maps AST node types to renderer classes. Each renderer (ParagraphRenderer, HeadingRenderer, CitationRenderer, etc.) appends styled attributed string ranges to a shared NSMutableAttributedString. The TextViewLayoutManager.mm subclass of NSLayoutManager draws custom backgrounds (code blocks, blockquotes) in drawBackgroundForGlyphRange:.

Citation chips: CitationChipAttachment is an NSTextAttachment subclass. It draws the chip (pill background, circular favicon, label) via CoreGraphics in imageForBounds:textContainer:characterIndex:. Chip images are cached globally by NSCache. Favicons are loaded asynchronously via NSURLSession and cached separately; on completion, invalidateDisplayForCharacterRange: triggers a re-composite without relayout.

Verified text: a <verified-text> tag with a label is a checked span. VerifiedTextRenderer marks its words with an ENRMVerifiedText, and VerifiedTextDrawer draws a dotted underline under them in drawBackgroundForGlyphRange:, in markdownStyle.verifiedText.color. The check's place is an ENRMVerifiedTextPlace, an empty attachment after the span's last letter with a word joiner before it, so the check wraps with its word and sits on the reading side in right-to-left text; the drawer paints the icon there in drawGlyphsForGlyphRange:, sized from the text's font. A copy leaves the place out. A tap on the words or the icon fires onVerifiedTextPress with the span's id; VoiceOver gets each checked span as a button that fires the same event. open="true" on the tag washes the words in the check's colour at 15%, makes the underline solid and rings the icon. bash ios/tests/run.sh runs the iOS tests in a booted simulator.

Keep room: a <verified-text> tag without a label is a span whose check has not come. Nothing is drawn for it and its place has no width, so no gap shows. ENRMVerifiedTextRoom lays the text out with every place open, which gives the line breaks the text will have once every check is in, then closes the awaited places and blocks the end of each of their lines by the same width with an exclusion path (the left end in right-to-left text). When the span's line is not the first of its paragraph, the line above is closed after its text too, so the span's last word cannot move up. The lines then break the same before and after a check lands. A span whose last word is as wide as its line has its place alone at the start of the next line; that place stays open. A new text makes the room forget what it kept, so the next width check keeps room again.

Draw-in: ENRMVerifiedTextDrawIn decides, for each new text, which checks draw in: a span that was awaited in the text the view showed before draws in now; a span that is new while the view is typing on from its last text draws in once its words have faded in; any other checked span is shown at once, so a text view that is built again never draws a check in twice. Spans are remembered only as far as the new text goes on from the last one. On each CADisplayLink frame the underline is swept (494 ms), then the place opens (338 ms) while the icon fades in (416 ms), and ENRMVerifiedTextRoom gives the line's end back as the place opens. Reduce Motion skips it.

Streaming animation: ENRMTailFadeInAnimator uses a canvas overlay approach. A transparent ENRMFadeOverlayView is added as a subview of the text view. On each CADisplayLink frame, the overlay's drawRect: uses NSLayoutManager line fragment enumeration to compute per-line rectangles for each active fade group, filling them with the resolved background color at a fading alpha (smoothstep easing). Text is always rendered at full opacity — the overlay simply covers and reveals it. This avoids any NSTextStorage mutations during animation.

Spoiler text: ENRMSpoilerOverlayManager adds per-span ENRMSpoilerOverlayView subviews on top of the text view, with particle or solid animation modes.

Android (Kotlin)

Text view: EnrichedMarkdownText.kt extends AppCompatTextView. Parsing and rendering run on a dedicated Executor; the result is posted to the main thread via Handler. Text is set with BufferType.EDITABLE so the buffer implements Spannable (required for LinkLongPressMovementMethod).

Rendering: RendererFactory (renderer/NodeRenderer.kt) maps AST node types to NodeRenderer implementations. Renderers append to a SpannableStringBuilder and apply Android spans. Math renderers (MathInlineRenderer, MathDisplayRenderer) are loaded via reflection from the optional math source set to keep the math dependency (MTMathView) out of the main build.

Citation chips: CitationChipSpan is a ReplacementSpan. It draws the chip directly on the Canvas — rounded rect background, circular favicon via BitmapShader, label text. Colors and font size are pulled from SpanStyleCache (which reads the JS citationStyle config) with hardcoded defaults as fallback. Favicons load asynchronously via ImageDownloader; on completion, the span re-seats itself on the Editable text (removeSpan + setSpan) to trigger DynamicLayout's SpanWatcher, forcing a redraw.

Streaming animation: TailFadeInAnimator draws background-colored rectangles over new text in onDraw (called after super.onDraw()). Each frame, Choreographer.FrameCallback triggers invalidate(). The overlay color is resolved by walking the view hierarchy via SpoilerDrawContext.resolveBackgroundColor, which uses React Native's BackgroundStyleApplicator to extract colors from RN's CompositeBackgroundDrawable. The cached color resets on configuration changes (dark/light mode).

Touch handling: dispatchTouchEvent checks whether ACTION_DOWN lands on an interactive span (LinkSpan or CitationChipSpan) via shared charOffsetAt/isInteractiveOffset extensions (TouchUtils.kt). Only interactive hits call requestDisallowInterceptTouchEvent(true); plain-text touches allow parent scroll/drawer gestures. LinkLongPressMovementMethod handles link long-press, citation tap dispatch, and spoiler reveal.


Upstream library by Software Mansion.

About

Markdown Input & Text for React Native

Resources

Code of conduct

Contributing

Accessibility

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages