Skip to content

feat: add createUrlTransform factory for custom safe protocols#943

Open
Aimee1608 wants to merge 1 commit into
remarkjs:mainfrom
Aimee1608:feat/create-url-transform
Open

feat: add createUrlTransform factory for custom safe protocols#943
Aimee1608 wants to merge 1 commit into
remarkjs:mainfrom
Aimee1608:feat/create-url-transform

Conversation

@Aimee1608
Copy link
Copy Markdown

@Aimee1608 Aimee1608 commented May 28, 2026

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and could not find anything (or linked relevant results below)
  • I made sure the docs are up to date
  • I included tests (or that is not needed)

Description of changes

Summary

Adds a createUrlTransform(safeProtocol?) factory function that returns a URL transform function with a custom set of allowed protocols, without requiring callers to reimplement the full sanitization logic.

Motivation

Currently, extending the list of safe protocols (e.g. adding tel:) requires fully reimplementing defaultUrlTransform, which means callers lose the benefit of any future improvements to that function (see #941).

Changes

  • lib/index.js: Extract the URL sanitization logic into createUrlTransform(safeProtocol?) and redefine defaultUrlTransform as createUrlTransform() — fully backward-compatible.
  • index.js: Export createUrlTransform from the package entry point.
  • test.jsx: Add a createUrlTransform test suite covering default behaviour, custom protocols, blocked protocols, component integration, and parity with defaultUrlTransform.
  • readme.md: Document createUrlTransform with a usage example.

Usage example

import Markdown, {createUrlTransform} from 'react-markdown'

// Allow tel: in addition to the default protocols
const urlTransform = createUrlTransform(/^(https?|ircs?|mailto|xmpp|tel)$/i)

<Markdown urlTransform={urlTransform}>
  {'[Call us](tel:+1-555-0100)'}
</Markdown>

Backward compatibility

defaultUrlTransform behaviour is unchanged — it is now defined as createUrlTransform() with the same default regex.

Closes #941

Introduce `createUrlTransform(safeProtocol?)` which returns a URL
transform function identical to `defaultUrlTransform` but with a
user-supplied protocol allowlist.  This lets callers extend the
default set (e.g. add `tel:`) without reimplementing the full
sanitization logic.

`defaultUrlTransform` is now defined as `createUrlTransform()`,
keeping all existing behaviour unchanged.

Closes remarkjs#941
@github-actions github-actions Bot added the 👋 phase/new Post is being triaged automatically label May 28, 2026
@github-actions

This comment has been minimized.

@JounQin
Copy link
Copy Markdown
Member

JounQin commented May 28, 2026

kind of duplicate of #942

@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (fda7fa5) to head (1f9b88d).

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #943    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files            3         3            
  Lines         1743      1862   +119     
  Branches       123       129     +6     
==========================================
+ Hits          1743      1862   +119     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤞 phase/open Post is being triaged manually

Development

Successfully merging this pull request may close these issues.

Allow safeProtocol to be passed as a parameter for defaultUrlTransform

2 participants