mirror of
https://github.com/charmbracelet/glow
synced 2025-03-04 23:07:12 +00:00
Handle relative URLs in markdown, when a BaseURL is provided
This commit is contained in:
parent
5766f35363
commit
8eabf0f403
3 changed files with 29 additions and 16 deletions
|
@ -22,7 +22,7 @@ func isGitHubURL(s string) (string, bool) {
|
|||
}
|
||||
|
||||
// findGitHubREADME tries to find the correct README filename in a repository
|
||||
func findGitHubREADME(s string) (*http.Response, error) {
|
||||
func findGitHubREADME(s string) (*Source, error) {
|
||||
u, err := url.ParseRequestURI(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -39,7 +39,7 @@ func findGitHubREADME(s string) (*http.Response, error) {
|
|||
}
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return resp, nil
|
||||
return &Source{resp.Body, v.String()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ func isGitLabURL(s string) (string, bool) {
|
|||
}
|
||||
|
||||
// findGitLabREADME tries to find the correct README filename in a repository
|
||||
func findGitLabREADME(s string) (*http.Response, error) {
|
||||
func findGitLabREADME(s string) (*Source, error) {
|
||||
u, err := url.ParseRequestURI(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -38,7 +38,7 @@ func findGitLabREADME(s string) (*http.Response, error) {
|
|||
}
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return resp, nil
|
||||
return &Source{resp.Body, v.String()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
37
main.go
37
main.go
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/muesli/go-wordwrap"
|
||||
|
@ -31,24 +32,29 @@ var (
|
|||
width uint
|
||||
)
|
||||
|
||||
func readerFromArg(s string) (io.ReadCloser, error) {
|
||||
type Source struct {
|
||||
reader io.ReadCloser
|
||||
URL string
|
||||
}
|
||||
|
||||
func readerFromArg(s string) (*Source, error) {
|
||||
if s == "-" {
|
||||
return os.Stdin, nil
|
||||
return &Source{reader: os.Stdin}, nil
|
||||
}
|
||||
|
||||
if u, ok := isGitHubURL(s); ok {
|
||||
resp, err := findGitHubREADME(u)
|
||||
src, err := findGitHubREADME(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Body, nil
|
||||
return src, nil
|
||||
}
|
||||
if u, ok := isGitLabURL(s); ok {
|
||||
resp, err := findGitLabREADME(u)
|
||||
src, err := findGitLabREADME(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Body, nil
|
||||
return src, nil
|
||||
}
|
||||
|
||||
if u, err := url.ParseRequestURI(s); err == nil {
|
||||
|
@ -63,21 +69,22 @@ func readerFromArg(s string) (io.ReadCloser, error) {
|
|||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("HTTP status %d", resp.StatusCode)
|
||||
}
|
||||
return resp.Body, nil
|
||||
return &Source{resp.Body, u.String()}, nil
|
||||
}
|
||||
|
||||
if len(s) == 0 {
|
||||
for _, v := range readmeNames {
|
||||
r, err := os.Open(v)
|
||||
if err == nil {
|
||||
return r, nil
|
||||
return &Source{reader: r}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("missing markdown source")
|
||||
}
|
||||
|
||||
return os.Open(s)
|
||||
r, err := os.Open(s)
|
||||
return &Source{reader: r}, err
|
||||
}
|
||||
|
||||
func execute(cmd *cobra.Command, args []string) error {
|
||||
|
@ -85,12 +92,12 @@ func execute(cmd *cobra.Command, args []string) error {
|
|||
if len(args) > 0 {
|
||||
arg = args[0]
|
||||
}
|
||||
in, err := readerFromArg(arg)
|
||||
src, err := readerFromArg(arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
b, _ := ioutil.ReadAll(in)
|
||||
defer src.reader.Close()
|
||||
b, _ := ioutil.ReadAll(src.reader)
|
||||
|
||||
r := gold.NewPlainTermRenderer()
|
||||
if isatty.IsTerminal(os.Stdout.Fd()) {
|
||||
|
@ -100,6 +107,12 @@ func execute(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
u, err := url.ParseRequestURI(src.URL)
|
||||
if err == nil {
|
||||
u.Path = filepath.Dir(u.Path)
|
||||
r.BaseURL = u.String() + "/"
|
||||
}
|
||||
|
||||
out := r.RenderBytes(b)
|
||||
fmt.Printf("%s", wordwrap.WrapString(string(out), width))
|
||||
return nil
|
||||
|
|
Loading…
Add table
Reference in a new issue