// // TSMarkDownTool.swift // AIEmoji // // Created by 100Years on 2025/2/23. // let kLineSpacing = 8.0 let KFont = UIFont.font(size: 16) let kSendColor = "#111111".uiColor let paragraphStyle:NSMutableParagraphStyle = { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = kLineSpacing return paragraphStyle }() ///发送的Attributed func kMDSendAttributedString(text:String) -> NSAttributedString{ let attributes : [NSAttributedString.Key : Any] = [ .font: KFont, .foregroundColor:kSendColor, .paragraphStyle:paragraphStyle ] let attributedString = NSMutableAttributedString(string: text, attributes: attributes) // attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length)) return attributedString } var md: MarkdownParser = { let parser = MarkdownParser(font: KFont,color: .white) parser.enabledElements = .disabledAutomaticLink let code = TSCustomMarkdownCode() code.textBackgroundColor = .clear code.color = .white.withAlphaComponent(0.8) code.textHighlightColor = .white.withAlphaComponent(0.8) parser.replaceDefaultElement(parser.code, with: code) return parser }() //ai 对话的 字符串转成 markdown NSAttributedString func kMDAttributedString(text:String) -> NSAttributedString{ let attributedString = NSMutableAttributedString(attributedString: md.parse(text)) attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length)) return attributedString } open class TSCustomMarkdownCode: MarkdownCode { fileprivate static let regex = "(^|\\s?)(\\`{1,3})(.+?)(\\2)" open override var regex: String { return TSCustomMarkdownCode.regex } } //import SwiftyMarkdown //private var md: SwiftyMarkdown = { // // var characterRules = SwiftyMarkdown.characterRules // characterRules.append( // CharacterRule( // primaryTag: CharacterRuleTag(tag: "```", type: .repeating), // otherTags: [], // styles: [1 : CharacterStyle.code], // shouldCancelRemainingRules: true, // balancedTags: true // ) // ) // SwiftyMarkdown.characterRules = characterRules // // let md = SwiftyMarkdown(string: "") // md.setFontColorForAllStyles(with: .white) //// md.code.color = .red // //// let characterRules = [ //// CharacterRule(primaryTag: CharacterRuleTag(tag: "```", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.code], shouldCancelRemainingRules: true, balancedTags: true) //// ] //// let processor = SwiftyTokeniser( with : characterRules) //// let string = "The elf will speak now: %Here is my elf speaking%" //// let tokens = processor.process(string) // // // // return md //}() // ////字符串转成 markdown NSAttributedString //func kMDAttributedString(text:String) -> NSAttributedString{ // return md.attributedString(from: text) //}