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,25 +199,55 @@ func writeURL(w *strings.Builder, addr []byte) {
|
||||||
case "bitcoin", "ftp", "geo", "http", "im", "irc", "ircs", "magnet", "mailto",
|
case "bitcoin", "ftp", "geo", "http", "im", "irc", "ircs", "magnet", "mailto",
|
||||||
"mms", "news", "nntp", "openpgp4fpr", "sip", "sftp", "sms", "smsto", "ssh",
|
"mms", "news", "nntp", "openpgp4fpr", "sip", "sftp", "sms", "smsto", "ssh",
|
||||||
"tel", "urn", "webcal", "wtai", "xmpp", "https":
|
"tel", "urn", "webcal", "wtai", "xmpp", "https":
|
||||||
case "mxc", "matrix":
|
w.WriteString(`<a`)
|
||||||
// TODO
|
if matrixURI, err := id.ProcessMatrixToURL(parsedURL); err == nil {
|
||||||
fallthrough
|
writeAttribute(w, "href", matrixURI.String())
|
||||||
|
writeAttribute(w, "class", matrixURIClassName(matrixURI)+" hicli-matrix-uri-plaintext")
|
||||||
|
} else {
|
||||||
|
writeAttribute(w, "target", "_blank")
|
||||||
|
writeAttribute(w, "rel", "noreferrer noopener")
|
||||||
|
writeAttribute(w, "href", parsedURL.String())
|
||||||
|
}
|
||||||
|
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:
|
default:
|
||||||
writeEscapedBytes(w, addr)
|
writeEscapedBytes(w, addr)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
w.WriteString(`<a`)
|
}
|
||||||
if matrixURI, err := id.ProcessMatrixToURL(parsedURL); err == nil {
|
|
||||||
writeAttribute(w, "href", matrixURI.String())
|
func init() {
|
||||||
writeAttribute(w, "class", matrixURIClassName(matrixURI)+" hicli-matrix-uri-plaintext")
|
if !slices.Contains(xurls.SchemesNoAuthority, "matrix") {
|
||||||
} else {
|
xurls.SchemesNoAuthority = append(xurls.SchemesNoAuthority, "matrix")
|
||||||
writeAttribute(w, "target", "_blank")
|
}
|
||||||
writeAttribute(w, "rel", "noreferrer noopener")
|
if !slices.Contains(xurls.Schemes, "mxc") {
|
||||||
writeAttribute(w, "href", parsedURL.String())
|
xurls.Schemes = append(xurls.Schemes, "mxc")
|
||||||
}
|
}
|
||||||
w.WriteByte('>')
|
|
||||||
writeEscapedBytes(w, addr)
|
|
||||||
w.WriteString("</a>")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func linkifyAndWriteBytes(w *strings.Builder, s []byte) {
|
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{}
|
mxc = id.ContentURI{}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
writeAttribute(w, "class", "hicli-mxc-url")
|
||||||
|
writeAttribute(w, "data-mxc", mxc.String())
|
||||||
href = fmt.Sprintf(HTMLSanitizerImgSrcTemplate, mxc.Homeserver, mxc.FileID)
|
href = fmt.Sprintf(HTMLSanitizerImgSrcTemplate, mxc.Homeserver, mxc.FileID)
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
|
|
Loading…
Add table
Reference in a new issue