Code
Raptor provides built-in syntax highlighting for code blocks. When you use CodeBlock or Code with an explicit language—or rely on the Site’s default post processor to render Markdown—Raptor automatically tracks and includes the required syntax highlighters at build time.
Code Blocks
Create code blocks with CodeBlock:
CodeBlock(.swift) {
"""
let value = compute()
print(value)
"""
}
Override the site’s default syntax highlighting behavior using modifiers:
CodeBlock(.swift) {
"""
let value = compute()
print(value)
"""
}
.lineNumberVisibility(.visible)
.whitespaceCharacterVisibility(.visible)
.highlightedLines(2)
.syntaxHighlighterTheme(.monokai)
let value = compute()
print(value)
Available modifiers include:
lineNumberVisibility(_:)— Show or hide line numberswhitespaceCharacterVisibility(_:)— Show or hide spaces and line breakshighlightedLines(_:)— Emphasize specific lines or rangessyntaxHighlighterTheme(_:)— Override the syntax highlighting theme for this code blocklineHighlightOffset(_:)— Fine-tune vertical alignment of highlighted lines (useful for custom typefaces)
Syntax Highlighting Configuration
Configure syntax highlighting behavior site-wide using syntaxHighlighterConfiguration:
struct MySite: Site {
var syntaxHighlighterConfiguration = SyntaxHighlighterConfiguration(
defaultLanguage: .swift,
lineNumberVisibility: .visible,
inlineCodeHighlighting: .enabled
)
}
When using Raptor’s default post processor, these settings apply automatically. Custom post processors are responsible for honoring the site’s configuration.
For more on configuring syntax highlighting colors and themes, see Syntax Highlighting.