-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GLI-3964) Able to bold and italicize the same word when editing a no…
…te. (#3)
- Loading branch information
1 parent
7abbd35
commit 332b9bb
Showing
7 changed files
with
241 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ Pod::Spec.new do |s| | |
s.license = "MIT" | ||
s.authors = { "Tobias Sundstrand" => "[email protected]", "Antoine Cœur" => "" } | ||
s.social_media_url = "https://twitter.com/laptobbe" | ||
s.ios.deployment_target = "6.0" | ||
s.tvos.deployment_target = "9.0" | ||
s.osx.deployment_target = "10.7" | ||
s.ios.deployment_target = "8.0" | ||
s.tvos.deployment_target = "10.0" | ||
s.osx.deployment_target = "10.9" | ||
s.source = { :git => "https://github.com/laptobbe/TSMarkdownParser.git", :tag => s.version.to_s } | ||
s.source_files = "TSMarkdownParser/**/*.{h,m}" | ||
s.source_files = "TSMarkdownParser/**/*.{h,m,swift}" | ||
s.requires_arc = true | ||
s.ios.framework = 'UIKit' | ||
s.tvos.framework = 'UIKit' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// | ||
// NSAttributedString+Markdown.swift | ||
// TSMarkdownParserLib | ||
// | ||
// Created by Stephan Heilner on 10/30/17. | ||
// Copyright © 2017 Computertalk Sweden. All rights reserved. | ||
// | ||
|
||
#if !os(OSX) | ||
|
||
import UIKit | ||
import Foundation | ||
|
||
public extension NSAttributedString { | ||
|
||
public func markdownString() -> String { | ||
let bulletCharacter = Character("\u{2022}") | ||
let nonBreakingSpaceCharacter = Character("\u{00A0}") | ||
|
||
var markdownString = "" | ||
|
||
enum FormattingChange { | ||
case enable | ||
case disable | ||
case keep | ||
|
||
static func getFormattingChange(_ before: Bool, after: Bool) -> FormattingChange { | ||
if !before && after { return .enable } | ||
if before && !after { return .disable } | ||
return .keep | ||
} | ||
} | ||
|
||
var stringHasBoldEnabled = false | ||
var stringHasItalicEnabled = false | ||
var closingString = "" | ||
var characterOnBulletedListLine = false | ||
var openedNumberedListStarter = false | ||
var characterOnNumberedListLine = false | ||
var numberedListIsFirstLine = false | ||
var previousCharacter: Character? | ||
|
||
enumerateAttributes(in: NSRange(location: 0, length: length), options: []) { attributes, range, shouldStop in | ||
if let traits = (attributes[NSAttributedStringKey.font] as? UIFont)?.fontDescriptor.symbolicTraits { | ||
let boldChange = FormattingChange.getFormattingChange(stringHasBoldEnabled, after: traits.contains(.traitBold)) | ||
let italicChange = FormattingChange.getFormattingChange(stringHasItalicEnabled, after: traits.contains(.traitItalic)) | ||
var formatString = "" | ||
switch boldChange { | ||
case .enable: | ||
formatString += "**" | ||
closingString = "**\(closingString)" | ||
case .disable: | ||
if stringHasItalicEnabled && italicChange == .keep { | ||
formatString += "_**_" | ||
closingString = "_" | ||
} else { | ||
formatString += "**" | ||
closingString = "" | ||
} | ||
case .keep: | ||
break | ||
} | ||
|
||
switch italicChange { | ||
case .enable: | ||
formatString += "_" | ||
closingString = "_\(closingString)" | ||
case .disable: | ||
if stringHasBoldEnabled && boldChange == .keep { | ||
formatString = "**_**\(formatString)" | ||
closingString = "**" | ||
} else { | ||
formatString = "_\(formatString)" | ||
closingString = "" | ||
} | ||
case .keep: | ||
break | ||
} | ||
|
||
markdownString += formatString | ||
|
||
stringHasBoldEnabled = traits.contains(.traitBold) | ||
stringHasItalicEnabled = traits.contains(.traitItalic) | ||
} | ||
|
||
let preprocessedString = (self.string as NSString).substring(with: range) | ||
let processedString = preprocessedString.characters.reduce("") { resultString, character in | ||
var stringToAppend = "" | ||
|
||
switch character { | ||
case "\\", "`", "*", "_", "{", "}", "[", "]", "(", ")", "#", "+", "-", "!": | ||
stringToAppend = "\\\(character)" | ||
case "\n", "\u{2028}": | ||
stringToAppend = "\(closingString)\(character)" | ||
if !characterOnBulletedListLine && !characterOnNumberedListLine { | ||
stringToAppend += String(closingString.characters.reversed()) | ||
} | ||
|
||
characterOnBulletedListLine = false | ||
characterOnNumberedListLine = false | ||
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0": | ||
if previousCharacter == "\n" || previousCharacter == nil || previousCharacter == nonBreakingSpaceCharacter { | ||
openedNumberedListStarter = true | ||
} | ||
|
||
numberedListIsFirstLine = previousCharacter == nil ? true : numberedListIsFirstLine | ||
stringToAppend = "\(character)" | ||
case bulletCharacter: | ||
characterOnBulletedListLine = true | ||
stringToAppend = "+ \(previousCharacter != nil ? String(closingString.characters.reversed()) : markdownString)" | ||
markdownString = previousCharacter == nil ? "" : markdownString | ||
case ".": | ||
if openedNumberedListStarter { | ||
openedNumberedListStarter = false | ||
characterOnNumberedListLine = true | ||
|
||
stringToAppend = "\(character) \(!numberedListIsFirstLine ? String(closingString.characters.reversed()) : markdownString)" | ||
|
||
if numberedListIsFirstLine { | ||
markdownString = "" | ||
numberedListIsFirstLine = false | ||
} | ||
break | ||
} | ||
stringToAppend = "\\\(character)" | ||
case nonBreakingSpaceCharacter: | ||
if characterOnBulletedListLine || characterOnNumberedListLine { | ||
break | ||
} | ||
stringToAppend = " " | ||
default: | ||
if (previousCharacter == "\n" || previousCharacter == "\u{2028}") && characterOnBulletedListLine { | ||
characterOnBulletedListLine = false | ||
stringToAppend = "\(String(closingString.characters.reversed()))\(character)" | ||
} else { | ||
stringToAppend = "\(character)" | ||
} | ||
} | ||
|
||
previousCharacter = character | ||
return "\(resultString)\(stringToAppend)" | ||
} | ||
markdownString += processedString | ||
} | ||
markdownString += closingString | ||
markdownString = markdownString.replacingOccurrences(of: "**__**", with: "").replacingOccurrences(of: "****", with: "") | ||
.replacingOccurrences(of: "__", with: "") | ||
// Help the user because they probably didn't intend to have empty bullets and it will make markdown have a + if we leave them | ||
markdownString = markdownString.replacingOccurrences(of: "+ \n", with: "") | ||
if markdownString.hasSuffix("+ ") { | ||
markdownString = (markdownString as NSString).substring(to: markdownString.characters.count - 2) | ||
} | ||
|
||
return markdownString | ||
} | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.