Estou usando a biblioteca swift-markdown no meu aplicativo.
Estou tentando descobrir como adicionar meu próprio costume Markup
ao MarkupVisitor
?
Consegui usar uma solução alternativa visitInlineAttributes
que se aplica a ^[]()
.
Existe uma maneira de adicionar minha própria marcação personalizada?
Por exemplo, se eu quisesse minha própria marcação inline onde o texto dentro @[text]
teria cor cinza? Semelhante a como **bold** deixa as coisas em negrito.
Alguém fez essa pergunta na biblioteca, mas não entendi como implementar a única resposta lá.
https://github.com/swiftlang/swift-markdown/issues/122
Aqui está um exemplo que eu tenho:
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)
}
}
}
Isso mostra o seguinte:
Não há uma maneira geral de adicionar um novo tipo de
Markup
que tenha sintaxe arbitrária (por exemplo, começando com@[
e terminando com]
). Para fazer isso, você teria que bifurcar cmark e swift-markdown. Da documentação deMarkup
,O GitHub que você encontrou é sobre analisar um
BlockDirective
. Isso é como estender a sintaxe, porque você pode atribuir significados diferentes para diretivas de bloco com nomes diferentes. Por exemplo, você pode implementar@Gray { ... }
significado texto cinza e@LaTeX { ... }
significado código LaTeX. Mas no final do dia, estes são apenasBlockDirective
s. Não há nenhum novo tipo deMarkup
ser analisado. É sempre a mesma sintaxe -@
seguida pelo nome, opcionalmente seguida por alguns argumentos, opcionalmente seguida pelos filhos.Se você quiser adaptar a solução no problema vinculado do GitHub, precisará passar a
parseBlockDirectives
opção ao criar oDocument
.Em seguida, implemente o método de visitante correspondente
Em seguida, adicione um caso correspondente em seu
switch
:No entanto, este é um contêiner de bloco , não um inline como
**bold**
. Ou seja, você deve escrevê-lo em uma linha separada na fonte markdown:Se você quer que ele seja inline, então
InlineAttributes
é a maneira pretendida de fazer isso. Você já está fazendo isso corretamente.O texto dentro de um
^[]()
não precisa ser um link. Você pode simplesmente escrevere verifique assim:
Assim como o nome e os argumentos de um
BlockDirective
, dados arbitrários podem ser colocados no()
de umInlineAttributes
. Você pode implementar^[...](gray)
para significar texto cinza,^[...](serif)
para significar que o texto deve ser escrito com uma fonte serifada, e assim por diante.