mirror of https://github.com/go-gitea/gitea.git
Refactor markup render system (#32645)
This PR mainly removes some global variables, moves some code and renames some functions to make code clearer. This PR also removes a testing-only option ForceHardLineBreak during refactoring since the behavior is clear now.
This commit is contained in:
parent
87bb5ed0bc
commit
b6ce2d6dc9
|
@ -5,9 +5,9 @@ package markup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -133,18 +133,15 @@ func CustomLinkURLSchemes(schemes []string) {
|
||||||
common.GlobalVars().LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
|
common.GlobalVars().LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
|
||||||
}
|
}
|
||||||
|
|
||||||
type postProcessError struct {
|
|
||||||
context string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *postProcessError) Error() string {
|
|
||||||
return "PostProcess: " + p.context + ", " + p.err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
type processor func(ctx *RenderContext, node *html.Node)
|
type processor func(ctx *RenderContext, node *html.Node)
|
||||||
|
|
||||||
var defaultProcessors = []processor{
|
// PostProcessDefault does the final required transformations to the passed raw HTML
|
||||||
|
// data, and ensures its validity. Transformations include: replacing links and
|
||||||
|
// emails with HTML links, parsing shortlinks in the format of [[Link]], like
|
||||||
|
// MediaWiki, linking issues in the format #ID, and mentions in the format
|
||||||
|
// @user, and others.
|
||||||
|
func PostProcessDefault(ctx *RenderContext, input io.Reader, output io.Writer) error {
|
||||||
|
procs := []processor{
|
||||||
fullIssuePatternProcessor,
|
fullIssuePatternProcessor,
|
||||||
comparePatternProcessor,
|
comparePatternProcessor,
|
||||||
codePreviewPatternProcessor,
|
codePreviewPatternProcessor,
|
||||||
|
@ -158,18 +155,14 @@ var defaultProcessors = []processor{
|
||||||
emailAddressProcessor,
|
emailAddressProcessor,
|
||||||
emojiProcessor,
|
emojiProcessor,
|
||||||
emojiShortCodeProcessor,
|
emojiShortCodeProcessor,
|
||||||
|
}
|
||||||
|
return postProcess(ctx, procs, input, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostProcess does the final required transformations to the passed raw HTML
|
// RenderCommitMessage will use the same logic as PostProcess, but will disable
|
||||||
// data, and ensures its validity. Transformations include: replacing links and
|
// the shortLinkProcessor.
|
||||||
// emails with HTML links, parsing shortlinks in the format of [[Link]], like
|
func RenderCommitMessage(ctx *RenderContext, content string) (string, error) {
|
||||||
// MediaWiki, linking issues in the format #ID, and mentions in the format
|
procs := []processor{
|
||||||
// @user, and others.
|
|
||||||
func PostProcess(ctx *RenderContext, input io.Reader, output io.Writer) error {
|
|
||||||
return postProcess(ctx, defaultProcessors, input, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
var commitMessageProcessors = []processor{
|
|
||||||
fullIssuePatternProcessor,
|
fullIssuePatternProcessor,
|
||||||
comparePatternProcessor,
|
comparePatternProcessor,
|
||||||
fullHashPatternProcessor,
|
fullHashPatternProcessor,
|
||||||
|
@ -181,27 +174,8 @@ var commitMessageProcessors = []processor{
|
||||||
emailAddressProcessor,
|
emailAddressProcessor,
|
||||||
emojiProcessor,
|
emojiProcessor,
|
||||||
emojiShortCodeProcessor,
|
emojiShortCodeProcessor,
|
||||||
}
|
}
|
||||||
|
return postProcessString(ctx, procs, content)
|
||||||
// RenderCommitMessage will use the same logic as PostProcess, but will disable
|
|
||||||
// the shortLinkProcessor and will add a defaultLinkProcessor if defaultLink is
|
|
||||||
// set, which changes every text node into a link to the passed default link.
|
|
||||||
func RenderCommitMessage(ctx *RenderContext, content string) (string, error) {
|
|
||||||
procs := commitMessageProcessors
|
|
||||||
return renderProcessString(ctx, procs, content)
|
|
||||||
}
|
|
||||||
|
|
||||||
var commitMessageSubjectProcessors = []processor{
|
|
||||||
fullIssuePatternProcessor,
|
|
||||||
comparePatternProcessor,
|
|
||||||
fullHashPatternProcessor,
|
|
||||||
linkProcessor,
|
|
||||||
mentionProcessor,
|
|
||||||
issueIndexPatternProcessor,
|
|
||||||
commitCrossReferencePatternProcessor,
|
|
||||||
hashCurrentPatternProcessor,
|
|
||||||
emojiShortCodeProcessor,
|
|
||||||
emojiProcessor,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var emojiProcessors = []processor{
|
var emojiProcessors = []processor{
|
||||||
|
@ -214,7 +188,18 @@ var emojiProcessors = []processor{
|
||||||
// emailAddressProcessor, will add a defaultLinkProcessor if defaultLink is set,
|
// emailAddressProcessor, will add a defaultLinkProcessor if defaultLink is set,
|
||||||
// which changes every text node into a link to the passed default link.
|
// which changes every text node into a link to the passed default link.
|
||||||
func RenderCommitMessageSubject(ctx *RenderContext, defaultLink, content string) (string, error) {
|
func RenderCommitMessageSubject(ctx *RenderContext, defaultLink, content string) (string, error) {
|
||||||
procs := slices.Clone(commitMessageSubjectProcessors)
|
procs := []processor{
|
||||||
|
fullIssuePatternProcessor,
|
||||||
|
comparePatternProcessor,
|
||||||
|
fullHashPatternProcessor,
|
||||||
|
linkProcessor,
|
||||||
|
mentionProcessor,
|
||||||
|
issueIndexPatternProcessor,
|
||||||
|
commitCrossReferencePatternProcessor,
|
||||||
|
hashCurrentPatternProcessor,
|
||||||
|
emojiShortCodeProcessor,
|
||||||
|
emojiProcessor,
|
||||||
|
}
|
||||||
procs = append(procs, func(ctx *RenderContext, node *html.Node) {
|
procs = append(procs, func(ctx *RenderContext, node *html.Node) {
|
||||||
ch := &html.Node{Parent: node, Type: html.TextNode, Data: node.Data}
|
ch := &html.Node{Parent: node, Type: html.TextNode, Data: node.Data}
|
||||||
node.Type = html.ElementNode
|
node.Type = html.ElementNode
|
||||||
|
@ -223,19 +208,19 @@ func RenderCommitMessageSubject(ctx *RenderContext, defaultLink, content string)
|
||||||
node.Attr = []html.Attribute{{Key: "href", Val: defaultLink}, {Key: "class", Val: "muted"}}
|
node.Attr = []html.Attribute{{Key: "href", Val: defaultLink}, {Key: "class", Val: "muted"}}
|
||||||
node.FirstChild, node.LastChild = ch, ch
|
node.FirstChild, node.LastChild = ch, ch
|
||||||
})
|
})
|
||||||
return renderProcessString(ctx, procs, content)
|
return postProcessString(ctx, procs, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderIssueTitle to process title on individual issue/pull page
|
// RenderIssueTitle to process title on individual issue/pull page
|
||||||
func RenderIssueTitle(ctx *RenderContext, title string) (string, error) {
|
func RenderIssueTitle(ctx *RenderContext, title string) (string, error) {
|
||||||
// do not render other issue/commit links in an issue's title - which in most cases is already a link.
|
// do not render other issue/commit links in an issue's title - which in most cases is already a link.
|
||||||
return renderProcessString(ctx, []processor{
|
return postProcessString(ctx, []processor{
|
||||||
emojiShortCodeProcessor,
|
emojiShortCodeProcessor,
|
||||||
emojiProcessor,
|
emojiProcessor,
|
||||||
}, title)
|
}, title)
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderProcessString(ctx *RenderContext, procs []processor, content string) (string, error) {
|
func postProcessString(ctx *RenderContext, procs []processor, content string) (string, error) {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
if err := postProcess(ctx, procs, strings.NewReader(content), &buf); err != nil {
|
if err := postProcess(ctx, procs, strings.NewReader(content), &buf); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -246,7 +231,7 @@ func renderProcessString(ctx *RenderContext, procs []processor, content string)
|
||||||
// RenderDescriptionHTML will use similar logic as PostProcess, but will
|
// RenderDescriptionHTML will use similar logic as PostProcess, but will
|
||||||
// use a single special linkProcessor.
|
// use a single special linkProcessor.
|
||||||
func RenderDescriptionHTML(ctx *RenderContext, content string) (string, error) {
|
func RenderDescriptionHTML(ctx *RenderContext, content string) (string, error) {
|
||||||
return renderProcessString(ctx, []processor{
|
return postProcessString(ctx, []processor{
|
||||||
descriptionLinkProcessor,
|
descriptionLinkProcessor,
|
||||||
emojiShortCodeProcessor,
|
emojiShortCodeProcessor,
|
||||||
emojiProcessor,
|
emojiProcessor,
|
||||||
|
@ -256,7 +241,7 @@ func RenderDescriptionHTML(ctx *RenderContext, content string) (string, error) {
|
||||||
// RenderEmoji for when we want to just process emoji and shortcodes
|
// RenderEmoji for when we want to just process emoji and shortcodes
|
||||||
// in various places it isn't already run through the normal markdown processor
|
// in various places it isn't already run through the normal markdown processor
|
||||||
func RenderEmoji(ctx *RenderContext, content string) (string, error) {
|
func RenderEmoji(ctx *RenderContext, content string) (string, error) {
|
||||||
return renderProcessString(ctx, emojiProcessors, content)
|
return postProcessString(ctx, emojiProcessors, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output io.Writer) error {
|
func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output io.Writer) error {
|
||||||
|
@ -276,7 +261,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
|
||||||
strings.NewReader("</body></html>"),
|
strings.NewReader("</body></html>"),
|
||||||
))
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &postProcessError{"invalid HTML", err}
|
return fmt.Errorf("markup.postProcess: invalid HTML: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if node.Type == html.DocumentNode {
|
if node.Type == html.DocumentNode {
|
||||||
|
@ -308,7 +293,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
|
||||||
// Render everything to buf.
|
// Render everything to buf.
|
||||||
for _, node := range newNodes {
|
for _, node := range newNodes {
|
||||||
if err := html.Render(output, node); err != nil {
|
if err := html.Render(output, node); err != nil {
|
||||||
return &postProcessError{"error rendering processed HTML", err}
|
return fmt.Errorf("markup.postProcess: html.Render: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -277,12 +277,12 @@ func TestRender_AutoLink(t *testing.T) {
|
||||||
|
|
||||||
test := func(input, expected string) {
|
test := func(input, expected string) {
|
||||||
var buffer strings.Builder
|
var buffer strings.Builder
|
||||||
err := PostProcess(NewTestRenderContext(localMetas), strings.NewReader(input), &buffer)
|
err := PostProcessDefault(NewTestRenderContext(localMetas), strings.NewReader(input), &buffer)
|
||||||
assert.Equal(t, err, nil)
|
assert.Equal(t, err, nil)
|
||||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
|
||||||
|
|
||||||
buffer.Reset()
|
buffer.Reset()
|
||||||
err = PostProcess(NewTestRenderContext(localMetas), strings.NewReader(input), &buffer)
|
err = PostProcessDefault(NewTestRenderContext(localMetas), strings.NewReader(input), &buffer)
|
||||||
assert.Equal(t, err, nil)
|
assert.Equal(t, err, nil)
|
||||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -445,14 +445,14 @@ func Test_ParseClusterFuzz(t *testing.T) {
|
||||||
data := "<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
|
data := "<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
|
||||||
|
|
||||||
var res strings.Builder
|
var res strings.Builder
|
||||||
err := markup.PostProcess(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotContains(t, res.String(), "<html")
|
assert.NotContains(t, res.String(), "<html")
|
||||||
|
|
||||||
data = "<!DOCTYPE html>\n<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
|
data = "<!DOCTYPE html>\n<A><maTH><tr><MN><bodY ÿ><temPlate></template><tH><tr></A><tH><d<bodY "
|
||||||
|
|
||||||
res.Reset()
|
res.Reset()
|
||||||
err = markup.PostProcess(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
err = markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotContains(t, res.String(), "<html")
|
assert.NotContains(t, res.String(), "<html")
|
||||||
|
@ -464,7 +464,7 @@ func TestPostProcess_RenderDocument(t *testing.T) {
|
||||||
|
|
||||||
test := func(input, expected string) {
|
test := func(input, expected string) {
|
||||||
var res strings.Builder
|
var res strings.Builder
|
||||||
err := markup.PostProcess(markup.NewTestRenderContext(markup.TestAppURL, map[string]string{"user": "go-gitea", "repo": "gitea"}), strings.NewReader(input), &res)
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(markup.TestAppURL, map[string]string{"user": "go-gitea", "repo": "gitea"}), strings.NewReader(input), &res)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
|
||||||
}
|
}
|
||||||
|
@ -501,7 +501,7 @@ func TestIssue16020(t *testing.T) {
|
||||||
data := `<img src="data:image/png;base64,i//V"/>`
|
data := `<img src="data:image/png;base64,i//V"/>`
|
||||||
|
|
||||||
var res strings.Builder
|
var res strings.Builder
|
||||||
err := markup.PostProcess(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, data, res.String())
|
assert.Equal(t, data, res.String())
|
||||||
}
|
}
|
||||||
|
@ -514,7 +514,7 @@ func BenchmarkEmojiPostprocess(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
var res strings.Builder
|
var res strings.Builder
|
||||||
err := markup.PostProcess(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
||||||
assert.NoError(b, err)
|
assert.NoError(b, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -522,7 +522,7 @@ func BenchmarkEmojiPostprocess(b *testing.B) {
|
||||||
func TestFuzz(t *testing.T) {
|
func TestFuzz(t *testing.T) {
|
||||||
s := "t/l/issues/8#/../../a"
|
s := "t/l/issues/8#/../../a"
|
||||||
renderContext := markup.NewTestRenderContext()
|
renderContext := markup.NewTestRenderContext()
|
||||||
err := markup.PostProcess(renderContext, strings.NewReader(s), io.Discard)
|
err := markup.PostProcessDefault(renderContext, strings.NewReader(s), io.Discard)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -530,7 +530,7 @@ func TestIssue18471(t *testing.T) {
|
||||||
data := `http://domain/org/repo/compare/783b039...da951ce`
|
data := `http://domain/org/repo/compare/783b039...da951ce`
|
||||||
|
|
||||||
var res strings.Builder
|
var res strings.Builder
|
||||||
err := markup.PostProcess(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
err := markup.PostProcessDefault(markup.NewTestRenderContext(localMetas), strings.NewReader(data), &res)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, `<a href="http://domain/org/repo/compare/783b039...da951ce" class="compare"><code class="nohighlight">783b039...da951ce</code></a>`, res.String())
|
assert.Equal(t, `<a href="http://domain/org/repo/compare/783b039...da951ce" class="compare"><code class="nohighlight">783b039...da951ce</code></a>`, res.String())
|
||||||
|
|
|
@ -80,9 +80,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
|
||||||
// many places render non-comment contents with no mode=document, then these contents also use comment's hard line break setting
|
// many places render non-comment contents with no mode=document, then these contents also use comment's hard line break setting
|
||||||
// especially in many tests.
|
// especially in many tests.
|
||||||
markdownLineBreakStyle := ctx.RenderOptions.Metas["markdownLineBreakStyle"]
|
markdownLineBreakStyle := ctx.RenderOptions.Metas["markdownLineBreakStyle"]
|
||||||
if markup.RenderBehaviorForTesting.ForceHardLineBreak {
|
if markdownLineBreakStyle == "comment" {
|
||||||
v.SetHardLineBreak(true)
|
|
||||||
} else if markdownLineBreakStyle == "comment" {
|
|
||||||
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
|
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
|
||||||
} else if markdownLineBreakStyle == "document" {
|
} else if markdownLineBreakStyle == "document" {
|
||||||
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)
|
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)
|
||||||
|
|
|
@ -85,92 +85,11 @@ func TestRender_Images(t *testing.T) {
|
||||||
`<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
|
`<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAnswers(baseURL string) []string {
|
func TestTotal_RenderString(t *testing.T) {
|
||||||
return []string{
|
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
||||||
`<p>Wiki! Enjoy :)</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="` + baseURL + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
|
|
||||||
<li><a href="` + baseURL + `/Tips" rel="nofollow">Tips</a></li>
|
|
||||||
</ul>
|
|
||||||
<p>See commit <a href="/` + testRepoOwnerName + `/` + testRepoName + `/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
|
|
||||||
<p>Ideas and codes</p>
|
|
||||||
<ul>
|
|
||||||
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li>
|
|
||||||
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="` + FullURL + `issues/786" class="ref-issue" rel="nofollow">#786</a></li>
|
|
||||||
<li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
|
|
||||||
<li><a href="` + baseURL + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
|
|
||||||
<li><a href="` + baseURL + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
|
|
||||||
</ul>
|
|
||||||
`,
|
|
||||||
`<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
|
|
||||||
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
|
|
||||||
<h2 id="user-content-quick-links">Quick Links</h2>
|
|
||||||
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th><a href="` + baseURL + `/images/icon-install.png" rel="nofollow"><img src="` + baseURL + `/images/icon-install.png" title="icon-install.png" alt="images/icon-install.png"/></a></th>
|
|
||||||
<th><a href="` + baseURL + `/Installation" rel="nofollow">Installation</a></th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<tr>
|
|
||||||
<td><a href="` + baseURL + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURL + `/images/icon-usage.png" title="icon-usage.png" alt="images/icon-usage.png"/></a></td>
|
|
||||||
<td><a href="` + baseURL + `/Usage" rel="nofollow">Usage</a></td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
`,
|
|
||||||
`<p><a href="http://www.excelsiorjet.com/" rel="nofollow">Excelsior JET</a> allows you to create native executables for Windows, Linux and Mac OS X.</p>
|
|
||||||
<ol>
|
|
||||||
<li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a><br/>
|
|
||||||
<a href="` + baseURL + `/images/1.png" rel="nofollow"><img src="` + baseURL + `/images/1.png" title="1.png" alt="images/1.png"/></a></li>
|
|
||||||
<li>Perform a test run by hitting the Run! button.<br/>
|
|
||||||
<a href="` + baseURL + `/images/2.png" rel="nofollow"><img src="` + baseURL + `/images/2.png" title="2.png" alt="images/2.png"/></a></li>
|
|
||||||
</ol>
|
|
||||||
<h2 id="user-content-custom-id">More tests</h2>
|
|
||||||
<p>(from <a href="https://www.markdownguide.org/extended-syntax/" rel="nofollow">https://www.markdownguide.org/extended-syntax/</a>)</p>
|
|
||||||
<h3 id="user-content-checkboxes">Checkboxes</h3>
|
|
||||||
<ul>
|
|
||||||
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="434"/>unchecked</li>
|
|
||||||
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="450" checked=""/>checked</li>
|
|
||||||
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="464"/>still unchecked</li>
|
|
||||||
</ul>
|
|
||||||
<h3 id="user-content-definition-list">Definition list</h3>
|
|
||||||
<dl>
|
|
||||||
<dt>First Term</dt>
|
|
||||||
<dd>This is the definition of the first term.</dd>
|
|
||||||
<dt>Second Term</dt>
|
|
||||||
<dd>This is one definition of the second term.</dd>
|
|
||||||
<dd>This is another definition of the second term.</dd>
|
|
||||||
</dl>
|
|
||||||
<h3 id="user-content-footnotes">Footnotes</h3>
|
|
||||||
<p>Here is a simple footnote,<sup id="fnref:user-content-1"><a href="#fn:user-content-1" rel="nofollow">1</a></sup> and here is a longer one.<sup id="fnref:user-content-bignote"><a href="#fn:user-content-bignote" rel="nofollow">2</a></sup></p>
|
|
||||||
<div>
|
|
||||||
<hr/>
|
|
||||||
<ol>
|
|
||||||
<li id="fn:user-content-1">
|
|
||||||
<p>This is the first footnote. <a href="#fnref:user-content-1" rel="nofollow">↩︎</a></p>
|
|
||||||
</li>
|
|
||||||
<li id="fn:user-content-bignote">
|
|
||||||
<p>Here is one with multiple paragraphs and code.</p>
|
|
||||||
<p>Indent paragraphs to include them in the footnote.</p>
|
|
||||||
<p><code>{ my code }</code></p>
|
|
||||||
<p>Add as many paragraphs as you like. <a href="#fnref:user-content-bignote" rel="nofollow">↩︎</a></p>
|
|
||||||
</li>
|
|
||||||
</ol>
|
|
||||||
</div>
|
|
||||||
`, `<ul>
|
|
||||||
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="3"/> If you want to rebase/retry this PR, click this checkbox.</li>
|
|
||||||
</ul>
|
|
||||||
<hr/>
|
|
||||||
<p>This PR has been generated by <a href="https://github.com/renovatebot/renovate" rel="nofollow">Renovate Bot</a>.</p>
|
|
||||||
`,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test cases without ambiguous links
|
// Test cases without ambiguous links (It is not right to copy a whole file here, instead it should clearly test what is being tested)
|
||||||
var sameCases = []string{
|
sameCases := []string{
|
||||||
// dear imgui wiki markdown extract: special wiki syntax
|
// dear imgui wiki markdown extract: special wiki syntax
|
||||||
`Wiki! Enjoy :)
|
`Wiki! Enjoy :)
|
||||||
- [[Links, Language bindings, Engine bindings|Links]]
|
- [[Links, Language bindings, Engine bindings|Links]]
|
||||||
|
@ -245,21 +164,101 @@ Here is a simple footnote,[^1] and here is a longer one.[^bignote]
|
||||||
This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
|
This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
|
||||||
|
|
||||||
<!-- test-comment -->`,
|
<!-- test-comment -->`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
baseURL := ""
|
||||||
|
testAnswers := []string{
|
||||||
|
`<p>Wiki! Enjoy :)</p>
|
||||||
|
<ul>
|
||||||
|
<li><a href="` + baseURL + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
|
||||||
|
<li><a href="` + baseURL + `/Tips" rel="nofollow">Tips</a></li>
|
||||||
|
</ul>
|
||||||
|
<p>See commit <a href="/` + testRepoOwnerName + `/` + testRepoName + `/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
|
||||||
|
<p>Ideas and codes</p>
|
||||||
|
<ul>
|
||||||
|
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li>
|
||||||
|
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="` + FullURL + `issues/786" class="ref-issue" rel="nofollow">#786</a></li>
|
||||||
|
<li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
|
||||||
|
<li><a href="` + baseURL + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
|
||||||
|
<li><a href="` + baseURL + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
|
||||||
|
</ul>
|
||||||
|
`,
|
||||||
|
`<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
|
||||||
|
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
|
||||||
|
<h2 id="user-content-quick-links">Quick Links</h2>
|
||||||
|
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><a href="` + baseURL + `/images/icon-install.png" rel="nofollow"><img src="` + baseURL + `/images/icon-install.png" title="icon-install.png" alt="images/icon-install.png"/></a></th>
|
||||||
|
<th><a href="` + baseURL + `/Installation" rel="nofollow">Installation</a></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td><a href="` + baseURL + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURL + `/images/icon-usage.png" title="icon-usage.png" alt="images/icon-usage.png"/></a></td>
|
||||||
|
<td><a href="` + baseURL + `/Usage" rel="nofollow">Usage</a></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
`,
|
||||||
|
`<p><a href="http://www.excelsiorjet.com/" rel="nofollow">Excelsior JET</a> allows you to create native executables for Windows, Linux and Mac OS X.</p>
|
||||||
|
<ol>
|
||||||
|
<li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
|
||||||
|
<a href="` + baseURL + `/images/1.png" rel="nofollow"><img src="` + baseURL + `/images/1.png" title="1.png" alt="images/1.png"/></a></li>
|
||||||
|
<li>Perform a test run by hitting the Run! button.
|
||||||
|
<a href="` + baseURL + `/images/2.png" rel="nofollow"><img src="` + baseURL + `/images/2.png" title="2.png" alt="images/2.png"/></a></li>
|
||||||
|
</ol>
|
||||||
|
<h2 id="user-content-custom-id">More tests</h2>
|
||||||
|
<p>(from <a href="https://www.markdownguide.org/extended-syntax/" rel="nofollow">https://www.markdownguide.org/extended-syntax/</a>)</p>
|
||||||
|
<h3 id="user-content-checkboxes">Checkboxes</h3>
|
||||||
|
<ul>
|
||||||
|
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="434"/>unchecked</li>
|
||||||
|
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="450" checked=""/>checked</li>
|
||||||
|
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="464"/>still unchecked</li>
|
||||||
|
</ul>
|
||||||
|
<h3 id="user-content-definition-list">Definition list</h3>
|
||||||
|
<dl>
|
||||||
|
<dt>First Term</dt>
|
||||||
|
<dd>This is the definition of the first term.</dd>
|
||||||
|
<dt>Second Term</dt>
|
||||||
|
<dd>This is one definition of the second term.</dd>
|
||||||
|
<dd>This is another definition of the second term.</dd>
|
||||||
|
</dl>
|
||||||
|
<h3 id="user-content-footnotes">Footnotes</h3>
|
||||||
|
<p>Here is a simple footnote,<sup id="fnref:user-content-1"><a href="#fn:user-content-1" rel="nofollow">1</a></sup> and here is a longer one.<sup id="fnref:user-content-bignote"><a href="#fn:user-content-bignote" rel="nofollow">2</a></sup></p>
|
||||||
|
<div>
|
||||||
|
<hr/>
|
||||||
|
<ol>
|
||||||
|
<li id="fn:user-content-1">
|
||||||
|
<p>This is the first footnote. <a href="#fnref:user-content-1" rel="nofollow">↩︎</a></p>
|
||||||
|
</li>
|
||||||
|
<li id="fn:user-content-bignote">
|
||||||
|
<p>Here is one with multiple paragraphs and code.</p>
|
||||||
|
<p>Indent paragraphs to include them in the footnote.</p>
|
||||||
|
<p><code>{ my code }</code></p>
|
||||||
|
<p>Add as many paragraphs as you like. <a href="#fnref:user-content-bignote" rel="nofollow">↩︎</a></p>
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
</div>
|
||||||
|
`,
|
||||||
|
`<ul>
|
||||||
|
<li class="task-list-item"><input type="checkbox" disabled="" data-source-position="3"/> If you want to rebase/retry this PR, click this checkbox.</li>
|
||||||
|
</ul>
|
||||||
|
<hr/>
|
||||||
|
<p>This PR has been generated by <a href="https://github.com/renovatebot/renovate" rel="nofollow">Renovate Bot</a>.</p>
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
|
||||||
func TestTotal_RenderString(t *testing.T) {
|
|
||||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.ForceHardLineBreak, true)()
|
|
||||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
|
||||||
markup.Init(&markup.RenderHelperFuncs{
|
markup.Init(&markup.RenderHelperFuncs{
|
||||||
IsUsernameMentionable: func(ctx context.Context, username string) bool {
|
IsUsernameMentionable: func(ctx context.Context, username string) bool {
|
||||||
return username == "r-lyeh"
|
return username == "r-lyeh"
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
answers := testAnswers("")
|
|
||||||
for i := 0; i < len(sameCases); i++ {
|
for i := 0; i < len(sameCases); i++ {
|
||||||
line, err := markdown.RenderString(markup.NewTestRenderContext(localMetas), sameCases[i])
|
line, err := markdown.RenderString(markup.NewTestRenderContext(localMetas), sameCases[i])
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, answers[i], string(line))
|
assert.Equal(t, testAnswers[i], string(line))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,10 +311,9 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) {
|
||||||
testcase := `![image1](/image1)
|
testcase := `![image1](/image1)
|
||||||
![image2](/image2)
|
![image2](/image2)
|
||||||
`
|
`
|
||||||
expected := `<p><a href="/image1" target="_blank" rel="nofollow noopener"><img src="/image1" alt="image1"></a><br>
|
expected := `<p><a href="/image1" target="_blank" rel="nofollow noopener"><img src="/image1" alt="image1"></a>
|
||||||
<a href="/image2" target="_blank" rel="nofollow noopener"><img src="/image2" alt="image2"></a></p>
|
<a href="/image2" target="_blank" rel="nofollow noopener"><img src="/image2" alt="image2"></a></p>
|
||||||
`
|
`
|
||||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.ForceHardLineBreak, true)()
|
|
||||||
res, err := markdown.RenderRawString(markup.NewTestRenderContext(), testcase)
|
res, err := markdown.RenderRawString(markup.NewTestRenderContext(), testcase)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, expected, res)
|
assert.Equal(t, expected, res)
|
||||||
|
@ -525,43 +523,33 @@ mail@domain.com
|
||||||
space${SPACE}${SPACE}
|
space${SPACE}${SPACE}
|
||||||
`
|
`
|
||||||
input = strings.ReplaceAll(input, "${SPACE}", " ") // replace ${SPACE} with " ", to avoid some editor's auto-trimming
|
input = strings.ReplaceAll(input, "${SPACE}", " ") // replace ${SPACE} with " ", to avoid some editor's auto-trimming
|
||||||
cases := []struct {
|
expected := `<p>space @mention-user<br/>
|
||||||
Expected string
|
/just/a/path.bin
|
||||||
}{
|
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a>
|
||||||
{
|
<a href="/file.bin" rel="nofollow">local link</a>
|
||||||
Expected: `<p>space @mention-user<br/>
|
<a href="https://example.com" rel="nofollow">remote link</a>
|
||||||
/just/a/path.bin<br/>
|
<a href="/file.bin" rel="nofollow">local link</a>
|
||||||
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
|
<a href="https://example.com" rel="nofollow">remote link</a>
|
||||||
<a href="/file.bin" rel="nofollow">local link</a><br/>
|
<a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a>
|
||||||
<a href="https://example.com" rel="nofollow">remote link</a><br/>
|
<a href="/path/file" target="_blank" rel="nofollow noopener"><img src="/path/file" alt="local image"/></a>
|
||||||
<a href="/file.bin" rel="nofollow">local link</a><br/>
|
<a href="/path/file" target="_blank" rel="nofollow noopener"><img src="/path/file" alt="local image"/></a>
|
||||||
<a href="https://example.com" rel="nofollow">remote link</a><br/>
|
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a>
|
||||||
<a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a><br/>
|
<a href="/image.jpg" rel="nofollow"><img src="/image.jpg" title="local image" alt="local image"/></a>
|
||||||
<a href="/path/file" target="_blank" rel="nofollow noopener"><img src="/path/file" alt="local image"/></a><br/>
|
<a href="https://example.com/image.jpg" rel="nofollow"><img src="https://example.com/image.jpg" title="remote link" alt="remote link"/></a>
|
||||||
<a href="/path/file" target="_blank" rel="nofollow noopener"><img src="/path/file" alt="local image"/></a><br/>
|
<a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" rel="nofollow"><code>88fc37a3c0...12fc37a3c0 (hash)</code></a>
|
||||||
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a><br/>
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
|
||||||
<a href="/image.jpg" rel="nofollow"><img src="/image.jpg" title="local image" alt="local image"/></a><br/>
|
<a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" rel="nofollow"><code>88fc37a3c0</code></a>
|
||||||
<a href="https://example.com/image.jpg" rel="nofollow"><img src="https://example.com/image.jpg" title="remote link" alt="remote link"/></a><br/>
|
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
|
||||||
<a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" rel="nofollow"><code>88fc37a3c0...12fc37a3c0 (hash)</code></a><br/>
|
<span class="emoji" aria-label="thumbs up">👍</span>
|
||||||
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare<br/>
|
<a href="mailto:mail@domain.com" rel="nofollow">mail@domain.com</a>
|
||||||
<a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" rel="nofollow"><code>88fc37a3c0</code></a><br/>
|
@mention-user test
|
||||||
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit<br/>
|
#123
|
||||||
<span class="emoji" aria-label="thumbs up">👍</span><br/>
|
|
||||||
<a href="mailto:mail@domain.com" rel="nofollow">mail@domain.com</a><br/>
|
|
||||||
@mention-user test<br/>
|
|
||||||
#123<br/>
|
|
||||||
space</p>
|
space</p>
|
||||||
`,
|
`
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.ForceHardLineBreak, true)()
|
|
||||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
|
||||||
for i, c := range cases {
|
|
||||||
result, err := markdown.RenderString(markup.NewTestRenderContext(localMetas), input)
|
result, err := markdown.RenderString(markup.NewTestRenderContext(localMetas), input)
|
||||||
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, c.Expected, string(result), "Unexpected result in testcase %v", i)
|
assert.Equal(t, expected, string(result))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAttention(t *testing.T) {
|
func TestAttention(t *testing.T) {
|
||||||
|
|
|
@ -28,14 +28,6 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var RenderBehaviorForTesting struct {
|
var RenderBehaviorForTesting struct {
|
||||||
// Markdown line break rendering has 2 default behaviors:
|
|
||||||
// * Use hard: replace "\n" with "<br>" for comments, setting.Markdown.EnableHardLineBreakInComments=true
|
|
||||||
// * Keep soft: "\n" for non-comments (a.k.a. documents), setting.Markdown.EnableHardLineBreakInDocuments=false
|
|
||||||
// In history, there was a mess:
|
|
||||||
// * The behavior was controlled by `Metas["mode"] != "document",
|
|
||||||
// * However, many places render the content without setting "mode" in Metas, all these places used comment line break setting incorrectly
|
|
||||||
ForceHardLineBreak bool
|
|
||||||
|
|
||||||
// Gitea will emit some additional attributes for various purposes, these attributes don't affect rendering.
|
// Gitea will emit some additional attributes for various purposes, these attributes don't affect rendering.
|
||||||
// But there are too many hard-coded test cases, to avoid changing all of them again and again, we can disable emitting these internal attributes.
|
// But there are too many hard-coded test cases, to avoid changing all of them again and again, we can disable emitting these internal attributes.
|
||||||
DisableAdditionalAttributes bool
|
DisableAdditionalAttributes bool
|
||||||
|
@ -218,7 +210,7 @@ func render(ctx *RenderContext, renderer Renderer, input io.Reader, output io.Wr
|
||||||
|
|
||||||
eg.Go(func() (err error) {
|
eg.Go(func() (err error) {
|
||||||
if r, ok := renderer.(PostProcessRenderer); ok && r.NeedPostProcess() {
|
if r, ok := renderer.(PostProcessRenderer); ok && r.NeedPostProcess() {
|
||||||
err = PostProcess(ctx, pr1, pw2)
|
err = PostProcessDefault(ctx, pr1, pw2)
|
||||||
} else {
|
} else {
|
||||||
_, err = io.Copy(pw2, pr1)
|
_, err = io.Copy(pw2, pr1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,6 @@ func FuzzMarkdownRenderRaw(f *testing.F) {
|
||||||
func FuzzMarkupPostProcess(f *testing.F) {
|
func FuzzMarkupPostProcess(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
setting.AppURL = "http://localhost:3000/"
|
setting.AppURL = "http://localhost:3000/"
|
||||||
markup.PostProcess(newFuzzRenderContext(), bytes.NewReader(data), io.Discard)
|
markup.PostProcessDefault(newFuzzRenderContext(), bytes.NewReader(data), io.Discard)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue