Skip to content

Commit

Permalink
fix: Filter out empty Str nodes without stripping trailing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
idreyn committed Jul 20, 2022
1 parent 0f4765a commit 42d5eac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/transform/fromProsemirror/__tests__/fromProsemirror.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ describe("fromProsemirror", () => {
]);
});

it("transforms a string with trailing spaces", () => {
expect(
fromProsemirror(
{ type: "text", text: "Hello world " },
rules
).asArray()
).toEqual([
{ type: "Str", content: "Hello" },
{ type: "Space" },
{ type: "Str", content: "world" },
{ type: "Space" },
]);
});


it("performs a simple transformation from Prosemirror marks to nodes", () => {
expect(
fromProsemirror(
Expand Down
12 changes: 7 additions & 5 deletions src/transform/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ export const intersperse = (

export const textToStrSpace = (text: string): (Str | Space)[] =>
intersperse(
text
.split(" ")
.filter((word) => word.length > 0)
.map((word) => ({ type: "Str", content: word })),
text.split(" ").map((word) => ({ type: "Str", content: word })),
() => ({ type: "Space" })
);
).filter((node) => {
if (node.type === "Str" && node.content.length === 0) {
return false;
}
return true;
});

export const asArray = <T>(item: T | T[]): T[] => {
return Array.isArray(item) ? item : [item];
Expand Down

0 comments on commit 42d5eac

Please sign in to comment.