From bab8a51206160238dc4d799ffffb80755e69ca2c Mon Sep 17 00:00:00 2001 From: Daniel Pokorny Date: Thu, 18 Apr 2024 10:27:30 +0200 Subject: [PATCH] reset links array when childCount is zero --- .../portable-text-transformer.ts | 5 +- .../portable-text-transformer.spec.ts.snap | 58 +++++++++++++++++++ .../portable-text-transformer.spec.ts | 8 +++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/transformers/portable-text-transformer/portable-text-transformer.ts b/src/transformers/portable-text-transformer/portable-text-transformer.ts index ba79420..7e5ff81 100644 --- a/src/transformers/portable-text-transformer/portable-text-transformer.ts +++ b/src/transformers/portable-text-transformer/portable-text-transformer.ts @@ -126,9 +126,10 @@ const mergeSpansAndMarks: MergePortableTextItemsFunction = (itemsToMerge) => { * * in this case, a link can have multiple child nodes if some of its text is styled. * as a result, keeping a counter for the link's children and decrementing it with each subsequent span occurrence - * is required so that the link mark doesn't extend beyond its scope. + * is required so that the link mark doesn't extend beyond its scope. links array is reset when the counter reaches zero. */ - item.marks = [...marks, ...(linkChildCount > 0 ? links : [])]; + links = linkChildCount > 0 ? links : []; + item.marks = [...marks, ...links]; // ensures the child count doesn't go below zero linkChildCount = Math.max(0, linkChildCount - 1); mergedItems.push(item); diff --git a/tests/transfomers/portable-text-transformer/__snapshots__/portable-text-transformer.spec.ts.snap b/tests/transfomers/portable-text-transformer/__snapshots__/portable-text-transformer.spec.ts.snap index b86f426..3860738 100644 --- a/tests/transfomers/portable-text-transformer/__snapshots__/portable-text-transformer.spec.ts.snap +++ b/tests/transfomers/portable-text-transformer/__snapshots__/portable-text-transformer.spec.ts.snap @@ -12227,3 +12227,61 @@ exports[`portable text transformer transforms table with input { expect(nodeResult).toMatchSnapshot(); expect(nodeResult).toMatchObject(browserResult); }) + + it("with multiple links in a paragraph, doesn't extend linkmark beyond the first", () => { + const input = `

Text inner text 1 text between inner text 2.

`; + const { nodeResult, browserResult } = transformInput(input); + + expect(nodeResult).toMatchSnapshot(); + expect(nodeResult).toMatchObject(browserResult); + }) })