Skip to content

Commit 34b3a7b

Browse files
HoverBaummarijnh
authored andcommitted
[markdown mode] Add more token styles for image references
- Images now come with tons of information to style them. This mainly enables to style alt-texts and potentially make them look like links. While still differentialting between links and images. Closes codemirror#4082
1 parent 24d86a8 commit 34b3a7b

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

mode/markdown/markdown.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
6363
list2: "variable-3",
6464
list3: "keyword",
6565
hr: "hr",
66-
image: "tag",
66+
image: "image",
67+
imageAltText: "image-alt-text",
68+
imageMarker: "image-marker",
6769
formatting: "formatting",
6870
linkInline: "link",
6971
linkEmail: "link",
@@ -313,6 +315,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
313315
if (state.strikethrough) { styles.push(tokenTypes.strikethrough); }
314316
if (state.linkText) { styles.push(tokenTypes.linkText); }
315317
if (state.code) { styles.push(tokenTypes.code); }
318+
if (state.image) { styles.push(tokenTypes.image); }
319+
if (state.imageAltText) { styles.push(tokenTypes.imageAltText, "link"); }
320+
if (state.imageMarker) { styles.push(tokenTypes.imageMarker); }
316321
}
317322

318323
if (state.header) { styles.push(tokenTypes.header, tokenTypes.header + "-" + state.header); }
@@ -432,12 +437,29 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
432437
}
433438

434439
if (ch === '!' && stream.match(/\[[^\]]*\] ?(?:\(|\[)/, false)) {
435-
stream.match(/\[[^\]]*\]/);
440+
state.imageMarker = true;
441+
state.image = true;
442+
if (modeCfg.highlightFormatting) state.formatting = "image";
443+
return getType(state);
444+
}
445+
446+
if (ch === '[' && state.imageMarker) {
447+
state.imageMarker = false;
448+
state.imageAltText = true
449+
if (modeCfg.highlightFormatting) state.formatting = "image";
450+
return getType(state);
451+
}
452+
453+
if (ch === ']' && state.imageAltText) {
454+
if (modeCfg.highlightFormatting) state.formatting = "image";
455+
var type = getType(state);
456+
state.imageAltText = false;
457+
state.image = false;
436458
state.inline = state.f = linkHref;
437-
return tokenTypes.image;
459+
return type;
438460
}
439461

440-
if (ch === '[' && stream.match(/[^\]]*\](\(.*\)| ?\[.*?\])/, false)) {
462+
if (ch === '[' && stream.match(/[^\]]*\](\(.*\)| ?\[.*?\])/, false) && !state.image) {
441463
state.linkText = true;
442464
if (modeCfg.highlightFormatting) state.formatting = "link";
443465
return getType(state);

mode/markdown/test.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"list3" : "override-list3",
2323
"hr" : "override-hr",
2424
"image" : "override-image",
25+
"imageAltText": "override-image-alt-text",
26+
"imageMarker": "override-image-marker",
2527
"linkInline" : "override-link-inline",
2628
"linkEmail" : "override-link-email",
2729
"linkText" : "override-link-text",
@@ -89,6 +91,9 @@
8991
FT("formatting_escape",
9092
"[formatting-escape \\*]");
9193

94+
FT("formatting_image",
95+
"[formatting&formatting-image&image&image-marker !][formatting&formatting-image&image&image-alt-text&link [[][image&image-alt-text&link alt text][formatting&formatting-image&image&image-alt-text&link ]]][formatting&formatting-link-string&string&url (][url&string http://link.to/image.jpg][formatting&formatting-link-string&string&url )]");
96+
9297
MT("plainText",
9398
"foo");
9499

@@ -589,6 +594,20 @@
589594
MT("hrDashLong",
590595
"[hr ---------------------------------------]");
591596

597+
//Images
598+
MT("Images",
599+
"[image&image-marker !][image&image-alt-text&link [[alt text]]][string&url (http://link.to/image.jpg)]")
600+
601+
//Images with highlight alt text
602+
MT("imageEm",
603+
"[image&image-marker !][image&image-alt-text&link [[][image-alt-text&em&image&link *alt text*][image&image-alt-text&link ]]][string&url (http://link.to/image.jpg)]");
604+
605+
MT("imageStrong",
606+
"[image&image-marker !][image&image-alt-text&link [[][image-alt-text&strong&image&link **alt text**][image&image-alt-text&link ]]][string&url (http://link.to/image.jpg)]");
607+
608+
MT("imageEmStrong",
609+
"[image&image-marker !][image&image-alt-text&link [[][image-alt-text&image&strong&link **][image&image-alt-text&em&strong&link *alt text**][image&image-alt-text&em&link *][image&image-alt-text&link ]]][string&url (http://link.to/image.jpg)]");
610+
592611
// Inline link with title
593612
MT("linkTitle",
594613
"[link [[foo]]][string&url (http://example.com/ \"bar\")] hello");
@@ -599,7 +618,7 @@
599618

600619
// Inline link with image
601620
MT("linkImage",
602-
"[link [[][tag ![[foo]]][string&url (http://example.com/)][link ]]][string&url (http://example.com/)] bar");
621+
"[link [[][link&image&image-marker !][link&image&image-alt-text&link [[alt text]]][string&url (http://link.to/image.jpg)][link ]]][string&url (http://example.com/)] bar");
603622

604623
// Inline link with Em
605624
MT("linkEm",
@@ -615,15 +634,15 @@
615634

616635
// Image with title
617636
MT("imageTitle",
618-
"[tag ![[foo]]][string&url (http://example.com/ \"bar\")] hello");
637+
"[image&image-marker !][image&image-alt-text&link [[alt text]]][string&url (http://example.com/ \"bar\")] hello");
619638

620639
// Image without title
621640
MT("imageNoTitle",
622-
"[tag ![[foo]]][string&url (http://example.com/)] bar");
641+
"[image&image-marker !][image&image-alt-text&link [[alt text]]][string&url (http://example.com/)] bar");
623642

624643
// Image with asterisks
625644
MT("imageAsterisks",
626-
"[tag ![[*foo*]]][string&url (http://example.com/)] bar");
645+
"[image&image-marker !][image&image-alt-text&link [[ ][image&image-alt-text&em&link *alt text*][image&image-alt-text&link ]]][string&url (http://link.to/image.jpg)] bar");
627646

628647
// Not a link. Should be normal text due to square brackets being used
629648
// regularly in text, especially in quoted material, and no space is allowed
@@ -878,7 +897,7 @@
878897
"[override-hr * * *]");
879898

880899
TokenTypeOverrideTest("overrideImage",
881-
"[override-image ![[foo]]][override-link-href&url (http://example.com/)]")
900+
"[override-image&override-image-marker !][override-image&override-image-alt-text&link [[alt text]]][override-link-href&url (http://link.to/image.jpg)]");
882901

883902
TokenTypeOverrideTest("overrideLinkText",
884903
"[override-link-text [[foo]]][override-link-href&url (http://example.com)]");

0 commit comments

Comments
 (0)