Я пытаюсь понять, как добавить свою собственную разметку в MarkupVisitor ?
Мне удалось использовать обходной путь с помощью visitInlineAttributes, который применяется к ^[]().
Есть ли способ добавить свою собственную разметку?
Например, если бы я хотел создать собственную встроенную разметку, где текст внутри @[text] имел бы серый цвет? Аналогично тому, как **жирный** делает вещи жирными.
Кто-то задал этот вопрос в библиотеке, но я не понимаю, как реализовать там единственный ответ.
https://github.com/swiftlang/swift-markdown/issues/122
Вот пример ниже:
Код: Выделить всё
import UIKit
import Markdown
import SnapKit
class ViewController: UIViewController, UITextViewDelegate {
let label = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
label.isEditable = false
label.delegate = self
label.linkTextAttributes = [:]
view.addSubview(label)
label.snp.makeConstraints { make in
make.edges.equalTo(view.safeAreaLayoutGuide).inset(10)
}
var md = MarkdownParser()
let attr = md.attributedString(from: "Hello World! Here's some text which is **bold** and here's some which is *italics*. Here's some ^[[metadata containing link](https://old.reddit.com/r/SaaS/comments/1fgv248/fuck_founder_mode_work_in_fuck_off_mode/)]() which is gray and not underlined. And here's some normal [link](https://hckrnews.com) which is underlined and red. You are welcome!")
print("ATTR:\n\n\(attr)")
label.attributedText = attr
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("shouldInteractWith: \(URL)")
return true
}
}
struct MarkdownParser: MarkupVisitor {
typealias Result = NSMutableAttributedString
mutating func attributedString(from : String) -> NSMutableAttributedString {
let document = Document(parsing: from)
print(document.debugDescription())
return NSMutableAttributedString(attributedString: visit(document))
}
mutating func visit(_ markup: Markup) -> NSAttributedString {
return markup.accept(&self)
}
mutating func defaultVisit(_ markup: any Markdown.Markup) -> NSMutableAttributedString {
let result = NSMutableAttributedString()
for child in markup.children {
result.append(visit(child))
}
return result
}
mutating func visitText(_ text: Text) -> NSMutableAttributedString {
return NSMutableAttributedString(string: text.plainText, attributes: [.font:UIFont.systemFont(ofSize: 18, weight: .regular),.foregroundColor:UIColor.label])
}
mutating func visitEmphasis(_ emphasis: Emphasis) -> NSMutableAttributedString {
return doVisit(emphasis)
}
mutating func visitStrong(_ strong: Strong) -> NSMutableAttributedString {
return doVisit(strong)
}
mutating func visitInlineAttributes(_ attributes: InlineAttributes) -> NSMutableAttributedString {
return doVisit(attributes)
}
mutating func visitLink(_ link: Link) -> NSMutableAttributedString {
return doVisit(link)
}
mutating func doVisit(_ markup: any Markup) -> NSMutableAttributedString {
let result = NSMutableAttributedString()
for child in markup.children {
result.append(visit(child))
}
switch markup {
case is Strong:
fallthrough
case is Emphasis:
result.enumerateAttribute(.font, in: result.fullRange, options: []) { value, range, stop in
if let newFont = (value as? UIFont)?.addTrait(trait: markup is Strong ? .traitBold : .traitItalic) {
result.addAttribute(.font, value: newFont, range: range)
}
}
case is InlineAttributes:
result.removeAttributes([.underlineStyle,.underlineColor])
result.addAttribute(.foregroundColor, value: UIColor.tertiaryLabel)
case is Link:
if let destination = (markup as? Link)?.destination, let url = URL(string: destination) {
let color = UIColor.systemRed
result.addAttributes([.underlineStyle : NSUnderlineStyle.single.rawValue, .underlineColor : color,.foregroundColor : color, .link : url])
}
default:
break
}
return result
}
}
extension NSAttributedString {
var fullRange : NSRange {
NSRange(location: 0, length: length)
}
}
extension NSMutableAttributedString {
func addAttribute(_ name: NSAttributedString.Key, value: Any) {
addAttribute(name, value: value, range: fullRange)
}
func addAttributes(_ attrs: [NSAttributedString.Key : Any]) {
addAttributes(attrs, range: fullRange)
}
func removeAttribute(_ name: NSAttributedString.Key) {
removeAttribute(name, range: fullRange)
}
func removeAttributes(_ names: [NSAttributedString.Key]) {
for attribute in names {
removeAttribute(attribute)
}
}
}
extension UIFont {
func addTrait(trait : UIFontDescriptor.SymbolicTraits) -> UIFont {
var traits = fontDescriptor.symbolicTraits
if traits.contains(trait) {
return self
} else {
traits.insert([trait])
return UIFont(descriptor: fontDescriptor.withSymbolicTraits(traits)!, size: pointSize)
}
}
}

Подробнее здесь: https://stackoverflow.com/questions/789 ... t-markdown