fix(markup): handle UTF-16

Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2023-05-28 17:09:37 +05:30
parent aacabdd54d
commit f1ec8f7729
3 changed files with 1641 additions and 319 deletions

View File

@ -14,7 +14,7 @@ import (
) )
func PostData(postId string) (*entities.MediumResponse, error) { func PostData(postId string) (*entities.MediumResponse, error) {
if config.Conf.Env == "devd" { if config.Conf.Env == "dev" {
file, err := os.ReadFile("response.json") file, err := os.ReadFile("response.json")
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
"unicode/utf16"
"github.com/medium.rip/pkg/entities" "github.com/medium.rip/pkg/entities"
) )
@ -36,7 +37,7 @@ func ranges(text string, markups []entities.Markup) []RangeWithMarkup {
// include the start and end indexes of the text // include the start and end indexes of the text
markupBoundaries = append([]int{0}, markupBoundaries...) markupBoundaries = append([]int{0}, markupBoundaries...)
markupBoundaries = append(markupBoundaries, len([]rune(text))) markupBoundaries = append(markupBoundaries, len(utf16.Encode([]rune(text))))
// remove duplicates // remove duplicates
markupBoundaries = unique(markupBoundaries) markupBoundaries = unique(markupBoundaries)
@ -72,8 +73,10 @@ func ranges(text string, markups []entities.Markup) []RangeWithMarkup {
func ConvertMarkup(text string, markups []entities.Markup) string { func ConvertMarkup(text string, markups []entities.Markup) string {
var markedUp strings.Builder var markedUp strings.Builder
for _, r := range ranges(text, markups) { for _, r := range ranges(text, markups) {
runeText := []rune(text) // very important otherwise we can't handle UTF-8 // handle utf-16
textToWrap := string(runeText[r.Range[0]:r.Range[1]]) utf16Text := utf16.Encode([]rune(text))
ranged := utf16Text[r.Range[0]:r.Range[1]]
textToWrap := string(utf16.Decode(ranged))
markedUp.WriteString(wrapInMarkups(textToWrap, r.Markups)) markedUp.WriteString(wrapInMarkups(textToWrap, r.Markups))
} }

File diff suppressed because it is too large Load Diff