Lightweight, dependency-free detection of the likely format of a string — Markdown, HTML,
or plain text. Useful for deciding how to render free-text content (e.g. a long entity
field) without requiring callers to declare the format up front.
The detection is intentionally conservative: it requires reasonably strong signals
before classifying content as Markdown or HTML, so ordinary prose (which may contain the
odd < or *) stays 'plain'. Markdown is treated as the safe superset — content
showing Markdown signals is classified 'markdown' even when it also contains inline HTML,
because Markdown renderers handle embedded HTML.
Only the leading maxScanLength characters are inspected — format signals, when present,
appear early, so a small window keeps the regex work cheap on large values and slower
machines. If nothing matches within that window, the content is assumed 'plain'.
NOTE: this classifies; it does NOT sanitize. Anything rendered as HTML must still be passed
through an HTML sanitizer (e.g. Angular's [innerHTML] binding, which sanitizes in HTML
context) before being injected into the DOM.
Parameters
value: string|null|undefined
The text to classify (null/undefined/blank → 'plain').
maxScanLength: number = DEFAULT_RICH_TEXT_SCAN_LENGTH
Lightweight, dependency-free detection of the likely format of a string — Markdown, HTML, or plain text. Useful for deciding how to render free-text content (e.g. a long entity field) without requiring callers to declare the format up front.
The detection is intentionally conservative: it requires reasonably strong signals before classifying content as Markdown or HTML, so ordinary prose (which may contain the odd
<or*) stays'plain'. Markdown is treated as the safe superset — content showing Markdown signals is classified'markdown'even when it also contains inline HTML, because Markdown renderers handle embedded HTML.Only the leading
maxScanLengthcharacters are inspected — format signals, when present, appear early, so a small window keeps the regex work cheap on large values and slower machines. If nothing matches within that window, the content is assumed'plain'.NOTE: this classifies; it does NOT sanitize. Anything rendered as HTML must still be passed through an HTML sanitizer (e.g. Angular's
[innerHTML]binding, which sanitizes in HTML context) before being injected into the DOM.