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

Fix: reference image markdown when there is not reference. #525

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 24 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,20 @@ describe('images', () => {
</p>
`)
})

it('should handle an image reference with a missing reference', () => {
render(
compiler(theredoc`
![test][1]
`)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<span>
![test][1]
</span>
`)
})
})

describe('links', () => {
Expand Down Expand Up @@ -848,6 +862,16 @@ describe('links', () => {
`)
})

it('should handle a link reference with a missing reference', () => {
render(compiler('[foo][1]'))

expect(root.innerHTML).toMatchInlineSnapshot(`
<span>
[foo][1]
</span>
`)
})

it('list item should break paragraph', () => {
render(compiler('foo\n- item'))

Expand Down
16 changes: 13 additions & 3 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1660,23 +1660,33 @@
refImage: {
_match: simpleInlineRegex(REFERENCE_IMAGE_R),
_order: Priority.MAX,
_parse(capture) {
_parse(capture, parse, state) {
return {
_alt: capture[1] || undefined,
_fallbackContent: parse(
capture[0].replace(SQUARE_BRACKETS_R, '\\$1'),

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.
state
),
_ref: capture[2],
}
},
_react(node, output, state) {
return (
return refs[node._ref] ? (
<img
key={state._key}
alt={node._alt}
src={sanitizeUrl(refs[node._ref]._target)}
title={refs[node._ref]._title}
/>
) : (
<span key={state._key}>{ output(node._fallbackContent, state) }</span>
)
},
} as MarkdownToJSX.Rule<{ _alt?: string; _ref: string }>,
} as MarkdownToJSX.Rule<{
_alt?: string
_fallbackContent: MarkdownToJSX.ParserResult
_ref: string
}>,

refLink: {
_match: inlineRegex(REFERENCE_LINK_R),
Expand Down
Loading