Skip to content

Commit

Permalink
refactor: improve inline code performance
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Jan 20, 2025
1 parent 6de5543 commit 9332d50
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
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
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
15 changes: 10 additions & 5 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const BREAK_THEMATIC_R = /^(?:( *[-*_])){3,} *(?:\n *)+\n/
const CODE_BLOCK_FENCED_R =
/^(?: {1,3})?(`{3,}|~{3,}) *(\S+)? *([^\n]*?)?\n([\s\S]*?)(?:\1\n?|$)/
const CODE_BLOCK_R = /^(?: {4}[^\n]+\n*)+(?:\n *)+\n?/
const CODE_INLINE_R = /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/
const CODE_INLINE_R = /^(`+)((?:\\`|[^`])+)\1/
const CONSECUTIVE_NEWLINE_R = /^(?:\n *)*\n/
const CR_NEWLINE_R = /\r\n?/g

Expand Down Expand Up @@ -320,6 +320,7 @@ const TEXT_MARKED_R = new RegExp(`^==${INLINE_SKIP_R}==`)
const TEXT_STRIKETHROUGHED_R = new RegExp(`^~~${INLINE_SKIP_R}~~`)

const TEXT_ESCAPED_R = /^\\([^0-9A-Za-z\s])/
const TEXT_UNESCAPE_R = /\\([^0-9A-Za-z\s])/g

/**
* Always take the first character, then eagerly take text until a double space
Expand Down Expand Up @@ -460,6 +461,7 @@ function generateListRule(
.match(LIST_ITEM_R)

let lastItemWasAParagraph = false

const itemContent = items.map(function (item, i) {
// We need to see how far indented the item is:
const space = LIST_ITEM_PREFIX_R.exec(item)[0].length
Expand Down Expand Up @@ -495,7 +497,7 @@ function generateListRule(
containsBlocks || (isLastItem && lastItemWasAParagraph)
lastItemWasAParagraph = thisItemIsAParagraph

// backup our state for restoration afterwards. We're going to
// backup our state for delta afterwards. We're going to
// want to set state.list to true, and state.inline depending
// on our list's looseness.
const oldStateInline = state.inline
Expand Down Expand Up @@ -1400,7 +1402,10 @@ export function compiler(
parse(capture /*, parse, state*/) {
return {
lang: undefined,
text: capture[0].replace(/^ {4}/gm, '').replace(/\n+$/, ''),
text: capture[0]
.replace(/^ {4}/gm, '')
.replace(/\n+$/, '')
.replaceAll(TEXT_UNESCAPE_R, '$1'),
}
},

Expand Down Expand Up @@ -1430,7 +1435,7 @@ export function compiler(
// if capture[3] it's additional metadata
attrs: attrStringToMap('code', capture[3] || ''),
lang: capture[2] || undefined,
text: capture[4],
text: capture[4].replaceAll(TEXT_UNESCAPE_R, '$1'),
type: RuleType.codeBlock,
}
},
Expand All @@ -1441,7 +1446,7 @@ export function compiler(
order: Priority.LOW,
parse(capture /*, parse, state*/) {
return {
text: capture[2],
text: capture[2].replaceAll(TEXT_UNESCAPE_R, '$1'),
}
},
render(node, output, state) {
Expand Down

0 comments on commit 9332d50

Please sign in to comment.