mirror of
https://github.com/charmbracelet/glow
synced 2024-12-12 13:12:32 +00:00
8e51396575
* fix: check other possible readme paths/branches Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: url Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: Readme.md --------- Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// isGitHubURL tests a string to determine if it is a well-structured GitHub URL.
|
|
func isGitHubURL(s string) (string, bool) {
|
|
if strings.HasPrefix(s, "github.com/") {
|
|
s = "https://" + s
|
|
}
|
|
|
|
u, err := url.ParseRequestURI(s)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
|
|
return u.String(), strings.ToLower(u.Host) == "github.com"
|
|
}
|
|
|
|
// findGitHubREADME tries to find the correct README filename in a repository.
|
|
func findGitHubREADME(s string) (*source, error) {
|
|
u, err := url.ParseRequestURI(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
u.Host = "raw.githubusercontent.com"
|
|
|
|
for _, b := range readmeBranches {
|
|
for _, r := range readmeNames {
|
|
v := *u
|
|
v.Path += fmt.Sprintf("/%s/%s", b, r)
|
|
|
|
// nolint:bodyclose
|
|
// it is closed on the caller
|
|
resp, err := http.Get(v.String())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
return &source{resp.Body, v.String()}, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, errors.New("can't find README in GitHub repository")
|
|
}
|