2017-09-16 19:17:57 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-16 19:17:57 +02:00
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-04-20 00:25:08 +02:00
|
|
|
"io"
|
2017-09-16 19:17:57 +02:00
|
|
|
"regexp"
|
2024-11-16 09:41:44 +01:00
|
|
|
"slices"
|
2017-09-16 19:17:57 +02:00
|
|
|
"strings"
|
2021-07-15 22:33:56 +02:00
|
|
|
"sync"
|
2017-09-16 19:17:57 +02:00
|
|
|
|
2019-12-31 02:53:28 +01:00
|
|
|
"code.gitea.io/gitea/modules/markup/common"
|
2017-09-16 19:17:57 +02:00
|
|
|
|
|
|
|
"golang.org/x/net/html"
|
2018-02-27 08:09:18 +01:00
|
|
|
"golang.org/x/net/html/atom"
|
2019-03-27 12:15:23 +01:00
|
|
|
"mvdan.cc/xurls/v2"
|
2017-09-16 19:17:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Issue name styles
|
|
|
|
const (
|
|
|
|
IssueNameStyleNumeric = "numeric"
|
|
|
|
IssueNameStyleAlphanumeric = "alphanumeric"
|
2022-06-10 07:39:53 +02:00
|
|
|
IssueNameStyleRegexp = "regexp"
|
2017-09-16 19:17:57 +02:00
|
|
|
)
|
|
|
|
|
2024-11-16 09:41:44 +01:00
|
|
|
type globalVarsType struct {
|
|
|
|
hashCurrentPattern *regexp.Regexp
|
|
|
|
shortLinkPattern *regexp.Regexp
|
|
|
|
anyHashPattern *regexp.Regexp
|
|
|
|
comparePattern *regexp.Regexp
|
|
|
|
fullURLPattern *regexp.Regexp
|
|
|
|
emailRegex *regexp.Regexp
|
|
|
|
blackfridayExtRegex *regexp.Regexp
|
|
|
|
emojiShortCodeRegex *regexp.Regexp
|
|
|
|
issueFullPattern *regexp.Regexp
|
|
|
|
filesChangedFullPattern *regexp.Regexp
|
2024-11-18 06:25:42 +01:00
|
|
|
codePreviewPattern *regexp.Regexp
|
2024-11-16 09:41:44 +01:00
|
|
|
|
|
|
|
tagCleaner *regexp.Regexp
|
|
|
|
nulCleaner *strings.Replacer
|
|
|
|
}
|
|
|
|
|
|
|
|
var globalVars = sync.OnceValue[*globalVarsType](func() *globalVarsType {
|
|
|
|
v := &globalVarsType{}
|
2017-09-16 19:17:57 +02:00
|
|
|
// 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
|
|
|
|
|
2021-12-11 18:21:36 +01:00
|
|
|
// valid chars in encoded path and parameter: [-+~_%.a-zA-Z0-9/]
|
|
|
|
|
2024-01-19 17:05:02 +01:00
|
|
|
// hashCurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
|
|
|
|
// Although SHA1 hashes are 40 chars long, SHA256 are 64, the regex matches the hash from 7 to 64 chars in length
|
2021-12-11 18:21:36 +01:00
|
|
|
// so that abbreviated hash links can be used as well. This matches git and GitHub usability.
|
2024-11-16 09:41:44 +01:00
|
|
|
v.hashCurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,64})(?:\s|$|\)|\]|[.,:](\s|$))`)
|
2017-09-16 19:17:57 +02:00
|
|
|
|
2018-02-27 08:09:18 +01:00
|
|
|
// shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
|
2024-11-16 09:41:44 +01:00
|
|
|
v.shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
|
2017-09-16 19:17:57 +02:00
|
|
|
|
2024-02-21 11:08:08 +01:00
|
|
|
// anyHashPattern splits url containing SHA into parts
|
2024-11-16 09:41:44 +01:00
|
|
|
v.anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})(/[-+~%./\w]+)?(\?[-+~%.\w&=]+)?(#[-+~%.\w]+)?`)
|
2021-12-11 18:21:36 +01:00
|
|
|
|
|
|
|
// comparePattern matches "http://domain/org/repo/compare/COMMIT1...COMMIT2#hash"
|
2024-11-16 09:41:44 +01:00
|
|
|
v.comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,64})(\.\.\.?)([0-9a-f]{7,64})?(#[-+~_%.a-zA-Z0-9]+)?`)
|
2017-09-16 19:17:57 +02:00
|
|
|
|
2024-02-21 11:08:08 +01:00
|
|
|
// fullURLPattern matches full URL like "mailto:...", "https://..." and "ssh+git://..."
|
2024-11-16 09:41:44 +01:00
|
|
|
v.fullURLPattern = regexp.MustCompile(`^[a-z][-+\w]+:`)
|
2018-02-27 08:09:18 +01:00
|
|
|
|
2024-02-21 11:08:08 +01:00
|
|
|
// emailRegex is definitely not perfect with edge cases,
|
|
|
|
// it is still accepted by the CommonMark specification, as well as the HTML5 spec:
|
2018-02-27 08:09:18 +01:00
|
|
|
// http://spec.commonmark.org/0.28/#email-address
|
|
|
|
// https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail)
|
2024-11-16 09:41:44 +01:00
|
|
|
v.emailRegex = regexp.MustCompile("(?:\\s|^|\\(|\\[)([a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]{2,}(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)(?:\\s|$|\\)|\\]|;|,|\\?|!|\\.(\\s|$))")
|
2018-02-27 08:09:18 +01:00
|
|
|
|
2024-02-21 11:08:08 +01:00
|
|
|
// blackfridayExtRegex is for blackfriday extensions create IDs like fn:user-content-footnote
|
2024-11-16 09:41:44 +01:00
|
|
|
v.blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`)
|
2020-04-28 20:05:39 +02:00
|
|
|
|
2024-02-21 11:08:08 +01:00
|
|
|
// emojiShortCodeRegex find emoji by alias like :smile:
|
2024-11-16 09:41:44 +01:00
|
|
|
v.emojiShortCodeRegex = regexp.MustCompile(`:[-+\w]+:`)
|
2017-09-16 19:17:57 +02:00
|
|
|
|
2024-11-16 09:41:44 +01:00
|
|
|
// example: https://domain/org/repo/pulls/27#hash
|
|
|
|
v.issueFullPattern = regexp.MustCompile(`https?://(?:\S+/)[\w_.-]+/[\w_.-]+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#](\S+)?)?\b`)
|
|
|
|
|
|
|
|
// example: https://domain/org/repo/pulls/27/files#hash
|
|
|
|
v.filesChangedFullPattern = regexp.MustCompile(`https?://(?:\S+/)[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b`)
|
|
|
|
|
2024-11-18 06:25:42 +01:00
|
|
|
// codePreviewPattern matches "http://domain/.../{owner}/{repo}/src/commit/{commit}/{filepath}#L10-L20"
|
|
|
|
v.codePreviewPattern = regexp.MustCompile(`https?://\S+/([^\s/]+)/([^\s/]+)/src/commit/([0-9a-f]{7,64})(/\S+)#(L\d+(-L\d+)?)`)
|
|
|
|
|
2024-11-16 09:41:44 +01:00
|
|
|
v.tagCleaner = regexp.MustCompile(`<((?:/?\w+/\w+)|(?:/[\w ]+/)|(/?[hH][tT][mM][lL]\b)|(/?[hH][eE][aA][dD]\b))`)
|
|
|
|
v.nulCleaner = strings.NewReplacer("\000", "")
|
|
|
|
return v
|
|
|
|
})
|
2019-10-14 00:29:10 +02:00
|
|
|
|
2024-02-21 11:08:08 +01:00
|
|
|
// IsFullURLBytes reports whether link fits valid format.
|
|
|
|
func IsFullURLBytes(link []byte) bool {
|
2024-11-16 09:41:44 +01:00
|
|
|
return globalVars().fullURLPattern.Match(link)
|
2017-09-16 19:17:57 +02:00
|
|
|
}
|
|
|
|
|
2024-02-21 11:08:08 +01:00
|
|
|
func IsFullURLString(link string) bool {
|
2024-11-16 09:41:44 +01:00
|
|
|
return globalVars().fullURLPattern.MatchString(link)
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
2024-06-21 20:23:54 +02:00
|
|
|
func IsNonEmptyRelativePath(link string) bool {
|
|
|
|
return link != "" && !IsFullURLString(link) && link[0] != '/' && link[0] != '?' && link[0] != '#'
|
|
|
|
}
|
|
|
|
|
2019-10-15 03:31:09 +02:00
|
|
|
// CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text
|
|
|
|
func CustomLinkURLSchemes(schemes []string) {
|
|
|
|
schemes = append(schemes, "http", "https")
|
|
|
|
withAuth := make([]string, 0, len(schemes))
|
|
|
|
validScheme := regexp.MustCompile(`^[a-z]+$`)
|
|
|
|
for _, s := range schemes {
|
|
|
|
if !validScheme.MatchString(s) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
without := false
|
|
|
|
for _, sna := range xurls.SchemesNoAuthority {
|
|
|
|
if s == sna {
|
|
|
|
without = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if without {
|
|
|
|
s += ":"
|
|
|
|
} else {
|
|
|
|
s += "://"
|
|
|
|
}
|
|
|
|
withAuth = append(withAuth, s)
|
|
|
|
}
|
2024-11-18 06:25:42 +01:00
|
|
|
common.GlobalVars().LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
|
2019-10-15 03:31:09 +02:00
|
|
|
}
|
|
|
|
|
2018-02-27 08:09:18 +01:00
|
|
|
type postProcessError struct {
|
|
|
|
context string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postProcessError) Error() string {
|
2019-06-12 21:41:28 +02:00
|
|
|
return "PostProcess: " + p.context + ", " + p.err.Error()
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 00:25:08 +02:00
|
|
|
type processor func(ctx *RenderContext, node *html.Node)
|
2018-02-27 08:09:18 +01:00
|
|
|
|
|
|
|
var defaultProcessors = []processor{
|
|
|
|
fullIssuePatternProcessor,
|
2021-12-11 18:21:36 +01:00
|
|
|
comparePatternProcessor,
|
2024-04-02 19:48:27 +02:00
|
|
|
codePreviewPatternProcessor,
|
2024-01-19 17:05:02 +01:00
|
|
|
fullHashPatternProcessor,
|
2019-03-27 16:37:54 +01:00
|
|
|
shortLinkProcessor,
|
|
|
|
linkProcessor,
|
|
|
|
mentionProcessor,
|
2018-02-27 08:09:18 +01:00
|
|
|
issueIndexPatternProcessor,
|
2023-01-30 02:50:01 +01:00
|
|
|
commitCrossReferencePatternProcessor,
|
2024-01-19 17:05:02 +01:00
|
|
|
hashCurrentPatternProcessor,
|
2018-02-27 08:09:18 +01:00
|
|
|
emailAddressProcessor,
|
2020-04-28 20:05:39 +02:00
|
|
|
emojiProcessor,
|
|
|
|
emojiShortCodeProcessor,
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostProcess 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.
|
2024-11-18 06:25:42 +01:00
|
|
|
func PostProcess(ctx *RenderContext, input io.Reader, output io.Writer) error {
|
2021-04-20 00:25:08 +02:00
|
|
|
return postProcess(ctx, defaultProcessors, input, output)
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var commitMessageProcessors = []processor{
|
|
|
|
fullIssuePatternProcessor,
|
2021-12-11 18:21:36 +01:00
|
|
|
comparePatternProcessor,
|
2024-01-19 17:05:02 +01:00
|
|
|
fullHashPatternProcessor,
|
2019-03-27 16:37:54 +01:00
|
|
|
linkProcessor,
|
|
|
|
mentionProcessor,
|
2018-02-27 08:09:18 +01:00
|
|
|
issueIndexPatternProcessor,
|
2023-01-30 02:50:01 +01:00
|
|
|
commitCrossReferencePatternProcessor,
|
2024-01-19 17:05:02 +01:00
|
|
|
hashCurrentPatternProcessor,
|
2018-02-27 08:09:18 +01:00
|
|
|
emailAddressProcessor,
|
2020-04-28 20:05:39 +02:00
|
|
|
emojiProcessor,
|
|
|
|
emojiShortCodeProcessor,
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2024-11-18 06:25:42 +01:00
|
|
|
func RenderCommitMessage(ctx *RenderContext, content string) (string, error) {
|
2022-01-20 18:46:10 +01:00
|
|
|
procs := commitMessageProcessors
|
2021-04-20 00:25:08 +02:00
|
|
|
return renderProcessString(ctx, procs, content)
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 11:03:30 +02:00
|
|
|
var commitMessageSubjectProcessors = []processor{
|
|
|
|
fullIssuePatternProcessor,
|
2021-12-11 18:21:36 +01:00
|
|
|
comparePatternProcessor,
|
2024-01-19 17:05:02 +01:00
|
|
|
fullHashPatternProcessor,
|
2019-09-10 11:03:30 +02:00
|
|
|
linkProcessor,
|
|
|
|
mentionProcessor,
|
|
|
|
issueIndexPatternProcessor,
|
2023-01-30 02:50:01 +01:00
|
|
|
commitCrossReferencePatternProcessor,
|
2024-01-19 17:05:02 +01:00
|
|
|
hashCurrentPatternProcessor,
|
2020-04-28 20:05:39 +02:00
|
|
|
emojiShortCodeProcessor,
|
|
|
|
emojiProcessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
var emojiProcessors = []processor{
|
|
|
|
emojiShortCodeProcessor,
|
|
|
|
emojiProcessor,
|
2019-09-10 11:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenderCommitMessageSubject will use the same logic as PostProcess and
|
|
|
|
// RenderCommitMessage, but will disable the shortLinkProcessor and
|
|
|
|
// emailAddressProcessor, will add a defaultLinkProcessor if defaultLink is set,
|
|
|
|
// which changes every text node into a link to the passed default link.
|
2024-11-18 06:25:42 +01:00
|
|
|
func RenderCommitMessageSubject(ctx *RenderContext, defaultLink, content string) (string, error) {
|
2024-11-16 09:41:44 +01:00
|
|
|
procs := slices.Clone(commitMessageSubjectProcessors)
|
|
|
|
procs = append(procs, func(ctx *RenderContext, node *html.Node) {
|
|
|
|
ch := &html.Node{Parent: node, Type: html.TextNode, Data: node.Data}
|
|
|
|
node.Type = html.ElementNode
|
|
|
|
node.Data = "a"
|
|
|
|
node.DataAtom = atom.A
|
|
|
|
node.Attr = []html.Attribute{{Key: "href", Val: defaultLink}, {Key: "class", Val: "muted"}}
|
|
|
|
node.FirstChild, node.LastChild = ch, ch
|
|
|
|
})
|
2021-04-20 00:25:08 +02:00
|
|
|
return renderProcessString(ctx, procs, content)
|
2019-09-10 11:03:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 11:50:47 +01:00
|
|
|
// RenderIssueTitle to process title on individual issue/pull page
|
2024-11-18 06:25:42 +01:00
|
|
|
func RenderIssueTitle(ctx *RenderContext, title string) (string, error) {
|
2024-11-16 09:41:44 +01:00
|
|
|
// do not render other issue/commit links in an issue's title - which in most cases is already a link.
|
2021-04-20 00:25:08 +02:00
|
|
|
return renderProcessString(ctx, []processor{
|
|
|
|
emojiShortCodeProcessor,
|
|
|
|
emojiProcessor,
|
|
|
|
}, title)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderProcessString(ctx *RenderContext, procs []processor, content string) (string, error) {
|
|
|
|
var buf strings.Builder
|
|
|
|
if err := postProcess(ctx, procs, strings.NewReader(content), &buf); err != nil {
|
|
|
|
return "", err
|
2020-12-03 11:50:47 +01:00
|
|
|
}
|
2021-04-20 00:25:08 +02:00
|
|
|
return buf.String(), nil
|
2020-12-03 11:50:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 03:23:34 +01:00
|
|
|
// RenderDescriptionHTML will use similar logic as PostProcess, but will
|
|
|
|
// use a single special linkProcessor.
|
2024-11-18 06:25:42 +01:00
|
|
|
func RenderDescriptionHTML(ctx *RenderContext, content string) (string, error) {
|
2021-04-20 00:25:08 +02:00
|
|
|
return renderProcessString(ctx, []processor{
|
|
|
|
descriptionLinkProcessor,
|
|
|
|
emojiShortCodeProcessor,
|
|
|
|
emojiProcessor,
|
|
|
|
}, content)
|
2019-03-12 03:23:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 20:05:39 +02:00
|
|
|
// RenderEmoji for when we want to just process emoji and shortcodes
|
2021-07-08 13:38:13 +02:00
|
|
|
// in various places it isn't already run through the normal markdown processor
|
2024-11-18 06:25:42 +01:00
|
|
|
func RenderEmoji(ctx *RenderContext, content string) (string, error) {
|
2023-03-05 22:59:05 +01:00
|
|
|
return renderProcessString(ctx, emojiProcessors, content)
|
2020-04-28 20:05:39 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 00:25:08 +02:00
|
|
|
func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output io.Writer) error {
|
2021-06-21 00:39:12 +02:00
|
|
|
defer ctx.Cancel()
|
2021-04-20 00:25:08 +02:00
|
|
|
// FIXME: don't read all content to memory
|
2021-09-22 07:38:34 +02:00
|
|
|
rawHTML, err := io.ReadAll(input)
|
2021-04-20 00:25:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the HTML
|
2022-12-12 04:03:54 +01:00
|
|
|
node, err := html.Parse(io.MultiReader(
|
|
|
|
// prepend "<html><body>"
|
|
|
|
strings.NewReader("<html><body>"),
|
|
|
|
// Strip out nuls - they're always invalid
|
2024-11-16 09:41:44 +01:00
|
|
|
bytes.NewReader(globalVars().tagCleaner.ReplaceAll([]byte(globalVars().nulCleaner.Replace(string(rawHTML))), []byte("<$1"))),
|
2022-12-12 04:03:54 +01:00
|
|
|
// close the tags
|
|
|
|
strings.NewReader("</body></html>"),
|
|
|
|
))
|
2018-02-27 08:09:18 +01:00
|
|
|
if err != nil {
|
2021-04-20 00:25:08 +02:00
|
|
|
return &postProcessError{"invalid HTML", err}
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:12:22 +02:00
|
|
|
if node.Type == html.DocumentNode {
|
|
|
|
node = node.FirstChild
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
2024-01-15 09:49:24 +01:00
|
|
|
visitNode(ctx, procs, node)
|
2021-01-20 16:10:50 +01:00
|
|
|
|
2021-06-22 00:12:22 +02:00
|
|
|
newNodes := make([]*html.Node, 0, 5)
|
|
|
|
|
|
|
|
if node.Data == "html" {
|
|
|
|
node = node.FirstChild
|
|
|
|
for node != nil && node.Data != "body" {
|
|
|
|
node = node.NextSibling
|
2021-01-20 16:10:50 +01:00
|
|
|
}
|
2021-06-22 00:12:22 +02:00
|
|
|
}
|
|
|
|
if node != nil {
|
2021-01-20 16:10:50 +01:00
|
|
|
if node.Data == "body" {
|
|
|
|
child := node.FirstChild
|
|
|
|
for child != nil {
|
|
|
|
newNodes = append(newNodes, child)
|
|
|
|
child = child.NextSibling
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newNodes = append(newNodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 08:09:18 +01:00
|
|
|
// Render everything to buf.
|
2021-04-20 00:25:08 +02:00
|
|
|
for _, node := range newNodes {
|
2022-03-17 19:04:36 +01:00
|
|
|
if err := html.Render(output, node); err != nil {
|
2021-04-20 00:25:08 +02:00
|
|
|
return &postProcessError{"error rendering processed HTML", err}
|
2017-09-16 19:17:57 +02:00
|
|
|
}
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
2021-04-20 00:25:08 +02:00
|
|
|
return nil
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
2024-11-18 06:25:42 +01:00
|
|
|
func isEmojiNode(node *html.Node) bool {
|
|
|
|
if node.Type == html.ElementNode && node.Data == atom.Span.String() {
|
|
|
|
for _, attr := range node.Attr {
|
|
|
|
if (attr.Key == "class" || attr.Key == "data-attr-class") && strings.Contains(attr.Val, "emoji") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-04 14:19:41 +02:00
|
|
|
func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Node {
|
2023-01-31 06:21:29 +01:00
|
|
|
// Add user-content- to IDs and "#" links if they don't already have them
|
2019-12-23 23:38:50 +01:00
|
|
|
for idx, attr := range node.Attr {
|
2023-01-31 06:21:29 +01:00
|
|
|
val := strings.TrimPrefix(attr.Val, "#")
|
2024-11-16 09:41:44 +01:00
|
|
|
notHasPrefix := !(strings.HasPrefix(val, "user-content-") || globalVars().blackfridayExtRegex.MatchString(val))
|
2023-01-31 06:21:29 +01:00
|
|
|
|
|
|
|
if attr.Key == "id" && notHasPrefix {
|
2019-12-23 23:38:50 +01:00
|
|
|
node.Attr[idx].Val = "user-content-" + attr.Val
|
|
|
|
}
|
2020-04-28 20:05:39 +02:00
|
|
|
|
2023-01-31 06:21:29 +01:00
|
|
|
if attr.Key == "href" && strings.HasPrefix(attr.Val, "#") && notHasPrefix {
|
|
|
|
node.Attr[idx].Val = "#user-content-" + val
|
|
|
|
}
|
2019-12-23 23:38:50 +01:00
|
|
|
}
|
2020-04-28 20:05:39 +02:00
|
|
|
|
2018-02-27 08:09:18 +01:00
|
|
|
switch node.Type {
|
|
|
|
case html.TextNode:
|
2024-11-18 06:25:42 +01:00
|
|
|
for _, proc := range procs {
|
|
|
|
proc(ctx, node) // it might add siblings
|
|
|
|
}
|
|
|
|
|
2018-02-27 08:09:18 +01:00
|
|
|
case html.ElementNode:
|
2024-11-18 06:25:42 +01:00
|
|
|
if isEmojiNode(node) {
|
|
|
|
// TextNode emoji will be converted to `<span class="emoji">`, then the next iteration will visit the "span"
|
|
|
|
// if we don't stop it, it will go into the TextNode again and create an infinite recursion
|
2024-06-21 20:23:54 +02:00
|
|
|
return node.NextSibling
|
2024-11-18 06:25:42 +01:00
|
|
|
} else if node.Data == "code" || node.Data == "pre" {
|
|
|
|
return node.NextSibling // ignore code and pre nodes
|
2024-06-21 20:23:54 +02:00
|
|
|
} else if node.Data == "img" {
|
|
|
|
return visitNodeImg(ctx, node)
|
|
|
|
} else if node.Data == "video" {
|
|
|
|
return visitNodeVideo(ctx, node)
|
2020-02-28 05:16:05 +01:00
|
|
|
} else if node.Data == "a" {
|
2024-11-18 06:25:42 +01:00
|
|
|
procs = emojiProcessors // Restrict text in links to emojis
|
2017-09-16 19:17:57 +02:00
|
|
|
}
|
2024-06-04 14:19:41 +02:00
|
|
|
for n := node.FirstChild; n != nil; {
|
|
|
|
n = visitNode(ctx, procs, n)
|
2017-09-16 19:17:57 +02:00
|
|
|
}
|
2024-06-18 00:56:45 +02:00
|
|
|
default:
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
2024-06-04 14:19:41 +02:00
|
|
|
return node.NextSibling
|
2018-02-27 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
2019-10-14 00:29:10 +02:00
|
|
|
// createKeyword() renders a highlighted version of an action keyword
|
2024-11-18 06:25:42 +01:00
|
|
|
func createKeyword(ctx *RenderContext, content string) *html.Node {
|
|
|
|
// CSS class for action keywords (e.g. "closes: #1")
|
|
|
|
const keywordClass = "issue-keyword"
|
|
|
|
|
2019-10-14 00:29:10 +02:00
|
|
|
span := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Span.String(),
|
|
|
|
Attr: []html.Attribute{},
|
|
|
|
}
|
2024-11-18 06:25:42 +01:00
|
|
|
span.Attr = append(span.Attr, ctx.RenderInternal.NodeSafeAttr("class", keywordClass))
|
2019-10-14 00:29:10 +02:00
|
|
|
|
|
|
|
text := &html.Node{
|
|
|
|
Type: html.TextNode,
|
|
|
|
Data: content,
|
|
|
|
}
|
|
|
|
span.AppendChild(text)
|
|
|
|
|
|
|
|
return span
|
|
|
|
}
|
|
|
|
|
2024-11-18 06:25:42 +01:00
|
|
|
func createLink(ctx *RenderContext, href, content, class string) *html.Node {
|
2019-04-09 05:18:48 +02:00
|
|
|
a := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.A.String(),
|
2024-11-14 06:02:11 +01:00
|
|
|
Attr: []html.Attribute{{Key: "href", Val: href}},
|
|
|
|
}
|
|
|
|
if !RenderBehaviorForTesting.DisableInternalAttributes {
|
|
|
|
a.Attr = append(a.Attr, html.Attribute{Key: "data-markdown-generated-content"})
|
2019-04-09 05:18:48 +02:00
|
|
|
}
|
2019-09-10 11:03:30 +02:00
|
|
|
if class != "" {
|
2024-11-18 06:25:42 +01:00
|
|
|
a.Attr = append(a.Attr, ctx.RenderInternal.NodeSafeAttr("class", class))
|
2019-09-10 11:03:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 05:18:48 +02:00
|
|
|
text := &html.Node{
|
2018-02-27 08:09:18 +01:00
|
|
|
Type: html.TextNode,
|
|
|
|
Data: content,
|
|
|
|
}
|
2019-04-09 05:18:48 +02:00
|
|
|
|
|
|
|
a.AppendChild(text)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-10-14 00:29:10 +02:00
|
|
|
// replaceContent takes text node, and in its content it replaces a section of
|
|
|
|
// it with the specified newNode.
|
2018-02-27 08:09:18 +01:00
|
|
|
func replaceContent(node *html.Node, i, j int, newNode *html.Node) {
|
2019-10-14 00:29:10 +02:00
|
|
|
replaceContentList(node, i, j, []*html.Node{newNode})
|
|
|
|
}
|
|
|
|
|
|
|
|
// replaceContentList takes text node, and in its content it replaces a section of
|
|
|
|
// it with the specified newNodes. An example to visualize how this can work can
|
|
|
|
// be found here: https://play.golang.org/p/5zP8NnHZ03s
|
|
|
|
func replaceContentList(node *html.Node, i, j int, newNodes []*html.Node) {
|
2018-02-27 08:09:18 +01:00
|
|
|
// get the data before and after the match
|
|
|
|
before := node.Data[:i]
|
|
|
|
after := node.Data[j:]
|
|
|
|
|
|
|
|
// Replace in the current node the text, so that it is only what it is
|
|
|
|
// supposed to have.
|
|
|
|
node.Data = before
|
|
|
|
|
|
|
|
// Get the current next sibling, before which we place the replaced data,
|
|
|
|
// and after that we place the new text node.
|
|
|
|
nextSibling := node.NextSibling
|
2019-10-14 00:29:10 +02:00
|
|
|
for _, n := range newNodes {
|
|
|
|
node.Parent.InsertBefore(n, nextSibling)
|
|
|
|
}
|
2018-02-27 08:09:18 +01:00
|
|
|
if after != "" {
|
|
|
|
node.Parent.InsertBefore(&html.Node{
|
|
|
|
Type: html.TextNode,
|
|
|
|
Data: after,
|
|
|
|
}, nextSibling)
|
2017-09-16 19:17:57 +02:00
|
|
|
}
|
|
|
|
}
|