You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

html.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package markup
  5. import (
  6. "bytes"
  7. "net/url"
  8. "path"
  9. "path/filepath"
  10. "regexp"
  11. "strings"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/references"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/util"
  18. "github.com/unknwon/com"
  19. "golang.org/x/net/html"
  20. "golang.org/x/net/html/atom"
  21. "mvdan.cc/xurls/v2"
  22. )
  23. // Issue name styles
  24. const (
  25. IssueNameStyleNumeric = "numeric"
  26. IssueNameStyleAlphanumeric = "alphanumeric"
  27. )
  28. var (
  29. // NOTE: All below regex matching do not perform any extra validation.
  30. // Thus a link is produced even if the linked entity does not exist.
  31. // While fast, this is also incorrect and lead to false positives.
  32. // TODO: fix invalid linking issue
  33. // sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
  34. // Although SHA1 hashes are 40 chars long, the regex matches the hash from 7 to 40 chars in length
  35. // so that abbreviated hash links can be used as well. This matches git and github useability.
  36. sha1CurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,40})(?:\s|$|\)|\]|\.(\s|$))`)
  37. // shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
  38. shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
  39. // anySHA1Pattern allows to split url containing SHA into parts
  40. anySHA1Pattern = regexp.MustCompile(`https?://(?:\S+/){4}([0-9a-f]{40})(/[^#\s]+)?(#\S+)?`)
  41. validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
  42. // While this email regex is definitely not perfect and I'm sure you can come up
  43. // with edge cases, it is still accepted by the CommonMark specification, as
  44. // well as the HTML5 spec:
  45. // http://spec.commonmark.org/0.28/#email-address
  46. // https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail)
  47. 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|$))")
  48. linkRegex, _ = xurls.StrictMatchingScheme("https?://")
  49. )
  50. // CSS class for action keywords (e.g. "closes: #1")
  51. const keywordClass = "issue-keyword"
  52. // regexp for full links to issues/pulls
  53. var issueFullPattern *regexp.Regexp
  54. // IsLink reports whether link fits valid format.
  55. func IsLink(link []byte) bool {
  56. return isLink(link)
  57. }
  58. // isLink reports whether link fits valid format.
  59. func isLink(link []byte) bool {
  60. return validLinksPattern.Match(link)
  61. }
  62. func isLinkStr(link string) bool {
  63. return validLinksPattern.MatchString(link)
  64. }
  65. func getIssueFullPattern() *regexp.Regexp {
  66. if issueFullPattern == nil {
  67. appURL := setting.AppURL
  68. if len(appURL) > 0 && appURL[len(appURL)-1] != '/' {
  69. appURL += "/"
  70. }
  71. issueFullPattern = regexp.MustCompile(appURL +
  72. `\w+/\w+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#]\S+.(\S+)?)?\b`)
  73. }
  74. return issueFullPattern
  75. }
  76. // IsSameDomain checks if given url string has the same hostname as current Gitea instance
  77. func IsSameDomain(s string) bool {
  78. if strings.HasPrefix(s, "/") {
  79. return true
  80. }
  81. if uapp, err := url.Parse(setting.AppURL); err == nil {
  82. if u, err := url.Parse(s); err == nil {
  83. return u.Host == uapp.Host
  84. }
  85. return false
  86. }
  87. return false
  88. }
  89. type postProcessError struct {
  90. context string
  91. err error
  92. }
  93. func (p *postProcessError) Error() string {
  94. return "PostProcess: " + p.context + ", " + p.err.Error()
  95. }
  96. type processor func(ctx *postProcessCtx, node *html.Node)
  97. var defaultProcessors = []processor{
  98. fullIssuePatternProcessor,
  99. fullSha1PatternProcessor,
  100. shortLinkProcessor,
  101. linkProcessor,
  102. mentionProcessor,
  103. issueIndexPatternProcessor,
  104. sha1CurrentPatternProcessor,
  105. emailAddressProcessor,
  106. }
  107. type postProcessCtx struct {
  108. metas map[string]string
  109. urlPrefix string
  110. isWikiMarkdown bool
  111. // processors used by this context.
  112. procs []processor
  113. }
  114. // PostProcess does the final required transformations to the passed raw HTML
  115. // data, and ensures its validity. Transformations include: replacing links and
  116. // emails with HTML links, parsing shortlinks in the format of [[Link]], like
  117. // MediaWiki, linking issues in the format #ID, and mentions in the format
  118. // @user, and others.
  119. func PostProcess(
  120. rawHTML []byte,
  121. urlPrefix string,
  122. metas map[string]string,
  123. isWikiMarkdown bool,
  124. ) ([]byte, error) {
  125. // create the context from the parameters
  126. ctx := &postProcessCtx{
  127. metas: metas,
  128. urlPrefix: urlPrefix,
  129. isWikiMarkdown: isWikiMarkdown,
  130. procs: defaultProcessors,
  131. }
  132. return ctx.postProcess(rawHTML)
  133. }
  134. var commitMessageProcessors = []processor{
  135. fullIssuePatternProcessor,
  136. fullSha1PatternProcessor,
  137. linkProcessor,
  138. mentionProcessor,
  139. issueIndexPatternProcessor,
  140. sha1CurrentPatternProcessor,
  141. emailAddressProcessor,
  142. }
  143. // RenderCommitMessage will use the same logic as PostProcess, but will disable
  144. // the shortLinkProcessor and will add a defaultLinkProcessor if defaultLink is
  145. // set, which changes every text node into a link to the passed default link.
  146. func RenderCommitMessage(
  147. rawHTML []byte,
  148. urlPrefix, defaultLink string,
  149. metas map[string]string,
  150. ) ([]byte, error) {
  151. ctx := &postProcessCtx{
  152. metas: metas,
  153. urlPrefix: urlPrefix,
  154. procs: commitMessageProcessors,
  155. }
  156. if defaultLink != "" {
  157. // we don't have to fear data races, because being
  158. // commitMessageProcessors of fixed len and cap, every time we append
  159. // something to it the slice is realloc+copied, so append always
  160. // generates the slice ex-novo.
  161. ctx.procs = append(ctx.procs, genDefaultLinkProcessor(defaultLink))
  162. }
  163. return ctx.postProcess(rawHTML)
  164. }
  165. var commitMessageSubjectProcessors = []processor{
  166. fullIssuePatternProcessor,
  167. fullSha1PatternProcessor,
  168. linkProcessor,
  169. mentionProcessor,
  170. issueIndexPatternProcessor,
  171. sha1CurrentPatternProcessor,
  172. }
  173. // RenderCommitMessageSubject will use the same logic as PostProcess and
  174. // RenderCommitMessage, but will disable the shortLinkProcessor and
  175. // emailAddressProcessor, will add a defaultLinkProcessor if defaultLink is set,
  176. // which changes every text node into a link to the passed default link.
  177. func RenderCommitMessageSubject(
  178. rawHTML []byte,
  179. urlPrefix, defaultLink string,
  180. metas map[string]string,
  181. ) ([]byte, error) {
  182. ctx := &postProcessCtx{
  183. metas: metas,
  184. urlPrefix: urlPrefix,
  185. procs: commitMessageSubjectProcessors,
  186. }
  187. if defaultLink != "" {
  188. // we don't have to fear data races, because being
  189. // commitMessageSubjectProcessors of fixed len and cap, every time we
  190. // append something to it the slice is realloc+copied, so append always
  191. // generates the slice ex-novo.
  192. ctx.procs = append(ctx.procs, genDefaultLinkProcessor(defaultLink))
  193. }
  194. return ctx.postProcess(rawHTML)
  195. }
  196. // RenderDescriptionHTML will use similar logic as PostProcess, but will
  197. // use a single special linkProcessor.
  198. func RenderDescriptionHTML(
  199. rawHTML []byte,
  200. urlPrefix string,
  201. metas map[string]string,
  202. ) ([]byte, error) {
  203. ctx := &postProcessCtx{
  204. metas: metas,
  205. urlPrefix: urlPrefix,
  206. procs: []processor{
  207. descriptionLinkProcessor,
  208. },
  209. }
  210. return ctx.postProcess(rawHTML)
  211. }
  212. var byteBodyTag = []byte("<body>")
  213. var byteBodyTagClosing = []byte("</body>")
  214. func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
  215. if ctx.procs == nil {
  216. ctx.procs = defaultProcessors
  217. }
  218. // give a generous extra 50 bytes
  219. res := make([]byte, 0, len(rawHTML)+50)
  220. res = append(res, byteBodyTag...)
  221. res = append(res, rawHTML...)
  222. res = append(res, byteBodyTagClosing...)
  223. // parse the HTML
  224. nodes, err := html.ParseFragment(bytes.NewReader(res), nil)
  225. if err != nil {
  226. return nil, &postProcessError{"invalid HTML", err}
  227. }
  228. for _, node := range nodes {
  229. ctx.visitNode(node)
  230. }
  231. // Create buffer in which the data will be placed again. We know that the
  232. // length will be at least that of res; to spare a few alloc+copy, we
  233. // reuse res, resetting its length to 0.
  234. buf := bytes.NewBuffer(res[:0])
  235. // Render everything to buf.
  236. for _, node := range nodes {
  237. err = html.Render(buf, node)
  238. if err != nil {
  239. return nil, &postProcessError{"error rendering processed HTML", err}
  240. }
  241. }
  242. // remove initial parts - because Render creates a whole HTML page.
  243. res = buf.Bytes()
  244. res = res[bytes.Index(res, byteBodyTag)+len(byteBodyTag) : bytes.LastIndex(res, byteBodyTagClosing)]
  245. // Everything done successfully, return parsed data.
  246. return res, nil
  247. }
  248. func (ctx *postProcessCtx) visitNode(node *html.Node) {
  249. // We ignore code, pre and already generated links.
  250. switch node.Type {
  251. case html.TextNode:
  252. ctx.textNode(node)
  253. case html.ElementNode:
  254. if node.Data == "a" || node.Data == "code" || node.Data == "pre" {
  255. return
  256. }
  257. for n := node.FirstChild; n != nil; n = n.NextSibling {
  258. ctx.visitNode(n)
  259. }
  260. }
  261. // ignore everything else
  262. }
  263. // textNode runs the passed node through various processors, in order to handle
  264. // all kinds of special links handled by the post-processing.
  265. func (ctx *postProcessCtx) textNode(node *html.Node) {
  266. for _, processor := range ctx.procs {
  267. processor(ctx, node)
  268. }
  269. }
  270. // createKeyword() renders a highlighted version of an action keyword
  271. func createKeyword(content string) *html.Node {
  272. span := &html.Node{
  273. Type: html.ElementNode,
  274. Data: atom.Span.String(),
  275. Attr: []html.Attribute{},
  276. }
  277. span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: keywordClass})
  278. text := &html.Node{
  279. Type: html.TextNode,
  280. Data: content,
  281. }
  282. span.AppendChild(text)
  283. return span
  284. }
  285. func createLink(href, content, class string) *html.Node {
  286. a := &html.Node{
  287. Type: html.ElementNode,
  288. Data: atom.A.String(),
  289. Attr: []html.Attribute{{Key: "href", Val: href}},
  290. }
  291. if class != "" {
  292. a.Attr = append(a.Attr, html.Attribute{Key: "class", Val: class})
  293. }
  294. text := &html.Node{
  295. Type: html.TextNode,
  296. Data: content,
  297. }
  298. a.AppendChild(text)
  299. return a
  300. }
  301. func createCodeLink(href, content, class string) *html.Node {
  302. a := &html.Node{
  303. Type: html.ElementNode,
  304. Data: atom.A.String(),
  305. Attr: []html.Attribute{{Key: "href", Val: href}},
  306. }
  307. if class != "" {
  308. a.Attr = append(a.Attr, html.Attribute{Key: "class", Val: class})
  309. }
  310. text := &html.Node{
  311. Type: html.TextNode,
  312. Data: content,
  313. }
  314. code := &html.Node{
  315. Type: html.ElementNode,
  316. Data: atom.Code.String(),
  317. Attr: []html.Attribute{{Key: "class", Val: "nohighlight"}},
  318. }
  319. code.AppendChild(text)
  320. a.AppendChild(code)
  321. return a
  322. }
  323. // replaceContent takes text node, and in its content it replaces a section of
  324. // it with the specified newNode.
  325. func replaceContent(node *html.Node, i, j int, newNode *html.Node) {
  326. replaceContentList(node, i, j, []*html.Node{newNode})
  327. }
  328. // replaceContentList takes text node, and in its content it replaces a section of
  329. // it with the specified newNodes. An example to visualize how this can work can
  330. // be found here: https://play.golang.org/p/5zP8NnHZ03s
  331. func replaceContentList(node *html.Node, i, j int, newNodes []*html.Node) {
  332. // get the data before and after the match
  333. before := node.Data[:i]
  334. after := node.Data[j:]
  335. // Replace in the current node the text, so that it is only what it is
  336. // supposed to have.
  337. node.Data = before
  338. // Get the current next sibling, before which we place the replaced data,
  339. // and after that we place the new text node.
  340. nextSibling := node.NextSibling
  341. for _, n := range newNodes {
  342. node.Parent.InsertBefore(n, nextSibling)
  343. }
  344. if after != "" {
  345. node.Parent.InsertBefore(&html.Node{
  346. Type: html.TextNode,
  347. Data: after,
  348. }, nextSibling)
  349. }
  350. }
  351. func mentionProcessor(_ *postProcessCtx, node *html.Node) {
  352. // We replace only the first mention; other mentions will be addressed later
  353. found, loc := references.FindFirstMentionBytes([]byte(node.Data))
  354. if !found {
  355. return
  356. }
  357. mention := node.Data[loc.Start:loc.End]
  358. replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mention[1:]), mention, "mention"))
  359. }
  360. func shortLinkProcessor(ctx *postProcessCtx, node *html.Node) {
  361. shortLinkProcessorFull(ctx, node, false)
  362. }
  363. func shortLinkProcessorFull(ctx *postProcessCtx, node *html.Node, noLink bool) {
  364. m := shortLinkPattern.FindStringSubmatchIndex(node.Data)
  365. if m == nil {
  366. return
  367. }
  368. content := node.Data[m[2]:m[3]]
  369. tail := node.Data[m[4]:m[5]]
  370. props := make(map[string]string)
  371. // MediaWiki uses [[link|text]], while GitHub uses [[text|link]]
  372. // It makes page handling terrible, but we prefer GitHub syntax
  373. // And fall back to MediaWiki only when it is obvious from the look
  374. // Of text and link contents
  375. sl := strings.Split(content, "|")
  376. for _, v := range sl {
  377. if equalPos := strings.IndexByte(v, '='); equalPos == -1 {
  378. // There is no equal in this argument; this is a mandatory arg
  379. if props["name"] == "" {
  380. if isLinkStr(v) {
  381. // If we clearly see it is a link, we save it so
  382. // But first we need to ensure, that if both mandatory args provided
  383. // look like links, we stick to GitHub syntax
  384. if props["link"] != "" {
  385. props["name"] = props["link"]
  386. }
  387. props["link"] = strings.TrimSpace(v)
  388. } else {
  389. props["name"] = v
  390. }
  391. } else {
  392. props["link"] = strings.TrimSpace(v)
  393. }
  394. } else {
  395. // There is an equal; optional argument.
  396. sep := strings.IndexByte(v, '=')
  397. key, val := v[:sep], html.UnescapeString(v[sep+1:])
  398. // When parsing HTML, x/net/html will change all quotes which are
  399. // not used for syntax into UTF-8 quotes. So checking val[0] won't
  400. // be enough, since that only checks a single byte.
  401. if (strings.HasPrefix(val, "“") && strings.HasSuffix(val, "”")) ||
  402. (strings.HasPrefix(val, "‘") && strings.HasSuffix(val, "’")) {
  403. const lenQuote = len("‘")
  404. val = val[lenQuote : len(val)-lenQuote]
  405. }
  406. props[key] = val
  407. }
  408. }
  409. var name, link string
  410. if props["link"] != "" {
  411. link = props["link"]
  412. } else if props["name"] != "" {
  413. link = props["name"]
  414. }
  415. if props["title"] != "" {
  416. name = props["title"]
  417. } else if props["name"] != "" {
  418. name = props["name"]
  419. } else {
  420. name = link
  421. }
  422. name += tail
  423. image := false
  424. switch ext := filepath.Ext(link); ext {
  425. // fast path: empty string, ignore
  426. case "":
  427. break
  428. case ".jpg", ".jpeg", ".png", ".tif", ".tiff", ".webp", ".gif", ".bmp", ".ico", ".svg":
  429. image = true
  430. }
  431. childNode := &html.Node{}
  432. linkNode := &html.Node{
  433. FirstChild: childNode,
  434. LastChild: childNode,
  435. Type: html.ElementNode,
  436. Data: "a",
  437. DataAtom: atom.A,
  438. }
  439. childNode.Parent = linkNode
  440. absoluteLink := isLinkStr(link)
  441. if !absoluteLink {
  442. if image {
  443. link = strings.Replace(link, " ", "+", -1)
  444. } else {
  445. link = strings.Replace(link, " ", "-", -1)
  446. }
  447. if !strings.Contains(link, "/") {
  448. link = url.PathEscape(link)
  449. }
  450. }
  451. urlPrefix := ctx.urlPrefix
  452. if image {
  453. if !absoluteLink {
  454. if IsSameDomain(urlPrefix) {
  455. urlPrefix = strings.Replace(urlPrefix, "/src/", "/raw/", 1)
  456. }
  457. if ctx.isWikiMarkdown {
  458. link = util.URLJoin("wiki", "raw", link)
  459. }
  460. link = util.URLJoin(urlPrefix, link)
  461. }
  462. title := props["title"]
  463. if title == "" {
  464. title = props["alt"]
  465. }
  466. if title == "" {
  467. title = path.Base(name)
  468. }
  469. alt := props["alt"]
  470. if alt == "" {
  471. alt = name
  472. }
  473. // make the childNode an image - if we can, we also place the alt
  474. childNode.Type = html.ElementNode
  475. childNode.Data = "img"
  476. childNode.DataAtom = atom.Img
  477. childNode.Attr = []html.Attribute{
  478. {Key: "src", Val: link},
  479. {Key: "title", Val: title},
  480. {Key: "alt", Val: alt},
  481. }
  482. if alt == "" {
  483. childNode.Attr = childNode.Attr[:2]
  484. }
  485. } else {
  486. if !absoluteLink {
  487. if ctx.isWikiMarkdown {
  488. link = util.URLJoin("wiki", link)
  489. }
  490. link = util.URLJoin(urlPrefix, link)
  491. }
  492. childNode.Type = html.TextNode
  493. childNode.Data = name
  494. }
  495. if noLink {
  496. linkNode = childNode
  497. } else {
  498. linkNode.Attr = []html.Attribute{{Key: "href", Val: link}}
  499. }
  500. replaceContent(node, m[0], m[1], linkNode)
  501. }
  502. func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) {
  503. if ctx.metas == nil {
  504. return
  505. }
  506. m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
  507. if m == nil {
  508. return
  509. }
  510. link := node.Data[m[0]:m[1]]
  511. id := "#" + node.Data[m[2]:m[3]]
  512. // extract repo and org name from matched link like
  513. // http://localhost:3000/gituser/myrepo/issues/1
  514. linkParts := strings.Split(path.Clean(link), "/")
  515. matchOrg := linkParts[len(linkParts)-4]
  516. matchRepo := linkParts[len(linkParts)-3]
  517. if matchOrg == ctx.metas["user"] && matchRepo == ctx.metas["repo"] {
  518. // TODO if m[4]:m[5] is not nil, then link is to a comment,
  519. // and we should indicate that in the text somehow
  520. replaceContent(node, m[0], m[1], createLink(link, id, "issue"))
  521. } else {
  522. orgRepoID := matchOrg + "/" + matchRepo + id
  523. replaceContent(node, m[0], m[1], createLink(link, orgRepoID, "issue"))
  524. }
  525. }
  526. func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
  527. if ctx.metas == nil {
  528. return
  529. }
  530. var (
  531. found bool
  532. ref *references.RenderizableReference
  533. )
  534. if ctx.metas["style"] == IssueNameStyleAlphanumeric {
  535. found, ref = references.FindRenderizableReferenceAlphanumeric(node.Data)
  536. } else {
  537. found, ref = references.FindRenderizableReferenceNumeric(node.Data)
  538. }
  539. if !found {
  540. return
  541. }
  542. var link *html.Node
  543. reftext := node.Data[ref.RefLocation.Start:ref.RefLocation.End]
  544. if _, ok := ctx.metas["format"]; ok {
  545. ctx.metas["index"] = ref.Issue
  546. link = createLink(com.Expand(ctx.metas["format"], ctx.metas), reftext, "issue")
  547. } else if ref.Owner == "" {
  548. link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", ref.Issue), reftext, "issue")
  549. } else {
  550. link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, "issues", ref.Issue), reftext, "issue")
  551. }
  552. if ref.Action == references.XRefActionNone {
  553. replaceContent(node, ref.RefLocation.Start, ref.RefLocation.End, link)
  554. return
  555. }
  556. // Decorate action keywords
  557. keyword := createKeyword(node.Data[ref.ActionLocation.Start:ref.ActionLocation.End])
  558. spaces := &html.Node{
  559. Type: html.TextNode,
  560. Data: node.Data[ref.ActionLocation.End:ref.RefLocation.Start],
  561. }
  562. replaceContentList(node, ref.ActionLocation.Start, ref.RefLocation.End, []*html.Node{keyword, spaces, link})
  563. }
  564. // fullSha1PatternProcessor renders SHA containing URLs
  565. func fullSha1PatternProcessor(ctx *postProcessCtx, node *html.Node) {
  566. if ctx.metas == nil {
  567. return
  568. }
  569. m := anySHA1Pattern.FindStringSubmatchIndex(node.Data)
  570. if m == nil {
  571. return
  572. }
  573. urlFull := node.Data[m[0]:m[1]]
  574. text := base.ShortSha(node.Data[m[2]:m[3]])
  575. // 3rd capture group matches a optional path
  576. subpath := ""
  577. if m[5] > 0 {
  578. subpath = node.Data[m[4]:m[5]]
  579. }
  580. // 4th capture group matches a optional url hash
  581. hash := ""
  582. if m[7] > 0 {
  583. hash = node.Data[m[6]:m[7]][1:]
  584. }
  585. start := m[0]
  586. end := m[1]
  587. // If url ends in '.', it's very likely that it is not part of the
  588. // actual url but used to finish a sentence.
  589. if strings.HasSuffix(urlFull, ".") {
  590. end--
  591. urlFull = urlFull[:len(urlFull)-1]
  592. if hash != "" {
  593. hash = hash[:len(hash)-1]
  594. } else if subpath != "" {
  595. subpath = subpath[:len(subpath)-1]
  596. }
  597. }
  598. if subpath != "" {
  599. text += subpath
  600. }
  601. if hash != "" {
  602. text += " (" + hash + ")"
  603. }
  604. replaceContent(node, start, end, createCodeLink(urlFull, text, "commit"))
  605. }
  606. // sha1CurrentPatternProcessor renders SHA1 strings to corresponding links that
  607. // are assumed to be in the same repository.
  608. func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
  609. if ctx.metas == nil || ctx.metas["user"] == "" || ctx.metas["repo"] == "" || ctx.metas["repoPath"] == "" {
  610. return
  611. }
  612. m := sha1CurrentPattern.FindStringSubmatchIndex(node.Data)
  613. if m == nil {
  614. return
  615. }
  616. hash := node.Data[m[2]:m[3]]
  617. // The regex does not lie, it matches the hash pattern.
  618. // However, a regex cannot know if a hash actually exists or not.
  619. // We could assume that a SHA1 hash should probably contain alphas AND numerics
  620. // but that is not always the case.
  621. // Although unlikely, deadbeef and 1234567 are valid short forms of SHA1 hash
  622. // as used by git and github for linking and thus we have to do similar.
  623. // Because of this, we check to make sure that a matched hash is actually
  624. // a commit in the repository before making it a link.
  625. if _, err := git.NewCommand("rev-parse", "--verify", hash).RunInDirBytes(ctx.metas["repoPath"]); err != nil {
  626. if !strings.Contains(err.Error(), "fatal: Needed a single revision") {
  627. log.Debug("sha1CurrentPatternProcessor git rev-parse: %v", err)
  628. }
  629. return
  630. }
  631. replaceContent(node, m[2], m[3],
  632. createCodeLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "commit", hash), base.ShortSha(hash), "commit"))
  633. }
  634. // emailAddressProcessor replaces raw email addresses with a mailto: link.
  635. func emailAddressProcessor(ctx *postProcessCtx, node *html.Node) {
  636. m := emailRegex.FindStringSubmatchIndex(node.Data)
  637. if m == nil {
  638. return
  639. }
  640. mail := node.Data[m[2]:m[3]]
  641. replaceContent(node, m[2], m[3], createLink("mailto:"+mail, mail, "mailto"))
  642. }
  643. // linkProcessor creates links for any HTTP or HTTPS URL not captured by
  644. // markdown.
  645. func linkProcessor(ctx *postProcessCtx, node *html.Node) {
  646. m := linkRegex.FindStringIndex(node.Data)
  647. if m == nil {
  648. return
  649. }
  650. uri := node.Data[m[0]:m[1]]
  651. replaceContent(node, m[0], m[1], createLink(uri, uri, "link"))
  652. }
  653. func genDefaultLinkProcessor(defaultLink string) processor {
  654. return func(ctx *postProcessCtx, node *html.Node) {
  655. ch := &html.Node{
  656. Parent: node,
  657. Type: html.TextNode,
  658. Data: node.Data,
  659. }
  660. node.Type = html.ElementNode
  661. node.Data = "a"
  662. node.DataAtom = atom.A
  663. node.Attr = []html.Attribute{
  664. {Key: "href", Val: defaultLink},
  665. {Key: "class", Val: "default-link"},
  666. }
  667. node.FirstChild, node.LastChild = ch, ch
  668. }
  669. }
  670. // descriptionLinkProcessor creates links for DescriptionHTML
  671. func descriptionLinkProcessor(ctx *postProcessCtx, node *html.Node) {
  672. m := linkRegex.FindStringIndex(node.Data)
  673. if m == nil {
  674. return
  675. }
  676. uri := node.Data[m[0]:m[1]]
  677. replaceContent(node, m[0], m[1], createDescriptionLink(uri, uri))
  678. }
  679. func createDescriptionLink(href, content string) *html.Node {
  680. textNode := &html.Node{
  681. Type: html.TextNode,
  682. Data: content,
  683. }
  684. linkNode := &html.Node{
  685. FirstChild: textNode,
  686. LastChild: textNode,
  687. Type: html.ElementNode,
  688. Data: "a",
  689. DataAtom: atom.A,
  690. Attr: []html.Attribute{
  691. {Key: "href", Val: href},
  692. {Key: "target", Val: "_blank"},
  693. {Key: "rel", Val: "noopener noreferrer"},
  694. },
  695. }
  696. textNode.Parent = linkNode
  697. return linkNode
  698. }