Skip to main content

Module richtext

Module richtext 

Source
Expand description

| richtext Tera filter — {{ page.body | richtext }}.

The filter rewrites the storage shape the editor emits — <a linktype="page" id="42">…</a> — into the renderable shape — <a href="/about/team">…</a> — at template-render time. Storing page-id references in rich-text bodies means anchors keep working when pages move.

Concretely, the filter:

  • rewrites <a linktype="page" id="N"> → <a href="<page url>">
  • rewrites <a linktype="media" id="N"> → <a href="<media url>">
  • marks the output safe (is_safe = true) so Tera doesn’t double-escape on top of the editor’s already-HTML-escaped content
  • missing / unresolvable ids fall back to href="#" — the anchor still renders, just inert (a “broken link” placeholder)

Resolution maps:

  • Page urls — reuses crate::page_url’s thread-local (populated by the public-render handler via crate::page_url::install in crate::render). Any extension-level *_id field that pointed at a page is resolvable through here too.
  • Everything else — anchors the filter cannot resolve are left intact, and resolve_internal_links rewrites them over the page’s final HTML, where stream blocks’ links are visible too: one query for the pages, one for the media.

§Usage

{{ extension.hero_body | richtext }}

§Implementation note

Avoids pulling html5ever / ammonia in for what’s effectively a constrained-shape rewrite. The editor’s output for internal links is well-formed (always <a followed by whitespace + a single >-terminated open tag), so a small string scanner that walks <a ...> openings and edits the attribute set in place is sufficient. Anything that doesn’t match linktype="page|media" + id="…" is left verbatim — including arbitrary editor-emitted markup like <img> / <ul> / <strong>.

Functions§

register_tera_filter
Register the richtext Tera filter. Called once at Tera setup — wired from crate::urls::register_tera_helpers.
resolve_internal_links
Rewrite every internal-link anchor in a rendered page.