From 8eabf0f4036efd861679d3a064870c1466e11c01 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Tue, 26 Nov 2019 00:08:06 +0100 Subject: [PATCH] Handle relative URLs in markdown, when a BaseURL is provided --- github.go | 4 ++-- gitlab.go | 4 ++-- main.go | 37 +++++++++++++++++++++++++------------ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/github.go b/github.go index 2a7661a..1b04da7 100644 --- a/github.go +++ b/github.go @@ -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 } } diff --git a/gitlab.go b/gitlab.go index 2c5676e..7c6dfa5 100644 --- a/gitlab.go +++ b/gitlab.go @@ -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 } } diff --git a/main.go b/main.go index b5244a8..dffb5cc 100644 --- a/main.go +++ b/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