Links and Images
The library does not decode site routes or load images by default. Links and media addresses are decided by the host; the library validates, sanitizes, and renders them.
Links
Default behavior
External links allow only http:, https:, and mailto:; addresses that cannot be parsed safely degrade to blocked, readable text. Absolute HTTP(S) / mailto links default to target="_blank" and rel="nofollow noopener noreferrer".
resolveLink
Return values:
request provides kind and target; internal links may also have page and fragment. context provides the original parse snapshot document, the current working tree ast, and the current node.
Resolvers return plain data, never React elements. To fully take over link appearance and interaction, use semanticComponents.link.
The example below gives internal links a site path while external links keep the default handling:
See this library and the example site.
External link policy
policy.allowRelativeLinksdefaults totrue, allowing resolver-returned relative addresses.policy.externalLinkTargetdefaults to"_blank"; set it tonullto omittarget.- Relative links opened with
_blankgainnoopener noreferrer; finalization removes invalid targets and unknownreltokens.
Images
Default behavior
File nodes render as placeholders with captions. Without resolveImage, the library never produces <img> and never requests an image URL.
resolveImage
- Returning
nullkeeps the placeholder; returning an object authorizes the resource. srcallows onlyhttp:/https:; enablepolicy.allowRelativeImagesfor relative images.requestprovidesname,parameters, parsedwidth/height,alt, andcaption.- Dimensions are clamped to 1–4096; resolver values take precedence.
- The generated
<img>always hasloading="lazy"anddecoding="async";altfalls back to the file name.
Captions and links
- The caption prefers explicit
caption=, otherwise the unnamed caption; links and template placeholders inside it render recursively. - The file parameter
link=is sanitized and produces a link;class=becomes an extra class. - Inline files use
<span>; block files use<figure>. Compatibility classes such asmw-default-sizeandmw-halign-*are preserved.
Authorization and security
Preparation records the image addresses that a resolver actually returned and the library validated. During finalization:
- An unauthorized
<img>is removed. Itsalttext is preserved when present, and anUNAUTHORIZED_IMAGEdiagnostic is recorded. Resolver output usesauthorizedImages, source HTML usespolicy.allowSourceImages, and plugin output usespolicy.allowPluginImages. - Plugins cannot bypass the default no-image policy by inserting arbitrary
<img>elements. - URLs are entity-decoded, stripped of control characters, and validated against the protocol whitelist before becoming attributes.
You can pre-process URLs with sanitizeHref, sanitizeImageUrl, and sanitizedAttributesToProps from /hast. The final stage validates again.
Policy and components
To adjust defaults without a resolver:
policy is shallow-merged with defaults. extraTags and stripTags are replaced wholesale, not appended. Spread DEFAULT_PREPARE_POLICY when you want to keep the defaults.
Final link and file appearance can be taken over with HTML or semantic components. Custom components are trusted code; their output is not sanitized again.