legends810/testingnew
0
1import rehypeRaw from 'rehype-raw';2import remarkGfm from 'remark-gfm';3import type { PluggableList, Plugin } from 'unified';4import rehypeSanitize, { defaultSchema, type Options as RehypeSanitizeOptions } from 'rehype-sanitize';5import { SKIP, visit } from 'unist-util-visit';6import type { UnistNode, UnistParent } from 'node_modules/unist-util-visit/lib';7 8export const allowedHTMLElements = [9 'a',10 'b',11 'blockquote',12 'br',13 'code',14 'dd',15 'del',16 'details',17 'div',18 'dl',19 'dt',20 'em',21 'h1',22 'h2',23 'h3',24 'h4',25 'h5',26 'h6',27 'hr',28 'i',29 'ins',30 'kbd',31 'li',32 'ol',33 'p',34 'pre',35 'q',36 'rp',37 'rt',38 'ruby',39 's',40 'samp',41 'source',42 'span',43 'strike',44 'strong',45 'sub',46 'summary',47 'sup',48 'table',49 'tbody',50 'td',51 'tfoot',52 'th',53 'thead',54 'tr',55 'ul',56 'var',57 'think',58];59 60// Add custom rehype plugin61function remarkThinkRawContent() {62 return (tree: any) => {63 visit(tree, (node: any) => {64 if (node.type === 'html' && node.value && node.value.startsWith('<think>')) {65 const cleanedContent = node.value.slice(7);66 node.value = `<div class="__boltThought__">${cleanedContent}`;67 68 return;69 }70 71 if (node.type === 'html' && node.value && node.value.startsWith('</think>')) {72 const cleanedContent = node.value.slice(8);73 node.value = `</div>${cleanedContent}`;74 }75 });76 };77}78 79const rehypeSanitizeOptions: RehypeSanitizeOptions = {80 ...defaultSchema,81 tagNames: allowedHTMLElements,82 attributes: {83 ...defaultSchema.attributes,84 div: [85 ...(defaultSchema.attributes?.div ?? []),86 'data*',87 ['className', '__boltArtifact__', '__boltThought__'],88 89 // ['className', '__boltThought__']90 ],91 },92 strip: [],93};94 95export function remarkPlugins(limitedMarkdown: boolean) {96 const plugins: PluggableList = [remarkGfm];97 98 if (limitedMarkdown) {99 plugins.unshift(limitedMarkdownPlugin);100 }101 102 plugins.unshift(remarkThinkRawContent);103 104 return plugins;105}106 107export function rehypePlugins(html: boolean) {108 const plugins: PluggableList = [];109 110 if (html) {111 plugins.push(rehypeRaw, [rehypeSanitize, rehypeSanitizeOptions]);112 }113 114 return plugins;115}116 117const limitedMarkdownPlugin: Plugin = () => {118 return (tree, file) => {119 const contents = file.toString();120 121 visit(tree, (node: UnistNode, index, parent: UnistParent) => {122 if (123 index == null ||124 ['paragraph', 'text', 'inlineCode', 'code', 'strong', 'emphasis'].includes(node.type) ||125 !node.position126 ) {127 return true;128 }129 130 let value = contents.slice(node.position.start.offset, node.position.end.offset);131 132 if (node.type === 'heading') {133 value = `\n${value}`;134 }135 136 parent.children[index] = {137 type: 'text',138 value,139 } as any;140 141 return [SKIP, index] as const;142 });143 };144};145 