in HuggingChat-Mac/Models/ModelAttributedStringParser.swift [34:77]
mutating func parserResults(from document: Document) -> [ParserResult] {
var results = [ParserResult]()
let paragraph = NSMutableParagraphStyle()
paragraph.lineHeightMultiple = 1.45
var currentAttrString = NSMutableAttributedString(string: "", attributes: [.font: NSFont.systemFont(ofSize: 14), .foregroundColor: isDarkMode ? NSColor.HF.gray300 : NSColor.HF.gray700, .paragraphStyle: paragraph])
func appendCurrentAttrString() {
if !currentAttrString.string.isEmpty {
results.append(.init(attributedString: currentAttrString, resultType: .text))
}
}
document.children.enumerated().forEach { (index, markup) in
if index != 0 {
currentAttrString.append(.singleNewline(withFontSize: newLineFontSize))
}
let attrString = visit(markup)
if let codeBlock = markup as? CodeBlock {
appendCurrentAttrString()
let m = NSMutableAttributedString(attributedString: attrString)
results.append(.init(attributedString: m, resultType: .codeBlock(codeBlock.language)))
currentAttrString = NSMutableAttributedString()
} else if markup.children.contains(where: { $0 is Markdown.Image }) {
markup.children.forEach { mk in
let a = visit(mk)
if let i = mk as? Markdown.Image {
appendCurrentAttrString()
results.append(.init(attributedString: NSMutableAttributedString(attributedString: a), resultType: .image(i.source ?? "")))
currentAttrString = NSMutableAttributedString()
} else {
currentAttrString.append(a)
}
}
} else {
currentAttrString.append(attrString)
}
}
appendCurrentAttrString()
return results
}