You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown-utils.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }
  33. /**
  34. * Transforms a URI for use in react-markdown, ensuring security and compatibility.
  35. * This function is designed to work with react-markdown v9+ which has stricter
  36. * default URL handling.
  37. *
  38. * Behavior:
  39. * 1. Always allows the custom 'abbr:' protocol.
  40. * 2. Always allows page-local fragments (e.g., "#some-id").
  41. * 3. Always allows protocol-relative URLs (e.g., "//example.com/path").
  42. * 4. Always allows purely relative paths (e.g., "path/to/file", "/abs/path").
  43. * 5. Allows absolute URLs if their scheme is in a permitted list (case-insensitive):
  44. * 'http:', 'https:', 'mailto:', 'xmpp:', 'irc:', 'ircs:'.
  45. * 6. Intelligently distinguishes colons used for schemes from colons within
  46. * paths, query parameters, or fragments of relative-like URLs.
  47. * 7. Returns the original URI if allowed, otherwise returns `undefined` to
  48. * signal that the URI should be removed/disallowed by react-markdown.
  49. */
  50. export const customUrlTransform = (uri: string): string | undefined => {
  51. const PERMITTED_SCHEME_REGEX = /^(https?|ircs?|mailto|xmpp|abbr):$/i
  52. if (uri.startsWith('#'))
  53. return uri
  54. if (uri.startsWith('//'))
  55. return uri
  56. const colonIndex = uri.indexOf(':')
  57. if (colonIndex === -1)
  58. return uri
  59. const slashIndex = uri.indexOf('/')
  60. const questionMarkIndex = uri.indexOf('?')
  61. const hashIndex = uri.indexOf('#')
  62. if (
  63. (slashIndex !== -1 && colonIndex > slashIndex)
  64. || (questionMarkIndex !== -1 && colonIndex > questionMarkIndex)
  65. || (hashIndex !== -1 && colonIndex > hashIndex)
  66. )
  67. return uri
  68. const scheme = uri.substring(0, colonIndex + 1).toLowerCase()
  69. if (PERMITTED_SCHEME_REGEX.test(scheme))
  70. return uri
  71. return undefined
  72. }