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 21KB

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