avatarArtturi Jalli

Summary

The website content provides guidance on formatting code in Medium articles, including inline code, larger code blocks, and embedding GitHub Gists.

Abstract

The article "How to Format Code on Medium" outlines three primary methods for code formatting within Medium posts. For inline code snippets, the author advises using the backtick key to highlight code within paragraphs. When more extensive code examples are necessary, the article instructs on creating larger code blocks using specific key combinations or three backticks. Additionally, the article emphasizes the use of GitHub Gists for embedding code, which maintains syntax highlighting and allows for updates that automatically reflect in the article. The author also promotes their free Medium course and an AI service for content creation, suggesting a commitment to enhancing the reader's writing experience on Medium.

Opinions

  • The author believes that using GitHub Gists is a superior method for adding code to articles, as it preserves formatting and allows for easy updates.
  • Inline code formatting is presented as a simple and effective way to include code within text for quick references.
  • The author suggests that using the "Paste and Match Style" option when pasting code can help maintain the original formatting, indicating a preference for clean and readable code blocks.
  • The article implies that the use of GitHub Gists may be a requirement for programming articles in certain Medium publications, highlighting a standard practice within the community.
  • By offering a free Medium course and recommending an AI service, the author expresses a commitment to providing value to readers and supporting their growth as writers on the platform.

How to Format Code on Medium

Learn three most common ways to format code in your Medium articles

Photo by Artem Sapegin on Unsplash

How to Write Inline Code

To format code inside a paragraph inline like this: <h1>Hello</h1>, simply highlight the code and press the backtick (`) key:

Remember: Inline code cannot be added to headings, quotes, or image captions.

👉 Join my Free Medium Course to learn how to grow your followers, reads, and earn more.

How to Create a Bigger Code Block

If inline code is not enough for your purposes, you can also create a bigger block of code like this:

while(1 < 2) {
    print("Hello, world!")
}

To create a block of code, press one of these key combinations depending on your system:

  • Windows: ctrl+alt+6
  • Mac: command+option+6
  • Linux: ctrl+alt+6

OR type three backticks (```) in a new line to start a new code block and start typing code.

Hint: If you’re copy-pasting code from an editor, you may lose code formating, i.e. indentations and line breaks, etc. To fix this, you can use the Paste and Match Style property by right-clicking the code block when pasting:

Github Gists

This is a very common way to add chunks of code to your article.

Embedding your code as a Github Gist preserves the code style e.g. syntax highlighting, indentations, etc:

Furthermore, if you need to make changes to the code in the gist, the changes will automatically be reflected in the article.

Also, some publications accept programming articles only if Github Gists are used when embedding code.

How to create a Github Gist:

  1. Go to https://gist.github.com/
  2. Give the gist a description, write the code, and remember to use the proper file extension. e.g. with javascript, name your gist like example.js
  3. When you’re ready, publish the gist by creating a public gist:

4. Copy the URL of the published gist and embed it into Medium:

Embedding a gist to a Medium article

5. Profit

Conclusion

I hope you find these code formatting tips useful.

Give it some claps to make others find it too! Make sure you follow me on Medium not to miss anything. Also, let’s be friends on Linkedin.

Further Reading

Writing
Technology
Startup
Business
Education
Recommended from ReadMedium