marked-mfm/lib/index.umd.js
2022-08-01 09:08:18 +03:00

196 lines
5.8 KiB
JavaScript

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.markedMfm = factory());
})(this, (function () { 'use strict';
function _wrapRegExp() {
_wrapRegExp = function (re, groups) {
return new BabelRegExp(re, void 0, groups);
};
var _super = RegExp.prototype,
_groups = new WeakMap();
function BabelRegExp(re, flags, groups) {
var _this = new RegExp(re, flags);
return _groups.set(_this, groups || _groups.get(re)), _setPrototypeOf(_this, BabelRegExp.prototype);
}
function buildGroups(result, re) {
var g = _groups.get(re);
return Object.keys(g).reduce(function (groups, name) {
return groups[name] = result[g[name]], groups;
}, Object.create(null));
}
return _inherits(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (str) {
var result = _super.exec.call(this, str);
return result && (result.groups = buildGroups(result, this)), result;
}, BabelRegExp.prototype[Symbol.replace] = function (str, substitution) {
if ("string" == typeof substitution) {
var groups = _groups.get(this);
return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) {
return "$" + groups[name];
}));
}
if ("function" == typeof substitution) {
var _this = this;
return _super[Symbol.replace].call(this, str, function () {
var args = arguments;
return "object" != typeof args[args.length - 1] && (args = [].slice.call(args)).push(buildGroups(args, _this)), substitution.apply(this, args);
});
}
return _super[Symbol.replace].call(this, str, substitution);
}, _wrapRegExp.apply(this, arguments);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
Object.defineProperty(subClass, "prototype", {
writable: false
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
var index = {
extensions: [{
name: 'mfm',
level: 'inline',
start: function start(src) {
var _src$match;
return (_src$match = src.match(/\$\[/)) == null ? void 0 : _src$match.index;
},
tokenizer: function tokenizer(src, tokens) {
// regex doesn't do well finding MFM tags: it's always either too lazy
// or too greedy.
var level = 0;
var walk = 0;
while (level > 0 || walk === 0) {
if (walk >= src.length) {
return null;
}
if (src[walk] + src[walk + 1] === '$[') {
level++;
walk++;
} else if (src[walk] === ']') {
level--;
}
walk++;
} // original regex, now definitely on the correct tag
var rule = /*#__PURE__*/_wrapRegExp(/^\$\[([\w\d]+)(?:\.(\S+))? ([\S\s]+)\]/, {
tag: 1,
options: 2,
text: 3
});
var match = rule.exec(src.slice(0, walk));
if (match) {
var token = {
type: 'mfm',
raw: match[0],
tag: match.groups.tag,
options: match.groups.options,
text: match.groups.text,
tokens: []
};
this.lexer.inline(token.text, token.tokens);
return token;
}
},
renderer: function renderer(token) {
var MFM_TAGS = ['tada', 'jelly', 'twitch', 'shake', 'spin', 'jump', 'bounce', 'flip', 'x2', 'x3', 'x4', 'font', 'blur', 'rainbow', 'rotate', 'sparkle'];
if (MFM_TAGS.includes(token.tag)) {
var options = [];
if (token.options) {
options = token.options.split(',').map(function (opt) {
return opt.split('=').length === 2 ? "data-" + opt.split('=')[0] + "=\"" + opt.split('=')[1] + "\"" : "data-" + opt;
});
}
return "<span class=\"mfm _mfm_" + token.tag + "_\" " + options.join(' ') + ">" + this.parser.parseInline(token.tokens) + "</span>";
}
return "$[" + token.tag + " " + this.parser.parseInline(token.tokens) + "]";
}
}, {
name: 'escapedMfm',
level: 'inline',
start: function start(src) {
return src.match(/\\\$\[/);
},
tokenizer: function tokenizer(src, tokens) {
if (/^\\\$\[/.exec(src)) {
return {
type: 'escapedMfm',
raw: '\\$['
};
}
},
renderer: function renderer(token) {
return '$[';
}
}, {
name: 'center',
level: 'block',
start: function start(src) {
return src.match(/<center>/);
},
tokenizer: function tokenizer(src, tokens) {
var rule = /^<center>([\S\s]*)<\/center>/;
var match = rule.exec(src);
if (match) {
var token = {
type: 'center',
raw: match[0],
text: match[1],
tokens: []
};
this.lexer.inline(token.text, token.tokens);
return token;
}
},
renderer: function renderer(token) {
return "<center>" + this.parser.parseInline(token.tokens) + "</center>\n";
}
}]
};
return index;
}));