@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
104 lines (93 loc) • 5.66 kB
JavaScript
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 <br> and <br/> tags in markdown text
* This plugin converts <br> and <br/> tags to proper HTML elements
* without requiring allowHtml to be enabled
*/
export var remarkBr = function remarkBr() {
return function (tree) {
// First try to process html nodes that might contain ONLY br tags
visit(tree, 'html', function (node, index, parent) {
if (!node.value || typeof node.value !== 'string') return;
// Only handle standalone br tags, not complex HTML containing br tags
var brRegex = /^\s*<\s*br\s*\/?>\s*$/gi;
if (brRegex.test(node.value)) {
// Replace the html node with a break node
parent.children.splice(index, 1, {
type: 'break'
});
return index;
}
});
// Also process text nodes
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;
// Check if the text contains <br> or <br/> tags
var brRegex = /<\s*br\s*\/?>/gi;
if (!brRegex.test(node.value)) return;
// Reset regex lastIndex for split operation
brRegex.lastIndex = 0;
// Split the text by br tags, but keep the matched tags
var parts = [];
var matches = [];
var lastIndex = 0;
var match;
while ((match = brRegex.exec(node.value)) !== null) {
// Add text before the match
if (match.index > lastIndex) {
parts.push(node.value.slice(lastIndex, match.index));
}
// Store the matched br tag
matches.push(match[0]);
lastIndex = match.index + match[0].length;
}
// Add remaining text after the last match
if (lastIndex < node.value.length) {
parts.push(node.value.slice(lastIndex));
}
// Create new nodes
var newNodes = [];
var _iterator = _createForOfIteratorHelper(parts.entries()),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _step$value = _slicedToArray(_step.value, 2),
i = _step$value[0],
part = _step$value[1];
// Add text node if not empty
if (part) {
newNodes.push({
type: 'text',
value: part
});
}
// Add br element if we have a corresponding match
if (i < matches.length) {
newNodes.push({
type: 'break'
});
}
}
// Replace the original text node with the new nodes
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
if (newNodes.length > 0) {
var _parent$children;
(_parent$children = parent.children).splice.apply(_parent$children, [index, 1].concat(newNodes));
return index + newNodes.length - 1; // Skip the newly added nodes
}
});
};
};