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

Disable Autolinking Option #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/markdown-to-jsx.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/markdown-to-jsx.js.map

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3552,6 +3552,104 @@ describe('options.slugify', () => {
})
})

describe('options.disableAutolinking', () => {
it('should not handle autolink style', () => {
render(compiler('<https://google.com>', { disableAutolinking: true }))

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

it('should not handle autolinks after a paragraph (regression)', () => {
render(
compiler(
theredoc`
**autolink** style

<https://google.com>
`,
{ disableAutolinking: true }
)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<div>
<p>
<strong>
autolink
</strong>
style
</p>
<p>
&lt;https://google.com&gt;
</p>
</div>
`)
})

it('should not handle mailto autolinks after a paragraph', () => {
render(
compiler(
theredoc`
**autolink** style

<mailto:[email protected]>
`,
{ disableAutolinking: true }
)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<div>
<p>
<strong>
autolink
</strong>
style
</p>
<p>
&lt;mailto:[email protected]&gt;
</p>
</div>
`)
})

it('should not handle a mailto autolink', () => {
render(
compiler('<mailto:[email protected]>', { disableAutolinking: true })
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<span>
&lt;mailto:[email protected]&gt;
</span>
`)
})

it('should not autolink email and add a mailto: prefix', () => {
render(compiler('<[email protected]>', { disableAutolinking: true }))

expect(root.innerHTML).toMatchInlineSnapshot(`
<span>
&lt;[email protected]&gt;
</span>
`)
})

it('should not automatically link found URLs', () => {
render(compiler('https://google.com', { disableAutolinking: true }))

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

describe('overrides', () => {
it('should substitute the appropriate JSX tag if given a component', () => {
class FakeParagraph extends React.Component {
Expand Down
141 changes: 75 additions & 66 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ export namespace MarkdownToJSX {
* HTML IDs for anchor linking purposes.
*/
slugify: (source: string) => string

/**
* Override the default parser rules.
* This will disable detection of URLs and email addresses in the text
* and disable automatic linking using anchor tag.
*/
disableAutolinking: boolean
}>
}

Expand Down Expand Up @@ -1543,72 +1550,6 @@ export function compiler(
_title?: string
}>,

// https://daringfireball.net/projects/markdown/syntax#autolink
linkAngleBraceStyleDetector: {
_match: inlineRegex(LINK_AUTOLINK_R),
_order: Priority.MAX,
_parse(capture /*, parse, state*/) {
return {
_content: [
{
_content: capture[1],
type: 'text',
},
],
_target: capture[1],
type: 'link',
}
},
},

linkBareUrlDetector: {
_match: (source, state) => {
if (state._inAnchor) {
return null
}
return inlineRegex(LINK_AUTOLINK_BARE_URL_R)(source, state)
},
_order: Priority.MAX,
_parse(capture /*, parse, state*/) {
return {
_content: [
{
_content: capture[1],
type: 'text',
},
],
_target: capture[1],
_title: undefined,
type: 'link',
}
},
},

linkMailtoDetector: {
_match: inlineRegex(LINK_AUTOLINK_MAILTO_R),
_order: Priority.MAX,
_parse(capture /*, parse, state*/) {
let address = capture[1]
let target = capture[1]

// Check for a `mailto:` already existing in the link:
if (!AUTOLINK_MAILTO_CHECK_R.test(target)) {
target = 'mailto:' + target
}

return {
_content: [
{
_content: address.replace('mailto:', ''),
type: 'text',
},
],
_target: target,
type: 'link',
}
},
},

orderedList: generateListRule(h, ORDERED),
unorderedList: generateListRule(h, UNORDERED),

Expand Down Expand Up @@ -1870,6 +1811,74 @@ export function compiler(
// }
// })

if (!options.disableAutolinking) {
// https://daringfireball.net/projects/markdown/syntax#autolink
rules.linkAngleBraceStyleDetector = {
_match: inlineRegex(LINK_AUTOLINK_R),
_order: Priority.MAX,
_parse(capture /*, parse, state*/) {
return {
_content: [
{
_content: capture[1],
type: 'text',
},
],
_target: capture[1],
type: 'link',
}
},
}

rules.linkBareUrlDetector = {
_match: (source, state) => {
if (state._inAnchor) {
return null
}
return inlineRegex(LINK_AUTOLINK_BARE_URL_R)(source, state)
},
_order: Priority.MAX,
_parse(capture /*, parse, state*/) {
return {
_content: [
{
_content: capture[1],
type: 'text',
},
],
_target: capture[1],
_title: undefined,
type: 'link',
}
},
}

rules.linkMailtoDetector = {
_match: inlineRegex(LINK_AUTOLINK_MAILTO_R),
_order: Priority.MAX,
_parse(capture /*, parse, state*/) {
let address = capture[1]
let target = capture[1]

// Check for a `mailto:` already existing in the link:
if (!AUTOLINK_MAILTO_CHECK_R.test(target)) {
target = 'mailto:' + target
}

return {
_content: [
{
_content: address.replace('mailto:', ''),
type: 'text',
},
],
_target: target,
type: 'link',
}
},
}
}

if (options.disableParsingRawHTML !== true) {
rules.htmlBlock = {
/**
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "markdown-to-jsx",
"name": "@refrens/markdown-to-jsx",
"description": "Convert markdown to JSX with ease for React and React-like projects. Super lightweight and highly configurable.",
"homepage": "https://probablyup.github.io/markdown-to-jsx",
"license": "MIT",
Expand All @@ -16,7 +16,7 @@
"html"
],
"author": "Evan Jacobs <[email protected]>",
"repository": "probablyup/markdown-to-jsx",
"repository": "refrens/markdown-to-jsx",
"bugs": "https://github.com/probablyup/markdown-to-jsx/issues",
"files": [
"dist",
Expand Down Expand Up @@ -83,6 +83,9 @@
"size": "size-limit",
"benchmark": "node -r esm benchmark.js"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"mangle": {
"regex": "^_"
},
Expand Down