forgejo/modules/markup/markdown/transform_codeblock_lang.go
TurtleArmy 2b42fdaa26 feat(ui): Fix comma separated attributes in code blocks language preventing syntax-highlighting (#12056)
Currently, forgejo does not support syntax highlighting code-blocks that have comma separated attributes after the language. This is a pattern sometimes seen in Rust code blocks, with tests like this:

\`\`\`rust
#[test]
fn run_this_test() { /* ... */ }
\`\`\`

\`\`\`rust,ignore
#[test]
fn skip_this_test() { /* ... */ }
\`\`\`

Currently, forgejo only does syntax highlighting in the first case:

```rust
#[test]
fn run_this_test() { /* ... */ }
```

```rust,ignore
#[test]
fn skip_this_test() { /* ... */ }
```

An example of this causing problems can be seen in this commit (5be9c5b7d2) causing the following issue (https://codeberg.org/zesterer/ariadne/issues/188).

This PR fixes fixes the second case not getting proper syntax highlighting.

Co-authored-by: TurtleArmy <44322335+TurtleArmyMc@users.noreply.github.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12056
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: Ellen Εμίλια Άννα Zscheile <fogti@noreply.codeberg.org>
Co-authored-by: TurtleArmy <turtlearmy@noreply.codeberg.org>
Co-committed-by: TurtleArmy <turtlearmy@noreply.codeberg.org>
2026-04-12 18:30:30 +02:00

27 lines
646 B
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markdown
import (
"bytes"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
)
func (g *ASTTransformer) transformCodeblockLanguage(v *ast.FencedCodeBlock, reader text.Reader) {
src := reader.Source()
info := v.Info.Segment.Value(src)
// Strip language after commas
//
// For example,
// ```rust,ignore
// ...
// ```
// Should have a language of "rust", not "rust,ignore"
if i := bytes.IndexByte(info, ','); i != -1 {
start := v.Info.Segment.Start
v.Info = ast.NewTextSegment(text.NewSegment(start, start+i))
}
}