Skip to content

Commit

Permalink
fix: tables must start with a pipe
Browse files Browse the repository at this point in the history
Fixes #595
  • Loading branch information
quantizor committed Nov 14, 2024
1 parent 857809a commit 0274445
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-lobsters-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Fix false detection of tables in some scenarios.
14 changes: 12 additions & 2 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,16 @@ describe('headings', () => {
</h1>
`)
})

it('#595 regression - handle pipe character inside header', () => {
render(compiler('# Heading | text'))

expect(root.innerHTML).toMatchInlineSnapshot(`
<h1 id="heading--text">
Heading | text
</h1>
`)
})
})

describe('images', () => {
Expand Down Expand Up @@ -1851,7 +1861,7 @@ describe('GFM tables', () => {
it('should handle a basic table', () => {
render(
compiler(theredoc`
foo|bar
|foo|bar|
---|---
1 |2
`)
Expand Down Expand Up @@ -1886,7 +1896,7 @@ describe('GFM tables', () => {
it('should handle a table with aligned columns', () => {
render(
compiler(theredoc`
foo|bar|baz
|foo|bar|baz|
--:|:---:|:--
1|2|3
`)
Expand Down
14 changes: 2 additions & 12 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ const LINK_AUTOLINK_BARE_URL_R = /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/
const LINK_AUTOLINK_MAILTO_R = /^<([^ >]+@[^ >]+)>/
const LINK_AUTOLINK_R = /^<([^ >]+:\/[^ >]+)>/
const CAPTURE_LETTER_AFTER_HYPHEN = /-([a-z])?/gi
const NP_TABLE_R =
/^(.*\|.*)\n(?: *(\|? *[-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*))?\n?/
const NP_TABLE_R = /^(\|.*)\n(?: *(\|? *[-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*))?\n?/
const PARAGRAPH_R = /^[^\n]+(?: \n|\n{2,})/
const REFERENCE_IMAGE_OR_LINK = /^\[([^\]]*)\]:\s+<?([^\s>]+)>?\s*("([^"]*)")?/
const REFERENCE_IMAGE_R = /^!\[([^\]]*)\] ?\[([^\]]*)\]/
Expand Down Expand Up @@ -926,20 +925,11 @@ function anyScopeRegex(regex: RegExp) {
}
}

function matchParagraph(
source: string,
state: MarkdownToJSX.State,
prevCapturedString?: string
) {
function matchParagraph(source: string, state: MarkdownToJSX.State) {
if (state.inline || state.simple) {
return null
}

if (prevCapturedString && !prevCapturedString.endsWith('\n')) {
// don't match continuation of a line
return null
}

let match = ''

source.split('\n').every(line => {
Expand Down

0 comments on commit 0274445

Please sign in to comment.