UNPKG

@diplodoc/transform

Version:

A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML

172 lines 8.44 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _AttrsParser_key, _AttrsParser_pending, _AttrsParser_isInsideQuotation, _AttrsParser_didQuotationClosed, _AttrsParser_currentKeyType, _AttrsParser_selectors, _AttrsParser_handlers, _AttrsParser_state; Object.defineProperty(exports, "__esModule", { value: true }); exports.AttrsParser = void 0; class AttrsParser { constructor() { this.DELIMITER = '='; this.SEPARATOR = ' '; this.QUOTATION = '"'; /* allowed in keys / values chars */ this.ALLOWED_CHARS = /[a-zA-Z0-9_\- {}.|/]/; /* allowed in all query chars */ this.VALIDATION_CHARS = /[a-zA-Z0-9_\- {}.#="|/]/; _AttrsParser_key.set(this, ''); _AttrsParser_pending.set(this, ''); _AttrsParser_isInsideQuotation.set(this, false); _AttrsParser_didQuotationClosed.set(this, false); _AttrsParser_currentKeyType.set(this, void 0); _AttrsParser_selectors.set(this, { id: /#/, class: /\./, attr: /[a-zA-Z-_]/, }); _AttrsParser_handlers.set(this, Object.entries(__classPrivateFieldGet(this, _AttrsParser_selectors, "f"))); _AttrsParser_state.set(this, {}); } parse(target) { /* escape from {} */ const content = this.extract(target); if (!content) { return {}; } for (const char of content) { this.next(char); } /* end-of-content mark */ this.next(this.SEPARATOR); this.clear(); return __classPrivateFieldGet(this, _AttrsParser_state, "f"); } extract(target) { if (!target.startsWith('{')) { return false; } let balance = 1; for (let i = 1; i < target.length; i++) { const char = target[i]; if (char === '}') { balance--; } if (char === '{') { balance++; } if (balance === 0) { const contentInside = target.slice(1, i).trim(); return contentInside; } if (balance < 0) { return false; } if (!this.VALIDATION_CHARS.test(char)) { return false; } } return false; } next(value) { if (!__classPrivateFieldGet(this, _AttrsParser_currentKeyType, "f")) { __classPrivateFieldSet(this, _AttrsParser_currentKeyType, this.type(value), "f"); if (__classPrivateFieldGet(this, _AttrsParser_currentKeyType, "f") === 'attr') { __classPrivateFieldSet(this, _AttrsParser_pending, value, "f"); } return; } if (this.isSeparator(value)) { if (!__classPrivateFieldGet(this, _AttrsParser_pending, "f")) { /* (name= ) construction */ if (!__classPrivateFieldGet(this, _AttrsParser_isInsideQuotation, "f")) { this.append(__classPrivateFieldGet(this, _AttrsParser_key, "f"), ' '); this.clear(); return; } } /* single key (.name #id contenteditable) */ if (!__classPrivateFieldGet(this, _AttrsParser_key, "f") && __classPrivateFieldGet(this, _AttrsParser_pending, "f")) { this.append(); this.clear(); return; } /* trying to find close quotation */ if (__classPrivateFieldGet(this, _AttrsParser_isInsideQuotation, "f") && !__classPrivateFieldGet(this, _AttrsParser_didQuotationClosed, "f")) { __classPrivateFieldSet(this, _AttrsParser_pending, __classPrivateFieldGet(this, _AttrsParser_pending, "f") + value, "f"); return; } if (__classPrivateFieldGet(this, _AttrsParser_isInsideQuotation, "f") && __classPrivateFieldGet(this, _AttrsParser_didQuotationClosed, "f")) { this.append(__classPrivateFieldGet(this, _AttrsParser_key, "f"), __classPrivateFieldGet(this, _AttrsParser_pending, "f")); } if (!__classPrivateFieldGet(this, _AttrsParser_isInsideQuotation, "f") && !__classPrivateFieldGet(this, _AttrsParser_didQuotationClosed, "f")) { this.append(__classPrivateFieldGet(this, _AttrsParser_key, "f"), __classPrivateFieldGet(this, _AttrsParser_pending, "f")); } this.clear(); return; } if (this.isAllowedChar(value)) { __classPrivateFieldSet(this, _AttrsParser_pending, __classPrivateFieldGet(this, _AttrsParser_pending, "f") + value, "f"); return; } if (this.isQuotation(value)) { if (__classPrivateFieldGet(this, _AttrsParser_isInsideQuotation, "f")) { __classPrivateFieldSet(this, _AttrsParser_didQuotationClosed, true, "f"); } else { __classPrivateFieldSet(this, _AttrsParser_isInsideQuotation, true, "f"); } } if (this.isDelimiter(value)) { /* symbol is not delimiter, adding it to value */ if (__classPrivateFieldGet(this, _AttrsParser_key, "f")) { __classPrivateFieldSet(this, _AttrsParser_pending, __classPrivateFieldGet(this, _AttrsParser_pending, "f") + value, "f"); return; } __classPrivateFieldSet(this, _AttrsParser_key, __classPrivateFieldGet(this, _AttrsParser_pending, "f"), "f"); __classPrivateFieldSet(this, _AttrsParser_pending, '', "f"); } } type(of) { var _a; return (_a = __classPrivateFieldGet(this, _AttrsParser_handlers, "f").find(([_, regex]) => regex.test(of))) === null || _a === void 0 ? void 0 : _a[0]; } append(key = __classPrivateFieldGet(this, _AttrsParser_currentKeyType, "f"), value = __classPrivateFieldGet(this, _AttrsParser_pending, "f")) { if (!key) { return; } if (!__classPrivateFieldGet(this, _AttrsParser_state, "f")[key]) { __classPrivateFieldGet(this, _AttrsParser_state, "f")[key] = []; } __classPrivateFieldGet(this, _AttrsParser_state, "f")[key].push(value); } clear() { __classPrivateFieldSet(this, _AttrsParser_key, '', "f"); __classPrivateFieldSet(this, _AttrsParser_pending, '', "f"); __classPrivateFieldSet(this, _AttrsParser_isInsideQuotation, false, "f"); __classPrivateFieldSet(this, _AttrsParser_didQuotationClosed, false, "f"); __classPrivateFieldSet(this, _AttrsParser_currentKeyType, undefined, "f"); } isDelimiter(target) { return target === this.DELIMITER; } isSeparator(target) { return target === this.SEPARATOR; } isQuotation(target) { return target === this.QUOTATION; } isAllowedChar(target) { return this.ALLOWED_CHARS.test(target); } } exports.AttrsParser = AttrsParser; _AttrsParser_key = new WeakMap(), _AttrsParser_pending = new WeakMap(), _AttrsParser_isInsideQuotation = new WeakMap(), _AttrsParser_didQuotationClosed = new WeakMap(), _AttrsParser_currentKeyType = new WeakMap(), _AttrsParser_selectors = new WeakMap(), _AttrsParser_handlers = new WeakMap(), _AttrsParser_state = new WeakMap(); //# sourceMappingURL=attrs.js.map