Make code blocks collapsible in preview mode with customizable collapse/expand behavior and horizontal scrolling options. This plugin enhances the readability of your notes by allowing you to collapse long code blocks when you don't need to see their full content.
- Toggle code blocks between collapsed and expanded states
- Customizable collapse/expand icons
- Optional horizontal scrolling for code blocks
- Configure number of visible lines when collapsed
- Default collapse state setting
- Control if a code block should be expanded or collapsed on a per-note basis by setting code-blocks to
collapsed
orexpanded
in the frontmatter - Smooth animations for collapse/expand transitions
- Maintains proper layout with surrounding content
- Open Obsidian Settings
- Navigate to Community Plugins and disable Safe Mode
- Click Browse and search for "Collapsible Code Blocks"
- Install the plugin
- Enable the plugin in your Community Plugins list
Once installed, code blocks in preview mode will automatically have a toggle button in their top-left corner. You can:
- Click the toggle button to collapse/expand the code block
- Use the keyboard (Enter or Space) when the toggle button is focused
- Configure default behavior in plugin settings
- Click the toggle button (▼/▶) to collapse or expand the code block
- When collapsed, only the specified number of lines will be visible
- Horizontal scrolling can be enabled/disabled globally
- Code blocks maintain their collapsed/expanded state when switching between notes
The plugin offers several customization options:
- Default Collapsed State: Choose whether code blocks should start collapsed
- Collapse/Expand Icons: Customize the icons shown for collapse/expand states
- Horizontal Scrolling: Toggle horizontal scrolling for code blocks
- Collapsed Lines: Set how many lines remain visible when collapsed
- Frontmatter Setting: Set the frontmatter key to control the default state of code blocks
Set a note to always have code blocks expanded or collapsed by default by adding the following to the note's frontmatter (note: this will override the global default setting):
code-blocks: collapsed
code-blocks: expanded
Here's how a code block appears with the plugin enabled and the code block expanded:
Here's how a code block appears with the code collapsed and set to show 0 lines.
If you encounter any issues or have suggestions:
- Create an issue on GitHub
- Support the development:
Want to contribute? Here's how to get started:
-
Clone the repository:
git clone https://github.com/bwya77/collapsible-code-blocks.git
-
Install dependencies:
npm install
-
Start development build:
npm run dev
- Create a symbolic link to your vault's
.obsidian/plugins/
directory - Enable the plugin in Obsidian's community plugins settings
- Use the developer console (Ctrl+Shift+I) to debug