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:Popular Languages
- 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
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
Editing Code Blocks
Click the edit button to modify code:Edit Modal
Edit Modal
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
Applying Changes
Applying Changes
After saving:
- Source and language are stored in data attributes
- New line numbers are generated based on updated content
- highlight.js re-highlights the code with the new language
- 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: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.
Best Practices
Language aliases: Many languages have multiple valid identifiers (e.g.,
js = javascript, py = python). Use the most common form for consistency.