Skip to content

Commit

Permalink
[markdown mode] Add more token styles for image references
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
HoverBaum authored and marijnh committed Jul 1, 2016
1 parent 24d86a8 commit 34b3a7b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
30 changes: 26 additions & 4 deletions mode/markdown/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
list2: "variable-3",
list3: "keyword",
hr: "hr",
image: "tag",
image: "image",
imageAltText: "image-alt-text",
imageMarker: "image-marker",
formatting: "formatting",
linkInline: "link",
linkEmail: "link",
Expand Down Expand Up @@ -313,6 +315,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
if (state.strikethrough) { styles.push(tokenTypes.strikethrough); }
if (state.linkText) { styles.push(tokenTypes.linkText); }
if (state.code) { styles.push(tokenTypes.code); }
if (state.image) { styles.push(tokenTypes.image); }
if (state.imageAltText) { styles.push(tokenTypes.imageAltText, "link"); }
if (state.imageMarker) { styles.push(tokenTypes.imageMarker); }
}

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

if (ch === '!' && stream.match(/\[[^\]]*\] ?(?:\(|\[)/, false)) {
stream.match(/\[[^\]]*\]/);
state.imageMarker = true;
state.image = true;
if (modeCfg.highlightFormatting) state.formatting = "image";
return getType(state);
}

if (ch === '[' && state.imageMarker) {
state.imageMarker = false;
state.imageAltText = true
if (modeCfg.highlightFormatting) state.formatting = "image";
return getType(state);
}

if (ch === ']' && state.imageAltText) {
if (modeCfg.highlightFormatting) state.formatting = "image";
var type = getType(state);
state.imageAltText = false;
state.image = false;
state.inline = state.f = linkHref;
return tokenTypes.image;
return type;
}

if (ch === '[' && stream.match(/[^\]]*\](\(.*\)| ?\[.*?\])/, false)) {
if (ch === '[' && stream.match(/[^\]]*\](\(.*\)| ?\[.*?\])/, false) && !state.image) {
state.linkText = true;
if (modeCfg.highlightFormatting) state.formatting = "link";
return getType(state);
Expand Down
29 changes: 24 additions & 5 deletions mode/markdown/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"list3" : "override-list3",
"hr" : "override-hr",
"image" : "override-image",
"imageAltText": "override-image-alt-text",
"imageMarker": "override-image-marker",
"linkInline" : "override-link-inline",
"linkEmail" : "override-link-email",
"linkText" : "override-link-text",
Expand Down Expand Up @@ -89,6 +91,9 @@
FT("formatting_escape",
"[formatting-escape \\*]");

FT("formatting_image",
"[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 )]");

MT("plainText",
"foo");

Expand Down Expand Up @@ -589,6 +594,20 @@
MT("hrDashLong",
"[hr ---------------------------------------]");

//Images
MT("Images",
"[image&image-marker !][image&image-alt-text&link [[alt text]]][string&url (http://link.to/image.jpg)]")

//Images with highlight alt text
MT("imageEm",
"[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)]");

MT("imageStrong",
"[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)]");

MT("imageEmStrong",
"[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)]");

// Inline link with title
MT("linkTitle",
"[link [[foo]]][string&url (http://example.com/ \"bar\")] hello");
Expand All @@ -599,7 +618,7 @@

// Inline link with image
MT("linkImage",
"[link [[][tag ![[foo]]][string&url (http://example.com/)][link ]]][string&url (http://example.com/)] bar");
"[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");

// Inline link with Em
MT("linkEm",
Expand All @@ -615,15 +634,15 @@

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

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

// Image with asterisks
MT("imageAsterisks",
"[tag ![[*foo*]]][string&url (http://example.com/)] bar");
"[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");

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

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

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

0 comments on commit 34b3a7b

Please sign in to comment.