@prefix sioc: <http://rdfs.org/sioc/ns#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix content: <http://purl.org/rss/1.0/modules/content/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

<https://lhero.org/portfolio/lh-content-converter/>
  a sioc:Post ;
  dc:title "Block handlers" ;
  dcterms:identifier 152387 ;
  dc:modified "2026-10-24T23:18:33Z"^^xsd:dateTime ;
  dc:created "2026-10-24T23:18:22Z"^^xsd:dateTime ;
  sioc:link <https://lhero.org/portfolio/lh-content-converter/block-handlers/> ;
  sioc:has_creator <https://lhero.org/author/1/#account> ;
  sioc:has_container <https://lhero.org/#posts> ;
  content:encoded """<ul class="lh_portfolio-meta"><li><strong>Part of:</strong> <a href="https://lhero.org/portfolio/lh-content-converter/">LH Content Converter</a></li></ul>
<p class="wp-block-paragraph">Other plugins can turn parts of Markdown or HTML into their own blocks when content is converted to blocks. LH Mermaid uses this so a Mermaid diagram written in Markdown or HTML lands as its Mermaid Diagram block, and any plugin with a block the conversion library has no mapping for can do the same.</p>



<p class="wp-block-paragraph">Add a handler to the <code>lh_content_converter_block_handlers</code> filter, which takes and returns an array of callables. Each handler is called with the HTML and a claim function, and returns the HTML. Markdown has already been converted to HTML, so a code fence arrives as a pre element holding a code element with a language class. For each element it claims, the handler calls the claim function with the block markup it wants, and puts the placeholder it gets back in place of the element. LH Mermaid&#8217;s <code>Lh_Mermaid_Block::claim_diagrams()</code> is a worked example.</p>



<ul class="wp-block-list">
<li>The placeholder is a paragraph of its own, so claim only elements that can stand as a block where they are.</li>



<li>Once the library has run, each placeholder&#8217;s paragraph block is swapped for the block markup exactly as given, so give what the block editor would save.</li>



<li>Handlers run before code blocks are protected, so a handler can claim a fenced code block.</li>



<li>If a placeholder isn&#8217;t found exactly once afterwards, the conversion fails with the <code>lh_content_converter_block_restore_failed</code> error rather than return content that can&#8217;t be trusted.</li>
</ul>



<p class="wp-block-paragraph">Handlers only apply to conversions to blocks, and every caller gets them: the post abilities, the knowledge base, LH Email drafts and LH Presentations slides.</p>
"""^^rdf:XMLLiteral ;
  sioc:content """Part of: LH Content Converter
Other plugins can turn parts of Markdown or HTML into their own blocks when content is converted to blocks. LH Mermaid uses this so a Mermaid diagram written in Markdown or HTML lands as its Mermaid Diagram block, and any plugin with a block the conversion library has no mapping for can do the same.



Add a handler to the lh_content_converter_block_handlers filter, which takes and returns an array of callables. Each handler is called with the HTML and a claim function, and returns the HTML. Markdown has already been converted to HTML, so a code fence arrives as a pre element holding a code element with a language class. For each element it claims, the handler calls the claim function with the block markup it wants, and puts the placeholder it gets back in place of the element. LH Mermaid&#8217;s Lh_Mermaid_Block::claim_diagrams() is a worked example.




The placeholder is a paragraph of its own, so claim only elements that can stand as a block where they are.



Once the library has run, each placeholder&#8217;s paragraph block is swapped for the block markup exactly as given, so give what the block editor would save.



Handlers run before code blocks are protected, so a handler can claim a fenced code block.



If a placeholder isn&#8217;t found exactly once afterwards, the conversion fails with the lh_content_converter_block_restore_failed error rather than return content that can&#8217;t be trusted.




Handlers only apply to conversions to blocks, and every caller gets them: the post abilities, the knowledge base, LH Email drafts and LH Presentations slides.
""" ;
  sioc:topic <https://lhero.org/?taxonomy=author&term=cap-1> .

<https://lhero.org/author/1/#account> rdfs:seeAlso <https://lhero.org/author/1/?feed=lhrdf&format=turtle> .
<https://lhero.org/?taxonomy=author&term=cap-1> rdfs:seeAlso <https://lhero.org/?taxonomy=author&term=cap-1&feed=lhrdf&format=turtle> .
