Skip to content

Commit d8e3787

Browse files
committed
Fix double encoding in new url transform
ClosesGH-797.
1 parent 55d8d83 commit d8e3787

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎lib/index.js‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
import{unreachable}from'devlop'
8585
import{toJsxRuntime}from'hast-util-to-jsx-runtime'
8686
import{urlAttributes}from'html-url-attributes'
87-
import{sanitizeUri}from'micromark-util-sanitize-uri'
8887
// @ts-expect-error: untyped.
8988
import{Fragment,jsx,jsxs}from'react/jsx-runtime'
9089
importremarkParsefrom'remark-parse'
@@ -297,5 +296,26 @@ export function Markdown(options) {
297296
* Safe URL.
298297
*/
299298
exportfunctiondefaultUrlTransform(value){
300-
returnsanitizeUri(value,safeProtocol)
299+
// Same as:
300+
// <https://github.com/micromark/micromark/blob/929275e/packages/micromark-util-sanitize-uri/dev/index.js#L34>
301+
// But without the `encode` part.
302+
constcolon=value.indexOf(':')
303+
constquestionMark=value.indexOf('?')
304+
constnumberSign=value.indexOf('#')
305+
constslash=value.indexOf('/')
306+
307+
if(
308+
// If there is no protocol, it’s relative.
309+
colon<0||
310+
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
311+
(slash>-1&&colon>slash)||
312+
(questionMark>-1&&colon>questionMark)||
313+
(numberSign>-1&&colon>numberSign)||
314+
// It is a protocol, it should be allowed.
315+
safeProtocol.test(value.slice(0,colon))
316+
){
317+
returnvalue
318+
}
319+
320+
return''
301321
}

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
"hast-util-to-jsx-runtime": "^2.0.0",
8282
"html-url-attributes": "^3.0.0",
8383
"mdast-util-to-hast": "^13.0.0",
84-
"micromark-util-sanitize-uri": "^2.0.0",
8584
"remark-parse": "^11.0.0",
8685
"remark-rehype": "^11.0.0",
8786
"unified": "^11.0.0",

‎test.jsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ test('react-markdown', async function (t) {
326326
)
327327
})
328328

329+
awaitt.test('should support hash (`&`) in a URL',function(){
330+
assert.equal(
331+
asHtml(<Markdownchildren="[](a?b&c=d)"/>),
332+
'<p><a href="a?b&amp;c=d"></a></p>'
333+
)
334+
})
335+
329336
awaitt.test('should support hash (`#`) in a URL',function(){
330337
assert.equal(
331338
asHtml(<Markdownchildren="[](a#javascript:alert(1))"/>),

0 commit comments

Comments
 (0)