Skip to content

HtmlCodeBlockPlugin

Turns <code-block> tags in HTML pages and parts into highlighted code blocks, the same output markdown fences get. Markdown already has fences, this is for HTML pages, layouts, and parts.

theme.HtmlCodeBlockPlugin({ rehypeExpressiveCodeOptions });

Options

OptionTypeDefaultRole
rehypeExpressiveCodeOptionsRehypeExpressiveCodeOptionsrequiredThe Expressive Code options, shared with markdown.
tagNamestring"code-block"Tag to look for.

Tag attributes

The code comes from the tag’s content or the code attribute, leading whitespace trimmed.

AttributeRole
langLanguage for highlighting.
titleFile name shown in the frame.
frameauto, code, terminal, or none.
no-iconHides the file icon.
showLineNumbersLine numbers. showLineNumbers="false" turns them off.
startLineNumberFirst line number.
wrapSoft wrap long lines.
preserveIndentKeep indentation when wrapping.
hangingIndentExtra indent for wrapped lines.
mark, ins, delLine or text markers, Expressive Code syntax, e.g. mark="{2-3}".
diffTreat the content as a diff.
collapseCollapsible sections, e.g. collapse="{1-5}".
collapsiblePreview the first lines with an expand button, collapsible="10" for a longer preview.
metaRaw Expressive Code meta string, appended as is.

Examples

A titled block:

<code-block lang="ts" title="example.ts">
const answer = 42;
</code-block>
example.ts
const answer = 42;

A terminal with a marked line:

<code-block lang="shellscript" frame="terminal" mark="{2}">
npm install
npm run build
</code-block>
Terminal window
npm install
npm run build

Code from an attribute, handy inside parts that receive it through $data:

<code-block lang="json" code="{{ $data.snippet }}"></code-block>

Cache

Identical blocks are highlighted once per build and stored across builds, see Expressive Code.