js-markdown
Version:
A markdown language js compiler.
117 lines (98 loc) • 2.59 kB
JavaScript
/**
* match a multi lines quote block
*
* (1) basic, syntax like this:
*
* > This is a blockquote with two paragraphs.
* >
* > Paragraph two.
*
* (2) Technically not every line needs to start with a `>`
* as long as there are no empty lines between paragraphs, like this:
*
* > This is a blockquote
* second line
* third line.
*
* > another paragraph.
*
* (3) blockquotes can be nested, like this:
*
* > blockquote
* >
* > > can be nested.
* > > > Multiple Levels
* >
* > ## This is a header.
* >
* > 1. This is the first list item.
* > 2. This is the second list item.
* >
* > Here's some example code:
* >
* > return 'Hellow World!';
*
*/
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _Valid = _interopRequireDefault(require("../../utils/Valid"));
/**
* remove space which starts with the blockquote content string
* @param data
* @returns {ArrayBuffer|Array.<T>|Blob|string|*}
*/
function handleData(data) {
return data.startsWith(' ') ? data.slice(1) : data;
}
function parse(line, index, lines, renderTree) {
var reg = /^(?:\>)(?:\s*?)(.*?)(?:\n|$)/;
var result = line.match(reg);
if (!result) {
return;
}
var block = {
// blockquote root node
type: 'Blockquote',
children: []
},
content = [handleData(result[1])]; // all blockquote content
var blankLineFlag = false,
lineType;
index++;
for (var len = lines.length; index < len; index++) {
// if this line is blank
if (_Valid["default"].isBlank(lines[index])) {
blankLineFlag = true;
content.push(lines[index]);
continue;
} // calculate this line type
lineType = this.parseBlock(lines[index], 0, lines.slice(index))[0].type;
if (blankLineFlag && lineType !== 'Blockquote') {
index--;
break;
}
result = lines[index].match(reg);
if (!result) {
content.push(lines[index]);
continue;
}
content.push(handleData(result[1]));
blankLineFlag = false;
} // parse recursively
this.parseBlocks(content, block);
return [block, index];
}
function render() {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var node = arguments.length > 1 ? arguments[1] : undefined;
return "<blockquote>".concat(node.rawValue || '').concat(data, "</blockquote>");
}
var _default = {
parse: parse,
render: render
};
exports["default"] = _default;