|
54 | 54 | shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
|
55 | 55 |
|
56 | 56 | // anySHA1Pattern allows to split url containing SHA into parts
|
57 |
| - anySHA1Pattern = regexp.MustCompile(`https?://(?:\S+/){4}([0-9a-f]{40})/?([^#\s]+)?(?:#(\S+))?`) |
| 57 | + anySHA1Pattern = regexp.MustCompile(`https?://(?:\S+/){4}([0-9a-f]{40})(/[^#\s]+)?(#\S+)?`) |
58 | 58 |
|
59 | 59 | validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
|
60 | 60 |
|
@@ -594,31 +594,46 @@ func fullSha1PatternProcessor(ctx *postProcessCtx, node *html.Node) {
|
594 | 594 | if m == nil {
|
595 | 595 | return
|
596 | 596 | }
|
597 |
| - // take out what's relevant |
| 597 | + |
598 | 598 | urlFull := node.Data[m[0]:m[1]]
|
599 |
| - hash := node.Data[m[2]:m[3]] |
| 599 | + text := base.ShortSha(node.Data[m[2]:m[3]]) |
600 | 600 |
|
601 |
| - var subtree, line string |
| 601 | + // 3rd capture group matches a optional path |
| 602 | + subpath := "" |
| 603 | + if m[5] > 0 { |
| 604 | + subpath = node.Data[m[4]:m[5]] |
| 605 | + } |
602 | 606 |
|
603 |
| - // optional, we do them depending on the length. |
| 607 | + // 4th capture group matches a optional url hash |
| 608 | + hash := "" |
604 | 609 | if m[7] > 0 {
|
605 |
| - line = node.Data[m[6]:m[7]] |
| 610 | + hash = node.Data[m[6]:m[7]][1:] |
606 | 611 | }
|
607 |
| - if m[5] > 0 { |
608 |
| - subtree = node.Data[m[4]:m[5]] |
| 612 | + |
| 613 | + start := m[0] |
| 614 | + end := m[1] |
| 615 | + |
| 616 | + // If url ends in '.', it's very likely that it is not part of the |
| 617 | + // actual url but used to finish a sentence. |
| 618 | + if strings.HasSuffix(urlFull, ".") { |
| 619 | + end-- |
| 620 | + urlFull = urlFull[:len(urlFull)-1] |
| 621 | + if hash != "" { |
| 622 | + hash = hash[:len(hash)-1] |
| 623 | + } else if subpath != "" { |
| 624 | + subpath = subpath[:len(subpath)-1] |
| 625 | + } |
609 | 626 | }
|
610 | 627 |
|
611 |
| - text := base.ShortSha(hash) |
612 |
| - if subtree != "" { |
613 |
| - text += "/" + subtree |
| 628 | + if subpath != "" { |
| 629 | + text += subpath |
614 | 630 | }
|
615 |
| - if line != "" { |
616 |
| - text += " (" |
617 |
| - text += line |
618 |
| - text += ")" |
| 631 | + |
| 632 | + if hash != "" { |
| 633 | + text += " (" + hash + ")" |
619 | 634 | }
|
620 | 635 |
|
621 |
| - replaceContent(node, m[0], m[1], createLink(urlFull, text)) |
| 636 | + replaceContent(node, start, end, createLink(urlFull, text)) |
622 | 637 | }
|
623 | 638 |
|
624 | 639 | // sha1CurrentPatternProcessor renders SHA1 strings to corresponding links that
|
|
0 commit comments