Markdown Code Block Line Breaks — Fenced Code, Long Lines, and PDF Tips

Fenced code blocks preserve all line breaks verbatim. Covers language tags, long-line overflow in PDF/print, escaping backticks, tabs vs spaces, and security tips for code in Markdown.

What you'll learn

The key difference from body text: fenced code blocks preserve newlines verbatim—no hard-break syntax needed.


In a fenced code block (three backticks ``` … ```), line breaks in the source are preserved as plain-text line endings. That is different from hard breaks in body paragraphs (two trailing spaces, etc.). Indentation and blank lines inside the fence are also kept, so writing intentional line counts matters for readability.

Basics: fences

Put a language name on the opening line (e.g. javascript) for syntax highlighting in many editors. If you forget the closing ```, everything below may be treated as code—a classic accident in long docs. Adding blank lines before and after the fence can stabilize parsing (implementation-dependent; always check preview).

```javascript
function hello() {
  console.log("line1");
  console.log("line2");
}
```

Line breaks inside the block are all kept.

Indented code blocks

Older Markdown allowed indented code blocks (four or more spaces at line start), but they clash easily with lists and blockquotes. Modern docs usually prefer fenced blocks. Follow your team style guide if one exists.

PDF and print

Very long single lines stretch horizontally in HTML and may overflow or shrink in PDF. Some tools support pre/code wrapping or avoid page breaks inside code blocks. When you can, break lines in the source at meaningful places (argument lists, operators). For one unavoidable long log line, horizontal scroll on screen may be acceptable.

Escaping backticks

To show backticks inside a block, use four or more backticks for the outer fence (or follow your environment's escaping rules). Bad escaping can break the closing fence and wreck the rest of the Markdown.

Tabs vs spaces

Indentation uses your editor's tab width (2 / 4 / 8). For shared repos, align on .editorconfig or formatters—spaces vs tabs—to reduce "misaligned" review noise. Full-width spaces pasted by mistake often cause compile errors; suspect the line if something odd happens.

Language tags

Tags like ```ts add highlighting; unknown language names may be ignored or warned. In internal docs, separate full copy-paste snippets from short excerpts and move the latter into bullet lists so code blocks do not balloon.

Security

Do not paste real secrets from logs or configs. Replace tokens with placeholders and share secrets over a safe channel. Code blocks are easy to search; mistaken secrets can linger in caches and indexes.

This app

Markdown to PDF lets you avoid page breaks inside code blocks in layout settings. Split long lines on purpose, and pair with How to control page breaks in Markdown PDF for pagination.


FAQ

Blank lines inside my code block disappear

Most renderers preserve blank lines inside fenced blocks. If they vanish, confirm the closing fence is correct and check parser settings.

How do I wrap long lines in a code block?

There is no Markdown standard for wrapping inside fenced blocks. Depending on the tool, a CSS white-space: pre-wrap setting can enable soft-wrapping. Check layout settings in this app for a wrap option.

How do I put three backticks inside a fenced block?

Use four or more backticks for the outer fence. Alternatively, tilde fences (~~~) work in some environments.

Code blocks split across pages in my PDF

Enable "Avoid break inside code block" in Layout settings. For very long blocks, either split them manually or use drag to control break positions.

Syntax highlighting doesn't appear in the PDF

The print dialog needs "Print background graphics" turned on. Google Chrome gives the most consistent output.