From 42d5eac47bbd24d62e1a7cfa81e6609c61dc4d40 Mon Sep 17 00:00:00 2001 From: Ian Reynolds Date: Wed, 20 Jul 2022 13:32:46 -0400 Subject: [PATCH] fix: Filter out empty Str nodes without stripping trailing spaces --- .../__tests__/fromProsemirror.test.ts | 15 +++++++++++++++ src/transform/util.ts | 12 +++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/transform/fromProsemirror/__tests__/fromProsemirror.test.ts b/src/transform/fromProsemirror/__tests__/fromProsemirror.test.ts index 2261da2..3f9d0bd 100644 --- a/src/transform/fromProsemirror/__tests__/fromProsemirror.test.ts +++ b/src/transform/fromProsemirror/__tests__/fromProsemirror.test.ts @@ -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( diff --git a/src/transform/util.ts b/src/transform/util.ts index 55d50a2..9f45c76 100644 --- a/src/transform/util.ts +++ b/src/transform/util.ts @@ -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 = (item: T | T[]): T[] => { return Array.isArray(item) ? item : [item];