Skip to content

Commit 22ce228

Browse files
authored
Merge pull request #608 from mayraamaral/master
chore: adjust the regex for previewing images so that it only attempts to render if the image has a bracket immediately preceding the parentheses
2 parents c93033b + e7a56e1 commit 22ce228

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference types="cypress" />
2+
3+
describe('Image rendering', () => {
4+
5+
const imageUrl = 'https://picsum.photos/id/237/150';
6+
7+
beforeEach(() => {
8+
cy.visit(__dirname + '/index.html');
9+
cy.intercept('GET', imageUrl).as('image');
10+
});
11+
12+
it('must render an image inside the editor', () => {
13+
cy.get('.EasyMDEContainer').should('be.visible');
14+
cy.get('#textarea').should('not.be.visible');
15+
16+
cy.get('.EasyMDEContainer .CodeMirror').type(imageUrl);
17+
cy.get('.EasyMDEContainer .CodeMirror').type('{home}![Dog!]({end})');
18+
19+
cy.wait('@image');
20+
21+
cy.get(`.EasyMDEContainer [data-img-src="${imageUrl}"]`).should('be.visible');
22+
23+
cy.previewOn();
24+
25+
cy.get('.EasyMDEContainer .editor-preview').should('contain.html', `<p><img src="${imageUrl}" alt="Dog!"></p>`);
26+
});
27+
28+
it('must be able to handle parentheses inside image alt text', () => {
29+
cy.get('.EasyMDEContainer').should('be.visible');
30+
cy.get('#textarea').should('not.be.visible');
31+
32+
cy.get('.EasyMDEContainer .CodeMirror').type(imageUrl);
33+
cy.get('.EasyMDEContainer .CodeMirror').type('{home}![Dog! (He\'s a good boy!)]({end})');
34+
35+
cy.wait('@image');
36+
37+
cy.get(`.EasyMDEContainer [data-img-src="${imageUrl}"]`).should('be.visible');
38+
39+
cy.previewOn();
40+
41+
cy.get('.EasyMDEContainer .editor-preview').should('contain.html', `<p><img src="${imageUrl}" alt="Dog! (He's a good boy!)"></p>`);
42+
});
43+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Default</title>
7+
<link rel="stylesheet" href="../../../dist/easymde.min.css">
8+
<script src="../../../dist/easymde.min.js"></script>
9+
</head>
10+
11+
<body>
12+
<textarea id="textarea"></textarea>
13+
<script>
14+
const easyMDE = new EasyMDE({
15+
previewImagesInEditor: true,
16+
});
17+
</script>
18+
</body>
19+
20+
</html>

src/js/easymde.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,7 @@ EasyMDE.prototype.render = function (el) {
22512251
return;
22522252
}
22532253
if (!parentEl.hasAttribute('data-img-src')) {
2254-
var srcAttr = parentEl.innerText.match('\\((.*)\\)'); // might require better parsing according to markdown spec
2254+
var srcAttr = parentEl.innerText.match(/!\[.*?\]\((.*?)\)/); // might require better parsing according to markdown spec
22552255
if (!window.EMDEimagesCache) {
22562256
window.EMDEimagesCache = {};
22572257
}

0 commit comments

Comments
 (0)