grokify/mogo

View on GitHub
type/stringsutil/markdown.go

Summary

Maintainability
A
35 mins
Test Coverage
package stringsutil

import (
    "fmt"
    "regexp"
)

func URLToMarkdownLinkHostname(url string) string {
    rx := regexp.MustCompile(`(?i)^https?://([^/]+)(/[^/])`)
    m := rx.FindStringSubmatch(url)
    if len(m) > 1 {
        suffix := ""
        if len(m) > 2 {
            suffix = "..."
        }
        return fmt.Sprintf("[%s%s](%s)", m[1], suffix, url)
    }
    return url
}