-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Change markdown rendering from blackfriday to goldmark #9533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package common | ||
|
||
import ( | ||
"mvdan.cc/xurls/v2" | ||
) | ||
|
||
var ( | ||
// NOTE: All below regex matching do not perform any extra validation. | ||
// Thus a link is produced even if the linked entity does not exist. | ||
// While fast, this is also incorrect and lead to false positives. | ||
// TODO: fix invalid linking issue | ||
|
||
// LinkRegex is a regexp matching a valid link | ||
LinkRegex, _ = xurls.StrictMatchingScheme("https?://") | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Copyright 2019 Yusuke Inuzuka | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Most of this file is a subtly changed version of github.com/yuin/goldmark/extension/linkify.go | ||
|
||
package common | ||
|
||
import ( | ||
"bytes" | ||
"regexp" | ||
|
||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
var wwwURLRegxp = regexp.MustCompile(`^www\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}((?:/|[#?])[-a-zA-Z0-9@:%_\+.~#!?&//=\(\);,'">\^{}\[\]` + "`" + `]*)?`) | ||
|
||
type linkifyParser struct { | ||
} | ||
|
||
var defaultLinkifyParser = &linkifyParser{} | ||
|
||
// NewLinkifyParser return a new InlineParser can parse | ||
// text that seems like a URL. | ||
func NewLinkifyParser() parser.InlineParser { | ||
return defaultLinkifyParser | ||
} | ||
|
||
func (s *linkifyParser) Trigger() []byte { | ||
// ' ' indicates any white spaces and a line head | ||
return []byte{' ', '*', '_', '~', '('} | ||
} | ||
|
||
var protoHTTP = []byte("http:") | ||
var protoHTTPS = []byte("https:") | ||
var protoFTP = []byte("ftp:") | ||
var domainWWW = []byte("www.") | ||
|
||
func (s *linkifyParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node { | ||
if pc.IsInLinkLabel() { | ||
return nil | ||
} | ||
line, segment := block.PeekLine() | ||
consumes := 0 | ||
start := segment.Start | ||
c := line[0] | ||
// advance if current position is not a line head. | ||
if c == ' ' || c == '*' || c == '_' || c == '~' || c == '(' { | ||
consumes++ | ||
start++ | ||
line = line[1:] | ||
} | ||
|
||
var m []int | ||
var protocol []byte | ||
var typ ast.AutoLinkType = ast.AutoLinkURL | ||
if bytes.HasPrefix(line, protoHTTP) || bytes.HasPrefix(line, protoHTTPS) || bytes.HasPrefix(line, protoFTP) { | ||
m = LinkRegex.FindSubmatchIndex(line) | ||
} | ||
if m == nil && bytes.HasPrefix(line, domainWWW) { | ||
m = wwwURLRegxp.FindSubmatchIndex(line) | ||
protocol = []byte("http") | ||
} | ||
if m != nil { | ||
lastChar := line[m[1]-1] | ||
if lastChar == '.' { | ||
m[1]-- | ||
} else if lastChar == ')' { | ||
closing := 0 | ||
for i := m[1] - 1; i >= m[0]; i-- { | ||
if line[i] == ')' { | ||
closing++ | ||
} else if line[i] == '(' { | ||
closing-- | ||
} | ||
} | ||
if closing > 0 { | ||
m[1] -= closing | ||
} | ||
} else if lastChar == ';' { | ||
i := m[1] - 2 | ||
for ; i >= m[0]; i-- { | ||
if util.IsAlphaNumeric(line[i]) { | ||
continue | ||
} | ||
break | ||
} | ||
if i != m[1]-2 { | ||
if line[i] == '&' { | ||
m[1] -= m[1] - i | ||
} | ||
} | ||
} | ||
} | ||
if m == nil { | ||
if len(line) > 0 && util.IsPunct(line[0]) { | ||
return nil | ||
} | ||
typ = ast.AutoLinkEmail | ||
stop := util.FindEmailIndex(line) | ||
if stop < 0 { | ||
return nil | ||
} | ||
at := bytes.IndexByte(line, '@') | ||
m = []int{0, stop, at, stop - 1} | ||
if m == nil || bytes.IndexByte(line[m[2]:m[3]], '.') < 0 { | ||
return nil | ||
} | ||
lastChar := line[m[1]-1] | ||
if lastChar == '.' { | ||
m[1]-- | ||
} | ||
if m[1] < len(line) { | ||
nextChar := line[m[1]] | ||
if nextChar == '-' || nextChar == '_' { | ||
return nil | ||
} | ||
} | ||
} | ||
if m == nil { | ||
return nil | ||
} | ||
if consumes != 0 { | ||
s := segment.WithStop(segment.Start + 1) | ||
ast.MergeOrAppendTextSegment(parent, s) | ||
} | ||
consumes += m[1] | ||
block.Advance(consumes) | ||
n := ast.NewTextSegment(text.NewSegment(start, start+m[1])) | ||
link := ast.NewAutoLink(typ, n) | ||
link.Protocol = protocol | ||
return link | ||
} | ||
|
||
func (s *linkifyParser) CloseBlock(parent ast.Node, pc parser.Context) { | ||
// nothing to do | ||
} | ||
|
||
type linkify struct { | ||
} | ||
|
||
// Linkify is an extension that allow you to parse text that seems like a URL. | ||
var Linkify = &linkify{} | ||
|
||
func (e *linkify) Extend(m goldmark.Markdown) { | ||
m.Parser().AddOptions( | ||
parser.WithInlineParsers( | ||
util.Prioritized(NewLinkifyParser(), 999), | ||
), | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package markdown | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/markup" | ||
"code.gitea.io/gitea/modules/markup/common" | ||
giteautil "code.gitea.io/gitea/modules/util" | ||
|
||
"github.com/yuin/goldmark/ast" | ||
east "github.com/yuin/goldmark/extension/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/renderer" | ||
"github.com/yuin/goldmark/renderer/html" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
var byteMailto = []byte("mailto:") | ||
|
||
// GiteaASTTransformer is a default transformer of the goldmark tree. | ||
type GiteaASTTransformer struct{} | ||
|
||
// Transform transforms the given AST tree. | ||
func (g *GiteaASTTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { | ||
_ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) { | ||
if !entering { | ||
return ast.WalkContinue, nil | ||
} | ||
|
||
switch v := n.(type) { | ||
case *ast.Image: | ||
// Images need two things: | ||
// | ||
// 1. Their src needs to munged to be a real value | ||
// 2. If they're not wrapped with a link they need a link wrapper | ||
|
||
// Check if the destination is a real link | ||
link := v.Destination | ||
if len(link) > 0 && !markup.IsLink(link) { | ||
prefix := pc.Get(urlPrefixKey).(string) | ||
if pc.Get(isWikiKey).(bool) { | ||
prefix = giteautil.URLJoin(prefix, "wiki", "raw") | ||
} | ||
prefix = strings.Replace(prefix, "/src/", "/media/", 1) | ||
|
||
lnk := string(link) | ||
lnk = giteautil.URLJoin(prefix, lnk) | ||
lnk = strings.Replace(lnk, " ", "+", -1) | ||
link = []byte(lnk) | ||
} | ||
v.Destination = link | ||
|
||
parent := n.Parent() | ||
// Create a link around image only if parent is not already a link | ||
if _, ok := parent.(*ast.Link); !ok && parent != nil { | ||
wrap := ast.NewLink() | ||
wrap.Destination = link | ||
wrap.Title = v.Title | ||
parent.ReplaceChild(parent, n, wrap) | ||
wrap.AppendChild(wrap, n) | ||
} | ||
case *ast.Link: | ||
// Links need their href to munged to be a real value | ||
link := v.Destination | ||
if len(link) > 0 && !markup.IsLink(link) && | ||
link[0] != '#' && !bytes.HasPrefix(link, byteMailto) { | ||
// special case: this is not a link, a hash link or a mailto:, so it's a | ||
// relative URL | ||
lnk := string(link) | ||
if pc.Get(isWikiKey).(bool) { | ||
lnk = giteautil.URLJoin("wiki", lnk) | ||
} | ||
link = []byte(giteautil.URLJoin(pc.Get(urlPrefixKey).(string), lnk)) | ||
} | ||
v.Destination = link | ||
} | ||
return ast.WalkContinue, nil | ||
}) | ||
} | ||
|
||
type prefixedIDs struct { | ||
values map[string]bool | ||
} | ||
|
||
// Generate generates a new element id. | ||
func (p *prefixedIDs) Generate(value []byte, kind ast.NodeKind) []byte { | ||
dft := []byte("id") | ||
if kind == ast.KindHeading { | ||
dft = []byte("heading") | ||
} | ||
return p.GenerateWithDefault(value, dft) | ||
} | ||
|
||
// Generate generates a new element id. | ||
func (p *prefixedIDs) GenerateWithDefault(value []byte, dft []byte) []byte { | ||
result := common.CleanValue(value) | ||
if len(result) == 0 { | ||
result = dft | ||
} | ||
if !bytes.HasPrefix(result, []byte("user-content-")) { | ||
result = append([]byte("user-content-"), result...) | ||
} | ||
if _, ok := p.values[util.BytesToReadOnlyString(result)]; !ok { | ||
p.values[util.BytesToReadOnlyString(result)] = true | ||
return result | ||
} | ||
for i := 1; ; i++ { | ||
newResult := fmt.Sprintf("%s-%d", result, i) | ||
if _, ok := p.values[newResult]; !ok { | ||
p.values[newResult] = true | ||
return []byte(newResult) | ||
} | ||
} | ||
} | ||
|
||
// Put puts a given element id to the used ids table. | ||
func (p *prefixedIDs) Put(value []byte) { | ||
p.values[util.BytesToReadOnlyString(value)] = true | ||
} | ||
|
||
func newPrefixedIDs() *prefixedIDs { | ||
return &prefixedIDs{ | ||
values: map[string]bool{}, | ||
} | ||
} | ||
|
||
// NewTaskCheckBoxHTMLRenderer creates a TaskCheckBoxHTMLRenderer to render tasklists | ||
// in the gitea form. | ||
func NewTaskCheckBoxHTMLRenderer(opts ...html.Option) renderer.NodeRenderer { | ||
r := &TaskCheckBoxHTMLRenderer{ | ||
Config: html.NewConfig(), | ||
} | ||
for _, opt := range opts { | ||
opt.SetHTMLOption(&r.Config) | ||
} | ||
return r | ||
} | ||
|
||
// TaskCheckBoxHTMLRenderer is a renderer.NodeRenderer implementation that | ||
// renders checkboxes in list items. | ||
// Overrides the default goldmark one to present the gitea format | ||
type TaskCheckBoxHTMLRenderer struct { | ||
html.Config | ||
} | ||
|
||
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs. | ||
func (r *TaskCheckBoxHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { | ||
reg.Register(east.KindTaskCheckBox, r.renderTaskCheckBox) | ||
} | ||
|
||
func (r *TaskCheckBoxHTMLRenderer) renderTaskCheckBox(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
if !entering { | ||
return ast.WalkContinue, nil | ||
} | ||
n := node.(*east.TaskCheckBox) | ||
|
||
end := ">" | ||
if r.XHTML { | ||
end = " />" | ||
} | ||
var err error | ||
if n.IsChecked { | ||
_, err = w.WriteString(`<span class="ui fitted disabled checkbox"><input type="checkbox" disabled="disabled"` + end + `<label` + end + `</span>`) | ||
} else { | ||
_, err = w.WriteString(`<span class="ui checked fitted disabled checkbox"><input type="checkbox" checked="" disabled="disabled"` + end + `<label` + end + `</span>`) | ||
} | ||
if err != nil { | ||
return ast.WalkStop, err | ||
} | ||
return ast.WalkContinue, nil | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.