forked from forgejo/forgejo
fix #711
This commit is contained in:
parent
6f71632e3e
commit
9a1fe801e5
12 changed files with 84 additions and 48 deletions
|
@ -154,7 +154,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
|
|||
rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
|
||||
` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
|
||||
}
|
||||
rawBytes = RenderissueIndexPattern(rawBytes, urlPrefix)
|
||||
rawBytes = RenderIssueIndexPattern(rawBytes, urlPrefix)
|
||||
rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix)
|
||||
return rawBytes
|
||||
}
|
||||
|
@ -168,7 +168,7 @@ func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
|
|||
return rawBytes
|
||||
}
|
||||
|
||||
func RenderissueIndexPattern(rawBytes []byte, urlPrefix string) []byte {
|
||||
func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string) []byte {
|
||||
ms := issueIndexPattern.FindAll(rawBytes, -1)
|
||||
for _, m := range ms {
|
||||
rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
|
||||
|
|
|
@ -47,18 +47,23 @@ func ShortSha(sha1 string) string {
|
|||
return sha1
|
||||
}
|
||||
|
||||
func ToUtf8WithErr(content []byte) (error, string) {
|
||||
func DetectEncoding(content []byte) (string, error) {
|
||||
detector := chardet.NewTextDetector()
|
||||
result, err := detector.DetectBest(content)
|
||||
return result.Charset, err
|
||||
}
|
||||
|
||||
func ToUtf8WithErr(content []byte) (error, string) {
|
||||
charset, err := DetectEncoding(content)
|
||||
if err != nil {
|
||||
return err, ""
|
||||
}
|
||||
|
||||
if result.Charset == "utf8" {
|
||||
if charset == "utf8" {
|
||||
return nil, string(content)
|
||||
}
|
||||
|
||||
decoder := mahonia.NewDecoder(result.Charset)
|
||||
decoder := mahonia.NewDecoder(charset)
|
||||
if decoder != nil {
|
||||
return nil, decoder.ConvertString(string(content))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue