Skip to content

Commit 81f9767

Browse files
committed
Fix sliceTokens edge case
1 parent 2b791e5 commit 81f9767

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

packages/mdx/dev/content/section.mdx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Lorem ipsum dolor sit amet.
44

55
<CH.Section>
66

7-
Consectetur adipiscing elit, sed do eiusmod tempor [incididunt](focus://4:7) ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget.
8-
_`orem`_, _`sum`_
7+
Consectetur adipiscing elit, sed do eiusmod tempor [incididunt](focus://4:7) ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. _`rem(ipsum, dol`_ xxx
8+
_`orem`_, xxx _`sum`_
99

1010
```js
1111
function lorem(ipsum, dolor) {
@@ -49,3 +49,26 @@ function lorem(ipsum, dolor) {
4949
_`ackground-color: va`_
5050

5151
</CH.Section>
52+
53+
## Issue #117
54+
55+
<CH.Section>
56+
57+
{/* prettier-ignore */}
58+
```js
59+
const PostLayout = ({ post }) => {
60+
const MDXContent = useMDXComponent(
61+
post.body.code
62+
)
63+
return (
64+
<article style={{ width: 600 }}>
65+
<h1>{post.title}</h1>
66+
<MDXContent />
67+
</article>
68+
)
69+
}
70+
```
71+
72+
It transforms the _`post.body.code`_ into a React component.
73+
74+
</CH.Section>

packages/mdx/src/remark/transform.inline-code.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ function getExistingCode(
138138
return undefined
139139
}
140140

141+
/**
142+
* Slice a line of tokens from a given index to a given length
143+
* Turns ("[abc][de][fgh]", 2, 4) into "[c][de][f]")
144+
*/
141145
function sliceTokens(
142146
line: Code["lines"][0],
143147
start: number,
@@ -148,44 +152,62 @@ function sliceTokens(
148152

149153
let headTokens = [] as Code["lines"][0]["tokens"]
150154

151-
for (let i = 0; i < tokens.length; i++) {
155+
// this for loop remove the unwanted prefix tokens and put the rest in headTokens
156+
for (
157+
let tokenIndex = 0;
158+
tokenIndex < tokens.length;
159+
tokenIndex++
160+
) {
152161
if (currentLength === start) {
153-
headTokens = tokens.slice(i)
162+
headTokens = tokens.slice(tokenIndex)
154163
break
155164
}
156-
if (currentLength + tokens[i].content.length > start) {
165+
if (
166+
currentLength + tokens[tokenIndex].content.length >
167+
start
168+
) {
157169
const newToken = {
158-
...tokens[i],
159-
content: tokens[i].content.slice(
170+
...tokens[tokenIndex],
171+
content: tokens[tokenIndex].content.slice(
160172
start - currentLength
161173
),
162174
}
163-
headTokens = [newToken].concat(tokens.slice(i + 1))
175+
headTokens = [newToken].concat(
176+
tokens.slice(tokenIndex + 1)
177+
)
164178
break
165179
}
166-
currentLength += tokens[i].content.length
180+
currentLength += tokens[tokenIndex].content.length
167181
}
168-
182+
// headTokens is now "[c][de][fgh]" (from the example above)
169183
currentLength = 0
170-
for (let i = 0; i < headTokens.length; i++) {
171-
if (currentLength === length) {
172-
return headTokens.slice(0, i)
184+
// this for loop remove the unwanted suffix tokens from headTokens
185+
for (
186+
let headTokenIndex = 0;
187+
headTokenIndex <= headTokens.length;
188+
headTokenIndex++
189+
) {
190+
if (currentLength >= length) {
191+
return headTokens.slice(0, headTokenIndex)
173192
}
193+
const currentToken = headTokens[headTokenIndex]
174194
if (
175-
currentLength + headTokens[i].content.length >
195+
currentLength + currentToken.content.length >
176196
length
177197
) {
178198
const newToken = {
179-
...headTokens[i],
180-
content: headTokens[i].content.slice(
199+
...currentToken,
200+
content: currentToken.content.slice(
181201
0,
182202
length - currentLength
183203
),
184204
}
185205

186-
return headTokens.slice(0, i).concat([newToken])
206+
return headTokens
207+
.slice(0, headTokenIndex)
208+
.concat([newToken])
187209
}
188-
currentLength += headTokens[i].content.length
210+
currentLength += currentToken.content.length
189211
}
190212
return []
191213
}

0 commit comments

Comments
 (0)