index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Process ![test]( x =100x200)
  2. // ^^^^^^^^ this size specification
  3. 'use strict';
  4. var sizeOf = require('./imsize');
  5. var parseImageSize = require('./helpers/parse_image_size');
  6. function image_with_size(md, options) {
  7. return function(state, silent) {
  8. var attrs,
  9. code,
  10. label,
  11. labelEnd,
  12. labelStart,
  13. pos,
  14. ref,
  15. res,
  16. title,
  17. width = '',
  18. height = '',
  19. token,
  20. tokens,
  21. start,
  22. href = '',
  23. oldPos = state.pos,
  24. max = state.posMax;
  25. if (state.src.charCodeAt(state.pos) !== 0x21/* ! */) { return false; }
  26. if (state.src.charCodeAt(state.pos + 1) !== 0x5B/* [ */) { return false; }
  27. labelStart = state.pos + 2;
  28. labelEnd = md.helpers.parseLinkLabel(state, state.pos + 1, false);
  29. // parser failed to find ']', so it's not a valid link
  30. if (labelEnd < 0) { return false; }
  31. pos = labelEnd + 1;
  32. if (pos < max && state.src.charCodeAt(pos) === 0x28/* ( */) {
  33. //
  34. // Inline link
  35. //
  36. // [link]( <href> "title" )
  37. // ^^ skipping these spaces
  38. pos++;
  39. for (; pos < max; pos++) {
  40. code = state.src.charCodeAt(pos);
  41. if (code !== 0x20 && code !== 0x0A) { break; }
  42. }
  43. if (pos >= max) { return false; }
  44. // [link]( <href> "title" )
  45. // ^^^^^^ parsing link destination
  46. start = pos;
  47. res = md.helpers.parseLinkDestination(state.src, pos, state.posMax);
  48. if (res.ok) {
  49. href = state.md.normalizeLink(res.str);
  50. if (state.md.validateLink(href)) {
  51. pos = res.pos;
  52. } else {
  53. href = '';
  54. }
  55. }
  56. // [link]( <href> "title" )
  57. // ^^ skipping these spaces
  58. start = pos;
  59. for (; pos < max; pos++) {
  60. code = state.src.charCodeAt(pos);
  61. if (code !== 0x20 && code !== 0x0A) { break; }
  62. }
  63. // [link]( <href> "title" )
  64. // ^^^^^^^ parsing link title
  65. res = md.helpers.parseLinkTitle(state.src, pos, state.posMax);
  66. if (pos < max && start !== pos && res.ok) {
  67. title = res.str;
  68. pos = res.pos;
  69. // [link]( <href> "title" )
  70. // ^^ skipping these spaces
  71. for (; pos < max; pos++) {
  72. code = state.src.charCodeAt(pos);
  73. if (code !== 0x20 && code !== 0x0A) { break; }
  74. }
  75. } else {
  76. title = '';
  77. }
  78. // [link]( <href> "title" =WxH )
  79. // ^^^^ parsing image size
  80. if (pos - 1 >= 0) {
  81. code = state.src.charCodeAt(pos - 1);
  82. // there must be at least one white spaces
  83. // between previous field and the size
  84. if (code === 0x20) {
  85. res = parseImageSize(state.src, pos, state.posMax);
  86. if (res.ok) {
  87. width = res.width;
  88. height = res.height;
  89. pos = res.pos;
  90. // [link]( <href> "title" =WxH )
  91. // ^^ skipping these spaces
  92. for (; pos < max; pos++) {
  93. code = state.src.charCodeAt(pos);
  94. if (code !== 0x20 && code !== 0x0A) { break; }
  95. }
  96. }
  97. }
  98. }
  99. if (pos >= max || state.src.charCodeAt(pos) !== 0x29/* ) */) {
  100. state.pos = oldPos;
  101. return false;
  102. }
  103. pos++;
  104. } else {
  105. //
  106. // Link reference
  107. //
  108. if (typeof state.env.references === 'undefined') { return false; }
  109. // [foo] [bar]
  110. // ^^ optional whitespace (can include newlines)
  111. for (; pos < max; pos++) {
  112. code = state.src.charCodeAt(pos);
  113. if (code !== 0x20 && code !== 0x0A) { break; }
  114. }
  115. if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
  116. start = pos + 1;
  117. pos = md.helpers.parseLinkLabel(state, pos);
  118. if (pos >= 0) {
  119. label = state.src.slice(start, pos++);
  120. } else {
  121. pos = labelEnd + 1;
  122. }
  123. } else {
  124. pos = labelEnd + 1;
  125. }
  126. // covers label === '' and label === undefined
  127. // (collapsed reference link and shortcut reference link respectively)
  128. if (!label) { label = state.src.slice(labelStart, labelEnd); }
  129. ref = state.env.references[md.utils.normalizeReference(label)];
  130. if (!ref) {
  131. state.pos = oldPos;
  132. return false;
  133. }
  134. href = ref.href;
  135. title = ref.title;
  136. }
  137. //
  138. // We found the end of the link, and know for a fact it's a valid link;
  139. // so all that's left to do is to call tokenizer.
  140. //
  141. if (!silent) {
  142. state.pos = labelStart;
  143. state.posMax = labelEnd;
  144. var newState = new state.md.inline.State(
  145. state.src.slice(labelStart, labelEnd),
  146. state.md,
  147. state.env,
  148. tokens = []
  149. );
  150. newState.md.inline.tokenize(newState);
  151. // if 'autofill' option is specified
  152. // and width/height are both blank,
  153. // they are filled automatically
  154. if (options) {
  155. if (options.autofill && width === '' && height === '') {
  156. try {
  157. var dimensions = sizeOf(href);
  158. width = dimensions.width;
  159. height = dimensions.height;
  160. } catch (e) { }
  161. }
  162. }
  163. token = state.push('image', 'img', 0);
  164. token.attrs = attrs = [ [ 'src', href ],
  165. [ 'alt', '' ] ];
  166. token.children = tokens;
  167. if (title) {
  168. attrs.push([ 'title', title ]);
  169. }
  170. if (width !== '') {
  171. attrs.push([ 'width', width ]);
  172. }
  173. if (height !== '') {
  174. attrs.push([ 'height', height ]);
  175. }
  176. }
  177. state.pos = pos;
  178. state.posMax = max;
  179. return true;
  180. };
  181. }
  182. module.exports = function imsize_plugin(md, options) {
  183. md.inline.ruler.before('emphasis', 'image', image_with_size(md, options));
  184. };