Code block features¶
A live tour of every code-block feature the site supports. Use this page as a
reference when authoring boilerplate. Delete it whenever you don't want it
anymore — it lives at docs/examples/code-blocks.md and is referenced
in nav: only.
1. Plain syntax highlighting¶
A normal fenced block with a language hint:
variable "environment" {
type = string
validation {
condition = contains(["dev", "stg", "prod"], var.environment)
error_message = "environment must be one of: dev, stg, prod."
}
}
Pygments handles every common language. Some samples:
from pydantic import BaseModel, Field
class User(BaseModel):
id: int
email: str = Field(pattern=r"^[^@]+@[^@]+$")
func Healthz(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"ok"}`))
}
FROM python:3.12-slim AS runtime
COPY --from=builder /app/.venv /app/.venv
USER 1000:1000
ENTRYPOINT ["/app/.venv/bin/uvicorn", "app:app"]
2. Title (filename) header¶
Add title="..." to put a header bar above the block:
3. Line numbers¶
Add linenums="1" (the number is the starting line):
| app/main.py | |
|---|---|
Click any line number to copy a permalink to that exact line.
4. Highlighted lines¶
Add hl_lines="3 5-7" to subtly highlight a single line and a range:
| main.tf | |
|---|---|
5. Code annotations (numbered popovers)¶
The single most useful feature for boilerplate. Two things are required:
- The fence must use the brace header form with
.annotateadded:``` { .hcl .annotate title="vpc.tf" linenums="1" } - Inside the code, drop
# (1)!(or// (1)!,-- (1)!,<!-- (1)! -->, etc.) on whichever line you want to annotate, then add a numbered list immediately after the closing fence.
Each list item becomes a popover anchored to that line.
| vpc.tf | |
|---|---|
- Custom validation runs at
terraform plantime. Multiplevalidationblocks per variable are allowed and evaluated independently. - The
can()wrapper turns a parse exception intofalse, which is whatconditionexpects. Without it, an invalid CIDR would crash the plan instead of producing a clean error message.
Annotation popovers support full Markdown — bold, links, lists, even nested code:
| settings.py | |
|---|---|
-
model_configreplaces the oldclass Config:style in pydantic v2. See the pydantic-settings docs for every option. -
No default → required. Set it via the environment:
Or in
.env:
6. Diff highlighting¶
- resource "aws_s3_bucket" "logs" {
- bucket = "${var.project}-logs"
- }
+ resource "aws_s3_bucket" "logs" {
+ bucket = "${var.project}-${var.environment}-logs"
+ force_destroy = var.environment != "prod"
+ }
7. Tabbed alternatives — same problem, different stacks¶
Powered by pymdownx.tabbed. Great for "do X in Python / Go / Node" pages.
8. Inline code & keyboard shortcuts¶
Inline code: terraform apply -auto-approve is monospace inline.
Keyboard chords via pymdownx.keys: press Ctrl+C to copy, Cmd+Shift+P
on macOS for the command palette.
9. Admonitions / callouts¶
These pair well with code blocks for context:
Use can() for validations
Wrap any function that might raise with can() so a malformed input
becomes a clean validation error instead of a stack trace.
Don't commit *.tfvars
They almost always contain environment-specific secrets or account IDs.
terraform destroy in prod
No undo. Always run terraform plan -destroy first and review the output.
Collapsible — click me
Use ??? instead of !!! to make a collapsible block. Useful for long
appendices that aren't needed by default.
10. Combining everything¶
-
Compose the bucket name from
locals rather than inlining the format string everywhere. Keep the naming logic in one place. -
Always attach a public-access block. AWS makes this opt-in per bucket, and the default leaves you exposed if a misconfigured policy slips through.
-
Server-side encryption is now on by default for new buckets, but pinning the algorithm explicitly makes intent clear and lets you use a customer KMS key when
var.kms_key_arnis set.