-
Notifications
You must be signed in to change notification settings - Fork 8
/
MarkdownStylesheet.swift
121 lines (100 loc) · 3.74 KB
/
MarkdownStylesheet.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Stylesheet.swift
//
//
// Created by Juul Spee on 08/07/2022.
//
import Foundation
/// A type that defines styles for various markdown elements by setting properties on a given `Attributes` value.
public protocol Stylesheet {
func emphasis(attributes: inout Attributes)
func strong(attributes: inout Attributes)
func inlineCode(attributes: inout Attributes)
func codeBlock(attributes: inout Attributes)
func blockQuote(attributes: inout Attributes)
func link(attributes: inout Attributes, destination: String)
func heading(level: Int, attributes: inout Attributes)
func headingLink(path: [String], attributes: inout Attributes)
func listItem(attributes: inout Attributes, checkbox: Bool?)
func list(attributes: inout Attributes, level: Int)
func orderedListItemPrefix(number: Int) -> String
func orderedListItemPrefix(attributes: inout Attributes)
var unorderedListItemPrefix: String { get }
func unorderedListItemPrefix(attributes: inout Attributes)
var checkboxCheckedPrefix: String { get }
func checkboxCheckedPrefix(attributes: inout Attributes)
var checkboxUncheckedPrefix: String { get }
func checkboxUncheckedPrefix(attributes: inout Attributes)
func footnote(attributes: inout Attributes)
}
extension Stylesheet {
public func emphasis(attributes: inout Attributes) {
attributes.italic = true
}
public func strong(attributes: inout Attributes) {
attributes.bold = true
}
public func link(attributes: inout Attributes, destination: String) {
attributes.textColor = .blue
if let u = URL(string: destination) {
attributes.link = u
}
}
public func blockQuote(attributes: inout Attributes) {
attributes.italic = true
attributes.firstlineHeadIndent += 20
attributes.headIndent += 20
}
public func listItem(attributes: inout Attributes, checkbox: Bool?) {
if checkbox == true {
attributes.textColor = .secondaryLabelColor
}
}
public func list(attributes: inout Attributes, level: Int) {
}
public func orderedListItemPrefix(number: Int) -> String {
"\(number)."
}
public func orderedListItemPrefix(attributes: inout Attributes) { }
public var unorderedListItemPrefix: String {
"•"
}
public func unorderedListItemPrefix(attributes: inout Attributes) { }
public var checkboxCheckedPrefix: String {
"[x]" // TODO: fix NSTextView image clicks for ""
}
public func checkboxCheckedPrefix(attributes: inout Attributes) {
attributes.textColor = .controlAccentColor
attributes.cursor = .arrow
attributes.family = "Monaco" // monospaced
}
public var checkboxUncheckedPrefix: String {
"[ ]" // TODO: fix NSTextView image clicks for ""
}
public func checkboxUncheckedPrefix(attributes: inout Attributes) {
attributes.textColor = .secondaryLabelColor
attributes.cursor = .arrow
attributes.family = "Monaco" // monospaced
}
public func footnote(attributes: inout Attributes) {
attributes.size *= 0.8
}
public func inlineCode(attributes: inout Attributes) {
attributes.family = "Monaco"
}
public func codeBlock(attributes: inout Attributes) {
attributes.family = "Monaco"
attributes.lineHeightMultiple = 1
}
public func heading(level: Int, attributes: inout Attributes) {
attributes.bold = true
switch level {
case 1: attributes.size = 48
case 2: attributes.size = 36
case 3: attributes.size = 28
default: ()
}
}
public func headingLink(path: [String], attributes: inout Attributes) {
}
}