Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
3.7 KiB
150 lines
3.7 KiB
10 years ago
|
// Process ![image](<src> "title")
|
||
10 years ago
|
|
||
1 year ago
|
import { normalizeReference, isSpace } from '../common/utils.mjs';
|
||
10 years ago
|
|
||
|
|
||
1 year ago
|
export default function image(state, silent) {
|
||
10 years ago
|
var attrs,
|
||
|
code,
|
||
9 years ago
|
content,
|
||
10 years ago
|
label,
|
||
|
labelEnd,
|
||
|
labelStart,
|
||
10 years ago
|
pos,
|
||
|
ref,
|
||
10 years ago
|
res,
|
||
10 years ago
|
title,
|
||
10 years ago
|
token,
|
||
10 years ago
|
tokens,
|
||
10 years ago
|
start,
|
||
10 years ago
|
href = '',
|
||
10 years ago
|
oldPos = state.pos,
|
||
10 years ago
|
max = state.posMax;
|
||
10 years ago
|
|
||
10 years ago
|
if (state.src.charCodeAt(state.pos) !== 0x21/* ! */) { return false; }
|
||
|
if (state.src.charCodeAt(state.pos + 1) !== 0x5B/* [ */) { return false; }
|
||
10 years ago
|
|
||
10 years ago
|
labelStart = state.pos + 2;
|
||
8 years ago
|
labelEnd = state.md.helpers.parseLinkLabel(state, state.pos + 1, false);
|
||
10 years ago
|
|
||
|
// parser failed to find ']', so it's not a valid link
|
||
10 years ago
|
if (labelEnd < 0) { return false; }
|
||
10 years ago
|
|
||
|
pos = labelEnd + 1;
|
||
|
if (pos < max && state.src.charCodeAt(pos) === 0x28/* ( */) {
|
||
|
//
|
||
|
// Inline link
|
||
|
//
|
||
|
|
||
|
// [link]( <href> "title" )
|
||
|
// ^^ skipping these spaces
|
||
|
pos++;
|
||
10 years ago
|
for (; pos < max; pos++) {
|
||
|
code = state.src.charCodeAt(pos);
|
||
10 years ago
|
if (!isSpace(code) && code !== 0x0A) { break; }
|
||
10 years ago
|
}
|
||
|
if (pos >= max) { return false; }
|
||
10 years ago
|
|
||
|
// [link]( <href> "title" )
|
||
|
// ^^^^^^ parsing link destination
|
||
|
start = pos;
|
||
8 years ago
|
res = state.md.helpers.parseLinkDestination(state.src, pos, state.posMax);
|
||
10 years ago
|
if (res.ok) {
|
||
|
href = state.md.normalizeLink(res.str);
|
||
|
if (state.md.validateLink(href)) {
|
||
|
pos = res.pos;
|
||
|
} else {
|
||
|
href = '';
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
// [link]( <href> "title" )
|
||
|
// ^^ skipping these spaces
|
||
|
start = pos;
|
||
10 years ago
|
for (; pos < max; pos++) {
|
||
|
code = state.src.charCodeAt(pos);
|
||
10 years ago
|
if (!isSpace(code) && code !== 0x0A) { break; }
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
// [link]( <href> "title" )
|
||
|
// ^^^^^^^ parsing link title
|
||
8 years ago
|
res = state.md.helpers.parseLinkTitle(state.src, pos, state.posMax);
|
||
10 years ago
|
if (pos < max && start !== pos && res.ok) {
|
||
|
title = res.str;
|
||
|
pos = res.pos;
|
||
10 years ago
|
|
||
|
// [link]( <href> "title" )
|
||
|
// ^^ skipping these spaces
|
||
10 years ago
|
for (; pos < max; pos++) {
|
||
|
code = state.src.charCodeAt(pos);
|
||
10 years ago
|
if (!isSpace(code) && code !== 0x0A) { break; }
|
||
10 years ago
|
}
|
||
10 years ago
|
} else {
|
||
|
title = '';
|
||
|
}
|
||
|
|
||
|
if (pos >= max || state.src.charCodeAt(pos) !== 0x29/* ) */) {
|
||
10 years ago
|
state.pos = oldPos;
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
pos++;
|
||
10 years ago
|
} else {
|
||
|
//
|
||
|
// Link reference
|
||
|
//
|
||
10 years ago
|
if (typeof state.env.references === 'undefined') { return false; }
|
||
10 years ago
|
|
||
|
if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
|
||
|
start = pos + 1;
|
||
8 years ago
|
pos = state.md.helpers.parseLinkLabel(state, pos);
|
||
10 years ago
|
if (pos >= 0) {
|
||
|
label = state.src.slice(start, pos++);
|
||
|
} else {
|
||
10 years ago
|
pos = labelEnd + 1;
|
||
10 years ago
|
}
|
||
10 years ago
|
} else {
|
||
|
pos = labelEnd + 1;
|
||
10 years ago
|
}
|
||
|
|
||
|
// covers label === '' and label === undefined
|
||
|
// (collapsed reference link and shortcut reference link respectively)
|
||
|
if (!label) { label = state.src.slice(labelStart, labelEnd); }
|
||
|
|
||
10 years ago
|
ref = state.env.references[normalizeReference(label)];
|
||
10 years ago
|
if (!ref) {
|
||
10 years ago
|
state.pos = oldPos;
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
href = ref.href;
|
||
|
title = ref.title;
|
||
|
}
|
||
10 years ago
|
|
||
|
//
|
||
|
// We found the end of the link, and know for a fact it's a valid link;
|
||
|
// so all that's left to do is to call tokenizer.
|
||
|
//
|
||
10 years ago
|
if (!silent) {
|
||
9 years ago
|
content = state.src.slice(labelStart, labelEnd);
|
||
|
|
||
9 years ago
|
state.md.inline.parse(
|
||
9 years ago
|
content,
|
||
10 years ago
|
state.md,
|
||
|
state.env,
|
||
|
tokens = []
|
||
|
);
|
||
|
|
||
10 years ago
|
token = state.push('image', 'img', 0);
|
||
|
token.attrs = attrs = [ [ 'src', href ], [ 'alt', '' ] ];
|
||
|
token.children = tokens;
|
||
9 years ago
|
token.content = content;
|
||
|
|
||
10 years ago
|
if (title) {
|
||
|
attrs.push([ 'title', title ]);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
state.pos = pos;
|
||
10 years ago
|
state.posMax = max;
|
||
|
return true;
|
||
1 year ago
|
}
|