Skip to content

Commit

Permalink
feat: add option to disable autolinking (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor authored Nov 12, 2024
1 parent fb3d716 commit 2281a4d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
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 @@ -1648,9 +1648,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 @@ -2309,6 +2310,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

0 comments on commit 2281a4d

Please sign in to comment.