Render links as absolute URLs in repo view (#2363)
Co-authored-by: Cristian Dominguez <cristiand391@users.noreply.github.com>
This commit is contained in:
parent
91df27f30a
commit
fa8674386b
9 changed files with 12 additions and 9 deletions
|
|
@ -117,7 +117,7 @@ func viewRun(opts *ViewOptions) error {
|
|||
content := gistFile.Content
|
||||
if strings.Contains(gistFile.Type, "markdown") && !opts.Raw {
|
||||
style := markdown.GetStyle(opts.IO.DetectTerminalTheme())
|
||||
rendered, err := markdown.Render(gistFile.Content, style)
|
||||
rendered, err := markdown.Render(gistFile.Content, style, "")
|
||||
if err == nil {
|
||||
content = rendered
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
|
|||
if issue.Body != "" {
|
||||
fmt.Fprintln(out)
|
||||
style := markdown.GetStyle(io.TerminalTheme())
|
||||
md, err := markdown.Render(issue.Body, style)
|
||||
md, err := markdown.Render(issue.Body, style, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -255,7 +255,7 @@ func reviewSurvey(io *iostreams.IOStreams, editorCommand string) (*api.PullReque
|
|||
|
||||
if len(bodyAnswers.Body) > 0 {
|
||||
style := markdown.GetStyle(io.DetectTerminalTheme())
|
||||
renderedBody, err := markdown.Render(bodyAnswers.Body, style)
|
||||
renderedBody, err := markdown.Render(bodyAnswers.Body, style, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -183,7 +183,7 @@ func printHumanPrPreview(io *iostreams.IOStreams, pr *api.PullRequest) error {
|
|||
if pr.Body != "" {
|
||||
fmt.Fprintln(out)
|
||||
style := markdown.GetStyle(io.TerminalTheme())
|
||||
md, err := markdown.Render(pr.Body, style)
|
||||
md, err := markdown.Render(pr.Body, style, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ func renderReleaseTTY(io *iostreams.IOStreams, release *shared.Release) error {
|
|||
}
|
||||
|
||||
style := markdown.GetStyle(io.DetectTerminalTheme())
|
||||
renderedDescription, err := markdown.Render(release.Body, style)
|
||||
renderedDescription, err := markdown.Render(release.Body, style, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ var NotFoundError = errors.New("not found")
|
|||
type RepoReadme struct {
|
||||
Filename string
|
||||
Content string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
func RepositoryReadme(client *http.Client, repo ghrepo.Interface, branch string) (*RepoReadme, error) {
|
||||
|
|
@ -22,6 +23,7 @@ func RepositoryReadme(client *http.Client, repo ghrepo.Interface, branch string)
|
|||
var response struct {
|
||||
Name string
|
||||
Content string
|
||||
HTMLURL string `json:"html_url"`
|
||||
}
|
||||
|
||||
err := apiClient.REST(repo.RepoHost(), "GET", getReadmePath(repo, branch), nil, &response)
|
||||
|
|
@ -41,6 +43,7 @@ func RepositoryReadme(client *http.Client, repo ghrepo.Interface, branch string)
|
|||
return &RepoReadme{
|
||||
Filename: response.Name,
|
||||
Content: string(decoded),
|
||||
BaseURL: response.HTMLURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ func viewRun(opts *ViewOptions) error {
|
|||
} else if isMarkdownFile(readme.Filename) {
|
||||
var err error
|
||||
style := markdown.GetStyle(opts.IO.TerminalTheme())
|
||||
readmeContent, err = markdown.Render(readme.Content, style)
|
||||
readmeContent, err = markdown.Render(readme.Content, style, readme.BaseURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error rendering markdown: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ import (
|
|||
"github.com/charmbracelet/glamour"
|
||||
)
|
||||
|
||||
func Render(text, style string) (string, error) {
|
||||
func Render(text, style string, baseURL string) (string, error) {
|
||||
// Glamour rendering preserves carriage return characters in code blocks, but
|
||||
// we need to ensure that no such characters are present in the output.
|
||||
text = strings.ReplaceAll(text, "\r\n", "\n")
|
||||
|
||||
tr, err := glamour.NewTermRenderer(
|
||||
glamour.WithStylePath(style),
|
||||
// glamour.WithBaseURL(""), // TODO: make configurable
|
||||
glamour.WithBaseURL(baseURL),
|
||||
// glamour.WithWordWrap(80), // TODO: make configurable
|
||||
)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ func Test_Render(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := Render(tt.input.text, tt.input.style)
|
||||
_, err := Render(tt.input.text, tt.input.style, "")
|
||||
if tt.output.wantsErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue