Skip to content

Instantly share code, notes, and snippets.

@joshkopecek
Created August 19, 2021 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkopecek/11e3f27f0969b4155ccbe0562e5c7cd6 to your computer and use it in GitHub Desktop.
Save joshkopecek/11e3f27f0969b4155ccbe0562e5c7cd6 to your computer and use it in GitHub Desktop.
//
// RTLParser.swift
//
// Created by Josh Kopecek on 19/08/2021.
//
import Foundation
import MarkdownKit
class MarkdownRTL: MarkdownElement {
var regex: String = "(.+)"
func regularExpression() throws -> NSRegularExpression {
return try NSRegularExpression(pattern: regex, options: [])
}
func match(_ match: NSTextCheckingResult,
attributedString: NSMutableAttributedString) {
let matchAttr = attributedString.attributedSubstring(from: match.range)
let firstCharAsUnicodeScalar = matchAttr.string.unicodeScalars.first?.value
if firstCharAsUnicodeScalar != nil && firstCharAsUnicodeScalar! >= 1424 && firstCharAsUnicodeScalar! <= 1983 {
let rightAlign = NSMutableParagraphStyle()
rightAlign.alignment = .right
attributedString.addAttributes([.paragraphStyle: rightAlign], range: match.range)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment