本站源代码
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

822 líneas
23KB

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