8181import { unreachable } from 'devlop'
8282import { toJsxRuntime } from 'hast-util-to-jsx-runtime'
8383import { urlAttributes } from 'html-url-attributes'
84+ import { sanitizeUri } from 'micromark-util-sanitize-uri'
8485// @ts -expect-error: untyped.
8586import { Fragment , jsx , jsxs } from 'react/jsx-runtime'
8687import remarkParse from 'remark-parse'
@@ -89,8 +90,6 @@ import {unified} from 'unified'
8990import { visit } from 'unist-util-visit'
9091import { VFile } from 'vfile'
9192
92- const safeProtocols = [ 'http' , 'https' , 'mailto' , 'tel' ]
93-
9493const own = { } . hasOwnProperty
9594const changelog =
9695 'https://github.com/remarkjs/react-markdown/blob/main/changelog.md'
@@ -99,6 +98,7 @@ const changelog =
9998const emptyPlugins = [ ]
10099/** @type {Readonly<RemarkRehypeOptions> } */
101100const emptyRemarkRehypeOptions = { allowDangerousHtml : true }
101+ const safeProtocol = / ^ ( h t t p s ? | i r c s ? | m a i l t o | x m p p ) $ / i
102102
103103// Mutable because we `delete` any time it’s used and a message is sent.
104104/** @type {ReadonlyArray<Readonly<Deprecation>> } */
@@ -293,38 +293,5 @@ export function Markdown(options) {
293293 * Safe URL.
294294 */
295295export function defaultUrlTransform ( value ) {
296- const url = value . trim ( )
297- const first = url . charAt ( 0 )
298-
299- if ( first === '#' || first === '/' ) {
300- return url
301- }
302-
303- const colon = url . indexOf ( ':' )
304- if ( colon === - 1 ) {
305- return url
306- }
307-
308- for ( const protocol of safeProtocols ) {
309- if (
310- colon === protocol . length &&
311- url . slice ( 0 , protocol . length ) . toLowerCase ( ) === protocol
312- ) {
313- return url
314- }
315- }
316-
317- let index = url . indexOf ( '?' )
318- if ( index !== - 1 && colon > index ) {
319- return url
320- }
321-
322- index = url . indexOf ( '#' )
323- if ( index !== - 1 && colon > index ) {
324- return url
325- }
326-
327- // To do: is there an alternative?
328- // eslint-disable-next-line no-script-url
329- return 'javascript:void(0)'
296+ return sanitizeUri ( value , safeProtocol )
330297}
0 commit comments