2017-09-17 01:17:57 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-17 01:17:57 +08:00
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-04-20 06:25:08 +08:00
|
|
|
"io"
|
2017-09-17 01:17:57 +08:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-07-16 04:33:56 +08:00
|
|
|
"sync"
|
2017-09-17 01:17:57 +08:00
|
|
|
|
2019-12-31 09:53:28 +08:00
|
|
|
"code.gitea.io/gitea/modules/markup/common"
|
2017-09-17 01:17:57 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"golang.org/x/net/html"
|
2018-02-27 15:09:18 +08:00
|
|
|
"golang.org/x/net/html/atom"
|
2019-03-27 19:15:23 +08:00
|
|
|
"mvdan.cc/xurls/v2"
|
2017-09-17 01:17:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Issue name styles
|
|
|
|
const (
|
|
|
|
IssueNameStyleNumeric = "numeric"
|
|
|
|
IssueNameStyleAlphanumeric = "alphanumeric"
|
2022-06-10 13:39:53 +08:00
|
|
|
IssueNameStyleRegexp = "regexp"
|
2017-09-17 01:17:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-12-12 01:21:36 +08:00
|
|
|
// valid chars in encoded path and parameter: [-+~_%.a-zA-Z0-9/]
|
|
|
|
|
2024-01-20 00:05:02 +08: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-12 01:21:36 +08:00
|
|
|
// so that abbreviated hash links can be used as well. This matches git and GitHub usability.
|
2024-06-13 06:35:46 +08:00
|
|
|
hashCurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,64})(?:\s|$|\)|\]|[.,:](\s|$))`)
|
2017-09-17 01:17:57 +08:00
|
|
|
|
2018-02-27 15:09:18 +08:00
|
|
|
// shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
|
|
|
|
shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
|
2017-09-17 01:17:57 +08:00
|
|
|
|
2024-02-21 18:08:08 +08:00
|
|
|
// anyHashPattern splits url containing SHA into parts
|
2024-05-04 09:48:16 +08:00
|
|
|
anyHashPattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{40,64})(/[-+~%./\w]+)?(\?[-+~%.\w&=]+)?(#[-+~%.\w]+)?`)
|
2021-12-12 01:21:36 +08:00
|
|
|
|
|
|
|
// comparePattern matches "http://domain/org/repo/compare/COMMIT1...COMMIT2#hash"
|
2024-01-20 00:05:02 +08:00
|
|
|
comparePattern = regexp.MustCompile(`https?://(?:\S+/){4,5}([0-9a-f]{7,64})(\.\.\.?)([0-9a-f]{7,64})?(#[-+~_%.a-zA-Z0-9]+)?`)
|
2017-09-17 01:17:57 +08:00
|
|
|
|
2024-02-21 18:08:08 +08:00
|
|
|
// fullURLPattern matches full URL like "mailto:...", "https://..." and "ssh+git://..."
|
|
|
|
fullURLPattern = regexp.MustCompile(`^[a-z][-+\w]+:`)
|
2018-02-27 15:09:18 +08:00
|
|
|
|
2024-02-21 18:08:08 +08: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 15:09:18 +08:00
|
|
|
// http://spec.commonmark.org/0.28/#email-address
|
|
|
|
// https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail)
|
2023-11-11 12:08:19 +08:00
|
|
|
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 15:09:18 +08:00
|
|
|
|
2024-02-21 18:08:08 +08:00
|
|
|
// blackfridayExtRegex is for blackfriday extensions create IDs like fn:user-content-footnote
|
2019-12-24 06:38:50 +08:00
|
|
|
blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`)
|
2020-04-29 02:05:39 +08:00
|
|
|
|
2024-02-21 18:08:08 +08:00
|
|
|
// emojiShortCodeRegex find emoji by alias like :smile:
|
|
|
|
emojiShortCodeRegex = regexp.MustCompile(`:[-+\w]+:`)
|
2017-09-17 01:17:57 +08:00
|
|
|
)
|
|
|
|
|
2019-10-14 06:29:10 +08:00
|
|
|
// CSS class for action keywords (e.g. "closes: #1")
|
|
|
|
const keywordClass = "issue-keyword"
|
|
|
|
|
2024-02-21 18:08:08 +08:00
|
|
|
// IsFullURLBytes reports whether link fits valid format.
|
|
|
|
func IsFullURLBytes(link []byte) bool {
|
|
|
|
return fullURLPattern.Match(link)
|
2017-09-17 01:17:57 +08:00
|
|
|
}
|
|
|
|
|
2024-02-21 18:08:08 +08:00
|
|
|
func IsFullURLString(link string) bool {
|
|
|
|
return fullURLPattern.MatchString(link)
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
2024-06-22 02:23:54 +08:00
|
|
|
func IsNonEmptyRelativePath(link string) bool {
|
|
|
|
return link != "" && !IsFullURLString(link) && link[0] != '/' && link[0] != '?' && link[0] != '#'
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:33:56 +08:00
|
|
|
// regexp for full links to issues/pulls
|
|
|
|
var issueFullPattern *regexp.Regexp
|
|
|
|
|
|
|
|
// Once for to prevent races
|
|
|
|
var issueFullPatternOnce sync.Once
|
|
|
|
|
Append `(comment)` when a link points at a comment rather than the whole issue (#23734)
Close #23671
For the feature mentioned above, this PR append ' (comment)' to the
rendered html if it is a hashcomment.
After the PR, type in the following
```
pull request from other repo:
http://localhost:3000/testOrg/testOrgRepo/pulls/2
pull request from this repo:
http://localhost:3000/aaa/testA/pulls/2
issue comment from this repo:
http://localhost:3000/aaa/testA/issues/1#issuecomment-18
http://localhost:3000/aaa/testA/pulls/2#issue-9
issue comment from other repo:
http://localhost:3000/testOrg/testOrgRepo/pulls/2#issuecomment-24
http://localhost:3000/testOrg/testOrgRepo/pulls/2#issue
```
Gives:
<img width="687" alt="截屏2023-03-27 13 53 06"
src="https://user-images.githubusercontent.com/17645053/227852387-2b218e0d-3468-4d90-ad81-d702ddd17fd2.png">
Other than the above feature, this PR also includes two other changes:
1 Right now, the render of links from file changed tab in pull request
might not be very proper, for example, if type in the following. (not
sure if this is an issue or design, if not an issue, I will revert the
changes). example on
[try.gitea.io](https://try.gitea.io/HesterG/testrepo/pulls/1)
```
https://try.gitea.io/HesterG/testrepo/pulls/1/files#issuecomment-162725
https://try.gitea.io/HesterG/testrepo/pulls/1/files
```
it will render the following
<img width="899" alt="截屏2023-03-24 15 41 37"
src="https://user-images.githubusercontent.com/17645053/227456117-5eccedb7-9118-4540-929d-aee9a76de852.png">
In this PR, skip processing the link into a ref issue if it is a link
from files changed tab in pull request
After:
type in following
```
hash comment on files changed tab:
http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24
files changed link:
http://localhost:3000/testOrg/testOrgRepo/pulls/2/files
```
Gives
<img width="708" alt="截屏2023-03-27 22 09 02"
src="https://user-images.githubusercontent.com/17645053/227964273-5dc06c50-3713-489c-b05d-d95367d0ab0f.png">
2 Right now, after editing the comment area, there will not be tippys
attached to `ref-issue`; and no tippy attached on preview as well.
example:
https://user-images.githubusercontent.com/17645053/227850540-5ae34e2d-b1d7-4d0d-9726-7701bf825d1f.mov
In this PR, in frontend, make sure tippy is added after editing the
comment, and to the comment on preview tab
After:
https://user-images.githubusercontent.com/17645053/227853777-06f56b4c-1148-467c-b6f7-f79418e67504.mov
2023-04-03 16:02:57 +08:00
|
|
|
// regexp for full links to hash comment in pull request files changed tab
|
|
|
|
var filesChangedFullPattern *regexp.Regexp
|
|
|
|
|
|
|
|
// Once for to prevent races
|
|
|
|
var filesChangedFullPatternOnce sync.Once
|
|
|
|
|
2017-09-17 01:17:57 +08:00
|
|
|
func getIssueFullPattern() *regexp.Regexp {
|
2021-07-16 04:33:56 +08:00
|
|
|
issueFullPatternOnce.Do(func() {
|
Append `(comment)` when a link points at a comment rather than the whole issue (#23734)
Close #23671
For the feature mentioned above, this PR append ' (comment)' to the
rendered html if it is a hashcomment.
After the PR, type in the following
```
pull request from other repo:
http://localhost:3000/testOrg/testOrgRepo/pulls/2
pull request from this repo:
http://localhost:3000/aaa/testA/pulls/2
issue comment from this repo:
http://localhost:3000/aaa/testA/issues/1#issuecomment-18
http://localhost:3000/aaa/testA/pulls/2#issue-9
issue comment from other repo:
http://localhost:3000/testOrg/testOrgRepo/pulls/2#issuecomment-24
http://localhost:3000/testOrg/testOrgRepo/pulls/2#issue
```
Gives:
<img width="687" alt="截屏2023-03-27 13 53 06"
src="https://user-images.githubusercontent.com/17645053/227852387-2b218e0d-3468-4d90-ad81-d702ddd17fd2.png">
Other than the above feature, this PR also includes two other changes:
1 Right now, the render of links from file changed tab in pull request
might not be very proper, for example, if type in the following. (not
sure if this is an issue or design, if not an issue, I will revert the
changes). example on
[try.gitea.io](https://try.gitea.io/HesterG/testrepo/pulls/1)
```
https://try.gitea.io/HesterG/testrepo/pulls/1/files#issuecomment-162725
https://try.gitea.io/HesterG/testrepo/pulls/1/files
```
it will render the following
<img width="899" alt="截屏2023-03-24 15 41 37"
src="https://user-images.githubusercontent.com/17645053/227456117-5eccedb7-9118-4540-929d-aee9a76de852.png">
In this PR, skip processing the link into a ref issue if it is a link
from files changed tab in pull request
After:
type in following
```
hash comment on files changed tab:
http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24
files changed link:
http://localhost:3000/testOrg/testOrgRepo/pulls/2/files
```
Gives
<img width="708" alt="截屏2023-03-27 22 09 02"
src="https://user-images.githubusercontent.com/17645053/227964273-5dc06c50-3713-489c-b05d-d95367d0ab0f.png">
2 Right now, after editing the comment area, there will not be tippys
attached to `ref-issue`; and no tippy attached on preview as well.
example:
https://user-images.githubusercontent.com/17645053/227850540-5ae34e2d-b1d7-4d0d-9726-7701bf825d1f.mov
In this PR, in frontend, make sure tippy is added after editing the
comment, and to the comment on preview tab
After:
https://user-images.githubusercontent.com/17645053/227853777-06f56b4c-1148-467c-b6f7-f79418e67504.mov
2023-04-03 16:02:57 +08:00
|
|
|
// example: https://domain/org/repo/pulls/27#hash
|
2021-02-20 05:36:43 +08:00
|
|
|
issueFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
|
2022-02-26 07:26:43 +08:00
|
|
|
`[\w_.-]+/[\w_.-]+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#](\S+)?)?\b`)
|
2021-07-16 04:33:56 +08:00
|
|
|
})
|
2017-09-17 01:17:57 +08:00
|
|
|
return issueFullPattern
|
|
|
|
}
|
|
|
|
|
Append `(comment)` when a link points at a comment rather than the whole issue (#23734)
Close #23671
For the feature mentioned above, this PR append ' (comment)' to the
rendered html if it is a hashcomment.
After the PR, type in the following
```
pull request from other repo:
http://localhost:3000/testOrg/testOrgRepo/pulls/2
pull request from this repo:
http://localhost:3000/aaa/testA/pulls/2
issue comment from this repo:
http://localhost:3000/aaa/testA/issues/1#issuecomment-18
http://localhost:3000/aaa/testA/pulls/2#issue-9
issue comment from other repo:
http://localhost:3000/testOrg/testOrgRepo/pulls/2#issuecomment-24
http://localhost:3000/testOrg/testOrgRepo/pulls/2#issue
```
Gives:
<img width="687" alt="截屏2023-03-27 13 53 06"
src="https://user-images.githubusercontent.com/17645053/227852387-2b218e0d-3468-4d90-ad81-d702ddd17fd2.png">
Other than the above feature, this PR also includes two other changes:
1 Right now, the render of links from file changed tab in pull request
might not be very proper, for example, if type in the following. (not
sure if this is an issue or design, if not an issue, I will revert the
changes). example on
[try.gitea.io](https://try.gitea.io/HesterG/testrepo/pulls/1)
```
https://try.gitea.io/HesterG/testrepo/pulls/1/files#issuecomment-162725
https://try.gitea.io/HesterG/testrepo/pulls/1/files
```
it will render the following
<img width="899" alt="截屏2023-03-24 15 41 37"
src="https://user-images.githubusercontent.com/17645053/227456117-5eccedb7-9118-4540-929d-aee9a76de852.png">
In this PR, skip processing the link into a ref issue if it is a link
from files changed tab in pull request
After:
type in following
```
hash comment on files changed tab:
http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24
files changed link:
http://localhost:3000/testOrg/testOrgRepo/pulls/2/files
```
Gives
<img width="708" alt="截屏2023-03-27 22 09 02"
src="https://user-images.githubusercontent.com/17645053/227964273-5dc06c50-3713-489c-b05d-d95367d0ab0f.png">
2 Right now, after editing the comment area, there will not be tippys
attached to `ref-issue`; and no tippy attached on preview as well.
example:
https://user-images.githubusercontent.com/17645053/227850540-5ae34e2d-b1d7-4d0d-9726-7701bf825d1f.mov
In this PR, in frontend, make sure tippy is added after editing the
comment, and to the comment on preview tab
After:
https://user-images.githubusercontent.com/17645053/227853777-06f56b4c-1148-467c-b6f7-f79418e67504.mov
2023-04-03 16:02:57 +08:00
|
|
|
func getFilesChangedFullPattern() *regexp.Regexp {
|
|
|
|
filesChangedFullPatternOnce.Do(func() {
|
|
|
|
// example: https://domain/org/repo/pulls/27/files#hash
|
|
|
|
filesChangedFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
|
|
|
|
`[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b`)
|
|
|
|
})
|
|
|
|
return filesChangedFullPattern
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:31:09 +08: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)
|
|
|
|
}
|
2019-12-31 09:53:28 +08:00
|
|
|
common.LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
|
2019-10-15 09:31:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-27 15:09:18 +08:00
|
|
|
type postProcessError struct {
|
|
|
|
context string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postProcessError) Error() string {
|
2019-06-13 03:41:28 +08:00
|
|
|
return "PostProcess: " + p.context + ", " + p.err.Error()
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
2021-04-20 06:25:08 +08:00
|
|
|
type processor func(ctx *RenderContext, node *html.Node)
|
2018-02-27 15:09:18 +08:00
|
|
|
|
|
|
|
var defaultProcessors = []processor{
|
|
|
|
fullIssuePatternProcessor,
|
2021-12-12 01:21:36 +08:00
|
|
|
comparePatternProcessor,
|
2024-04-03 01:48:27 +08:00
|
|
|
codePreviewPatternProcessor,
|
2024-01-20 00:05:02 +08:00
|
|
|
fullHashPatternProcessor,
|
2019-03-27 23:37:54 +08:00
|
|
|
shortLinkProcessor,
|
|
|
|
linkProcessor,
|
|
|
|
mentionProcessor,
|
2018-02-27 15:09:18 +08:00
|
|
|
issueIndexPatternProcessor,
|
2023-01-30 09:50:01 +08:00
|
|
|
commitCrossReferencePatternProcessor,
|
2024-01-20 00:05:02 +08:00
|
|
|
hashCurrentPatternProcessor,
|
2018-02-27 15:09:18 +08:00
|
|
|
emailAddressProcessor,
|
2020-04-29 02:05:39 +08:00
|
|
|
emojiProcessor,
|
|
|
|
emojiShortCodeProcessor,
|
2018-02-27 15:09:18 +08: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.
|
|
|
|
func PostProcess(
|
2021-04-20 06:25:08 +08:00
|
|
|
ctx *RenderContext,
|
|
|
|
input io.Reader,
|
|
|
|
output io.Writer,
|
|
|
|
) error {
|
|
|
|
return postProcess(ctx, defaultProcessors, input, output)
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var commitMessageProcessors = []processor{
|
|
|
|
fullIssuePatternProcessor,
|
2021-12-12 01:21:36 +08:00
|
|
|
comparePatternProcessor,
|
2024-01-20 00:05:02 +08:00
|
|
|
fullHashPatternProcessor,
|
2019-03-27 23:37:54 +08:00
|
|
|
linkProcessor,
|
|
|
|
mentionProcessor,
|
2018-02-27 15:09:18 +08:00
|
|
|
issueIndexPatternProcessor,
|
2023-01-30 09:50:01 +08:00
|
|
|
commitCrossReferencePatternProcessor,
|
2024-01-20 00:05:02 +08:00
|
|
|
hashCurrentPatternProcessor,
|
2018-02-27 15:09:18 +08:00
|
|
|
emailAddressProcessor,
|
2020-04-29 02:05:39 +08:00
|
|
|
emojiProcessor,
|
|
|
|
emojiShortCodeProcessor,
|
2018-02-27 15:09:18 +08: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.
|
|
|
|
func RenderCommitMessage(
|
2021-04-20 06:25:08 +08:00
|
|
|
ctx *RenderContext,
|
|
|
|
content string,
|
|
|
|
) (string, error) {
|
2022-01-21 01:46:10 +08:00
|
|
|
procs := commitMessageProcessors
|
2021-04-20 06:25:08 +08:00
|
|
|
if ctx.DefaultLink != "" {
|
2018-02-27 15:09:18 +08:00
|
|
|
// we don't have to fear data races, because being
|
|
|
|
// commitMessageProcessors of fixed len and cap, every time we append
|
|
|
|
// something to it the slice is realloc+copied, so append always
|
|
|
|
// generates the slice ex-novo.
|
2021-04-20 06:25:08 +08:00
|
|
|
procs = append(procs, genDefaultLinkProcessor(ctx.DefaultLink))
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
2021-04-20 06:25:08 +08:00
|
|
|
return renderProcessString(ctx, procs, content)
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
2019-09-10 17:03:30 +08:00
|
|
|
var commitMessageSubjectProcessors = []processor{
|
|
|
|
fullIssuePatternProcessor,
|
2021-12-12 01:21:36 +08:00
|
|
|
comparePatternProcessor,
|
2024-01-20 00:05:02 +08:00
|
|
|
fullHashPatternProcessor,
|
2019-09-10 17:03:30 +08:00
|
|
|
linkProcessor,
|
|
|
|
mentionProcessor,
|
|
|
|
issueIndexPatternProcessor,
|
2023-01-30 09:50:01 +08:00
|
|
|
commitCrossReferencePatternProcessor,
|
2024-01-20 00:05:02 +08:00
|
|
|
hashCurrentPatternProcessor,
|
2020-04-29 02:05:39 +08:00
|
|
|
emojiShortCodeProcessor,
|
|
|
|
emojiProcessor,
|
|
|
|
}
|
|
|
|
|
|
|
|
var emojiProcessors = []processor{
|
|
|
|
emojiShortCodeProcessor,
|
|
|
|
emojiProcessor,
|
2019-09-10 17:03:30 +08: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.
|
|
|
|
func RenderCommitMessageSubject(
|
2021-04-20 06:25:08 +08:00
|
|
|
ctx *RenderContext,
|
|
|
|
content string,
|
|
|
|
) (string, error) {
|
2022-01-21 01:46:10 +08:00
|
|
|
procs := commitMessageSubjectProcessors
|
2021-04-20 06:25:08 +08:00
|
|
|
if ctx.DefaultLink != "" {
|
2019-09-10 17:03:30 +08:00
|
|
|
// we don't have to fear data races, because being
|
|
|
|
// commitMessageSubjectProcessors of fixed len and cap, every time we
|
|
|
|
// append something to it the slice is realloc+copied, so append always
|
|
|
|
// generates the slice ex-novo.
|
2021-04-20 06:25:08 +08:00
|
|
|
procs = append(procs, genDefaultLinkProcessor(ctx.DefaultLink))
|
2019-09-10 17:03:30 +08:00
|
|
|
}
|
2021-04-20 06:25:08 +08:00
|
|
|
return renderProcessString(ctx, procs, content)
|
2019-09-10 17:03:30 +08:00
|
|
|
}
|
|
|
|
|
2020-12-03 18:50:47 +08:00
|
|
|
// RenderIssueTitle to process title on individual issue/pull page
|
|
|
|
func RenderIssueTitle(
|
2021-04-20 06:25:08 +08:00
|
|
|
ctx *RenderContext,
|
|
|
|
title string,
|
|
|
|
) (string, error) {
|
|
|
|
return renderProcessString(ctx, []processor{
|
|
|
|
issueIndexPatternProcessor,
|
2023-01-30 09:50:01 +08:00
|
|
|
commitCrossReferencePatternProcessor,
|
2024-01-20 00:05:02 +08:00
|
|
|
hashCurrentPatternProcessor,
|
2021-04-20 06:25:08 +08:00
|
|
|
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 18:50:47 +08:00
|
|
|
}
|
2021-04-20 06:25:08 +08:00
|
|
|
return buf.String(), nil
|
2020-12-03 18:50:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 10:23:34 +08:00
|
|
|
// RenderDescriptionHTML will use similar logic as PostProcess, but will
|
|
|
|
// use a single special linkProcessor.
|
|
|
|
func RenderDescriptionHTML(
|
2021-04-20 06:25:08 +08:00
|
|
|
ctx *RenderContext,
|
|
|
|
content string,
|
|
|
|
) (string, error) {
|
|
|
|
return renderProcessString(ctx, []processor{
|
|
|
|
descriptionLinkProcessor,
|
|
|
|
emojiShortCodeProcessor,
|
|
|
|
emojiProcessor,
|
|
|
|
}, content)
|
2019-03-12 10:23:34 +08:00
|
|
|
}
|
|
|
|
|
2020-04-29 02:05:39 +08:00
|
|
|
// RenderEmoji for when we want to just process emoji and shortcodes
|
2021-07-08 19:38:13 +08:00
|
|
|
// in various places it isn't already run through the normal markdown processor
|
2020-04-29 02:05:39 +08:00
|
|
|
func RenderEmoji(
|
2023-03-06 05:59:05 +08:00
|
|
|
ctx *RenderContext,
|
2021-04-20 06:25:08 +08:00
|
|
|
content string,
|
|
|
|
) (string, error) {
|
2023-03-06 05:59:05 +08:00
|
|
|
return renderProcessString(ctx, emojiProcessors, content)
|
2020-04-29 02:05:39 +08:00
|
|
|
}
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
var (
|
|
|
|
tagCleaner = regexp.MustCompile(`<((?:/?\w+/\w+)|(?:/[\w ]+/)|(/?[hH][tT][mM][lL]\b)|(/?[hH][eE][aA][dD]\b))`)
|
|
|
|
nulCleaner = strings.NewReplacer("\000", "")
|
|
|
|
)
|
2021-03-16 07:20:05 +08:00
|
|
|
|
2021-04-20 06:25:08 +08:00
|
|
|
func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output io.Writer) error {
|
2021-06-21 06:39:12 +08:00
|
|
|
defer ctx.Cancel()
|
2021-04-20 06:25:08 +08:00
|
|
|
// FIXME: don't read all content to memory
|
2021-09-22 13:38:34 +08:00
|
|
|
rawHTML, err := io.ReadAll(input)
|
2021-04-20 06:25:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the HTML
|
2022-12-12 11:03:54 +08:00
|
|
|
node, err := html.Parse(io.MultiReader(
|
|
|
|
// prepend "<html><body>"
|
|
|
|
strings.NewReader("<html><body>"),
|
|
|
|
// Strip out nuls - they're always invalid
|
|
|
|
bytes.NewReader(tagCleaner.ReplaceAll([]byte(nulCleaner.Replace(string(rawHTML))), []byte("<$1"))),
|
|
|
|
// close the tags
|
|
|
|
strings.NewReader("</body></html>"),
|
|
|
|
))
|
2018-02-27 15:09:18 +08:00
|
|
|
if err != nil {
|
2021-04-20 06:25:08 +08:00
|
|
|
return &postProcessError{"invalid HTML", err}
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
2021-06-22 06:12:22 +08:00
|
|
|
if node.Type == html.DocumentNode {
|
|
|
|
node = node.FirstChild
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
2024-01-15 16:49:24 +08:00
|
|
|
visitNode(ctx, procs, node)
|
2021-01-20 23:10:50 +08:00
|
|
|
|
2021-06-22 06:12:22 +08: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 23:10:50 +08:00
|
|
|
}
|
2021-06-22 06:12:22 +08:00
|
|
|
}
|
|
|
|
if node != nil {
|
2021-01-20 23:10:50 +08: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 15:09:18 +08:00
|
|
|
// Render everything to buf.
|
2021-04-20 06:25:08 +08:00
|
|
|
for _, node := range newNodes {
|
2022-03-18 02:04:36 +08:00
|
|
|
if err := html.Render(output, node); err != nil {
|
2021-04-20 06:25:08 +08:00
|
|
|
return &postProcessError{"error rendering processed HTML", err}
|
2017-09-17 01:17:57 +08:00
|
|
|
}
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
2021-04-20 06:25:08 +08:00
|
|
|
return nil
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
2024-06-04 20:19:41 +08:00
|
|
|
func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Node {
|
2023-01-31 13:21:29 +08:00
|
|
|
// Add user-content- to IDs and "#" links if they don't already have them
|
2019-12-24 06:38:50 +08:00
|
|
|
for idx, attr := range node.Attr {
|
2023-01-31 13:21:29 +08:00
|
|
|
val := strings.TrimPrefix(attr.Val, "#")
|
|
|
|
notHasPrefix := !(strings.HasPrefix(val, "user-content-") || blackfridayExtRegex.MatchString(val))
|
|
|
|
|
|
|
|
if attr.Key == "id" && notHasPrefix {
|
2019-12-24 06:38:50 +08:00
|
|
|
node.Attr[idx].Val = "user-content-" + attr.Val
|
|
|
|
}
|
2020-04-29 02:05:39 +08:00
|
|
|
|
2023-01-31 13:21:29 +08:00
|
|
|
if attr.Key == "href" && strings.HasPrefix(attr.Val, "#") && notHasPrefix {
|
|
|
|
node.Attr[idx].Val = "#user-content-" + val
|
|
|
|
}
|
|
|
|
|
2020-04-29 02:05:39 +08:00
|
|
|
if attr.Key == "class" && attr.Val == "emoji" {
|
2024-01-15 16:49:24 +08:00
|
|
|
procs = nil
|
2020-04-29 02:05:39 +08:00
|
|
|
}
|
2019-12-24 06:38:50 +08:00
|
|
|
}
|
2020-04-29 02:05:39 +08:00
|
|
|
|
2018-02-27 15:09:18 +08:00
|
|
|
switch node.Type {
|
|
|
|
case html.TextNode:
|
2024-06-18 06:56:45 +08:00
|
|
|
processTextNodes(ctx, procs, node)
|
2018-02-27 15:09:18 +08:00
|
|
|
case html.ElementNode:
|
2024-06-22 02:23:54 +08:00
|
|
|
if node.Data == "code" || node.Data == "pre" {
|
|
|
|
// ignore code and pre nodes
|
|
|
|
return node.NextSibling
|
|
|
|
} else if node.Data == "img" {
|
|
|
|
return visitNodeImg(ctx, node)
|
|
|
|
} else if node.Data == "video" {
|
|
|
|
return visitNodeVideo(ctx, node)
|
2020-02-28 12:16:05 +08:00
|
|
|
} else if node.Data == "a" {
|
2021-12-16 07:49:12 +08:00
|
|
|
// Restrict text in links to emojis
|
2024-01-15 16:49:24 +08:00
|
|
|
procs = emojiProcessors
|
2020-04-24 21:22:36 +08:00
|
|
|
} else if node.Data == "i" {
|
|
|
|
for _, attr := range node.Attr {
|
|
|
|
if attr.Key != "class" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
classes := strings.Split(attr.Val, " ")
|
|
|
|
for i, class := range classes {
|
|
|
|
if class == "icon" {
|
|
|
|
classes[0], classes[i] = classes[i], classes[0]
|
|
|
|
attr.Val = strings.Join(classes, " ")
|
|
|
|
|
|
|
|
// Remove all children of icons
|
|
|
|
child := node.FirstChild
|
|
|
|
for child != nil {
|
|
|
|
node.RemoveChild(child)
|
|
|
|
child = node.FirstChild
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 01:17:57 +08:00
|
|
|
}
|
2024-06-04 20:19:41 +08:00
|
|
|
for n := node.FirstChild; n != nil; {
|
|
|
|
n = visitNode(ctx, procs, n)
|
2017-09-17 01:17:57 +08:00
|
|
|
}
|
2024-06-18 06:56:45 +08:00
|
|
|
default:
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
2024-06-04 20:19:41 +08:00
|
|
|
return node.NextSibling
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
|
|
|
|
2024-06-18 06:56:45 +08:00
|
|
|
// processTextNodes runs the passed node through various processors, in order to handle
|
2018-02-27 15:09:18 +08:00
|
|
|
// all kinds of special links handled by the post-processing.
|
2024-06-18 06:56:45 +08:00
|
|
|
func processTextNodes(ctx *RenderContext, procs []processor, node *html.Node) {
|
|
|
|
for _, p := range procs {
|
|
|
|
p(ctx, node)
|
2018-02-27 15:09:18 +08:00
|
|
|
}
|
2017-09-17 01:17:57 +08:00
|
|
|
}
|
|
|
|
|
2019-10-14 06:29:10 +08:00
|
|
|
// createKeyword() renders a highlighted version of an action keyword
|
|
|
|
func createKeyword(content string) *html.Node {
|
|
|
|
span := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.Span.String(),
|
|
|
|
Attr: []html.Attribute{},
|
|
|
|
}
|
|
|
|
span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: keywordClass})
|
|
|
|
|
|
|
|
text := &html.Node{
|
|
|
|
Type: html.TextNode,
|
|
|
|
Data: content,
|
|
|
|
}
|
|
|
|
span.AppendChild(text)
|
|
|
|
|
|
|
|
return span
|
|
|
|
}
|
|
|
|
|
2019-09-10 17:03:30 +08:00
|
|
|
func createLink(href, content, class string) *html.Node {
|
2019-04-09 11:18:48 +08:00
|
|
|
a := &html.Node{
|
|
|
|
Type: html.ElementNode,
|
|
|
|
Data: atom.A.String(),
|
|
|
|
Attr: []html.Attribute{{Key: "href", Val: href}},
|
|
|
|
}
|
2019-09-10 17:03:30 +08:00
|
|
|
|
|
|
|
if class != "" {
|
|
|
|
a.Attr = append(a.Attr, html.Attribute{Key: "class", Val: class})
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:18:48 +08:00
|
|
|
text := &html.Node{
|
2018-02-27 15:09:18 +08:00
|
|
|
Type: html.TextNode,
|
|
|
|
Data: content,
|
|
|
|
}
|
2019-04-09 11:18:48 +08:00
|
|
|
|
|
|
|
a.AppendChild(text)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-10-14 06:29:10 +08:00
|
|
|
// replaceContent takes text node, and in its content it replaces a section of
|
|
|
|
// it with the specified newNode.
|
2018-02-27 15:09:18 +08:00
|
|
|
func replaceContent(node *html.Node, i, j int, newNode *html.Node) {
|
2019-10-14 06:29:10 +08: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 15:09:18 +08: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 06:29:10 +08:00
|
|
|
for _, n := range newNodes {
|
|
|
|
node.Parent.InsertBefore(n, nextSibling)
|
|
|
|
}
|
2018-02-27 15:09:18 +08:00
|
|
|
if after != "" {
|
|
|
|
node.Parent.InsertBefore(&html.Node{
|
|
|
|
Type: html.TextNode,
|
|
|
|
Data: after,
|
|
|
|
}, nextSibling)
|
2017-09-17 01:17:57 +08:00
|
|
|
}
|
|
|
|
}
|