Loading...

Loading...

Code Highlighting & Enhancement

This page explains code block configuration: highlight engine and themes (prismjs/highlightjs), code block interaction enhancements (collapse/wrap/expand/copy/fullscreen), language labels and copy copyright. Everything lives in the code section of the theme config.

The authoritative source of the theme config is userConfig/_config.tmp.yml (it overwrites the theme config at CI time). Note: server-side prismjs rendering at the Markdown renderer level is handled by the prismjs plugin in the root _config.yml; this section covers the theme-level code block display config.

Reading guide: every section follows 「How to enable → Common options → All tunable keys → Boundaries & conflicts」 — check the Effect first, then the Corresponding config that produces it, and copy it to use.

1. Highlight Engine (highlight)

Purpose: choose the highlight implementation library, and control line numbers plus the light/dark theme pair.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

1.1 How to enable

Effect: ``` code blocks in posts are colored by the selected library, line numbers appear on the left, and dark mode automatically switches to the dark theme.

Corresponding config (theme config, code.highlight section):

code:
  highlight:
    enable: true          # master switch
    line_number: true     # whether to show line numbers
    lib: "prismjs"        # highlight library: highlightjs | prismjs

1.2 Common options

① Turn off line numbers

Effect: the 1, 2, 3… gutter on the left of code blocks disappears, giving the code area more width.

Corresponding config:

code:
  highlight:
    line_number: false

② Switch engine

Effect: highlighting is done by highlight.js instead, and the theme names and syntax coverage change accordingly (see §3).

Corresponding config:

code:
  highlight:
    lib: "highlightjs"

③ Disable highlighting entirely

Effect: code blocks become plain text with no coloring and no line numbers.

Corresponding config:

code:
  highlight:
    enable: false

1.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
code.highlight.enablebooltruetrue / falseHighlight master switch
code.highlight.line_numberbooltruetrue / falseShow line numbers
code.highlight.libstring"prismjs"highlightjs / prismjsHighlight library

1.4 Boundaries & conflicts

  • lib decides which engine is used and activates the matching sub-config below — prismjs reads code.highlight.prismjs, highlightjs reads code.highlight.highlightjs; both exist but only one takes effect.
  • This blog uses prismjs.
  • Engine-level highlighting and the prismjs plugin in the root _config.yml are two separate channels; changing the theme config does not change server-side rendering behavior.

1.5 Summary

  • Minimal setup: enable: true + lib
  • Line numbers feel cramped: line_number: false
  • Switching libraries: change lib, and remember to change the style in §2 or §3 to match

2. prismjs Themes & Render Mode

Purpose: the light/dark highlight theme pair (following light/dark mode), and how the highlight HTML is generated.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

2.1 How to enable

Effect: in light mode code blocks use the default palette and switch to tomorrow night automatically in dark mode; the three enhancement plugins treeview/autolinker/diffhighlight are active.

Corresponding config (theme config, code.highlight.prismjs section):

code:
  highlight:
    prismjs:
      style: "default"            # light theme: pick a style from the round buttons at prismjs.com, or a CSS link
      style_dark: "tomorrow night" # dark theme
      preprocess: false           # true = generated statically at build; false = generated by browser JS
      treeview: true              # show treeview in code blocks (tree -F tree form)
      autolinker: true            # links inside code blocks clickable
      diffhighlight: true         # code diff highlight (+/- lines)

2.2 Common options

① Change the light theme

Effect: code block colors switch to the chosen theme (background, keywords, and string colors all change).

Corresponding config:

code:
  highlight:
    prismjs:
      style: "tomorrow"

② Change the dark theme

Effect: code blocks use the new palette in dark mode.

Corresponding config:

code:
  highlight:
    prismjs:
      style_dark: "dark"

③ Switch to static rendering at build time

Effect: the highlight HTML is generated at build time, so coloring is visible even without JS (but treeview/autolinker/diffhighlight become unavailable).

Corresponding config:

code:
  highlight:
    prismjs:
      preprocess: true

④ Turn off diff highlighting

Effect: +/- lines no longer get green/red backgrounds.

Corresponding config:

code:
  highlight:
    prismjs:
      diffhighlight: false

Two render modes compared (preprocess):

ModeDescriptionTrade-off
preprocess: truestatic highlight HTML generated at buildno JS dependency, SEO friendly; but only some prismjs plugins supported
preprocess: false (current)highlight generated by JS in the browserall plugins supported (treeview/autolinker/diffhighlight); one extra JS execution on first paint

2.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
code.highlight.prismjs.stylestring"default"prismjs theme name or CSS linkLight theme
code.highlight.prismjs.style_darkstring"tomorrow night"prismjs theme name or CSS linkDark theme
code.highlight.prismjs.preprocessboolfalsetrue / falseStatic generation at build
code.highlight.prismjs.treeviewbooltruetrue / falsetreeview tree display
code.highlight.prismjs.autolinkerbooltruetrue / falseLinks in code clickable
code.highlight.prismjs.diffhighlightbooltruetrue / falsediff highlight

2.4 Boundaries & conflicts

  • Pick a style name at prismjs.com and fill it in (e.g. "tomorrow night"), or paste a CSS link directly.
  • style_dark applies automatically in dark mode; no manual switching needed.
  • preprocess: true conflicts with treeview/autolinker/diffhighlight: static generation supports only some plugins, so all three stop working.

2.5 Summary

  • Minimal setup: style + style_dark
  • Want the enhancement plugins: keep preprocess: false
  • Want SEO / no JS: preprocess: true, and accept that the enhancements stop working
  • Dark mode hard to read: tune style_dark first

3. highlightjs Themes (backup engine)

Purpose: use the following config if you switch to lib: "highlightjs".

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

3.1 How to enable

Effect: code blocks are colored by highlight.js — github gist for light, dark for dark.

Corresponding config (theme config, code.highlight.highlightjs section):

code:
  highlight:
    highlightjs:
      style: "github gist"   # light theme (pick at highlightjs.org/static/demo)
      style_dark: "dark"     # dark theme

You must also set code.highlight.lib to "highlightjs", otherwise this section has no effect.

3.2 Common options

① Change the light theme

Effect: the light-mode palette of code blocks changes.

Corresponding config:

code:
  highlight:
    highlightjs:
      style: "atom-one-light"

② Change the dark theme

Effect: the palette in dark mode changes.

Corresponding config:

code:
  highlight:
    highlightjs:
      style_dark: "atom-one-dark"

3.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
code.highlight.highlightjs.stylestring"github gist"highlight.js theme nameLight theme
code.highlight.highlightjs.style_darkstring"dark"highlight.js theme nameDark theme

3.4 Boundaries & conflicts

  • This section only takes effect when lib: "highlightjs"; with prismjs, changing it does nothing.
  • highlight.js has no equivalent of treeview/autolinker/diffhighlight.

3.5 Summary

  • To switch: change lib first, then fill in the two styles
  • Where theme names come from: the demo page at highlightjs.org
  • If you use prismjs, leave this section alone

4. Code Block Enhancements (code section)

Purpose: display and interaction behavior of code blocks: folding, wrapping, expand-all, fullscreen, copy button, language label.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

4.1 How to enable

Effect: a copy button and a fullscreen button appear at the top-right of code blocks; overly tall blocks collapse by default and show “expand all”; the language label appears at the top-left (TEXT when unspecified); long lines scroll horizontally instead of wrapping.

Corresponding config (theme config, code section):

code:
  shrink: true            # code block collapsible (fold)
  break: false            # wrap lines (false = long lines scroll horizontally)
  show_full: true         # show the whole code
  height_limit: "450px"   # show_full height threshold: "expand all" button above this
  show_expand: true       # fullscreen code box
  copy_btn: true          # copy code button
  language:
    enable: true          # code block language label
    default: "TEXT"       # default label when no language given

4.2 Common options

① Wrap long lines

Effect: overly long lines wrap automatically instead of scrolling horizontally (this breaks alignment).

Corresponding config:

code:
  break: true

② Adjust the “expand all” threshold

Effect: the smaller the threshold, the more code blocks get collapsed.

Corresponding config:

code:
  height_limit: "300px"

③ Turn off the copy button

Effect: code blocks no longer show the copy icon.

Corresponding config:

code:
  copy_btn: false

④ Change the default language label

Effect: code blocks without a language name show the new label.

Corresponding config:

code:
  language:
    default: "Code"

Effect notes (item by item):

  • show_full: true + height_limit: overly tall code blocks collapse by default; click “expand all” to view — prevents long code from blowing up the page
  • break: false: long lines scroll horizontally instead of wrapping, preserving code formatting
  • copy_btn: true: copy button appears on hover, one-click copy
  • code.language.default: code blocks with no language name show the TEXT label (can be changed to "Code" etc.)

4.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
code.shrinkbooltruetrue / falseCode block collapsible
code.breakboolfalsetrue / falseWhether long lines wrap
code.show_fullbooltruetrue / falseCollapse overly tall blocks
code.height_limitstring"450px"Length with unit (e.g. "300px")Expand button threshold
code.show_expandbooltruetrue / falseFullscreen button
code.copy_btnbooltruetrue / falseCopy button
code.language.enablebooltruetrue / falseLanguage label
code.language.defaultstring"TEXT"any stringLabel when no language given

4.4 Boundaries & conflicts

  • height_limit must be a string with a unit ("450px"); a bare number is ignored.
  • When show_full: false, height_limit is meaningless — long code is fully expanded.
  • With both break: true and line numbers enabled, continuation lines of a wrapped line have no line number, which hurts readability.

4.5 Summary

  • Minimal setup: keep the defaults
  • Long code blowing up the page: show_full + a suitable height_limit
  • Horizontal scrolling annoying on mobile: break: true
  • Want a localized label: language.default

Purpose: when copied post content (including code) exceeds a character threshold, automatically append a copyright notice to the clipboard.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

5.1 How to enable

Effect: after a reader copies more than 120 characters of body text/code, a line — “The copyright of this article belongs to the author. Please credit the source for any form of reproduction.” — is appended to the pasted content.

Corresponding config (theme config, code.copyright section):

code:
  copyright:
    enable: true
    minCharNumber: 120    # minimum copied chars before appending the notice
    description: The copyright of this article belongs to the author. Please credit the source for any form of reproduction.

5.2 Common options

① Adjust the trigger threshold

Effect: with minCharNumber: 300, short copies get nothing appended; only long selections carry the notice.

Corresponding config:

code:
  copyright:
    minCharNumber: 300

② Change the notice text

Effect: the copyright text produced on paste changes.

Corresponding config:

code:
  copyright:
    description: Please credit the source and keep the original link when reproducing.

③ Turn off copy copyright

Effect: nothing is appended to copied content.

Corresponding config:

code:
  copyright:
    enable: false

5.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
code.copyright.enablebooltruetrue / falseAppend copyright on copy
code.copyright.minCharNumbernumber120positive integer (characters)Trigger threshold
code.copyright.descriptionstringsee aboveany stringAppended copyright text

5.4 Boundaries & conflicts

  • This is copy-behavior copyright protection (distinct from the post-bottom copyright card post.copyright): after readers copy body/code, the pasted content ends with an auto-appended source notice.
  • The threshold counts characters; copying a whole code block usually far exceeds 120.
  • It is independent of post.copyright; each can be toggled separately.

5.5 Summary

  • Minimal setup: enable: true
  • Find it annoying: raise minCharNumber
  • Want different text: change only description
  • It is not the same thing as the post copyright card

Appendix: Code Highlight Quick Reference

Config itemLocationDescription
Highlight enginecode.highlight.libprismjs (current) | highlightjs
Line numberscode.highlight.line_numbertrue/false
prism themescode.highlight.prismjs.style / style_darklight/dark pair
Render modecode.highlight.prismjs.preprocessfalse = browser-generated (current)
prism extrastreeview / autolinker / diffhighlighttree/link/diff highlight
Collapse/wrapcode.shrink / code.breakfolding and long-line handling
Expand allcode.show_full + height_limitcollapse tall blocks + threshold
Fullscreencode.show_expandfullscreen code box
Copy buttoncode.copy_btnhover to copy
Language labelcode.languagedefault TEXT
Copy copyrightcode.copyrightappend copyright after 120+ chars
Comments
Data Loading...