TSMarkDownTool.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // TSMarkDownTool.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/2/23.
  6. //
  7. let kLineSpacing = 8.0
  8. let KFont = UIFont.font(size: 16)
  9. let kSendColor = "#111111".uiColor
  10. let paragraphStyle:NSMutableParagraphStyle = {
  11. let paragraphStyle = NSMutableParagraphStyle()
  12. paragraphStyle.lineSpacing = kLineSpacing
  13. return paragraphStyle
  14. }()
  15. ///发送的Attributed
  16. func kMDSendAttributedString(text:String) -> NSAttributedString{
  17. let attributes : [NSAttributedString.Key : Any] = [
  18. .font: KFont,
  19. .foregroundColor:kSendColor,
  20. .paragraphStyle:paragraphStyle
  21. ]
  22. let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
  23. // attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
  24. return attributedString
  25. }
  26. var md: MarkdownParser = {
  27. let parser = MarkdownParser(font: KFont,color: .white)
  28. parser.enabledElements = .disabledAutomaticLink
  29. let code = TSCustomMarkdownCode()
  30. code.textBackgroundColor = .clear
  31. code.color = .white.withAlphaComponent(0.8)
  32. code.textHighlightColor = .white.withAlphaComponent(0.8)
  33. parser.replaceDefaultElement(parser.code, with: code)
  34. return parser
  35. }()
  36. //ai 对话的 字符串转成 markdown NSAttributedString
  37. func kMDAttributedString(text:String) -> NSAttributedString{
  38. let attributedString = NSMutableAttributedString(attributedString: md.parse(text))
  39. attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
  40. return attributedString
  41. }
  42. open class TSCustomMarkdownCode: MarkdownCode {
  43. fileprivate static let regex = "(^|\\s?)(\\`{1,3})(.+?)(\\2)"
  44. open override var regex: String {
  45. return TSCustomMarkdownCode.regex
  46. }
  47. }
  48. //import SwiftyMarkdown
  49. //private var md: SwiftyMarkdown = {
  50. //
  51. // var characterRules = SwiftyMarkdown.characterRules
  52. // characterRules.append(
  53. // CharacterRule(
  54. // primaryTag: CharacterRuleTag(tag: "```", type: .repeating),
  55. // otherTags: [],
  56. // styles: [1 : CharacterStyle.code],
  57. // shouldCancelRemainingRules: true,
  58. // balancedTags: true
  59. // )
  60. // )
  61. // SwiftyMarkdown.characterRules = characterRules
  62. //
  63. // let md = SwiftyMarkdown(string: "")
  64. // md.setFontColorForAllStyles(with: .white)
  65. //// md.code.color = .red
  66. //
  67. //// let characterRules = [
  68. //// CharacterRule(primaryTag: CharacterRuleTag(tag: "```", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.code], shouldCancelRemainingRules: true, balancedTags: true)
  69. //// ]
  70. //// let processor = SwiftyTokeniser( with : characterRules)
  71. //// let string = "The elf will speak now: %Here is my elf speaking%"
  72. //// let tokens = processor.process(string)
  73. //
  74. //
  75. //
  76. // return md
  77. //}()
  78. //
  79. ////字符串转成 markdown NSAttributedString
  80. //func kMDAttributedString(text:String) -> NSAttributedString{
  81. // return md.attributedString(from: text)
  82. //}