Skip to content

Commit

Permalink
fix: handle emoji characters when spliting strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Oct 18, 2024
1 parent ece5357 commit 72ba0d2
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/draftConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ export class DraftConverter {
// Split the text into groups by their range
let currentRanges: (RawDraftEntityRange | RawDraftInlineStyleRange)[] = [];
let currentText: string = "";
for (let i = 0; i < options.block.text.length; i++) {
const text = Array.from(options.block.text);
for (let i = 0; i < text.length; i++) {
let styles = stylesAtPosition[i] || [];
if (
styles.length !== currentRanges.length ||
Expand All @@ -219,7 +220,8 @@ export class DraftConverter {
currentText = "";
currentRanges = styles;
}
currentText += options.block.text[i];

currentText += text[i];
}

if (currentText) {
Expand Down
98 changes: 98 additions & 0 deletions test/draft-inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,101 @@ test("draft-medium-complex", () => {
const converter = new DraftConverter();
expect(converter.convert(draftMediumComplex as any)).toMatchSnapshot();
});

test("draft-medium-complex", () => {
const converter = new DraftConverter();
expect(
converter.convert({
blocks: [
{
key: "a5rh8",
text: "🕺🏽 Krewe of Boo hosts a jazz second-line through the French Quarter at 3pm, followed by happy hour at Pat O'Brien's. (Details)",
type: "unstyled",
depth: 0,
inlineStyleRanges: [
{
style: "BOLD",
length: 12,
offset: 3,
},
{
style: "ITALIC",
length: 7,
offset: 118,
},
],
entityRanges: [
{
offset: 118,
length: 7,
key: 0,
},
],
data: {},
},
],
// @ts-ignore
entityMap: [
{
type: "LINK",
mutability: "MUTABLE",
data: {
rel: "noreferrer",
url: "https://www.kreweofboo.com/secondline",
href: "https://www.kreweofboo.com/secondline",
target: "_blank",
},
},
],
})
).toEqual({
type: "doc",
content: [
{
type: "paragraph",
content: [
{
type: "text",
text: "🕺🏽 ",
marks: [],
},
{
type: "text",
text: "Krewe of Boo",
marks: [
{
type: "bold",
},
],
},
{
type: "text",
text: " hosts a jazz second-line through the French Quarter at 3pm, followed by happy hour at Pat O'Brien's. (",
marks: [],
},
{
type: "text",
text: "Details",
marks: [
{
type: "link",
attrs: {
href: "https://www.kreweofboo.com/secondline",
target: "_blank",
},
},
{
type: "italic",
},
],
},
{
type: "text",
text: ")",
marks: [],
},
],
},
],
});
});

0 comments on commit 72ba0d2

Please sign in to comment.