All guides

Make a Code Snippet Readable as an Image

Published

Choose one fragment, keep lines inside the card, name the language for highlighting, and publish the copyable source next to the picture.

Steps

  1. Cut the sample to the point. Keep the function, command, or query that changed. Drop imports and scaffolding that the caption can name instead.
  2. Tag the fence and break long lines. Put the language on the opening fence. Split any line the preview marks as wider than the card.
  3. Preview on a dark and a light template only if you are unsure. Pick one, check token contrast, and export PNG.
  4. Paste the copyable source into the post. The image and the fence should match. If you edit one, edit the other.

One fragment, one reason

A code image works when a reader can see the interesting part without zooming and without already knowing which line you meant. That is a small surface. A 40-line function, a whole file, or a terminal session with the prompt and three retries will not become readable because the background is dark. Cut until the card shows the change and almost nothing else.

A useful test: cover the caption with your hand. Can you tell what is new? If the answer is no, the image is a file dump. Move the dump to a repository link and keep a shorter fence for the feed. The code snippet tool will render whatever you paste, including too much. The limit is editorial.

This sample is the whole card. The heading says what the helper is for, and the function is short enough that a feed can show every token:

export function imageName(slug: string, page = 1) {
  return slug + "-page-" + page + ".png";
}

The page number is in the file name, not only in the interface. If you need a template literal in the real source, keep that version in the repository and show this flatter line on the card, or break the template literal so it does not run off the right edge. The picture and the copyable fence can differ by whitespace. They should not differ by behavior.

Lines are clipped, not wrapped

The card's code block does not soft-wrap. A line that is wider than the inner width is cut off, and the preview reports horizontal overflow. The height estimate counts source lines, so a single very long line still looks like one line to the paginator while the right-hand side is simply gone. That is the bug people ship: the command looks complete in the editor textarea, which scrolls, and incomplete in the image, which does not.

Break the line in the Markdown before you export. For a shell command, continue with a backslash only if that is valid for the shell you are showing, and put the continuation where a person would actually type it. For a function call, break after a comma. Do not shrink the font from 36 toward 24 just to keep a 120-character line on a 1200-pixel card. You will win the pixels and lose the phone. Type and contrast is the arithmetic on what 36px becomes when the feed displays the file at a third of its width.

A fence that is too tall is split by source line, and each chunk keeps the opening fence, including the language tag. The split will not repeat a comment that only existed on line 1. If the interesting part is the last line, do not let the paginator hide it on page 2 of a carousel nobody asked for. Shorten the sample until it fits one card. The five-page limit and the rest of the break rules are in where to split a long note.

The language tag is the highlighting

Write the language on the opening fence: ts, js, py, bash, sql, or whatever the highlighter accepts. The page colors keywords, strings, numbers, and comments from those tokens. An untagged fence stays monospaced and uncolored, which is fine for a one-line command and flat for a function where the string and the keyword matter.

The palette depends on the template. Dark Code and Developer are dark surfaces, and the default highlight colors are light cyan, lime, and pink. Minimal and Newsletter are light surfaces, and the stylesheet switches those token colors to darker blue, green, and purple so they are not pastel-on-white. Highlighting is not a contrast checker. Look at the actual fence. A comment that is the point of the slide can be the least prominent color on the card. If the comment is the message, pull it out into the heading and let the code be the evidence.

Code is set in a monospace stack that asks for JetBrains Mono and then the browser's ui-monospace. The font file is not part of the site. Two machines can export the same Markdown with slightly different glyph widths, which is another reason a line that barely fits on your laptop may clip on someone else's. If the preview warns, the line is already too long on the machine you are using.

There is no separate code-size control. Body type runs from 24 to 56 and starts at 36. Code is drawn at about 0.7 times that size, so the default card uses a 25px monospace. Headings are about 1.9 times the body size. Those ratios are the editor's, not a suggestion you can override without changing the body size. Raising the body size to make code larger also raises the heading and the list under it. Sometimes that is what you want. Sometimes it pushes the fence onto a second page. Export the first page and read it before you accept the split.

Pick the template for the code, then stop

Dark Code is the terminal-like card: near-black background, light text, cyan accents. Use it when the snippet is the subject. Developer is the other dark option, slightly less extreme, and it still suits a function plus a sentence. Minimal and Newsletter are for a note in which the code is a short illustration inside a sentence, not the whole post. Launch Card's green is a poor home for a long fence. The syntax colors were not designed as a brand palette, and a launch announcement rarely needs more than one identifier.

Compare at most two templates. A grid of five themes is indecision. Choose the one whose comments you can still read, export PNG, and move on. PNG is the right master for highlighted code because the token edges are hard and JPEG will dirt them. The encoding argument, including one measured file from this editor, is in PNG, JPEG, and WebP for text.

Publish the text beside the picture

The PNG is flat. Readers cannot select the function. People who will run the command need it in the post, the docs, or a gist, in a real fence. Keep the two in sync. A common failure is to fix a bug in the image and forget the caption, or the reverse, so the thread argues about a line that is not the line you meant.

Alt text for a code card should say what the function does and name the language. It should not be a second paste of the body unless the body is a single short command that is not already in the post. The reasoning is in alt text for text images. On X or LinkedIn, the caption can hold the fence in a code block if the product supports it, and the image can be the visual for people who are scanning. If the product eats the formatting, a short indented command in the caption still beats a picture as the only copy.

Do not include line numbers you pasted by hand. The editor does not add them, and a column of numbers steals width from the only line that mattered. Do not include the editor chrome, the file tree, or a full test file to prove the function is real. Link the test.

A ten-second review

Before you upload, open the PNG at roughly the width of a phone column and answer four questions.

  • Is every character of the important line visible, with no clip warning left behind in the editor?
  • Can you tell which token is a string and which is a comment without squinting?
  • Does the heading say what to do with the code, or does it only repeat the function name?
  • Is there a copyable version that matches?

If the first answer is no, break the line. If the second is no, change template or pull the comment into the heading. If the third is no, rewrite the heading. If the fourth is no, paste the fence into the post before you publish the image. The examples include a short helper on the Open Graph canvas in the Dark Code template if you want a starting point that already fits.

A code image is a figure. Figures have captions. The caption here is the post, and the source of the figure is the Markdown you can export again next week when the function changes.

Frequently asked questions

Why is the right side of my command missing?
Code lines are not wrapped. The card clips anything wider than the canvas, and the preview warns you. Break the line in the Markdown and export again.
Does the language tag change the picture?
It changes the highlight colors. The site styles keywords, strings, numbers, and comments differently, and it uses a second palette on the light templates. An untagged fence is still monospaced, but it is not highlighted.
Which template should I use for code?
Dark Code and Developer are the dark pair. Minimal and Newsletter are light, and their highlight colors are the darker set so the tokens are not pastel on white. Look at the fence on the template you will actually publish.
Can readers copy the snippet from the PNG?
No. Put the same fence in the post, the gist, or the docs. The image is the invitation to look. The text is the thing they run.

Try the tools