Skip to content

Convert <pre> and Markdown code fences to code blocks - #48

Merged
ericof merged 1 commit into
mainfrom
issue-47
Oct 28, 2026
Merged

ericof merged 1 commit into
mainfrom
issue-47

Conversation

@ericof

@ericof ericof commented Oct 28, 2026

Copy link
Copy Markdown
Member

Adds an opt-in block converter for <pre>. Three inputs reach the HTML step as <pre>: Markdown fences, indented code (<pre><code> without a class) and HTML <pre>. Each becomes a codeBlock block of @plonegovbr/volto-code-block.

Setting. volto_blocks(), html_to_blocks() and markdown_to_blocks() take code_block. Pass a dict to enable the conversion. Its keys style, wrapLongLines and showLineNumbers override the defaults (dark, True, False). An empty dict enables it with the defaults. The convert command takes --code-block, plus --code-style, --wrap-long-lines/--no-wrap-long-lines and --show-line-numbers/--no-show-line-numbers, which require --code-block. The HTTP service is unchanged.

Language. Read from a language-* or lang-* class on the <code> or the <pre>, lowercased. shell, sh and console map to bash, and py to python. Without a language the block gets text. The code is the element text without one trailing newline.

Why opt-in. codeBlock comes from an add-on, not Volto core, so sites without it would get blocks they cannot render. With the option off, the <pre> converter is hidden during the conversion. This is needed, not just a default: registering a block converter for <pre> changes how the parser splits paragraphs and unwraps containers, so the default layout is pinned in tests for <pre> inside paragraphs, divs and sections.

Open questions

  • Should POST /volto and POST /markdown take the option now, or wait for a consumer?
  • Should the defaults move to settings rather than fixed values in code?
  • Is text the right fallback for a plain <pre>, or should it be plaintext?
  • Which further aliases should map (js, ts, yml, html)?

Closes #47

Add an opt-in block converter for <pre>. It covers Markdown fences, indented
code and HTML <pre>, and produces codeBlock blocks of
@plonegovbr/volto-code-block. The language comes from a language-* or lang-*
class on the <code> or the <pre>; shell, sh and console map to bash, and py
maps to python. Without a language, the block uses text.

The option is off by default, because the codeBlock type comes from an
add-on. Enable it with code_block in volto_blocks() or with --code-block in
the convert command. While it is off, the <pre> block converter is hidden
during the conversion, so the output stays unchanged.

Closes #47
@ericof
ericof merged commit 53d2364 into main Oct 28, 2026
12 checks passed
@ericof
ericof deleted the issue-47 branch October 28, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert <pre> and Markdown code fences to code blocks

1 participant