js-markdown
Version:
A markdown language js compiler.
81 lines (66 loc) • 1.68 kB
JavaScript
/**
* match a mark
*
* Use 3 or more "`" or "~" for the first line, and use info|warning|success|error, syntax like this:
*
* ```info
* here is the code
* multi lines
* ```
*
* or
*
* ~~~warning
* another example
* ~~~
*
*/
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _Str = _interopRequireDefault(require("../../utils/Str"));
/**
* exclude the condition that if there is a same identifier in the same line
* @param line
*/
function isInlineMatch(line) {
return line.match(/^([`~]{3,}).*\1/);
}
function parse(line, index, lines, renderTree) {
var result = line.match(/^([`~]{3,})\s*(info|warning|success|error)(?:\n|$)/);
if (!result) {
return;
} // exclude inline code
if (isInlineMatch(line)) {
return;
}
var block = {
// blockquote root node
type: 'Mark',
msgType: result[2],
children: []
},
content = [];
index++;
for (var len = lines.length; index < len; index++) {
if (_Str["default"].trimEnd(lines[index], ' \t') === result[1]) {
break;
}
content.push(lines[index]);
} // 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 "<mark ".concat(node.msgType, ">").concat(node.rawValue || '').concat(data, "</mark>");
}
var _default = {
parse: parse,
render: render
};
exports["default"] = _default;