Skip to content

Commit 7069ede

Browse files
authored
Headings on markdown preset use different sizes (#59)
* 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
1 parent 1de81b1 commit 7069ede

File tree

8 files changed

+11
-1
lines changed

8 files changed

+11
-1
lines changed

Sources/HighlightedTextEditor/HighlightedTextEditor.AppKit.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public struct HighlightedTextEditor: NSViewRepresentable, HighlightingTextEditor
5454

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

5859
let highlightedText = HighlightedTextEditor.getHighlightedText(
5960
text: text,
@@ -63,6 +64,7 @@ public struct HighlightedTextEditor: NSViewRepresentable, HighlightingTextEditor
6364
view.attributedText = highlightedText
6465
runIntrospect(view)
6566
view.selectedRanges = context.coordinator.selectedRanges
67+
view.textView.typingAttributes = typingAttributes
6668
context.coordinator.updatingNSView = false
6769
}
6870

Sources/HighlightedTextEditor/Presets/Markdown.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ let lighterColor = UIColor.lightGray
5353
let textColor = UIColor.label
5454
#endif
5555

56+
private let maxHeadingLevel = 6
57+
5658
public extension Sequence where Iterator.Element == HighlightRule {
5759
static var markdown: [HighlightRule] {
5860
[
5961
HighlightRule(pattern: inlineCodeRegex, formattingRule: TextFormattingRule(key: .font, value: codeFont)),
6062
HighlightRule(pattern: codeBlockRegex, formattingRule: TextFormattingRule(key: .font, value: codeFont)),
6163
HighlightRule(pattern: headingRegex, formattingRules: [
6264
TextFormattingRule(fontTraits: headingTraits),
63-
TextFormattingRule(key: .kern, value: 0.5)
65+
TextFormattingRule(key: .kern, value: 0.5),
66+
TextFormattingRule(key: .font, calculateValue: { content, _ in
67+
let uncappedLevel = content.prefix(while: { char in char == "#" }).count
68+
let level = Swift.min(maxHeadingLevel, uncappedLevel)
69+
let fontSize = CGFloat(maxHeadingLevel - level) * 2.5 + defaultEditorFont.pointSize
70+
return SystemFontAlias(descriptor: defaultEditorFont.fontDescriptor, size: fontSize) as Any
71+
})
6472
]),
6573
HighlightRule(
6674
pattern: linkOrImageRegex,
24.2 KB
Loading
9.37 KB
Loading
7.36 KB
Loading
8.18 KB
Loading
3.26 KB
Loading
3.28 KB
Loading

0 commit comments

Comments
 (0)