Move Markdown parsing to i18n service

This commit is contained in:
Jan Dittberner 2023-07-24 17:59:18 +02:00
parent b0daaf956f
commit fe1914fd89
2 changed files with 40 additions and 20 deletions

View file

@ -32,7 +32,6 @@ import (
"github.com/ory/hydra-client-go/client/admin" "github.com/ory/hydra-client-go/client/admin"
"github.com/ory/hydra-client-go/models" "github.com/ory/hydra-client-go/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/yuin/goldmark"
"code.cacert.org/cacert/oidc_idp/ui" "code.cacert.org/cacert/oidc_idp/ui"
@ -330,23 +329,13 @@ func (h *LoginHandler) renderNoEmailsInClientCertificate(w http.ResponseWriter,
} }
func (h *LoginHandler) renderNoChallengeInRequest(w http.ResponseWriter, localizer *i18n.Localizer) { func (h *LoginHandler) renderNoChallengeInRequest(w http.ResponseWriter, localizer *i18n.Localizer) {
trans := func(label string) string { err := h.templates[NoChallengeInRequest].Lookup("base").Execute(w, map[string]interface{}{
return h.messageCatalog.LookupMessage(label, nil, localizer) "Title": h.messageCatalog.LookupMessage("NoChallengeInRequestTitle", nil, localizer),
} "Explanation": template.HTML(h.messageCatalog.LookupMarkdownMessage( //nolint:gosec
"NoChallengeInRequestExplanation",
buf := &bytes.Buffer{} nil,
localizer,
err := goldmark.Convert([]byte(trans("NoChallengeInRequestExplanation")), buf) )),
if err != nil {
h.logger.WithError(err).Error("markdown conversion failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = h.templates[NoChallengeInRequest].Lookup("base").Execute(w, map[string]interface{}{
"Title": trans("NoChallengeInRequestTitle"),
"Explanation": template.HTML(buf.String()), //nolint:gosec
}) })
if err != nil { if err != nil {
h.logger.WithError(err).Error("template rendering failed") h.logger.WithError(err).Error("template rendering failed")

View file

@ -18,10 +18,12 @@ limitations under the License.
package services package services
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/yuin/goldmark"
"code.cacert.org/cacert/oidc_idp/translations" "code.cacert.org/cacert/oidc_idp/translations"
@ -195,6 +197,35 @@ func (m *MessageCatalog) LookupMessage(
return id return id
} }
func (m *MessageCatalog) LookupMarkdownMessage(
id string,
templateData map[string]interface{},
localizer *i18n.Localizer,
) string {
if message, ok := m.messages[id]; ok {
translation, err := localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: message,
TemplateData: templateData,
})
if err != nil {
return m.handleLocalizeError(id, translation, err)
}
buf := &bytes.Buffer{}
err = goldmark.Convert([]byte(translation), buf)
if err != nil {
return m.handleLocalizeError(id, translation, fmt.Errorf("markdown conversion error: %w", err))
}
return buf.String()
}
m.logger.Warnf("no translation found for %s", id)
return id
}
func (m *MessageCatalog) LookupMessagePlural( func (m *MessageCatalog) LookupMessagePlural(
id string, id string,
templateData map[string]interface{}, templateData map[string]interface{},
@ -223,13 +254,13 @@ func (m *MessageCatalog) handleLocalizeError(id string, translation string, err
var messageNotFound *i18n.MessageNotFoundErr var messageNotFound *i18n.MessageNotFoundErr
if errors.As(err, &messageNotFound) { if errors.As(err, &messageNotFound) {
m.logger.Warnf("message %s not found: %v", id, err) m.logger.WithError(err).WithField("message", id).Warn("message not found")
if translation != "" { if translation != "" {
return translation return translation
} }
} else { } else {
m.logger.Error(err) m.logger.WithError(err).WithField("message", id).Error("translation error")
} }
return id return id