Skip to content

Commit

Permalink
reset links array when childCount is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Apr 18, 2024
1 parent fce244f commit de42101
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12227,3 +12227,61 @@ exports[`portable text transformer transforms table with input <table><tbody><tr
},
]
`;

exports[`portable text transformer with multiple links in a paragraph, doesn't extend linkmark beyond the first 1`] = `
[
{
"_key": "guid",
"_type": "block",
"children": [
{
"_key": "guid",
"_type": "span",
"marks": [],
"text": "Text ",
},
{
"_key": "guid",
"_type": "span",
"marks": [
"guid",
],
"text": "inner text 1",
},
{
"_key": "guid",
"_type": "span",
"marks": [],
"text": " text between ",
},
{
"_key": "guid",
"_type": "span",
"marks": [
"guid",
],
"text": "inner text 2",
},
{
"_key": "guid",
"_type": "span",
"marks": [],
"text": ".",
},
],
"markDefs": [
{
"_key": "guid",
"_type": "link",
"href": "https://example.com",
},
{
"_key": "guid",
"_type": "link",
"href": "https://example.org",
},
],
"style": "normal",
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,12 @@ describe("portable text transformer", () => {
expect(nodeResult).toMatchSnapshot();
expect(nodeResult).toMatchObject(browserResult);
})

it("with multiple links in a paragraph, doesn't extend linkmark beyond the first", () => {
const input = `<p>Text <a href="https://example.com">inner text 1</a> text between <a href="https://example.org">inner text 2</a>.</p>`;
const { nodeResult, browserResult } = transformInput(input);

expect(nodeResult).toMatchSnapshot();
expect(nodeResult).toMatchObject(browserResult);
})
})

0 comments on commit de42101

Please sign in to comment.