Fixed showcase issue

Cleaned up bad unicode and control characters from snippet description
This commit is contained in:
Asaf Gartner 2022-07-14 03:41:41 +03:00
parent faa582f6da
commit d01731f4cd
3 changed files with 7 additions and 3 deletions

View File

@ -297,6 +297,8 @@ func LinkToTemplate(link *models.Link) Link {
return tlink
}
var controlCharRegex = regexp.MustCompile(`\p{Cc}`)
func TimelineItemsToJSON(items []TimelineItem) string {
// NOTE(asaf): As of 2021-06-22: This only serializes the data necessary for snippet showcase.
builder := strings.Builder{}
@ -314,11 +316,13 @@ func TimelineItemsToJSON(items []TimelineItem) string {
builder.WriteString(`"description":"`)
jsonString := string(item.Description)
jsonString = strings.ToValidUTF8(jsonString, "")
jsonString = strings.ReplaceAll(jsonString, `\`, `\\`)
jsonString = strings.ReplaceAll(jsonString, `"`, `\"`)
jsonString = strings.ReplaceAll(jsonString, "\n", "\\n")
jsonString = strings.ReplaceAll(jsonString, "\r", "\\r")
jsonString = strings.ReplaceAll(jsonString, "\t", "\\t")
jsonString = controlCharRegex.ReplaceAllString(jsonString, "")
builder.WriteString(jsonString)
builder.WriteString(`",`)

View File

@ -27,7 +27,7 @@
<a class="user" data-tmpl="userLink"></a>
<a data-tmpl="date" class="datetime tr" style="flex: 1 1 auto;"></a>
</div>
<div data-tmpl="description">
<div class="pre overflow-auto" data-tmpl="description">
Unknown description
</div>
<div data-tmpl="tags" class="pt2 flex"></div>
@ -42,4 +42,4 @@
<template id="timeline_item_tag">
<div data-tmpl="tag" class="bg-theme-dimmer ph2 pv1 br2"></div>
</template>
</template>

View File

@ -8,7 +8,7 @@
<a class="user" href="{{ .Snippet.OwnerUrl }}">{{ .Snippet.OwnerName }}</a>
<a class="tr" style="flex: 1 1 auto;" href="{{ .Snippet.Url }}">{{ timehtml (relativedate .Snippet.Date) .Snippet.Date }}</a>
</div>
<p class="mb2">{{ .Snippet.Description }}</p>
<div class="pre overflow-auto mb2">{{ .Snippet.Description }}</div>
<div>
{{ range .Snippet.EmbedMedia }}
{{ if eq .Type mediaimage }}