mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-26 10:04:29 +09:00
Make Markdown fenced code block work with more syntaxes (#37154)
This commit is contained in:
@@ -70,6 +70,8 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
|
||||
}
|
||||
case *ast.CodeSpan:
|
||||
g.transformCodeSpan(ctx, v, reader)
|
||||
case *ast.FencedCodeBlock:
|
||||
g.transformFencedCodeblock(v, reader)
|
||||
case *ast.Blockquote:
|
||||
return g.transformBlockquote(v, reader)
|
||||
}
|
||||
|
||||
@@ -600,3 +600,22 @@ func TestMarkdownUlDir(t *testing.T) {
|
||||
</ul>
|
||||
`, string(result))
|
||||
}
|
||||
|
||||
func TestMarkdownFencedCodeBlock(t *testing.T) {
|
||||
testRender := func(input, expected string) {
|
||||
buffer, err := markdown.RenderString(markup.NewTestRenderContext(), input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
|
||||
}
|
||||
const nl = "\n"
|
||||
const prefix = `<div class="code-block-container code-overflow-scroll"><pre class="code-block">`
|
||||
const suffix = `</pre></div>`
|
||||
|
||||
testRender("```\ncode\n```", prefix+`<code class="chroma language-text display">code`+nl+`</code>`+suffix)
|
||||
|
||||
const jsCommon = prefix + `<code class="chroma language-js display"><span class="nx">code</span>` + nl + `</code>` + suffix
|
||||
testRender("```js\ncode\n```", jsCommon)
|
||||
testRender("```js:app.ts\ncode\n```", jsCommon)
|
||||
testRender("```js,ignore\ncode\n```", jsCommon)
|
||||
testRender("```js ignore\ncode\n```", jsCommon)
|
||||
}
|
||||
|
||||
32
modules/markup/markdown/transform_codeblock.go
Normal file
32
modules/markup/markdown/transform_codeblock.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package markdown
|
||||
|
||||
import (
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/text"
|
||||
)
|
||||
|
||||
func (g *ASTTransformer) transformFencedCodeblock(v *ast.FencedCodeBlock, reader text.Reader) {
|
||||
// * Some engines support a meta syntax for appending the filename after the language, separated by a colon
|
||||
// * https://www.glukhov.org/documentation-tools/markdown/markdown-codeblocks/
|
||||
// * Some engines support additional "options" after the language, separated by a space or comma: ```rust,ignore```
|
||||
// * https://docs.readme.com/rdmd/docs/code-blocks
|
||||
// * https://next-book.vercel.app/reference/fencedcode
|
||||
if v.Info == nil {
|
||||
return
|
||||
}
|
||||
info := v.Info.Segment.Value(reader.Source())
|
||||
newEnd := -1
|
||||
for i, b := range info {
|
||||
if b == ' ' || b == ',' || b == ':' {
|
||||
newEnd = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if newEnd != -1 {
|
||||
start := v.Info.Segment.Start
|
||||
v.Info = ast.NewTextSegment(text.NewSegment(start, start+newEnd))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user