Skip to content

Commit

Permalink
feat: make text available to all span tree nodes (#55)
Browse files Browse the repository at this point in the history
* feat: make text available to all tree nodes

* chore: sort package.json
  • Loading branch information
angeloashmore committed May 5, 2022
1 parent 974bbed commit ea385cf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"main": "dist/index.cjs",
"module": "dist/index.js",
"react-native": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
Expand Down Expand Up @@ -65,6 +66,5 @@
},
"publishConfig": {
"access": "public"
},
"react-native": "dist/index.js"
}
}
6 changes: 4 additions & 2 deletions src/asTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const textNodeSpansToTreeNodeChildren = (
const parentSpanStart = (parentSpan && parentSpan.start) || 0;
const spanStart = span.start - parentSpanStart;
const spanEnd = span.end - parentSpanStart;
const text = node.text.slice(spanStart, spanEnd);

const childSpans: RTInlineNode[] = [];
for (let j = i; j < mutSpans.length; j++) {
Expand All @@ -166,14 +167,15 @@ const textNodeSpansToTreeNodeChildren = (
children.push(createTextTreeNode(node.text.slice(0, spanStart)));
}

const spanWithText = { ...span, text };
children.push(
createTreeNode(
span,
spanWithText,
textNodeSpansToTreeNodeChildren(
childSpans,
{
...node,
text: node.text.slice(spanStart, spanEnd),
text,
},
span,
),
Expand Down
39 changes: 26 additions & 13 deletions test/snapshots/asTree.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 36,
start: 19,
text: 'multi-block span!',
type: 'label',
},
text: undefined,
text: 'multi-block span!',
type: 'label',
},
],
Expand Down Expand Up @@ -118,9 +119,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 7,
start: 0,
text: 'Heading',
type: 'label',
},
text: undefined,
text: 'Heading',
type: 'label',
},
{
Expand Down Expand Up @@ -165,9 +167,10 @@ Generated by [AVA](https://avajs.dev).
node: {
end: 26,
start: 20,
text: 'italic',
type: 'em',
},
text: undefined,
text: 'italic',
type: 'em',
},
{
Expand All @@ -186,9 +189,10 @@ Generated by [AVA](https://avajs.dev).
node: {
end: 37,
start: 10,
text: 'bold text italic still bold',
type: 'strong',
},
text: undefined,
text: 'bold text italic still bold',
type: 'strong',
},
{
Expand Down Expand Up @@ -288,9 +292,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 25,
start: 19,
text: 'nested',
type: 'label',
},
text: undefined,
text: 'nested',
type: 'label',
},
{
Expand Down Expand Up @@ -427,9 +432,10 @@ Generated by [AVA](https://avajs.dev).
node: {
end: 19,
start: 9,
text: 'now italic',
type: 'em',
},
text: undefined,
text: 'now italic',
type: 'em',
},
{
Expand All @@ -448,9 +454,10 @@ Generated by [AVA](https://avajs.dev).
node: {
end: 35,
start: 0,
text: 'Bold and now italic but not anymore',
type: 'strong',
},
text: undefined,
text: 'Bold and now italic but not anymore',
type: 'strong',
},
{
Expand Down Expand Up @@ -482,9 +489,10 @@ Generated by [AVA](https://avajs.dev).
node: {
end: 53,
start: 49,
text: 'last',
type: 'strong',
},
text: undefined,
text: 'last',
type: 'strong',
},
{
Expand Down Expand Up @@ -737,9 +745,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 10,
start: 0,
text: 'A web link',
type: 'hyperlink',
},
text: undefined,
text: 'A web link',
type: 'hyperlink',
},
],
Expand Down Expand Up @@ -793,9 +802,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 15,
start: 0,
text: 'A document link',
type: 'hyperlink',
},
text: undefined,
text: 'A document link',
type: 'hyperlink',
},
],
Expand Down Expand Up @@ -853,9 +863,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 12,
start: 0,
text: 'A media link',
type: 'hyperlink',
},
text: undefined,
text: 'A media link',
type: 'hyperlink',
},
],
Expand Down Expand Up @@ -910,9 +921,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 22,
start: 0,
text: 'A non-image media link',
type: 'hyperlink',
},
text: undefined,
text: 'A non-image media link',
type: 'hyperlink',
},
],
Expand Down Expand Up @@ -979,9 +991,10 @@ Generated by [AVA](https://avajs.dev).
},
end: 25,
start: 19,
text: 'nested',
type: 'hyperlink',
},
text: undefined,
text: 'nested',
type: 'hyperlink',
},
{
Expand Down
Binary file modified test/snapshots/asTree.test.ts.snap
Binary file not shown.

0 comments on commit ea385cf

Please sign in to comment.