discourse/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/html-img.js

Failed to ignore revisions in .git-blame-ignore-revs.

80 lines
1.8 KiB
JavaScript
Raw Normal View History

// special handling for IMG tags on a line by themeselves
// we always have to handle it as so it is an inline
// see: https://talk.commonmark.org/t/newline-and-img-tags/2511
const REGEX = /^<img.*\\?>\s*$/i;
function rule(state, startLine, endLine) {
2018-06-15 11:03:24 -04:00
var nextLine,
token,
lineText,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
// if it's indented more than 3 spaces, it should be a code block
2018-06-15 11:03:24 -04:00
if (state.sCount[startLine] - state.blkIndent >= 4) {
return false;
}
2018-06-15 11:03:24 -04:00
if (!state.md.options.html) {
return false;
}
2018-06-15 11:03:24 -04:00
if (state.src.charCodeAt(pos) !== 0x3c /* < */) {
return false;
}
let pos1 = state.src.charCodeAt(pos + 1);
if (pos1 !== 73 /* I */ && pos1 !== 105 /* i */) {
return false;
}
lineText = state.src.slice(pos, max);
if (!REGEX.test(lineText)) {
return false;
}
let lines = [];
lines.push(lineText);
nextLine = startLine + 1;
for (; nextLine < endLine; nextLine++) {
pos = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
lineText = state.src.slice(pos, max);
if (lineText.trim() === "") {
break;
}
if (!REGEX.test(lineText)) {
break;
}
lines.push(lineText);
}
state.line = nextLine;
let oldParentType = state.parentType;
2018-06-15 11:03:24 -04:00
state.parentType = "paragraph";
2018-06-15 11:03:24 -04:00
token = state.push("paragraph_open", "p", 1);
token.map = [startLine, state.line];
2018-06-15 11:03:24 -04:00
token = state.push("inline", "", 0);
token.content = lines.join("\n");
token.map = [startLine, state.line];
token.children = [];
2018-06-15 11:03:24 -04:00
token = state.push("paragraph_close", "p", -1);
state.parentType = oldParentType;
return true;
}
export function setup(helper) {
2018-06-15 11:03:24 -04:00
helper.registerPlugin(md => {
md.block.ruler.before("html_block", "html_img", rule, { alt: ["fence"] });
});
}