forked from Mirrors/gomuks
hicli/html: linkify plaintext matrix: and mxc:// URIs
This commit is contained in:
parent
535393d47f
commit
34346cffec
1 changed files with 47 additions and 15 deletions
|
@ -199,13 +199,6 @@ func writeURL(w *strings.Builder, addr []byte) {
|
|||
case "bitcoin", "ftp", "geo", "http", "im", "irc", "ircs", "magnet", "mailto",
|
||||
"mms", "news", "nntp", "openpgp4fpr", "sip", "sftp", "sms", "smsto", "ssh",
|
||||
"tel", "urn", "webcal", "wtai", "xmpp", "https":
|
||||
case "mxc", "matrix":
|
||||
// TODO
|
||||
fallthrough
|
||||
default:
|
||||
writeEscapedBytes(w, addr)
|
||||
return
|
||||
}
|
||||
w.WriteString(`<a`)
|
||||
if matrixURI, err := id.ProcessMatrixToURL(parsedURL); err == nil {
|
||||
writeAttribute(w, "href", matrixURI.String())
|
||||
|
@ -218,6 +211,43 @@ func writeURL(w *strings.Builder, addr []byte) {
|
|||
w.WriteByte('>')
|
||||
writeEscapedBytes(w, addr)
|
||||
w.WriteString("</a>")
|
||||
case "mxc":
|
||||
mxc := id.ContentURIString(parsedURL.String()).ParseOrIgnore()
|
||||
if !mxc.IsValid() {
|
||||
writeEscapedBytes(w, addr)
|
||||
return
|
||||
}
|
||||
w.WriteString("<a")
|
||||
writeAttribute(w, "class", "hicli-mxc-url")
|
||||
writeAttribute(w, "data-mxc", mxc.String())
|
||||
writeAttribute(w, "href", fmt.Sprintf(HTMLSanitizerImgSrcTemplate, mxc.Homeserver, mxc.FileID))
|
||||
w.WriteByte('>')
|
||||
writeEscapedBytes(w, addr)
|
||||
w.WriteString("</a>")
|
||||
case "matrix":
|
||||
uri, err := id.ProcessMatrixURI(parsedURL)
|
||||
if err != nil {
|
||||
writeEscapedBytes(w, addr)
|
||||
return
|
||||
}
|
||||
w.WriteString("<a")
|
||||
writeAttribute(w, "class", matrixURIClassName(uri))
|
||||
writeAttribute(w, "href", uri.String())
|
||||
w.WriteByte('>')
|
||||
writeEscapedBytes(w, addr)
|
||||
w.WriteString("</a>")
|
||||
default:
|
||||
writeEscapedBytes(w, addr)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
if !slices.Contains(xurls.SchemesNoAuthority, "matrix") {
|
||||
xurls.SchemesNoAuthority = append(xurls.SchemesNoAuthority, "matrix")
|
||||
}
|
||||
if !slices.Contains(xurls.Schemes, "mxc") {
|
||||
xurls.Schemes = append(xurls.Schemes, "mxc")
|
||||
}
|
||||
}
|
||||
|
||||
func linkifyAndWriteBytes(w *strings.Builder, s []byte) {
|
||||
|
@ -370,6 +400,8 @@ func writeA(w *strings.Builder, attr []html.Attribute) (mxc id.ContentURI) {
|
|||
mxc = id.ContentURI{}
|
||||
return
|
||||
}
|
||||
writeAttribute(w, "class", "hicli-mxc-url")
|
||||
writeAttribute(w, "data-mxc", mxc.String())
|
||||
href = fmt.Sprintf(HTMLSanitizerImgSrcTemplate, mxc.Homeserver, mxc.FileID)
|
||||
default:
|
||||
return
|
||||
|
|
Loading…
Add table
Reference in a new issue