Skip to content

Commit

Permalink
Headings on markdown preset use different sizes (#59)
Browse files Browse the repository at this point in the history
* Headings increase in size

* Fixes insertion point graphical glitches

* Updates markdown preset snapshots for large headings

* Updates markdown preset snapshots for large headings in AppKit
  • Loading branch information
kyle-n authored Jan 24, 2022
1 parent 1de81b1 commit 7069ede
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public struct HighlightedTextEditor: NSViewRepresentable, HighlightingTextEditor

public func updateNSView(_ view: ScrollableTextView, context: Context) {
context.coordinator.updatingNSView = true
let typingAttributes = view.textView.typingAttributes

let highlightedText = HighlightedTextEditor.getHighlightedText(
text: text,
Expand All @@ -63,6 +64,7 @@ public struct HighlightedTextEditor: NSViewRepresentable, HighlightingTextEditor
view.attributedText = highlightedText
runIntrospect(view)
view.selectedRanges = context.coordinator.selectedRanges
view.textView.typingAttributes = typingAttributes
context.coordinator.updatingNSView = false
}

Expand Down
10 changes: 9 additions & 1 deletion Sources/HighlightedTextEditor/Presets/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ let lighterColor = UIColor.lightGray
let textColor = UIColor.label
#endif

private let maxHeadingLevel = 6

public extension Sequence where Iterator.Element == HighlightRule {
static var markdown: [HighlightRule] {
[
HighlightRule(pattern: inlineCodeRegex, formattingRule: TextFormattingRule(key: .font, value: codeFont)),
HighlightRule(pattern: codeBlockRegex, formattingRule: TextFormattingRule(key: .font, value: codeFont)),
HighlightRule(pattern: headingRegex, formattingRules: [
TextFormattingRule(fontTraits: headingTraits),
TextFormattingRule(key: .kern, value: 0.5)
TextFormattingRule(key: .kern, value: 0.5),
TextFormattingRule(key: .font, calculateValue: { content, _ in
let uncappedLevel = content.prefix(while: { char in char == "#" }).count
let level = Swift.min(maxHeadingLevel, uncappedLevel)
let fontSize = CGFloat(maxHeadingLevel - level) * 2.5 + defaultEditorFont.pointSize
return SystemFontAlias(descriptor: defaultEditorFont.fontDescriptor, size: fontSize) as Any
})
]),
HighlightRule(
pattern: linkOrImageRegex,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7069ede

Please sign in to comment.