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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. import { ALLOW_UNSAFE_DATA_SCHEME } from '@/config'
  8. export const preprocessLaTeX = (content: string) => {
  9. if (typeof content !== 'string')
  10. return content
  11. const codeBlockRegex = /```[\s\S]*?```/g
  12. const codeBlocks = content.match(codeBlockRegex) || []
  13. const escapeReplacement = (str: string) => str.replace(/\$/g, '_TMP_REPLACE_DOLLAR_')
  14. let processedContent = content.replace(codeBlockRegex, 'CODE_BLOCK_PLACEHOLDER')
  15. processedContent = flow([
  16. (str: string) => str.replace(/\\\[(.*?)\\\]/g, (_, equation) => `$$${equation}$$`),
  17. (str: string) => str.replace(/\\\[([\s\S]*?)\\\]/g, (_, equation) => `$$${equation}$$`),
  18. (str: string) => str.replace(/\\\((.*?)\\\)/g, (_, equation) => `$$${equation}$$`),
  19. (str: string) => str.replace(/(^|[^\\])\$(.+?)\$/g, (_, prefix, equation) => `${prefix}$${equation}$`),
  20. ])(processedContent)
  21. codeBlocks.forEach((block) => {
  22. processedContent = processedContent.replace('CODE_BLOCK_PLACEHOLDER', escapeReplacement(block))
  23. })
  24. processedContent = processedContent.replace(/_TMP_REPLACE_DOLLAR_/g, '$')
  25. return processedContent
  26. }
  27. export const preprocessThinkTag = (content: string) => {
  28. const thinkOpenTagRegex = /(<think>\n)+/g
  29. const thinkCloseTagRegex = /\n<\/think>/g
  30. return flow([
  31. (str: string) => str.replace(thinkOpenTagRegex, '<details data-think=true>\n'),
  32. (str: string) => str.replace(thinkCloseTagRegex, '\n[ENDTHINKFLAG]</details>'),
  33. (str: string) => str.replace(/(<\/details>)(?![^\S\r\n]*[\r\n])(?![^\S\r\n]*$)/g, '$1\n'),
  34. ])(content)
  35. }
  36. /**
  37. * Transforms a URI for use in react-markdown, ensuring security and compatibility.
  38. * This function is designed to work with react-markdown v9+ which has stricter
  39. * default URL handling.
  40. *
  41. * Behavior:
  42. * 1. Always allows the custom 'abbr:' protocol.
  43. * 2. Always allows page-local fragments (e.g., "#some-id").
  44. * 3. Always allows protocol-relative URLs (e.g., "//example.com/path").
  45. * 4. Always allows purely relative paths (e.g., "path/to/file", "/abs/path").
  46. * 5. Allows absolute URLs if their scheme is in a permitted list (case-insensitive):
  47. * 'http:', 'https:', 'mailto:', 'xmpp:', 'irc:', 'ircs:'.
  48. * 6. Intelligently distinguishes colons used for schemes from colons within
  49. * paths, query parameters, or fragments of relative-like URLs.
  50. * 7. Returns the original URI if allowed, otherwise returns `undefined` to
  51. * signal that the URI should be removed/disallowed by react-markdown.
  52. */
  53. export const customUrlTransform = (uri: string): string | undefined => {
  54. const PERMITTED_SCHEME_REGEX = /^(https?|ircs?|mailto|xmpp|abbr):$/i
  55. if (uri.startsWith('#'))
  56. return uri
  57. if (uri.startsWith('//'))
  58. return uri
  59. const colonIndex = uri.indexOf(':')
  60. if (colonIndex === -1)
  61. return uri
  62. const slashIndex = uri.indexOf('/')
  63. const questionMarkIndex = uri.indexOf('?')
  64. const hashIndex = uri.indexOf('#')
  65. if (
  66. (slashIndex !== -1 && colonIndex > slashIndex)
  67. || (questionMarkIndex !== -1 && colonIndex > questionMarkIndex)
  68. || (hashIndex !== -1 && colonIndex > hashIndex)
  69. )
  70. return uri
  71. const scheme = uri.substring(0, colonIndex + 1).toLowerCase()
  72. if (PERMITTED_SCHEME_REGEX.test(scheme))
  73. return uri
  74. if (ALLOW_UNSAFE_DATA_SCHEME && scheme === 'data:')
  75. return uri
  76. return undefined
  77. }