markdown: fall back to internal renderer if gitea's is unavailable
This commit is contained in:
parent
1adacc0d49
commit
fda3899a23
2
go.mod
2
go.mod
@ -6,3 +6,5 @@ require (
|
|||||||
github.com/BurntSushi/toml v0.3.1
|
github.com/BurntSushi/toml v0.3.1
|
||||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
|
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/yuin/goldmark v1.7.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -1,4 +1,6 @@
|
|||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
|
||||||
|
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
|
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
|
||||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/yuin/goldmark"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
|
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
|
||||||
@ -58,10 +60,27 @@ func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoNa
|
|||||||
readmeHtml, err := this.gitea.RenderMarkdown(ctx, repoName, strings.Join(lines, "\n"))
|
readmeHtml, err := this.gitea.RenderMarkdown(ctx, repoName, strings.Join(lines, "\n"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
|
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
|
||||||
|
|
||||||
|
// Failed to use Gitea's markdown renderer
|
||||||
|
// HTTP 401 (Unauthorized) started happening with this API sometime
|
||||||
|
// between Gitea 1.18 -> 1.21, and no authorization token seems
|
||||||
|
// sufficient to make it work again
|
||||||
|
// Use our own one instead as a fallback
|
||||||
|
buff := bytes.Buffer{}
|
||||||
|
err = goldmark.Convert(readme, &buff)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// Built-in markdown renderer didn't work either
|
||||||
|
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
|
||||||
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
|
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OK
|
||||||
|
readmeHtml = buff.Bytes()
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
|
||||||
readmeHtml = []byte(strings.Replace(string(readmeHtml), `%%REPLACEME__BADGE%%`, `<img src="/static/build_success_brightgreen.svg" style="width:90px;height:20px;">`, 1))
|
readmeHtml = []byte(strings.Replace(string(readmeHtml), `%%REPLACEME__BADGE%%`, `<img src="/static/build_success_brightgreen.svg" style="width:90px;height:20px;">`, 1))
|
||||||
|
|
||||||
images, err := this.gitea.ImageFilesForRepo(ctx, repoName)
|
images, err := this.gitea.ImageFilesForRepo(ctx, repoName)
|
||||||
|
Loading…
Reference in New Issue
Block a user