UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

186 lines (172 loc) 9.5 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } import { visit } from 'unist-util-visit'; /** * Remark plugin to handle <video> tags in markdown text * This plugin converts <video> tags to proper video elements * without requiring allowHtml to be enabled * * @example * <video src="https://example.com/video.mp4" /> * <video src="https://example.com/video.mp4" controls width="400" height="300" /> */ export var remarkVideo = function remarkVideo() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var _options$videoTags = options.videoTags, videoTags = _options$videoTags === void 0 ? ['video'] : _options$videoTags; return function (tree) { // 处理HTML节点中的video标签 visit(tree, 'html', function (node) { var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var parent = arguments.length > 2 ? arguments[2] : undefined; if (!node.value || typeof node.value !== 'string') return; var _iterator = _createForOfIteratorHelper(videoTags), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var tagName = _step.value; // 匹配自闭合的video标签,支持属性 var selfClosingPattern = "^<".concat(tagName, "([^>]*?)\\s*\\/?\\s*>$"); var selfClosingMatch = node.value.trim().match(new RegExp(selfClosingPattern, 'i')); if (selfClosingMatch) { var _selfClosingMatch$; var attributesStr = ((_selfClosingMatch$ = selfClosingMatch[1]) === null || _selfClosingMatch$ === void 0 ? void 0 : _selfClosingMatch$.trim()) || ''; // 解析属性 var properties = {}; var attrRegex = /(\w+)=["']([^"']*?)["']/g; var attrMatch = void 0; while ((attrMatch = attrRegex.exec(attributesStr)) !== null) { properties[attrMatch[1]] = attrMatch[2]; } console.log('remarkVideo: Found video tag:', tagName, properties); // 创建video节点 var newNode = { children: [], data: { hName: tagName, hProperties: properties }, type: tagName }; // 替换html节点为video节点 parent.children.splice(index, 1, newNode); return index; } // 匹配成对的video标签(虽然不太常见,但也支持) var pairedPattern = "^<".concat(tagName, "([^>]*?)>(.*?)<\\/").concat(tagName, ">$"); var pairedMatch = node.value.trim().match(new RegExp(pairedPattern, 'is')); if (pairedMatch) { var _pairedMatch$; var _attributesStr = ((_pairedMatch$ = pairedMatch[1]) === null || _pairedMatch$ === void 0 ? void 0 : _pairedMatch$.trim()) || ''; var content = pairedMatch[2] || ''; // 解析属性 var _properties = {}; var _attrRegex = /(\w+)=["']([^"']*?)["']/g; var _attrMatch = void 0; while ((_attrMatch = _attrRegex.exec(_attributesStr)) !== null) { _properties[_attrMatch[1]] = _attrMatch[2]; } console.log('remarkVideo: Found paired video tag:', tagName, _properties); // 创建video节点 var _newNode = { children: content ? [{ type: 'text', value: content }] : [], data: { hName: tagName, hProperties: _properties }, type: tagName }; // 替换html节点为video节点 parent.children.splice(index, 1, _newNode); return index; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }); // 处理文本节点中的video标签(HTML实体编码形式) visit(tree, 'text', function (node) { var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var parent = arguments.length > 2 ? arguments[2] : undefined; if (!node.value || typeof node.value !== 'string') return; var _iterator2 = _createForOfIteratorHelper(videoTags), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var tagName = _step2.value; // 处理HTML实体编码的自闭合video标签 var encodedSelfClosingPattern = "&lt;".concat(tagName, "([^&]*?)\\s*\\/?\\s*&gt;"); var encodedSelfClosingRegex = new RegExp(encodedSelfClosingPattern, 'gi'); if (!encodedSelfClosingRegex.test(node.value)) continue; // 重置正则表达式的 lastIndex encodedSelfClosingRegex.lastIndex = 0; var text = node.value; var newNodes = []; var lastIndex = 0; var match = void 0; while ((match = encodedSelfClosingRegex.exec(text)) !== null) { var _match = match, _match2 = _slicedToArray(_match, 2), fullMatch = _match2[0], attributesStr = _match2[1]; var startIndex = match.index; // 添加匹配前的文本 if (startIndex > lastIndex) { newNodes.push({ type: 'text', value: text.slice(lastIndex, startIndex) }); } // 解析属性(需要解码HTML实体) var decodedAttrs = attributesStr.replaceAll('&quot;', '"').replaceAll('&#39;', "'").replaceAll('&amp;', '&').replaceAll('&lt;', '<').replaceAll('&gt;', '>'); var properties = {}; var attrRegex = /(\w+)=["']([^"']*?)["']/g; var attrMatch = void 0; while ((attrMatch = attrRegex.exec(decodedAttrs)) !== null) { properties[attrMatch[1]] = attrMatch[2]; } console.log('remarkVideo: Found encoded video tag:', tagName, properties); // 添加video节点 newNodes.push({ children: [], data: { hName: tagName, hProperties: properties }, type: tagName }); lastIndex = startIndex + fullMatch.length; } // 添加剩余文本 if (lastIndex < text.length) { newNodes.push({ type: 'text', value: text.slice(lastIndex) }); } // 替换当前节点 if (newNodes.length > 0 && parent) { var _parent$children; (_parent$children = parent.children).splice.apply(_parent$children, [index, 1].concat(newNodes)); return index + newNodes.length - 1; } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } }); }; };