mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2024-11-16 11:25:57 +00:00
fix(markup): handle UTF-16
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
parent
aacabdd54d
commit
f1ec8f7729
@ -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
|
||||||
|
@ -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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1883
response.json
1883
response.json
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user