Created
February 5, 2016 22:06
-
-
Save getaaron/4100b9cba021d198806f to your computer and use it in GitHub Desktop.
Generates RedCarpet-compatible anchor text
This file contains hidden or 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
import Foundation | |
extension Character { | |
var invalid: Bool { | |
let invalid = Set(" -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'".characters) | |
return invalid.contains(self) | |
} | |
var isAscii: Bool { | |
guard let number = String(self).utf16.first else { return false } | |
return (65..<127) ~= number | |
} | |
var downcased: Character { | |
if let downcased = String(self).lowercaseString.characters.first { | |
return downcased | |
} | |
return self | |
} | |
} | |
func anchor(header: String) -> String { | |
var output = String(header.characters.map { | |
var char = $0.downcased | |
if $0.invalid || !$0.isAscii { | |
char = "-" | |
} | |
return char | |
}) | |
while output.containsString("--") { | |
output = output.stringByReplacingOccurrencesOfString("--", withString: "-") | |
} | |
return output | |
} | |
let input = "How closely should my résumé mirror my LinkedIn profile?" | |
// prints how-closely-should-my-r-sum-mirror-my-linkedin-profile- | |
print(anchor(input)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment