Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to disable autolinking #616

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .changeset/wicked-radios-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'markdown-to-jsx': minor
---

Add `options.disableAutoLink` to customize bare URL handling behavior.

By default, bare URLs in the markdown document will be converted into an anchor tag. This behavior can be disabled if desired.

```jsx
<Markdown options={{ disableAutoLink: true }}>
The URL https://quantizor.dev will not be rendered as an anchor tag.
</Markdown>

// or

compiler(
'The URL https://quantizor.dev will not be rendered as an anchor tag.',
{ disableAutoLink: true }
)

// renders:

<span>
The URL https://quantizor.dev will not be rendered as an anchor tag.
</span>
```
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The most lightweight, customizable React markdown component.
- [options.sanitizer](#optionssanitizer)
- [options.slugify](#optionsslugify)
- [options.namedCodesToUnicode](#optionsnamedcodestounicode)
- [options.disableAutoLink](#optionsdisableautolink)
- [options.disableParsingRawHTML](#optionsdisableparsingrawhtml)
- [Syntax highlighting](#syntax-highlighting)
- [Getting the smallest possible bundle size](#getting-the-smallest-possible-bundle-size)
Expand Down Expand Up @@ -507,9 +508,32 @@ compiler('This text is &le; than this text.', namedCodesToUnicode: {
<p>This text is ≤ than this text.</p>
```

#### options.disableAutoLink

By default, bare URLs in the markdown document will be converted into an anchor tag. This behavior can be disabled if desired.

```jsx
<Markdown options={{ disableAutoLink: true }}>
The URL https://quantizor.dev will not be rendered as an anchor tag.
</Markdown>

// or

compiler(
'The URL https://quantizor.dev will not be rendered as an anchor tag.',
{ disableAutoLink: true }
)

// renders:

<span>
The URL https://quantizor.dev will not be rendered as an anchor tag.
</span>
```

#### options.disableParsingRawHTML

By default, raw HTML is parsed to JSX. This behavior can be disabled with this option.
By default, raw HTML is parsed to JSX. This behavior can be disabled if desired.

```jsx
<Markdown options={{ disableParsingRawHTML: true }}>
Expand Down
10 changes: 10 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,16 @@ describe('links', () => {
`)
})

it('should not link bare URL if disabled via options', () => {
render(compiler('https://google.com', { disableAutoLink: true }))

expect(root.innerHTML).toMatchInlineSnapshot(`
<span>
https://google.com
</span>
`)
})

it('should not sanitize markdown when explicitly disabled', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
jest.spyOn(console, 'error').mockImplementation(() => {})
Expand Down
9 changes: 8 additions & 1 deletion index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1649,9 +1649,10 @@ export function compiler(

[RuleType.linkBareUrlDetector]: {
match: (source, state) => {
if (state.inAnchor) {
if (state.inAnchor || options.disableAutoLink) {
return null
}

return inlineRegex(LINK_AUTOLINK_BARE_URL_R)(source, state)
},
order: Priority.MAX,
Expand Down Expand Up @@ -2313,6 +2314,12 @@ export namespace MarkdownToJSX {
...children: React.ReactChild[]
) => React.ReactChild

/**
* The library automatically generates an anchor tag for bare URLs included in the markdown
* document, but this behavior can be disabled if desired.
*/
disableAutoLink: boolean

/**
* Disable the compiler's best-effort transcription of provided raw HTML
* into JSX-equivalent. This is the functionality that prevents the need to
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"size-limit": [
{
"path": "./dist/index.module.js",
"limit": "6.2 kB"
"limit": "6.25 kB"
},
{
"path": "./dist/index.modern.js",
Expand Down
Loading