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

various optimizations #651

Merged
merged 13 commits into from
Feb 17, 2025
Merged
5 changes: 5 additions & 0 deletions .changeset/great-rice-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Further optimize the plain text splitting regex.
5 changes: 5 additions & 0 deletions .changeset/grumpy-kids-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Remove redundant detectors when processing paragraphs.
5 changes: 5 additions & 0 deletions .changeset/hungry-bugs-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Replace some regexes with optimized functions to avoid polynomial time scenarios. Also fixes compatibility issues in some older browsers with the `trimEnd` API.
15 changes: 15 additions & 0 deletions .changeset/nervous-suns-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'markdown-to-jsx': patch
---

Optimize regexes and parsing to do less work.

```
+--------------------------+------------------------+-----------------------+
| │ simple markdown string │ large markdown string |
+--------------------------+------------------------+-----------------------+
| markdown-to-jsx (next) │ 86,340 ops/sec │ 307 ops/sec |
+--------------------------+------------------------+-----------------------+
| markdown-to-jsx (latest) │ 85,247 ops/sec │ 296 ops/sec |
+--------------------------+------------------------+-----------------------+
```
5 changes: 5 additions & 0 deletions .changeset/plenty-dodos-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Rework inline code syntax handling, handle escaped characters in code blocks correctly so they render without the backslash.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixture.md
6 changes: 3 additions & 3 deletions __snapshots__/index.compiler.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ line. To avoid this, you can backslash-escape the period:
</p>
<pre>
<code>
1986\\. What a great season.
1986. What a great season.
</code>
</pre>
<h3 id="precode">
Expand Down Expand Up @@ -1433,7 +1433,7 @@ escape it:
</p>
<pre>
<code>
\\*this text is surrounded by literal asterisks\\*
*this text is surrounded by literal asterisks*
</code>
</pre>
<h3 id="code">
Expand Down Expand Up @@ -1688,7 +1688,7 @@ backslashes before the asterisks, like this:
</p>
<pre>
<code>
\\*literal asterisks\\*
*literal asterisks*
</code>
</pre>
<p>
Expand Down
20 changes: 14 additions & 6 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cliProgress from 'cli-progress'
import * as fs from 'fs'
import SimpleMarkdown from 'simple-markdown'
import MarkdownIt from 'markdown-it'
import { compiler as latestCompiler } from 'markdown-to-jsx-latest'
import { compiler } from './dist/index.module.js'

const mdIt = new MarkdownIt()
Expand All @@ -19,12 +20,19 @@ const bar = new cliProgress.SingleBar(
let totalCycles

// add tests
suite
.addFunction('markdown-to-jsx', input => compiler(input))
.addFunction('simple-markdown', input =>
SimpleMarkdown.defaultReactOutput(SimpleMarkdown.defaultBlockParse(input))
)
.addFunction('markdown-it', input => mdIt.render(input))
const evals = suite
.addFunction('markdown-to-jsx (next)', input => compiler(input))
.addFunction('markdown-to-jsx (latest)', input => latestCompiler(input))

if (process.argv.includes('--all')) {
evals
.addFunction('simple-markdown', input =>
SimpleMarkdown.defaultReactOutput(SimpleMarkdown.defaultBlockParse(input))
)
.addFunction('markdown-it', input => mdIt.render(input))
}

evals
.addInput('simple markdown string', ['_Hello_ **world**!'])
.addInput('large markdown string', [fixture])
.on('start', () => {
Expand Down
2 changes: 1 addition & 1 deletion fixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ escape it:

<h3 id="code">Code</h3>

To indicate a span of code, wrap it with backtick quotes (`` ` ``).
To indicate a span of code, wrap it with backtick quotes (`\``).
Unlike a pre-formatted code block, a code span indicates code within a
normal paragraph. For example:

Expand Down
8 changes: 4 additions & 4 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ describe('inline textual elements', () => {

render(
compiler(
'*This should not misinterpret the asterisk ~~*~~ in the strikethrough.*'
String.raw`*This should not misinterpret the asterisk ~~\*~~ in the strikethrough.*`
)
)

Expand Down Expand Up @@ -512,7 +512,7 @@ describe('inline textual elements', () => {

render(
compiler(
'_This should not misinterpret the under_score that forms part of a word._'
`_This should not misinterpret the under\\_score that forms part of a word._`
)
)

Expand All @@ -535,7 +535,7 @@ describe('inline textual elements', () => {

it('replaces custom named character codes with unicode equivalents so React will render correctly', () => {
render(
compiler('Apostrophe&#39;s and less than ≤ equal', {
compiler('Apostrophe&#39;s and &le; equal', {
namedCodesToUnicode: {
le: '\u2264',
'#39': '\u0027',
Expand All @@ -545,7 +545,7 @@ describe('inline textual elements', () => {

expect(root.innerHTML).toMatchInlineSnapshot(`
<span>
Apostrophe's and less than ≤ equal
Apostrophe's and ≤ equal
</span>
`)
})
Expand Down
Loading
Loading