Skip to content

[BUG] {lang} code blocks: parentElement.className is overwritten, wiping classes from other plugins #454

Description

@heshuren371

Describe the bug

When a code block uses the Rmd/Quarto-style language identifier (e.g. ```{python}), addToCodeBlock rewrites the classes of the parent <pre> element by assignment:

// src/RunButton.ts:126 (addToCodeBlock)
codeBlock.parentElement.className = codeBlock.className;

This replaces the entire class list of the <pre> instead of adding to it. Any classes previously added to the <pre> by Obsidian core or other plugins are silently removed.

To Reproduce

  1. Install a plugin that decorates code blocks at the <pre> level (e.g. Code Styler, which adds code-styler-pre and fold-state classes).
  2. Create a note with a ```{python} block.
  3. Switch to reading mode.

Expected behavior

The plugin should only adjust the classes it needs (e.g. via classList.add / classList.replace), preserving classes set by Obsidian or other plugins.

Actual behavior

All pre-existing <pre> classes are wiped. Downstream effects observed with Code Styler (reported as mayurankv/Obsidian-Code-Styler#334):

  • Code block styling and folding state are lost for {lang} blocks.
  • The copy button ends up mis-positioned / broken because the CSS selectors that place it depend on the removed classes.
  • Unload/cleanup routines of the other plugin can no longer find the block (pre.code-styler-pre selector misses), leaving stale header elements behind.

Suggested fix

// Instead of overwriting:
codeBlock.parentElement.className = codeBlock.className;
// Only add what's needed, e.g.:
codeBlock.classList.forEach((cls) => codeBlock.parentElement.classList.add(cls));

(or the minimal equivalent that adds the rewritten language-x { classes without clearing the rest.)

Environment

  • Plugin version: 2.1.2
  • Obsidian version: 1.8+
  • OS: macOS (likely affects all platforms)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions