Skip to content

Commit

Permalink
Fixing issue SimonFairbairn#103 - Underline
Browse files Browse the repository at this point in the history
  • Loading branch information
spneshaei committed Jul 7, 2021
1 parent 2bbf121 commit f5cdb57
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Sources/SwiftyMarkdown/SwiftyMarkdown+iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension SwiftyMarkdown {

var globalBold = false
var globalItalic = false
var globalUnderline = false

let style : FontProperties
// What type are we and is there a font name set?
Expand Down Expand Up @@ -72,6 +73,8 @@ extension SwiftyMarkdown {
globalBold = true
case .italic:
globalItalic = true
case .underline:
globalUnderline = true
case .boldItalic:
globalItalic = true
globalBold = true
Expand Down Expand Up @@ -99,6 +102,10 @@ extension SwiftyMarkdown {
fontName = italic.fontName ?? fontName
fontSize = italic.fontSize
globalItalic = true
case .underline:
fontName = underline.fontName ?? fontName
fontSize = underline.fontSize
globalUnderline = true
case .strikethrough:
fontName = strikethrough.fontName ?? fontName
fontSize = strikethrough.fontSize
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftyMarkdown/SwiftyMarkdown+macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension SwiftyMarkdown {

var globalBold = false
var globalItalic = false
var globalUnderline = false

let style : FontProperties
// What type are we and is there a font name set?
Expand Down Expand Up @@ -49,6 +50,8 @@ extension SwiftyMarkdown {
globalBold = true
case .italic:
globalItalic = true
case .underline:
globalUnderline = true
case .boldItalic:
globalItalic = true
globalBold = true
Expand All @@ -72,6 +75,10 @@ extension SwiftyMarkdown {
fontName = bold.fontName ?? fontName
fontSize = bold.fontSize
globalBold = true
case .underline:
fontName = underline.fontName ?? fontName
fontSize = underline.fontSize
globalUnderline = true
case .italic:
fontName = italic.fontName ?? fontName
fontSize = italic.fontSize
Expand Down
25 changes: 23 additions & 2 deletions Sources/SwiftyMarkdown/SwiftyMarkdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum CharacterStyle : CharacterStyling {
case none
case bold
case italic
case underline
case code
case link
case image
Expand Down Expand Up @@ -82,6 +83,7 @@ enum MarkdownLineStyle : LineStyling {
case normal
case bold
case italic
case underline
case boldItalic
}

Expand Down Expand Up @@ -201,8 +203,8 @@ If that is not set, then the system default will be used.
], styles: [1 : CharacterStyle.link], metadataLookup: false, definesBoundary: true),
CharacterRule(primaryTag: CharacterRuleTag(tag: "`", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.code], shouldCancelRemainingRules: true, balancedTags: true),
CharacterRule(primaryTag:CharacterRuleTag(tag: "~", type: .repeating), otherTags : [], styles: [2 : CharacterStyle.strikethrough], minTags:2 , maxTags:2),
CharacterRule(primaryTag: CharacterRuleTag(tag: "*", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.italic, 2 : CharacterStyle.bold], minTags:1 , maxTags:2),
CharacterRule(primaryTag: CharacterRuleTag(tag: "_", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.italic, 2 : CharacterStyle.bold], minTags:1 , maxTags:2)
CharacterRule(primaryTag: CharacterRuleTag(tag: "*", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.italic, 2 : CharacterStyle.bold, 3 : CharacterStyle.underline], minTags:1 , maxTags:3),
CharacterRule(primaryTag: CharacterRuleTag(tag: "_", type: .repeating), otherTags: [], styles: [1 : CharacterStyle.italic, 2 : CharacterStyle.bold, 3 : CharacterStyle.underline], minTags:1 , maxTags:3)
]

let lineProcessor = SwiftyLineProcessor(rules: SwiftyMarkdown.lineRules, defaultRule: MarkdownLineStyle.body, frontMatterRules: SwiftyMarkdown.frontMatterRules)
Expand Down Expand Up @@ -237,13 +239,17 @@ If that is not set, then the system default will be used.

/// The styles to apply to any bold text found in the Markdown
open var bold = BasicStyles()

/// The styles to apply to any underlined text found in the Markdown
open var underline = BasicStyles()

/// The styles to apply to any italic text found in the Markdown
open var italic = BasicStyles()

/// The styles to apply to any code blocks or inline code text found in the Markdown
open var code = BasicStyles()

/// The styles to apply to any text with 'strikethrough' decoration found in the Markdown
open var strikethrough = BasicStyles()

public var bullet : String = ""
Expand Down Expand Up @@ -327,6 +333,7 @@ If that is not set, then the system default will be used.
body.fontSize = size
italic.fontSize = size
bold.fontSize = size
underline.fontSize = size
code.fontSize = size
link.fontSize = size
link.fontSize = size
Expand All @@ -344,6 +351,7 @@ If that is not set, then the system default will be used.
body.color = color
italic.color = color
bold.color = color
underline.color = color
code.color = color
link.color = color
blockquotes.color = color
Expand All @@ -360,6 +368,7 @@ If that is not set, then the system default will be used.
body.color = color
italic.color = color
bold.color = color
underline.color = color
code.color = color
link.color = color
blockquotes.color = color
Expand All @@ -377,6 +386,7 @@ If that is not set, then the system default will be used.
body.fontName = name
italic.fontName = name
bold.fontName = name
underline.fontName = name
code.fontName = name
link.fontName = name
blockquotes.fontName = name
Expand Down Expand Up @@ -555,6 +565,7 @@ extension SwiftyMarkdown {
attributes[.font] = self.font(for: line)
attributes[.link] = nil
attributes[.strikethroughStyle] = nil
attributes[.underlineStyle] = nil
attributes[.foregroundColor] = self.color(for: line)
attributes[.underlineStyle] = nil
guard let styles = token.characterStyles as? [CharacterStyle] else {
Expand All @@ -568,6 +579,10 @@ extension SwiftyMarkdown {
attributes[.font] = self.font(for: line, characterOverride: .bold)
attributes[.foregroundColor] = self.bold.color
}
if styles.contains(.underline) {
attributes[.font] = self.font(for: line, characterOverride: .underline)
attributes[.foregroundColor] = self.underline.color
}

if let linkIdx = styles.firstIndex(of: .link), linkIdx < token.metadataStrings.count {
attributes[.foregroundColor] = self.link.color
Expand All @@ -586,6 +601,12 @@ extension SwiftyMarkdown {
attributes[.foregroundColor] = self.strikethrough.color
}

if styles.contains(.underline) {
attributes[.font] = self.font(for: line, characterOverride: .underline)
attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue as AnyObject
attributes[.foregroundColor] = self.underline.color
}

#if !os(watchOS)
if let imgIdx = styles.firstIndex(of: .image), imgIdx < token.metadataStrings.count {
if !self.applyAttachments {
Expand Down

0 comments on commit f5cdb57

Please sign in to comment.