Skip to content

Code block language hints are ignored by syntax highlighting #154

Description

@erwynNL

Description

When specifying a language for a fenced code block in markdown (e.g., ```plaintext or ```nohighlight), the language hint is ignored and highlight.js auto-detection is always used.

Current Behavior

In packages/web/src/components/Markdown/Markdown.tsx, the code applies syntax highlighting to all code blocks unconditionally:

rootRef.current?.querySelectorAll<HTMLElement>("pre code").forEach((block) => {
  hljs.highlightBlock(block);
});

This means:

  • ```plaintext still gets auto-detected (e.g., as pgsql or awk)
  • ```nohighlight is not respected
  • Any language hint is ignored

Expected Behavior

  • If a language is specified (e.g., ```bash), use that language for highlighting
  • If ```plaintext, ```text, or ```nohighlight is specified, skip highlighting entirely
  • Only use auto-detection when no language is specified (```)

Use Case

ASCII diagrams and other non-code content in ADRs get incorrectly highlighted, making them harder to read. Users should be able to disable highlighting for specific code blocks.

Suggested Fix

Check for the language class on the code element before highlighting:

rootRef.current?.querySelectorAll<HTMLElement>("pre code").forEach((block) => {
  const classes = block.className.split(' ');
  const hasNoHighlight = classes.some(c => 
    ['nohighlight', 'no-highlight', 'plaintext', 'text'].includes(c.replace('language-', ''))
  );
  
  if (!hasNoHighlight) {
    const language = classes.find(c => c.startsWith('language-'))?.replace('language-', '');
    if (language) {
      hljs.highlightBlock(block); // Will use the specified language
    } else {
      hljs.highlightBlock(block); // Auto-detect
    }
  }
});

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