Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

markdown-utils.ts 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * @fileoverview Utility functions for preprocessing Markdown content.
  3. * These functions were extracted from the main markdown renderer for better separation of concerns.
  4. * Includes preprocessing for LaTeX and custom "think" tags.
  5. */
  6. import { flow } from 'lodash-es'
  7. export const preprocessLaTeX = (content: string) => {
  8. if (typeof content !== 'string')
  9. return content
  10. const codeBlockRegex = /```[\s\S]*?```/g
  11. const codeBlocks = content.match(codeBlockRegex) || []
  12. let processedContent = content.replace(codeBlockRegex, 'CODE_BLOCK_PLACEHOLDER')
  13. processedContent = flow([
  14. (str: string) => str.replace(/\\\[(.*?)\\\]/g, (_, equation) => `$$${equation}$$`),
  15. (str: string) => str.replace(/\\\[([\s\S]*?)\\\]/g, (_, equation) => `$$${equation}$$`),
  16. (str: string) => str.replace(/\\\((.*?)\\\)/g, (_, equation) => `$$${equation}$$`),
  17. (str: string) => str.replace(/(^|[^\\])\$(.+?)\$/g, (_, prefix, equation) => `${prefix}$${equation}$`),
  18. ])(processedContent)
  19. codeBlocks.forEach((block) => {
  20. processedContent = processedContent.replace('CODE_BLOCK_PLACEHOLDER', block)
  21. })
  22. return processedContent
  23. }
  24. export const preprocessThinkTag = (content: string) => {
  25. const thinkOpenTagRegex = /<think>\n/g
  26. const thinkCloseTagRegex = /\n<\/think>/g
  27. return flow([
  28. (str: string) => str.replace(thinkOpenTagRegex, '<details data-think=true>\n'),
  29. (str: string) => str.replace(thinkCloseTagRegex, '\n[ENDTHINKFLAG]</details>'),
  30. (str: string) => str.replace(/(<\/details>)(?![^\S\r\n]*[\r\n])(?![^\S\r\n]*$)/g, '$1\n'),
  31. ])(content)
  32. }