Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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