This documentation is also published as Markdown for efficient machine reading: the whole site is indexed at /llms.txt, and every page has a clean Markdown copy under /_llms/. These are generated from the same source and cost far fewer tokens to read than this rendered HTML.

Skip to main content Skip to navigation

ShortcodeExpander Pennington.Markdown.Shortcodes

Expands Statiq-style pre-render shortcodes in a markdown source. Recognises <?# Name args ?>...<?#/ Name ?> blocks and <?# Name args /?> self-closing tags, dispatches each call to a matching IShortcode registered in DI, and splices the handler's output back into the source before Markdig parses it. Directives expand everywhere — including inside fenced code blocks — so install snippets and generated samples can stamp real values. To show a literal directive without expanding it, prefix the opener with a backslash (\<?# ... ?>); the expander consumes the backslash and emits the directive as-is. Unknown names and handler failures degrade to HTML comments and a diagnostic so one bad call site cannot fail the render.

Constructors

ShortcodeExpander

#
public ShortcodeExpander(IEnumerable<IShortcode> shortcodes = null, IHttpContextAccessor httpContextAccessor = null)

Creates the expander with the DI-registered handlers and an optional accessor for per-request DiagnosticContext.

Parameters

shortcodes IEnumerable<IShortcode>
httpContextAccessor IHttpContextAccessor

Methods

ExpandAsync

#
public Task<string> ExpandAsync(string markdown, ShortcodeContext context, CancellationToken cancellationToken)

Returns markdown with every shortcode call site replaced by its handler output. Returns the input unchanged when no handlers are registered or the source contains no shortcode opener.

Parameters

markdown string
context ShortcodeContext
cancellationToken CancellationToken

Returns

Task<string>

Pennington.Markdown.Shortcodes.ShortcodeExpander

namespace Pennington.Markdown.Shortcodes;

/// Expands Statiq-style pre-render shortcodes in a markdown source. Recognises <?# Name args ?>...<?#/ Name ?> blocks and <?# Name args /?> self-closing tags, dispatches each call to a matching IShortcode registered in DI, and splices the handler's output back into the source before Markdig parses it. Directives expand everywhere — including inside fenced code blocks — so install snippets and generated samples can stamp real values. To show a literal directive without expanding it, prefix the opener with a backslash (\<?# ... ?>); the expander consumes the backslash and emits the directive as-is. Unknown names and handler failures degrade to HTML comments and a diagnostic so one bad call site cannot fail the render.
public class ShortcodeExpander
{
    /// Returns markdown with every shortcode call site replaced by its handler output. Returns the input unchanged when no handlers are registered or the source contains no shortcode opener.
    
public Task<string> ExpandAsync(string markdown, ShortcodeContext context, CancellationToken cancellationToken)
; /// Creates the expander with the DI-registered handlers and an optional accessor for per-request DiagnosticContext.
public ShortcodeExpander(IEnumerable<IShortcode> shortcodes = null, IHttpContextAccessor httpContextAccessor = null)
; }