@yuntijs/ui
Version:
☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps
32 lines (30 loc) • 1.21 kB
JavaScript
import { visit } from 'unist-util-visit';
// eslint-disable-next-line unicorn/consistent-function-scoping
var rehypePlugin = function rehypePlugin() {
return function (tree) {
visit(tree, 'element', function (node, index, parent) {
if (node.type === 'element' && node.tagName === 'p') {
var children = node.children || [];
var openTagIndex = children.findIndex(function (child) {
return child.type === 'raw' && child.value === '<think>';
});
var closeTagIndex = children.findIndex(function (child) {
return child.type === 'raw' && child.value === '</think>';
});
if (openTagIndex !== -1 && closeTagIndex !== -1 && closeTagIndex > openTagIndex) {
var content = children.slice(openTagIndex + 1, closeTagIndex);
var thinkNode = {
children: content,
properties: {},
tagName: 'think',
type: 'element'
};
// Replace the entire paragraph with our new thinkNode node
parent.children.splice(index, 1, thinkNode);
return index; // Skip processing the newly inserted node
}
}
});
};
};
export default rehypePlugin;