mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-06-08 19:02:08 -04:00
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>
This commit is contained in:
parent
fd28fd896b
commit
2b42fdaa26
3 changed files with 53 additions and 0 deletions
|
|
@ -90,6 +90,8 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
|
|||
}
|
||||
case *ast.RawHTML:
|
||||
g.transformRawHTML(ctx, v, reader)
|
||||
case *ast.FencedCodeBlock:
|
||||
g.transformCodeblockLanguage(v, reader)
|
||||
}
|
||||
return ast.WalkContinue, nil
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1488,3 +1488,27 @@ func TestCallout(t *testing.T) {
|
|||
<p>Bad stuff is brewing here</p>
|
||||
</blockquote>`)
|
||||
}
|
||||
|
||||
func TestCodeblockLanguageStripping(t *testing.T) {
|
||||
test := func(input, expected string) {
|
||||
buffer, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, input)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
|
||||
}
|
||||
|
||||
// Unstripped
|
||||
test(
|
||||
"```rust\n"+
|
||||
"fn main() {}\n"+
|
||||
"```",
|
||||
`<pre class="code-block"><code class="chroma language-rust display"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w">
|
||||
</span></code></pre>`)
|
||||
|
||||
// Stripped
|
||||
test(
|
||||
"```rust,ignore\n"+
|
||||
"fn main() {}\n"+
|
||||
"```",
|
||||
`<pre class="code-block"><code class="chroma language-rust display"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w">
|
||||
</span></code></pre>`)
|
||||
}
|
||||
|
|
|
|||
27
modules/markup/markdown/transform_codeblock_lang.go
Normal file
27
modules/markup/markdown/transform_codeblock_lang.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// 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))
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue