Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/util"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -547,3 +548,45 @@ func TestMarkdownLink(t *testing.T) {
<a href="#user-content-foo" rel="nofollow">link3</a></p>
`, string(result))
}

func TestToCWithHTML(t *testing.T) {
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()

t1 := `tag <a href="link">link</a> and <b>Bold</b>`
t2 := "code block `<a>`"
t3 := "markdown **bold**"
input := `---
include_toc: true
---

# ` + t1 + `
# ` + t2 + `
# ` + t3 + `
`

resultHTML, err := markdown.RenderString(markup.NewTestRenderContext(), input)
assert.NoError(t, err)
result := string(resultHTML)

pos1, pos2 := strings.Index(result, `<ul>`), strings.Index(result, `</ul>`)
require.Positive(t, pos1)
require.Positive(t, pos2)
partToc, partContent := result[pos1:pos2+5], result[pos2+5:]
pos3 := strings.Index(partContent, "<h1")
require.Positive(t, pos3)
partContent = partContent[pos3:]

assert.Equal(t, `<ul>
<li>
<a href="#user-content-user-content-tag-a-hreflinklinka-and-bboldb" rel="nofollow">tag link and Bold</a></li>
<li>
<a href="#user-content-user-content-code-block-a" rel="nofollow">code block </a></li>
<li>
<a href="#user-content-user-content-markdown-bold" rel="nofollow">markdown bold</a></li>
</ul>`, partToc)

assert.Equal(t, `<h1 id="user-content-tag-a-hreflinklinka-and-bboldb">tag <a href="/link" rel="nofollow">link</a> and <b>Bold</b></h1>
<h1 id="user-content-code-block-a">code block <code>&lt;a&gt;</code></h1>
<h1 id="user-content-markdown-bold">markdown <strong>bold</strong></h1>
`, partContent)
}
2 changes: 2 additions & 0 deletions modules/markup/markdown/transform_heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/util"

"github.com/microcosm-cc/bluemonday"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
)
Expand All @@ -20,6 +21,7 @@ func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Headin
}
}
txt := v.Text(reader.Source()) //nolint:staticcheck // Text is deprecated
txt = bluemonday.StrictPolicy().SanitizeBytes(txt)
header := Header{
Text: util.UnsafeBytesToString(txt),
Level: v.Level,
Expand Down
Loading