-
Notifications
You must be signed in to change notification settings - Fork 137
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
Add support for inline image Markdown #658
Changes from all commits
c6698c6
4b7ed95
4111539
0764cf0
3e89f3f
f843a1f
c904af2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1879,3 +1879,59 @@ describe('multi-level blockquote', () => { | |
expect(parser.replace(quoteTestStartString)).toBe(quoteTestReplacedString); | ||
}); | ||
}); | ||
|
||
describe('Image markdown conversion to html tag', () => { | ||
test('Single image', () => { | ||
const testString = ''; | ||
const resultString = '<img src="https://example.com/image.png" alt="test" />'; | ||
expect(parser.replace(testString)).toBe(resultString); | ||
}); | ||
|
||
test('Text containing images', () => { | ||
const testString = 'An image of a banana:  an image of a developer: '; | ||
const resultString = 'An image of a banana: <img src="https://example.com/banana.png" alt="banana" /> an image of a developer: <img src="https://example.com/developer.png" alt="dev" />'; | ||
expect(parser.replace(testString)).toBe(resultString); | ||
}); | ||
|
||
// Currently any markdown used inside the square brackets is converted to html string in the alt attribute | ||
// The attributes should only contain plain text, but it doesn't seem possible to convert markdown to plain text | ||
// or let the parser know not to convert markdown to html for html attributes | ||
xtest('Image with alt text containing markdown', () => { | ||
const testString = ''; | ||
const resultString = '<img src="https://example.com/image.png" alt="*bold* _italic_ ~strike~" />'; | ||
expect(parser.replace(testString)).toBe(resultString); | ||
}); | ||
Comment on lines
+1896
to
+1903
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interaction of image Markdown with other Markdown features (e.g., bold, italic) currently exhibits partial functionality issues. Specifically, content intended for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created a fix for this in a separate PR: #661 |
||
|
||
test('Text containing image and autolink', () => { | ||
const testString = 'An image of a banana:  an autolink: example.com'; | ||
const resultString = 'An image of a banana: <img src="https://example.com/banana.png" alt="banana" /> an autolink: <a href="https://example.com" target="_blank" rel="noreferrer noopener">example.com</a>'; | ||
expect(parser.replace(testString)).toBe(resultString); | ||
}); | ||
|
||
test('Image with raw data attributes', () => { | ||
const testString = ''; | ||
const resultString = '<img src="https://example.com/image.png" alt="test" data-raw-href="https://example.com/image.png" data-link-variant="labeled" />'; | ||
expect(parser.replace(testString, {shouldKeepRawInput: true})).toBe(resultString); | ||
}); | ||
|
||
test('Image with invalid url should remain unchanged', () => { | ||
const testString = ''; | ||
expect(parser.replace(testString)).toBe(testString); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a test for something like:
Tbh not sure if xss like that is even remotely possible based on how the markdown parser works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That particular example is converted into an
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome thanks! |
||
|
||
test('Trying to pass additional attributes should not create an <img>', () => { | ||
const testString = ''; | ||
|
||
// It seems the autolink rule is applied. We might need to update this test if the autolink rule is changed | ||
// Ideally this test should return the same string as the input, or an <img> tag with the alt attribute set to | ||
// "test" and no other attributes | ||
const resultString = ''; | ||
expect(parser.replace(testString)).toBe(resultString); | ||
}); | ||
|
||
test('Trying to inject additional attributes should not work', () => { | ||
const testString = ''; | ||
const resultString = '<img src=\"https://example.com/image.png\" alt=\"test" onerror="alert('xss')\" />'; | ||
expect(parser.replace(testString)).toBe(resultString); | ||
}); | ||
Comment on lines
+1922
to
+1936
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the PR to include 2 more tests here, other updates are minor code style/formatting changes applied automatically |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xtest
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
xtest
function in Jest is a way to skip a particular test. When you prefix a test withxtest
instead oftest
, Jest will ignore this test during the test run. This is useful for temporarily disabling a test that might be failing due to unfinished features or bugs that are not yet resolved, without having to comment out the test code. It allows you to easily re-enable the test in the future by changingxtest
back totest
.