Skip to main content

Syntax Highlighting

Markdown-OS provides comprehensive syntax highlighting for code blocks using highlight.js. The editor supports 190+ programming languages with automatic language detection, line numbers, copy functionality, and theme-synchronized color schemes.

Creating Code Blocks

Code blocks are created using fenced code blocks with an optional language identifier:
The language identifier (e.g., javascript) is used for syntax highlighting. If omitted, the code is displayed as plain text.

Supported Languages

highlight.js supports a wide range of languages including:
  • JavaScript/TypeScript: javascript, typescript, jsx, tsx
  • Python: python, py
  • Java/Kotlin: java, kotlin
  • C/C++/C#: c, cpp, csharp, cs
  • Go: go, golang
  • Rust: rust, rs
  • Ruby: ruby, rb
  • PHP: php
  • Swift: swift

Web Technologies

  • HTML/XML: html, xml
  • CSS/SCSS: css, scss, sass, less
  • SQL: sql, mysql, postgresql
  • GraphQL: graphql, gql
  • JSON: json
  • YAML: yaml, yml

Shell & Config

  • Bash/Shell: bash, sh, shell
  • PowerShell: powershell, ps1
  • Dockerfile: dockerfile
  • Nginx: nginx
  • Apache: apache

Markup & Data

  • Markdown: markdown, md
  • LaTeX: latex, tex
  • TOML: toml
  • INI: ini
For the complete list of supported languages, see the highlight.js documentation.

Code Block Features

Every code block is enhanced with interactive features:

Visual Components

1

Language Label

Top-left corner shows the programming language (e.g., “javascript”, “python”)
2

Line Numbers

Left gutter displays line numbers for easy reference
3

Copy Button

Top-right copy icon copies the entire code block to clipboard
4

Edit Button

Edit icon opens a modal to modify the code and change the language

Implementation

Line Numbers

Line numbers are automatically generated based on the code content:
Line numbers are marked with aria-hidden="true" to prevent screen readers from announcing them.

Copy Functionality

The copy button provides visual feedback when code is copied:

Copy Feedback

The copy button temporarily changes to a checkmark for 1.5 seconds after successful copy, providing clear visual feedback.

Editing Code Blocks

Click the edit button to modify code:
The block editor opens with:
  • Title: “Edit code block”
  • Source textarea: Pre-filled with current code
  • Language input: Current language identifier (e.g., “javascript”)
  • Save/Cancel buttons: Keyboard shortcuts supported
After saving:
  1. Source and language are stored in data attributes
  2. New line numbers are generated based on updated content
  3. highlight.js re-highlights the code with the new language
  4. Change event is emitted for auto-save

Theme Integration

highlight.js color schemes synchronize with the active editor theme:

Dynamic Theme Loading

Theme stylesheets are loaded from the highlight.js CDN dynamically when the theme changes.

Language Detection

If no language is specified, the code is labeled as “text”:
Code blocks without a language identifier are not syntax-highlighted but still get line numbers, copy buttons, and edit functionality.

Markdown Serialization

When saving, code blocks are converted back to fenced code blocks:
The serialization uses the raw source from data-raw-source attributes to preserve the original code exactly.

Code Block Structure

Each code block has a specific DOM structure:
The wrapper has contenteditable="false" to prevent accidental editing, requiring users to use the edit button instead.

Cleanup on Serialization

Before converting to markdown, UI elements are removed:

Performance Considerations

Large code blocks: highlight.js handles code blocks with thousands of lines efficiently, but rendering many large blocks at once may cause brief delays.
Lazy highlighting: Consider splitting very large files into multiple smaller code blocks for better performance.

Best Practices

Always specify the language: This enables proper syntax highlighting and helps readers understand the code context.
Avoid extremely long lines: Lines over 200 characters may cause horizontal scrolling. Consider breaking them for better readability.
Language aliases: Many languages have multiple valid identifiers (e.g., js = javascript, py = python). Use the most common form for consistency.