2017-07-11 00:20:50 +08:00
|
|
|
// 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) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let nextLine,
|
2017-07-11 00:20:50 +08:00
|
|
|
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
|
|
|
|
if (state.sCount[startLine] - state.blkIndent >= 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.md.options.html) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
state.parentType = "paragraph";
|
|
|
|
|
2017-07-18 05:25:40 +08:00
|
|
|
token = state.push("paragraph_open", "p", 1);
|
2017-07-11 00:20:50 +08:00
|
|
|
token.map = [startLine, state.line];
|
|
|
|
|
|
|
|
token = state.push("inline", "", 0);
|
|
|
|
token.content = lines.join("\n");
|
|
|
|
token.map = [startLine, state.line];
|
|
|
|
token.children = [];
|
|
|
|
|
2020-05-08 15:19:48 +08:00
|
|
|
state.push("paragraph_close", "p", -1);
|
2017-07-11 00:20:50 +08:00
|
|
|
state.parentType = oldParentType;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
helper.registerPlugin((md) => {
|
|
|
|
md.block.ruler.before("html_block", "html_img", rule, { alt: ["fence"] });
|
|
|
|
});
|
|
|
|
}
|