PhoenixKit.Modules.Publishing.Renderer (PhoenixKitPublishing v0.6.0)

Copy Markdown View Source

Renders publishing post markdown to HTML with caching support.

Uses PhoenixKit.Cache for performance optimization of markdown rendering. Cache keys include content hashes for automatic invalidation.

Summary

Functions

Clears all publishing post caches.

Clears the render cache for a specific group.

The PHK component tags the renderer understands, for editors that must keep them intact. See @component_tags.

Returns whether the global render cache is enabled.

Returns whether render cache is enabled for a specific group. Does not check the global setting.

Invalidates cache for a specific post.

The post's author notes in document order: [%{number: n, id: stable_id, body: text}]. The id is derived from the note text (not the number), so a comment anchored to a note survives the author inserting an earlier note — it detaches only when the note text itself changes. The post template uses this to render the slide-out panels in "panel" notes style; a <Note> inside a code fence is ignored (same mask as rendering).

Stable DOM/comment anchor for a note: a short url-safe digest of the note text. Content-addressed on purpose — see list_notes/1. Repeated identical note texts get occurrence-suffixed digests (2nd, 3rd, …) so panel DOM ids stay unique; the first occurrence keeps the plain digest, so existing comments never detach when a duplicate appears later.

Returns the settings key for per-group render cache. Used by other modules that need to write to the setting.

Returns whether render caching is enabled for a group.

Renders markdown or PHK content directly without caching.

Renders a post's markdown content to HTML.

Functions

clear_all_cache()

@spec clear_all_cache() :: :ok

Clears all publishing post caches.

Useful for testing or when doing bulk updates.

clear_group_cache(group_slug)

@spec clear_group_cache(String.t()) :: {:ok, non_neg_integer()} | {:error, any()}

Clears the render cache for a specific group.

Returns {:ok, count} with the number of entries cleared.

Examples

Renderer.clear_group_cache("my-group")
# => {:ok, 15}

component_tags()

@spec component_tags() :: [String.t()]

The PHK component tags the renderer understands, for editors that must keep them intact. See @component_tags.

global_render_cache_enabled?()

@spec global_render_cache_enabled?() :: boolean()

Returns whether the global render cache is enabled.

group_render_cache_enabled?(group_slug)

@spec group_render_cache_enabled?(String.t()) :: boolean()

Returns whether render cache is enabled for a specific group. Does not check the global setting.

invalidate_cache(group_slug, identifier, language)

@spec invalidate_cache(String.t(), String.t(), String.t()) :: :ok

Invalidates cache for a specific post.

Called when a post is updated in the admin editor.

Examples

Renderer.invalidate_cache("docs", "getting-started", "en")

list_notes(content)

The post's author notes in document order: [%{number: n, id: stable_id, body: text}]. The id is derived from the note text (not the number), so a comment anchored to a note survives the author inserting an earlier note — it detaches only when the note text itself changes. The post template uses this to render the slide-out panels in "panel" notes style; a <Note> inside a code fence is ignored (same mask as rendering).

note_dom_id(body, occurrence \\ 1)

Stable DOM/comment anchor for a note: a short url-safe digest of the note text. Content-addressed on purpose — see list_notes/1. Repeated identical note texts get occurrence-suffixed digests (2nd, 3rd, …) so panel DOM ids stay unique; the first occurrence keeps the plain digest, so existing comments never detach when a duplicate appears later.

per_group_cache_key(group_slug)

@spec per_group_cache_key(String.t()) :: String.t()

Returns the settings key for per-group render cache. Used by other modules that need to write to the setting.

render_cache_enabled?(group_slug)

@spec render_cache_enabled?(String.t()) :: boolean()

Returns whether render caching is enabled for a group.

Checks both the global setting and per-group setting. Both must be enabled (or default to enabled) for caching to work.

render_markdown(content, opts \\ [])

@spec render_markdown(
  String.t() | any(),
  keyword()
) :: String.t()

Renders markdown or PHK content directly without caching.

Automatically detects PHK XML format and routes to PageBuilder. Falls back to MDEx markdown rendering for non-XML content.

Examples

html = Renderer.render_markdown(content)

render_post(post, opts \\ [])

@spec render_post(
  map(),
  keyword()
) :: {:ok, String.t()} | {:error, any()}

Renders a post's markdown content to HTML.

Caches the result for published posts using content-hash-based keys. Lazy-loads cache (only caches after first render).

Respects publishing_render_cache_enabled (global) and publishing_render_cache_enabled_{group_slug} (per-group) settings.

Examples

{:ok, html} = Renderer.render_post(post)