Technical writing

How to Add Code Blocks to Substack Without Breaking the Layout

Code examples make a technical newsletter useful, but they also create some of its worst mobile failures. Long lines, large logs, deeply nested JSON, and formatting copied from an IDE can overwhelm the narrow reading column or arrive as unstructured plain text.

By StackDraft Editorial8 min read
Short answer

Use inline code for short identifiers and a fenced Markdown block for multi-line examples. Render the draft to rich text before pasting, trim long lines and irrelevant output, then test the final Substack email on a phone.

Prepare the draft while you follow the guide

StackDraft runs locally in your browser. No account or upload required.

Open StackDraft

Choose inline code or a code block

Substack’s editors expose code formatting, but the most portable source is still semantic Markdown. Backticks describe the role of the text without carrying an IDE theme, font, or background into the publishing workflow.

FormatGood useExample
Inline codeA command, function, flag, filename, or small value inside prosenpm run build
Code blockA multi-line program, config, command sequence, or focused output sampleA complete function or shell session
Linked sourceA long file, generated output, repository, or example readers may copyGitHub file or runnable sandbox

Write a portable fenced code block

Put three backticks on their own lines and add a recognized language label immediately after the opening fence. The label helps a Markdown renderer choose syntax highlighting; it is not part of the code readers copy.

Markdown source
```ts
export function canonicalUrl(pathname: string) {
  return new URL(pathname, "https://www.stackdraft.app").toString()
}
```

Move Markdown code into Substack

  1. 01

    Reduce the example

    Delete setup and output that do not support the article’s point. Prefer one focused example over a full production file.

  2. 02

    Wrap long expressions intentionally

    Break chains, arrays, commands, and object literals at meaningful boundaries. Email readers should not need to discover the important token by scrolling sideways.

  3. 03

    Render the Markdown

    Load the draft in StackDraft and confirm the code appears as a distinct preformatted block. Long blocks trigger a publishing warning.

  4. 04

    Copy the prepared body

    Paste the rich-text result into the desktop Substack editor and confirm the code remains grouped as one block.

  5. 05

    Test copy and mobile behavior

    Open the test email on a phone, follow the explanation around the code, and copy a representative line if copying is part of the reader task.

Edit code for readers, not for the repository

  • Introduce what the example proves before showing it.
  • Keep lines near a narrow reading width when the language permits it.
  • Use comments to explain intent or omitted context, not obvious syntax.
  • Replace long generated IDs and payloads with clearly marked placeholders.
  • Show relevant error lines instead of an entire stack trace.
  • Put installation commands and application code in separate blocks.
  • Link to the tested full source when exact reproduction matters.

Avoid screenshots for code readers need to use

A code screenshot can preserve a visual theme, but it prevents selection, makes zooming necessary, and is difficult for assistive technology. Use real code text for instructions and examples. Reserve images for explaining a visual editor state or annotating a UI where pixels are the actual subject.

If you include a code image for visual emphasis, provide the meaningful snippet as text or link to an accessible source. Alt text is useful context, but it is not a practical replacement for a long runnable program.

Pre-publish code checklist

  • Every code block has a purpose stated in nearby prose.
  • The language label matches the syntax.
  • No secret, personal path, or internal hostname remains.
  • Long lines and large output samples have been reduced.
  • Placeholders are unmistakably placeholders.
  • The final Substack preview preserves whitespace and grouping.
  • The test email is readable without rotating or zooming the phone.
  • The complete tested source is linked when omissions matter.

Frequently asked questions

Does Substack support code formatting?

Yes. Substack’s editor includes code formatting options. For a complete Markdown draft, render fenced code to rich text before pasting and verify the final block in a test email.

Should I use syntax highlighting in a newsletter?

Light syntax highlighting can improve scanning, but structure and contrast matter more than an elaborate theme. The example should remain understandable if destination styling changes.

How do I stop code from overflowing on mobile?

Shorten the example, break long expressions at logical points, remove huge values, and test the actual email. A link to the complete file is better than forcing a full desktop-width program into the post.

Should code be an image in Substack?

Not when readers need to select, copy, search, translate, or inspect it. Use text code blocks and reserve screenshots for genuinely visual editor or interface states.

Sources and further reading

S
StackDraft Editorial

These guides are maintained alongside StackDraft’s Markdown renderer, clipboard workflow, and publishing checks. Product behavior is verified against the current code; destination-platform behavior is reviewed against official support documentation.

Review the compatibility report
Ready to publish?

Turn the Markdown draft into clean Substack copy.

Preview the post, catch fragile blocks, and copy the title and body separately.

Open StackDraft free