Mojo Code Blocks in Quarto

Quarto
Mojo code blocks with syntax highlighting in Quarto.
Author

Craig A Miller

Published

July 12, 2024

Abstract

How to use Mojo within Quarto to display syntax highlighted code.

Keywords

Quarto, Mojo, Syntax Highlighting, Code Block

Mojo Code Blocks in Quarto

To get mojo executing inside of a quarto QMD file.

  1. Install Modular’s Mojo/Max which includes the mojo-max-jupyter-kernel 1
Note

Verification: A good way to verify your intstallation within your VS Code Quarto project is to create a Jupyter notebook (mynotebook.ipynb) and verify that you can select the Mojo(Max) kernel.

  1. Inside your Quarto .qmd file’s front matter add:
jupyter: mojo-max-jupyter-kernel
  1. Declare a code block
Code
fn greet(name: String) -> String:
    return "Hello " + name + "🔥"

print(greet("Mojo"))
Hello Mojo🔥
Figure 1

Mojo syntax highlighting

Quarto doesn’t come with mojo syntax highlighting by default. You need to add it.

  • Add the mojo.xml syntax definition file to your project.
  • Add the following syntax-definition reference to the front matter of your qmd file or globally in your _quarto.yml
html:
  code-fold: true
  theme: slate
  syntax-definitions:
    - mojo.xml

Your mojo code should now be syntax highlighted like mine is.

Footnotes

  1. https://docs.modular.com↩︎