Browse Source

Render image labels as text

pull/14/head
Alex Kocharin 10 years ago
parent
commit
d7221761fb
  1. 20
      lib/renderer.js
  2. 23
      lib/rules_inline/links.js
  3. 108
      test/fixtures/commonmark/bad.txt
  4. 73
      test/fixtures/commonmark/good.txt

20
lib/renderer.js

@ -143,10 +143,10 @@ rules.link_close = function (/* tokens, idx, options, env */) {
}; };
rules.image = function (tokens, idx, options /*, env */) { rules.image = function (tokens, idx, options, env, self) {
var src = ' src="' + escapeHtml(tokens[idx].src) + '"'; var src = ' src="' + escapeHtml(tokens[idx].src) + '"';
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"'; var alt = ' alt="' + self.renderInlineAsText(tokens[idx].tokens, options, env) + '"';
var suffix = options.xhtmlOut ? ' /' : ''; var suffix = options.xhtmlOut ? ' /' : '';
return '<img' + src + alt + title + suffix + '>'; return '<img' + src + alt + title + suffix + '>';
}; };
@ -343,6 +343,22 @@ Renderer.prototype.renderInline = function (tokens, options, env) {
}; };
Renderer.prototype.renderInlineAsText = function (tokens, options, env) {
var result = '',
_rules = this.rules;
for (var i = 0, len = tokens.length; i < len; i++) {
if (tokens[i].type === 'text') {
result += _rules.text(tokens, i, options, env, this);
} else if (tokens[i].type === 'image') {
result += this.renderInlineAsText(tokens[i].tokens, options, env);
}
}
return result;
};
Renderer.prototype.render = function (tokens, options, env) { Renderer.prototype.render = function (tokens, options, env) {
var i, len, var i, len,
result = '', result = '',

23
lib/rules_inline/links.js

@ -6,17 +6,19 @@ var parseLinkLabel = require('../helpers/parse_link_label');
var parseLinkDestination = require('../helpers/parse_link_destination'); var parseLinkDestination = require('../helpers/parse_link_destination');
var parseLinkTitle = require('../helpers/parse_link_title'); var parseLinkTitle = require('../helpers/parse_link_title');
var normalizeReference = require('../helpers/normalize_reference'); var normalizeReference = require('../helpers/normalize_reference');
var StateInline = require('../rules_inline/state_inline');
module.exports = function links(state, silent) { module.exports = function links(state, silent) {
var labelStart, var code,
labelEnd,
label,
href, href,
title, label,
labelEnd,
labelStart,
pos, pos,
ref, ref,
code, title,
tokens,
isImage = false, isImage = false,
oldPos = state.pos, oldPos = state.pos,
max = state.posMax, max = state.posMax,
@ -138,11 +140,20 @@ module.exports = function links(state, silent) {
state.posMax = labelEnd; state.posMax = labelEnd;
if (isImage) { if (isImage) {
var newState = new StateInline(
state.src.slice(labelStart, labelEnd),
state.parser,
state.options,
state.env,
tokens = []
);
newState.parser.tokenize(newState);
state.push({ state.push({
type: 'image', type: 'image',
src: href, src: href,
title: title, title: title,
alt: state.src.substr(labelStart, labelEnd - labelStart), tokens: tokens,
level: state.level level: state.level
}); });
} else { } else {

108
test/fixtures/commonmark/bad.txt

@ -74,111 +74,3 @@ error:
<p><a href="/url">foo</a>bar</p> <p><a href="/url">foo</a>bar</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6020
.
![foo *bar*]
[foo *bar*]: train.jpg "train & tracks"
.
<p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
.
error:
<p><img src="train.jpg" alt="foo *bar*" title="train &amp; tracks" /></p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6028
.
![foo ![bar](/url)](/url2)
.
<p><img src="/url2" alt="foo bar" /></p>
.
error:
<p><img src="/url2" alt="foo ![bar](/url)" /></p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6034
.
![foo [bar](/url)](/url2)
.
<p><img src="/url2" alt="foo bar" /></p>
.
error:
<p><img src="/url2" alt="foo [bar](/url)" /></p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6047
.
![foo *bar*][]
[foo *bar*]: train.jpg "train & tracks"
.
<p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
.
error:
<p><img src="train.jpg" alt="foo *bar*" title="train &amp; tracks" /></p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6055
.
![foo *bar*][foobar]
[FOOBAR]: train.jpg "train & tracks"
.
<p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
.
error:
<p><img src="train.jpg" alt="foo *bar*" title="train &amp; tracks" /></p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6115
.
![*foo* bar][]
[*foo* bar]: /url "title"
.
<p><img src="/url" alt="foo bar" title="title" /></p>
.
error:
<p><img src="/url" alt="*foo* bar" title="title" /></p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6155
.
![*foo* bar]
[*foo* bar]: /url "title"
.
<p><img src="/url" alt="foo bar" title="title" /></p>
.
error:
<p><img src="/url" alt="*foo* bar" title="title" /></p>

73
test/fixtures/commonmark/good.txt

@ -5293,6 +5293,57 @@ src line: 6014
<p><img src="/url" alt="foo" title="title" /></p> <p><img src="/url" alt="foo" title="title" /></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6020
.
![foo *bar*]
[foo *bar*]: train.jpg "train & tracks"
.
<p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6028
.
![foo ![bar](/url)](/url2)
.
<p><img src="/url2" alt="foo bar" /></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6034
.
![foo [bar](/url)](/url2)
.
<p><img src="/url2" alt="foo bar" /></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6047
.
![foo *bar*][]
[foo *bar*]: train.jpg "train & tracks"
.
<p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6055
.
![foo *bar*][foobar]
[FOOBAR]: train.jpg "train & tracks"
.
<p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6063 src line: 6063
@ -5362,6 +5413,17 @@ src line: 6107
<p><img src="/url" alt="foo" title="title" /></p> <p><img src="/url" alt="foo" title="title" /></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6115
.
![*foo* bar][]
[*foo* bar]: /url "title"
.
<p><img src="/url" alt="foo bar" title="title" /></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6125 src line: 6125
@ -5396,6 +5458,17 @@ src line: 6147
<p><img src="/url" alt="foo" title="title" /></p> <p><img src="/url" alt="foo" title="title" /></p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6155
.
![*foo* bar]
[*foo* bar]: /url "title"
.
<p><img src="/url" alt="foo bar" title="title" /></p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 6165 src line: 6165

Loading…
Cancel
Save