/* A filter that extracts a named section from content marked with HTML comments. This is useful for splitting a single content file (like a Markdown post) into multiple parts that can be displayed and styled independently in your templates. ```js */ export default function (content, sectionName) { if (!content || typeof content !== "string") { return content; } if (typeof sectionName !== "string" || !sectionName) { return ""; } // Normalize section name for comparison (trim whitespace) const targetName = sectionName.trim().toLowerCase(); // Regex to match section markers with content up to the next section or end of string // Captures: (1) section names, (2) content until next section marker or end const sectionRegex = /([\s\S]*?)(?= ## Usage 1. Mark sections in your content file (e.g., `post.md`): ⚠️ `NOTE:` The `¡` symbol is used instead of `!` only to give examples below. Use `!` in your actual content files. ```markdown # My Post <¡--section:intro--> This is the introduction that appears at the top of the page. <¡--section:main--> This is the main body of the post with all the details. <¡--section:summary,sidebar--> This content appears in both the summary and the sidebar! ``` 2. Use the filter in your templates: ```jinja2 {# Get the intro section #}
{{ content | section('intro') | safe }}
{# Get the main section #}
{{ content | section('main') | safe }}
{# Get the sidebar section #} ``` */