UNPKG

@atlaskit/editor-wikimarkup-transformer

Version:

Wiki markup transformer for JIRA and Confluence

139 lines (138 loc) 5.79 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.rawContentProcessor = exports.quoteMacro = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _commonMacro = require("./common-macro"); var _text = require("../utils/text"); var _normalize = require("../utils/normalize"); var _text2 = require("../text"); function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var quoteMacro = exports.quoteMacro = function quoteMacro(_ref) { var input = _ref.input, position = _ref.position, schema = _ref.schema, context = _ref.context; return (0, _commonMacro.commonMacro)(input.substring(position), schema, { keyword: 'quote', paired: true, rawContentProcessor: rawContentProcessor, context: context }); }; var rawContentProcessor = exports.rawContentProcessor = function rawContentProcessor(_rawAttrs, rawContent, length, schema, context) { if (!rawContent.length) { var emptyQuote = emptyBlockquote(schema); return { type: 'pmnode', nodes: [emptyQuote], length: length }; } var parsedContent = (0, _text2.parseString)({ schema: schema, context: context, ignoreTokenTypes: [], input: rawContent }); var normalizedContent = (0, _normalize.normalizePMNodes)(parsedContent, schema); return { type: 'pmnode', nodes: sanitize(normalizedContent, schema), length: length }; }; function emptyBlockquote(schema) { var p = schema.nodes.paragraph.createChecked({}, []); return schema.nodes.blockquote.createChecked({}, p); } function sanitize(nodes, schema) { var output = []; var contentBuffer = []; var _iterator = _createForOfIteratorHelper(nodes), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var n = _step.value; switch (n.type.name) { case 'paragraph': case 'bulletList': case 'orderedList': case 'codeBlock': case 'mediaSingle': case 'mediaGroup': { /** * blockquote supports nesting paragraphs, lists, codeblocks and media nodes */ contentBuffer.push(n); break; } case 'heading': { /** * If a heading is inside a list item * - h1. Bold, Uppercase * - h2. Bold, Italic * - h3. Bold * - h4. Bold, Gray * - h5. Gray, Italic * - h6. Gray */ contentBuffer.push(transformHeading(n, schema)); break; } default: /** * Anything else should be lifted */ if (contentBuffer.length) { var _blockquote = schema.nodes.blockquote.createChecked({}, contentBuffer); output.push(_blockquote); contentBuffer = []; } output.push(n); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (contentBuffer.length) { var blockquote = schema.nodes.blockquote.createChecked({}, contentBuffer); output.push(blockquote); } return output; } function transformHeading(heading, schema) { var contentBuffer = []; heading.content.forEach(function (n) { var strong = schema.marks.strong.create(); var italic = schema.marks.em.create(); var gray = schema.marks.textColor.create({ color: '#97a0af' }); if (n.type.name === 'text') { if (n.text && heading.attrs.level === 1) { // @ts-ignore assigning to readonly prop to transform text n.text = n.text.toUpperCase(); } if (heading.attrs.level <= 4 && !(0, _text.hasAnyOfMarks)(n, ['strong', 'code'])) { n = n.mark([].concat((0, _toConsumableArray2.default)(n.marks), [strong])); } if ((heading.attrs.level === 5 || heading.attrs.level === 2) && !(0, _text.hasAnyOfMarks)(n, ['em', 'code'])) { n = n.mark([].concat((0, _toConsumableArray2.default)(n.marks), [italic])); } if (heading.attrs.level > 3 && !(0, _text.hasAnyOfMarks)(n, ['textColor', 'code'])) { n = n.mark([].concat((0, _toConsumableArray2.default)(n.marks), [gray])); } } contentBuffer.push(n); }); return schema.nodes.paragraph.createChecked({}, contentBuffer); }