@connectv/marked
Version:
component-based markdown renderer
50 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var compRegex = /^\[\:(\w+)(\s+(?:\w+\s*\=[^\,]+\s*)(?:\,\s*\w+\s*\=[^\,]+\s*)*)?\s*\]/;
var InlineCompProcessor = /** @class */ (function () {
function InlineCompProcessor(renderer, map) {
this.renderer = renderer;
this.map = map;
}
InlineCompProcessor.prototype.process = function (el) {
var _this = this;
this.markers(el).forEach(function (marker) {
var Comp = _this.map[marker.component];
if (!Comp)
throw new Error('Unrecognized Inline Component:: ' + marker.component);
var content = [];
marker.$.childNodes.forEach(function (child) { return content.push(child); });
_this.renderer.render(_this.renderer.create(Comp, marker.props, content)).before(marker.$);
marker.$.remove();
});
return el;
};
InlineCompProcessor.prototype.markers = function (el) {
var _this = this;
var markers = [];
el.querySelectorAll('em').forEach(function (em$) {
var _a;
var match;
if (em$.firstChild && em$.firstChild instanceof Text
&& (match = compRegex.exec(em$.firstChild.textContent || ''))) {
em$.firstChild.textContent = ((_a = em$.firstChild.textContent) === null || _a === void 0 ? void 0 : _a.substr(match[0].length)) || '';
markers.push({
$: em$,
component: match[1],
props: _this.props(match[2]),
});
}
});
return markers;
};
InlineCompProcessor.prototype.props = function (desc) {
return (desc === null || desc === void 0 ? void 0 : desc.trim().split(',').reduce(function (props, bit) {
var _a = bit.split('=').map(function (_) { return _.trim(); }), key = _a[0], val = _a[1];
props[key] = val;
return props;
}, {})) || {};
};
return InlineCompProcessor;
}());
exports.InlineCompProcessor = InlineCompProcessor;
//# sourceMappingURL=inline-comp.js.map