Fix resolving of URLs in markdown (#401)

Path-only URLs need an absolute reference to be resolved against for printing in markdown
Previously we resolved against the URL to the resource we were operating on (eg comment or issue URL).
The markdown renderer in the web UI resolves all such URLs relative to the repo base URL. This PR adopts this behaviour in tea, by trimming the URL to a repo base URL via regex.

This makes a custom patch to our markdown renderer `glamour` obsolete, which turned out to be an incorrect patch, meaning we can make use of upstream glamour again.

Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/401
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
Norwin
2021-12-03 02:59:02 +08:00
committed by 6543
parent dc16643e0d
commit d2295828d0
15 changed files with 28 additions and 34 deletions

View File

@ -6,10 +6,10 @@
<a href="https://pkg.go.dev/github.com/charmbracelet/glamour?tab=doc"><img src="https://godoc.org/github.com/golang/gddo?status.svg" alt="GoDoc"></a>
<a href="https://github.com/charmbracelet/glamour/actions"><img src="https://github.com/charmbracelet/glamour/workflows/build/badge.svg" alt="Build Status"></a>
<a href="https://coveralls.io/github/charmbracelet/glamour?branch=master"><img src="https://coveralls.io/repos/github/charmbracelet/glamour/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://goreportcard.com/report/charmbracelet/glamour"><img src="https://goreportcard.com/badge/charmbracelet/glamour" alt="Go ReportCard"></a>
<a href="http://goreportcard.com/report/charmbracelet/glamour"><img src="http://goreportcard.com/badge/charmbracelet/glamour" alt="Go ReportCard"></a>
</p>
Stylesheet-based markdown rendering for your CLI apps.
Write handsome command-line tools with *Glamour*.
![Glamour dark style example](https://stuff.charm.sh/glamour/glamour-example.png)

View File

@ -129,9 +129,6 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
}
if node.Parent().(*ast.List).IsOrdered() {
e = l
if node.Parent().(*ast.List).Start != 1 {
e += uint(node.Parent().(*ast.List).Start) - 1
}
}
post := "\n"
@ -156,7 +153,6 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
return Element{
Exiting: post,
Renderer: &ItemElement{
IsOrdered: node.Parent().(*ast.List).IsOrdered(),
Enumeration: e,
},
}

View File

@ -25,7 +25,7 @@ func (e *ImageElement) Render(w io.Writer, ctx RenderContext) error {
}
if len(e.URL) > 0 {
el := &BaseElement{
Token: resolveURL(e.BaseURL, e.URL),
Token: resolveRelativeURL(e.BaseURL, e.URL),
Prefix: " ",
Style: ctx.options.Styles.Image,
}

View File

@ -64,7 +64,7 @@ func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error {
}
el := &BaseElement{
Token: resolveURL(e.BaseURL, e.URL),
Token: resolveRelativeURL(e.BaseURL, e.URL),
Prefix: pre,
Style: style,
}

View File

@ -7,13 +7,12 @@ import (
// An ItemElement is used to render items inside a list.
type ItemElement struct {
IsOrdered bool
Enumeration uint
}
func (e *ItemElement) Render(w io.Writer, ctx RenderContext) error {
var el *BaseElement
if e.IsOrdered {
if e.Enumeration > 0 {
el = &BaseElement{
Style: ctx.options.Styles.Enumeration,
Prefix: strconv.FormatInt(int64(e.Enumeration), 10),

View File

@ -38,7 +38,7 @@ func (e *ParagraphElement) Finish(w io.Writer, ctx RenderContext) error {
mw := NewMarginWriter(ctx, w, rules)
if len(strings.TrimSpace(bs.Current().Block.String())) > 0 {
flow := wordwrap.NewWriter(int(bs.Width(ctx)))
flow.KeepNewlines = ctx.options.PreserveNewLines
flow.KeepNewlines = false
_, _ = flow.Write(bs.Current().Block.Bytes())
flow.Close()

View File

@ -3,6 +3,7 @@ package ansi
import (
"io"
"net/url"
"strings"
"github.com/muesli/termenv"
east "github.com/yuin/goldmark-emoji/ast"
@ -14,11 +15,10 @@ import (
// Options is used to configure an ANSIRenderer.
type Options struct {
BaseURL string
WordWrap int
PreserveNewLines bool
ColorProfile termenv.Profile
Styles StyleConfig
BaseURL string
WordWrap int
ColorProfile termenv.Profile
Styles StyleConfig
}
// ANSIRenderer renders markdown content as ANSI escaped sequences.
@ -149,7 +149,7 @@ func isChild(node ast.Node) bool {
return false
}
func resolveURL(baseURL string, rel string) string {
func resolveRelativeURL(baseURL string, rel string) string {
u, err := url.Parse(rel)
if err != nil {
return rel
@ -157,6 +157,7 @@ func resolveURL(baseURL string, rel string) string {
if u.IsAbs() {
return rel
}
u.Path = strings.TrimPrefix(u.Path, "/")
base, err := url.Parse(baseURL)
if err != nil {

View File

@ -185,14 +185,6 @@ func WithWordWrap(wordWrap int) TermRendererOption {
}
}
// WithWordWrap sets a TermRenderer's word wrap.
func WithPreservedNewLines() TermRendererOption {
return func(tr *TermRenderer) error {
tr.ansiOptions.PreserveNewLines = true
return nil
}
}
// WithEmoji sets a TermRenderer's emoji rendering.
func WithEmoji() TermRendererOption {
return func(tr *TermRenderer) error {

2
vendor/modules.txt vendored
View File

@ -74,7 +74,7 @@ github.com/araddon/dateparse
# github.com/aymerick/douceur v0.2.0
github.com/aymerick/douceur/css
github.com/aymerick/douceur/parser
# github.com/charmbracelet/glamour v0.3.0 => github.com/noerw/glamour v0.3.0-patch
# github.com/charmbracelet/glamour v0.3.0
github.com/charmbracelet/glamour
github.com/charmbracelet/glamour/ansi
# github.com/cpuguy83/go-md2man/v2 v2.0.1