2017-06-27 22:06:55 +08:00
|
|
|
let isWhiteSpace;
|
|
|
|
|
|
|
|
function trailingSpaceOnly(src, start, max) {
|
|
|
|
let i;
|
2018-06-15 23:03:24 +08:00
|
|
|
for (i = start; i < max; i++) {
|
2017-06-27 22:06:55 +08:00
|
|
|
let code = src.charCodeAt(i);
|
2018-06-15 23:03:24 +08:00
|
|
|
if (code === 0x0a) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!isWhiteSpace(code)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-27 22:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const ATTR_REGEX = /^\s*=(.+)$|((([a-z0-9]*)\s*)=)(["“”'].*?["“”']|\S+)/gi;
|
2017-07-12 02:43:24 +08:00
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
// parse a tag [test a=1 b=2] to a data structure
|
|
|
|
// {tag: "test", attrs={a: "1", b: "2"}
|
2017-06-27 22:06:55 +08:00
|
|
|
export function parseBBCodeTag(src, start, max, multiline) {
|
2017-06-09 06:02:30 +08:00
|
|
|
let i;
|
|
|
|
let tag;
|
|
|
|
let attrs = {};
|
|
|
|
let closed = false;
|
|
|
|
let length = 0;
|
|
|
|
let closingTag = false;
|
|
|
|
|
|
|
|
// closing tag
|
2018-06-15 23:03:24 +08:00
|
|
|
if (src.charCodeAt(start + 1) === 47) {
|
2017-06-09 06:02:30 +08:00
|
|
|
closingTag = true;
|
|
|
|
start += 1;
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
for (i = start + 1; i < max; i++) {
|
2017-06-09 06:02:30 +08:00
|
|
|
let letter = src[i];
|
2018-06-15 23:03:24 +08:00
|
|
|
if (
|
|
|
|
!((letter >= "a" && letter <= "z") || (letter >= "A" && letter <= "Z"))
|
|
|
|
) {
|
2017-06-09 06:02:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
tag = src.slice(start + 1, i);
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
if (!tag) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closingTag) {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (src[i] === "]") {
|
|
|
|
if (multiline && !trailingSpaceOnly(src, i + 1, max)) {
|
2017-06-27 22:06:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-05 00:40:54 +08:00
|
|
|
tag = tag.toLowerCase();
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
return { tag, length: tag.length + 3, closing: true };
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
for (; i < max; i++) {
|
2017-06-09 06:02:30 +08:00
|
|
|
let letter = src[i];
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (letter === "]") {
|
2017-06-09 06:02:30 +08:00
|
|
|
closed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closed) {
|
2018-06-15 23:03:24 +08:00
|
|
|
length = i - start + 1;
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
let raw = src.slice(start + tag.length + 1, i);
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
// trivial parser that is going to have to be rewritten at some point
|
|
|
|
if (raw) {
|
2017-07-12 02:43:24 +08:00
|
|
|
let match, key, val;
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
while ((match = ATTR_REGEX.exec(raw))) {
|
2017-07-25 06:36:17 +08:00
|
|
|
if (match[1]) {
|
2018-06-15 23:03:24 +08:00
|
|
|
key = "_default";
|
2017-07-25 06:36:17 +08:00
|
|
|
} else {
|
|
|
|
key = match[4];
|
2017-07-21 01:02:31 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 06:36:17 +08:00
|
|
|
val = match[1] || match[5];
|
2017-07-21 01:02:31 +08:00
|
|
|
|
|
|
|
if (val) {
|
2017-07-12 02:43:24 +08:00
|
|
|
val = val.trim();
|
2018-06-15 23:03:24 +08:00
|
|
|
val = val.replace(/^["'“”](.*)["'“”]$/, "$1");
|
2017-07-12 02:43:24 +08:00
|
|
|
attrs[key] = val;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (multiline && !trailingSpaceOnly(src, start + length, max)) {
|
2017-06-27 22:06:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
tag = tag.toLowerCase();
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
return { tag, attrs, length };
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
function findBlockCloseTag(state, openTag, startLine, endLine) {
|
|
|
|
let nesting = 0,
|
2018-06-15 23:03:24 +08:00
|
|
|
line = startLine - 1,
|
2017-07-28 05:55:04 +08:00
|
|
|
start,
|
|
|
|
closeTag,
|
|
|
|
max;
|
2017-06-27 22:06:55 +08:00
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
for (;;) {
|
2017-07-28 05:55:04 +08:00
|
|
|
line++;
|
|
|
|
if (line >= endLine) {
|
2017-06-27 04:50:46 +08:00
|
|
|
// unclosed bbcode block should not be autoclosed by end of document.
|
2017-07-28 05:55:04 +08:00
|
|
|
return;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
start = state.bMarks[line] + state.tShift[line];
|
|
|
|
max = state.eMarks[line];
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
if (start < max && state.sCount[line] < state.blkIndent) {
|
2017-06-09 06:02:30 +08:00
|
|
|
// non-empty line with negative indent should stop the list:
|
|
|
|
// - ```
|
|
|
|
// test
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bbcode close [ === 91
|
2018-06-15 23:03:24 +08:00
|
|
|
if (91 !== state.src.charCodeAt(start)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
if (state.sCount[line] - state.blkIndent >= 4) {
|
|
|
|
// closing bbcode less than 4 spaces
|
2017-06-09 06:02:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-27 22:06:55 +08:00
|
|
|
closeTag = parseBBCodeTag(state.src, start, max, true);
|
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
if (closeTag && closeTag.closing && closeTag.tag === openTag.tag) {
|
2017-06-27 22:06:55 +08:00
|
|
|
if (nesting === 0) {
|
2017-07-28 05:55:04 +08:00
|
|
|
closeTag.line = line;
|
|
|
|
closeTag.block = true;
|
2017-06-27 22:06:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
nesting--;
|
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
if (closeTag && !closeTag.closing && closeTag.tag === openTag.tag) {
|
2017-06-27 22:06:55 +08:00
|
|
|
nesting++;
|
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-06-27 22:06:55 +08:00
|
|
|
closeTag = null;
|
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
return closeTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findInlineCloseTag(state, openTag, start, max) {
|
|
|
|
let closeTag;
|
|
|
|
let possibleTag = false;
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
for (let j = max - 1; j > start; j--) {
|
2017-07-28 05:55:04 +08:00
|
|
|
if (!possibleTag) {
|
|
|
|
if (state.src.charCodeAt(j) === 93 /* ] */) {
|
|
|
|
possibleTag = true;
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
if (!isWhiteSpace(state.src.charCodeAt(j))) {
|
2017-07-28 05:55:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (state.src.charCodeAt(j) === 91 /* [ */) {
|
|
|
|
closeTag = parseBBCodeTag(state.src, j, max);
|
|
|
|
if (!closeTag || closeTag.tag !== openTag.tag || !closeTag.closing) {
|
|
|
|
closeTag = null;
|
2017-09-03 00:52:29 +08:00
|
|
|
} else {
|
|
|
|
closeTag.start = j;
|
2017-09-06 14:13:57 +08:00
|
|
|
break;
|
2017-07-28 05:55:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closeTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyBBCode(state, startLine, endLine, silent, md) {
|
|
|
|
var nextLine,
|
2018-06-15 23:03:24 +08:00
|
|
|
oldParent,
|
|
|
|
oldLineMax,
|
|
|
|
rule,
|
|
|
|
start = state.bMarks[startLine] + state.tShift[startLine],
|
|
|
|
initial = start,
|
|
|
|
max = state.eMarks[startLine];
|
2017-07-28 05:55:04 +08:00
|
|
|
|
|
|
|
// [ === 91
|
2018-06-15 23:03:24 +08:00
|
|
|
if (91 !== state.src.charCodeAt(start)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-28 05:55:04 +08:00
|
|
|
|
|
|
|
let info = parseBBCodeTag(state.src, start, max);
|
|
|
|
|
|
|
|
if (!info || info.closing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ruleInfo = md.block.bbcode.ruler.getRuleForTag(info.tag);
|
2018-06-15 23:03:24 +08:00
|
|
|
if (!ruleInfo) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-28 05:55:04 +08:00
|
|
|
|
|
|
|
rule = ruleInfo.rule;
|
|
|
|
|
|
|
|
// Since start is found, we can report success here in validation mode
|
2018-06-15 23:03:24 +08:00
|
|
|
if (silent) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-28 05:55:04 +08:00
|
|
|
|
|
|
|
// Search for the end of the block
|
|
|
|
nextLine = startLine;
|
|
|
|
|
|
|
|
// We might have a single inline bbcode
|
|
|
|
|
|
|
|
let closeTag = findInlineCloseTag(state, info, start + info.length, max);
|
|
|
|
|
|
|
|
if (!closeTag) {
|
|
|
|
if (!trailingSpaceOnly(state.src, start + info.length, max)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
closeTag = findBlockCloseTag(state, info, nextLine + 1, endLine);
|
2017-07-28 05:55:04 +08:00
|
|
|
}
|
|
|
|
|
2017-06-27 22:06:55 +08:00
|
|
|
if (!closeTag) {
|
|
|
|
return false;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
nextLine = closeTag.line || startLine;
|
|
|
|
|
|
|
|
oldParent = state.parentType;
|
|
|
|
oldLineMax = state.lineMax;
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
// this will prevent lazy continuations from ever going past our end marker
|
2017-07-28 05:55:04 +08:00
|
|
|
// which can happen if we are parsing a bbcode block
|
2017-06-09 06:02:30 +08:00
|
|
|
state.lineMax = nextLine;
|
|
|
|
|
2017-06-30 04:04:10 +08:00
|
|
|
if (rule.replace) {
|
2018-04-26 13:18:22 +08:00
|
|
|
let content;
|
|
|
|
|
|
|
|
if (startLine === nextLine) {
|
|
|
|
content = state.src.slice(start + info.length, closeTag.start);
|
|
|
|
} else {
|
2018-06-15 23:03:24 +08:00
|
|
|
content = state.src.slice(
|
|
|
|
state.bMarks[startLine + 1],
|
|
|
|
state.eMarks[nextLine - 1]
|
|
|
|
);
|
2018-04-26 13:18:22 +08:00
|
|
|
}
|
|
|
|
|
2017-06-30 04:04:10 +08:00
|
|
|
if (!rule.replace.call(this, state, info, content)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (rule.before) {
|
2018-06-15 23:03:24 +08:00
|
|
|
rule.before.call(
|
|
|
|
this,
|
|
|
|
state,
|
|
|
|
info,
|
|
|
|
state.src.slice(initial, initial + info.length + 1)
|
|
|
|
);
|
2017-06-30 04:04:10 +08:00
|
|
|
}
|
2017-06-23 23:36:45 +08:00
|
|
|
|
2017-06-30 04:04:10 +08:00
|
|
|
let wrapTag;
|
|
|
|
if (rule.wrap) {
|
2017-07-01 00:21:01 +08:00
|
|
|
let token;
|
2017-06-23 23:36:45 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (typeof rule.wrap === "function") {
|
|
|
|
token = new state.Token("wrap_bbcode", "div", 1);
|
|
|
|
token.level = state.level + 1;
|
2017-06-30 04:04:10 +08:00
|
|
|
|
2017-07-01 00:21:01 +08:00
|
|
|
if (!rule.wrap(token, info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.tokens.push(token);
|
|
|
|
state.level = token.level;
|
|
|
|
wrapTag = token.tag;
|
|
|
|
} else {
|
2018-06-15 23:03:24 +08:00
|
|
|
let split = rule.wrap.split(".");
|
2017-07-01 00:21:01 +08:00
|
|
|
wrapTag = split[0];
|
2018-06-15 23:03:24 +08:00
|
|
|
let className = split.slice(1).join(" ");
|
2017-07-01 00:21:01 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
token = state.push("wrap_bbcode", wrapTag, 1);
|
2017-07-01 00:21:01 +08:00
|
|
|
|
|
|
|
if (className) {
|
2018-06-15 23:03:24 +08:00
|
|
|
token.attrs = [["class", className]];
|
2017-07-01 00:21:01 +08:00
|
|
|
}
|
2017-06-30 04:04:10 +08:00
|
|
|
}
|
2017-06-23 23:36:45 +08:00
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
let lastToken = state.tokens[state.tokens.length - 1];
|
|
|
|
lastToken.map = [startLine, nextLine];
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
if (closeTag.block) {
|
|
|
|
state.md.block.tokenize(state, startLine + 1, nextLine);
|
|
|
|
} else {
|
2018-06-15 23:03:24 +08:00
|
|
|
let token = state.push("paragraph_open", "p", 1);
|
2017-07-28 05:55:04 +08:00
|
|
|
token.map = [startLine, startLine];
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
token = state.push("inline", "", 0);
|
2017-07-28 05:55:04 +08:00
|
|
|
token.children = [];
|
|
|
|
token.map = [startLine, startLine];
|
|
|
|
token.content = state.src.slice(start + info.length, closeTag.start);
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
state.push("paragraph_close", "p", -1);
|
2017-07-28 05:55:04 +08:00
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-06-30 04:04:10 +08:00
|
|
|
if (rule.wrap) {
|
2018-06-15 23:03:24 +08:00
|
|
|
state.push("wrap_bbcode", wrapTag, -1);
|
2017-06-30 04:04:10 +08:00
|
|
|
}
|
2017-06-23 23:36:45 +08:00
|
|
|
|
2017-06-30 04:04:10 +08:00
|
|
|
if (rule.after) {
|
2018-06-15 23:03:24 +08:00
|
|
|
rule.after.call(
|
|
|
|
this,
|
|
|
|
state,
|
|
|
|
lastToken,
|
|
|
|
state.src.slice(start - 2, start + closeTag.length - 1)
|
|
|
|
);
|
2017-06-30 04:04:10 +08:00
|
|
|
}
|
2017-06-23 23:36:45 +08:00
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-07-28 05:55:04 +08:00
|
|
|
state.parentType = oldParent;
|
|
|
|
state.lineMax = oldLineMax;
|
2018-06-15 23:03:24 +08:00
|
|
|
state.line = nextLine + 1;
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
helper.registerPlugin(md => {
|
2017-07-18 04:21:47 +08:00
|
|
|
const ruler = md.block.bbcode.ruler;
|
2017-06-30 04:04:10 +08:00
|
|
|
|
2018-12-25 23:02:28 +08:00
|
|
|
ruler.push("excerpt", {
|
|
|
|
tag: "excerpt",
|
|
|
|
wrap: "div.excerpt"
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
ruler.push("code", {
|
|
|
|
tag: "code",
|
2017-06-30 04:04:10 +08:00
|
|
|
replace: function(state, tagInfo, content) {
|
|
|
|
let token;
|
2018-06-15 23:03:24 +08:00
|
|
|
token = state.push("fence", "code", 0);
|
2017-06-30 04:04:10 +08:00
|
|
|
token.content = content;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-27 22:06:55 +08:00
|
|
|
isWhiteSpace = md.utils.isWhiteSpace;
|
2018-06-15 23:03:24 +08:00
|
|
|
md.block.ruler.after(
|
|
|
|
"fence",
|
|
|
|
"bbcode",
|
|
|
|
(state, startLine, endLine, silent) => {
|
|
|
|
return applyBBCode(state, startLine, endLine, silent, md);
|
|
|
|
},
|
|
|
|
{ alt: ["paragraph", "reference", "blockquote", "list"] }
|
|
|
|
);
|
2017-06-09 06:02:30 +08:00
|
|
|
});
|
|
|
|
}
|