Skip to content

armgit / chops

Public Imported from GitHub
0

Your AI agent skills, finally organized. A macOS app to browse, edit, and manage skills across Claude Code, Cursor, Codex, Windsurf, and Amp.

feat: replace MarkdownUI with WKWebView for preview text selection (#64)

Josh Pigford71ac110d39ef
diff --git a/Chops.xcodeproj/project.pbxproj b/Chops.xcodeproj/project.pbxproj
index 5cde62e..c08c940 100644
--- a/Chops.xcodeproj/project.pbxproj
+++ b/Chops.xcodeproj/project.pbxproj
@@ -18,17 +18,16 @@
60A6711BAB0C3B8A3ACBF985 /* SkillParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5155D141EF812A3FE9ADF59C /* SkillParser.swift */; };
6A8E0927D95BB14C9F377F4E /* SkillScanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A5B26D335C1AFB6CDE8207 /* SkillScanner.swift */; };
71E92A838AB4D1D6DC6174AF /* NewSkillSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = F02A00F80E4712872205EA7B /* NewSkillSheet.swift */; };
- 791AA125E9170DBA81BCBE62 /* MarkdownUI in Frameworks */ = {isa = PBXBuildFile; productRef = B79B2B9912FC7D2ADEA22F84 /* MarkdownUI */; };
+ 791AA125E9170DBA81BCBE62 /* Highlightr in Frameworks */ = {isa = PBXBuildFile; productRef = 704A14702546BFBD29BE27A8 /* Highlightr */; };
7A17C99C5EFAE3AA9E788D50 /* Collection.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9AFE1AF02B0F8057791C2A9 /* Collection.swift */; };
7EC6156934D559226BC78859 /* CollectionListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA102A49ACFB23BC15794F8 /* CollectionListView.swift */; };
7FD1DE6640F9339D5E97E09A /* MDCParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4F680B33C74A38FCF5ADD5 /* MDCParser.swift */; };
- 801A39D725B59A2F2BAF5DF2 /* Highlightr in Frameworks */ = {isa = PBXBuildFile; productRef = 704A14702546BFBD29BE27A8 /* Highlightr */; };
+ 801A39D725B59A2F2BAF5DF2 /* cmark in Frameworks */ = {isa = PBXBuildFile; productRef = 9F4BE66CFE1E8401894FEF1F /* cmark */; };
846B58F3B105D1734DC6FA75 /* SkillRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FC9D3E83D8640CC258CF361 /* SkillRegistry.swift */; };
87A469B631CC80C89B14B1C7 /* SkillMetadataBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C812515FD8DC222B89745F2 /* SkillMetadataBar.swift */; };
95163AEB7980654C901EC693 /* Skill.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27D6ED655F951177D2152351 /* Skill.swift */; };
98434D3DDFEEC7239F6F7B79 /* RegistrySheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FCD6C2A8BE68F553A9CA392 /* RegistrySheet.swift */; };
99EE8F6124FE91AC9576D6FD /* FileWatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD26AA4787A2E0CDC86AADC /* FileWatcher.swift */; };
- 9ED88D6237B36693ABE5CE85 /* HighlightrSyntaxHighlighter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57F2C41F08F6E1BEFC5ADD4 /* HighlightrSyntaxHighlighter.swift */; };
A99BA179E0520DCDC98E1382 /* AppLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = F45D5C8E8C732B2F4A867E43 /* AppLogger.swift */; };
AAC4A3CBF2A9B4DF50608948 /* SSHService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6797D4AC1970DB9ACB5ACA73 /* SSHService.swift */; };
AFDC3DAB708B352A7606F119 /* DiagnosticExporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D3C2567FBF8DBA4DE957B3 /* DiagnosticExporter.swift */; };
@@ -41,6 +40,7 @@
CECE9BEEC1D933C543B66A4E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 17F7FE50C96124BA4EE43A6F /* Assets.xcassets */; };
D4C107BE303FC07CAF9C66AF /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60E0F71982C4AF0045962380 /* ContentView.swift */; };
E6F4E1CF50F2F78F5BA9BAC9 /* SkillPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77E332B9970E6E3D00DCFBA3 /* SkillPreviewView.swift */; };
+ E7ED248D6F94C1B1DEF1E8BA /* MarkdownRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 372FA72F2C179B79470DF86A /* MarkdownRenderer.swift */; };
E98F7490B18A194F82ED2C0E /* SkillListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA64A368BBF950CD36C4F495 /* SkillListView.swift */; };
F98B1E8579B139FD2A801650 /* SearchService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FFC47BDE92105F4736B379F /* SearchService.swift */; };
FB295303DD30AED03290C726 /* SkillEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31B4DB87833D5160A11E39C9 /* SkillEditorView.swift */; };
@@ -57,6 +57,7 @@
2CF7822031FD82A28978D9C5 /* SidebarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarView.swift; sourceTree = "<group>"; };
31B4DB87833D5160A11E39C9 /* SkillEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkillEditorView.swift; sourceTree = "<group>"; };
3314080A44DEB895223E36D5 /* RemoteServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteServer.swift; sourceTree = "<group>"; };
+ 372FA72F2C179B79470DF86A /* MarkdownRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkdownRenderer.swift; sourceTree = "<group>"; };
3A4F680B33C74A38FCF5ADD5 /* MDCParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MDCParser.swift; sourceTree = "<group>"; };
4CA102A49ACFB23BC15794F8 /* CollectionListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListView.swift; sourceTree = "<group>"; };
5155D141EF812A3FE9ADF59C /* SkillParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkillParser.swift; sourceTree = "<group>"; };
@@ -78,7 +79,6 @@
B3F5CF5BB1FC8455FDDB004A /* Chops.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Chops.app; sourceTree = BUILT_PRODUCTS_DIR; };
C9AFE1AF02B0F8057791C2A9 /* Collection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Collection.swift; sourceTree = "<group>"; };
CAD26AA4787A2E0CDC86AADC /* FileWatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileWatcher.swift; sourceTree = "<group>"; };
- D57F2C41F08F6E1BEFC5ADD4 /* HighlightrSyntaxHighlighter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HighlightrSyntaxHighlighter.swift; sourceTree = "<group>"; };
D5D3C2567FBF8DBA4DE957B3 /* DiagnosticExporter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiagnosticExporter.swift; sourceTree = "<group>"; };
E684304D1723CA691F40335B /* RemoteServersSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteServersSettingsView.swift; sourceTree = "<group>"; };
E6A5B26D335C1AFB6CDE8207 /* SkillScanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkillScanner.swift; sourceTree = "<group>"; };
@@ -92,8 +92,8 @@
buildActionMask = 2147483647;
files = (
331F55021A7E6C9F0B666514 /* Sparkle in Frameworks */,
- 791AA125E9170DBA81BCBE62 /* MarkdownUI in Frameworks */,
- 801A39D725B59A2F2BAF5DF2 /* Highlightr in Frameworks */,
+ 791AA125E9170DBA81BCBE62 /* Highlightr in Frameworks */,
+ 801A39D725B59A2F2BAF5DF2 /* cmark in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -104,7 +104,7 @@
isa = PBXGroup;
children = (
7C77C5B30E6C885B39444E5B /* FrontmatterParser.swift */,
- D57F2C41F08F6E1BEFC5ADD4 /* HighlightrSyntaxHighlighter.swift */,
+ 372FA72F2C179B79470DF86A /* MarkdownRenderer.swift */,
3A4F680B33C74A38FCF5ADD5 /* MDCParser.swift */,
);
path = Utilities;
@@ -256,8 +256,8 @@
name = Chops;
packageProductDependencies = (
4338FC540625A275BBC9AAC9 /* Sparkle */,
- B79B2B9912FC7D2ADEA22F84 /* MarkdownUI */,
704A14702546BFBD29BE27A8 /* Highlightr */,
+ 9F4BE66CFE1E8401894FEF1F /* cmark */,
);
productName = Chops;
productReference = B3F5CF5BB1FC8455FDDB004A /* Chops.app */;
@@ -285,8 +285,8 @@
minimizedProjectReferenceProxies = 1;
packageReferences = (
7FA62842E4E9A8FD27AE24AF /* XCRemoteSwiftPackageReference "Highlightr" */,
- 7F6181572A727350272D9BEC /* XCRemoteSwiftPackageReference "swift-markdown-ui" */,
FD1D69AB767CC40E2CE9FED9 /* XCRemoteSwiftPackageReference "Sparkle" */,
+ 05A6F59368DE6489DCD607F1 /* XCRemoteSwiftPackageReference "cmark-gfm" */,
);
preferredProjectObjectVersion = 77;
productRefGroup = D3558033A0A7B4EE7E7234CE /* Products */;
@@ -325,8 +325,8 @@
AFDC3DAB708B352A7606F119 /* DiagnosticExporter.swift in Sources */,
99EE8F6124FE91AC9576D6FD /* FileWatcher.swift in Sources */,
CBD8FECF1C5A453C8B021665 /* FrontmatterParser.swift in Sources */,
- 9ED88D6237B36693ABE5CE85 /* HighlightrSyntaxHighlighter.swift in Sources */,
7FD1DE6640F9339D5E97E09A /* MDCParser.swift in Sources */,
+ E7ED248D6F94C1B1DEF1E8BA /* MarkdownRenderer.swift in Sources */,
71E92A838AB4D1D6DC6174AF /* NewSkillSheet.swift in Sources */,
98434D3DDFEEC7239F6F7B79 /* RegistrySheet.swift in Sources */,
5B81DED237A60776ADB04134 /* RemoteServer.swift in Sources */,
@@ -622,12 +622,12 @@
/* End XCConfigurationList section */
/* Begin XCRemoteSwiftPackageReference section */
- 7F6181572A727350272D9BEC /* XCRemoteSwiftPackageReference "swift-markdown-ui" */ = {
+ 05A6F59368DE6489DCD607F1 /* XCRemoteSwiftPackageReference "cmark-gfm" */ = {
isa = XCRemoteSwiftPackageReference;
- repositoryURL = "https://github.com/gonzalezreal/swift-markdown-ui";
+ repositoryURL = "https://github.com/brokenhandsio/cmark-gfm.git";
requirement = {
kind = upToNextMajorVersion;
- minimumVersion = 2.4.0;
+ minimumVersion = 2.1.0;
};
};
7FA62842E4E9A8FD27AE24AF /* XCRemoteSwiftPackageReference "Highlightr" */ = {
@@ -659,10 +659,10 @@
package = 7FA62842E4E9A8FD27AE24AF /* XCRemoteSwiftPackageReference "Highlightr" */;
productName = Highlightr;
};
- B79B2B9912FC7D2ADEA22F84 /* MarkdownUI */ = {
+ 9F4BE66CFE1E8401894FEF1F /* cmark */ = {
isa = XCSwiftPackageProductDependency;
- package = 7F6181572A727350272D9BEC /* XCRemoteSwiftPackageReference "swift-markdown-ui" */;
- productName = MarkdownUI;
+ package = 05A6F59368DE6489DCD607F1 /* XCRemoteSwiftPackageReference "cmark-gfm" */;
+ productName = cmark;
};
/* End XCSwiftPackageProductDependency section */
};
diff --git a/Chops.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Chops.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
index eb89504..5e4a5a4 100644
--- a/Chops.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
+++ b/Chops.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
@@ -1,22 +1,22 @@
{
- "originHash" : "d0e9b55e5a1f995aa86a208c3c1677dc39f38b65031258ce388e8453d06a06c8",
+ "originHash" : "cced8d2c7e8017967b1fbc19bce1489634e299f796e5c0826981d26ef3f07509",
"pins" : [
{
- "identity" : "highlightr",
+ "identity" : "cmark-gfm",
"kind" : "remoteSourceControl",
- "location" : "https://github.com/raspu/Highlightr",
+ "location" : "https://github.com/brokenhandsio/cmark-gfm.git",
"state" : {
- "revision" : "05e7fcc63b33925cd0c1faaa205cdd5681e7bbef",
- "version" : "2.3.0"
+ "revision" : "e97450a77a40f12b4f88f95891621c3b5d8669de",
+ "version" : "2.1.0"
}
},
{
- "identity" : "networkimage",
+ "identity" : "highlightr",
"kind" : "remoteSourceControl",
- "location" : "https://github.com/gonzalezreal/NetworkImage",
+ "location" : "https://github.com/raspu/Highlightr",
"state" : {
- "revision" : "2849f5323265386e200484b0d0f896e73c3411b9",
- "version" : "6.0.1"
+ "revision" : "05e7fcc63b33925cd0c1faaa205cdd5681e7bbef",
+ "version" : "2.3.0"
}
},
{
@@ -27,24 +27,6 @@
"revision" : "21d8df80440b1ca3b65fa82e40782f1e5a9e6ba2",
"version" : "2.9.0"
}
- },
- {
- "identity" : "swift-cmark",
- "kind" : "remoteSourceControl",
- "location" : "https://github.com/swiftlang/swift-cmark",
- "state" : {
- "revision" : "5d9bdaa4228b381639fff09403e39a04926e2dbe",
- "version" : "0.7.1"
- }
- },
- {
- "identity" : "swift-markdown-ui",
- "kind" : "remoteSourceControl",
- "location" : "https://github.com/gonzalezreal/swift-markdown-ui",
- "state" : {
- "revision" : "5f613358148239d0292c0cef674a3c2314737f9e",
- "version" : "2.4.1"
- }
}
],
"version" : 3
diff --git a/Chops/Utilities/HighlightrSyntaxHighlighter.swift b/Chops/Utilities/HighlightrSyntaxHighlighter.swift
deleted file mode 100644
index 35e1edd..0000000
--- a/Chops/Utilities/HighlightrSyntaxHighlighter.swift
+++ /dev/null
@@ -1,23 +0,0 @@
-import MarkdownUI
-import Highlightr
-import SwiftUI
-
-struct HighlightrSyntaxHighlighter: CodeSyntaxHighlighter {
- private static let shared: Highlightr = Highlightr()!
-
- private let highlightr: Highlightr
-
- init() {
- self.highlightr = Self.shared
- let isDark = NSApp.effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
- highlightr.setTheme(to: isDark ? "atom-one-dark" : "atom-one-light")
- }
-
- func highlightCode(_ code: String, language: String?) -> Text {
- let lang = language ?? "plaintext"
- if let highlighted = highlightr.highlight(code, as: lang) {
- return Text(AttributedString(highlighted))
- }
- return Text(code)
- }
-}
diff --git a/Chops/Utilities/MarkdownRenderer.swift b/Chops/Utilities/MarkdownRenderer.swift
new file mode 100644
index 0000000..6f7466d
--- /dev/null
+++ b/Chops/Utilities/MarkdownRenderer.swift
@@ -0,0 +1,194 @@
+import Foundation
+import Highlightr
+import JavaScriptCore
+import cmark
+
+enum MarkdownRenderer {
+ static func renderHTML(_ markdown: String, isDarkMode: Bool) -> String {
+ guard !markdown.isEmpty else { return "" }
+
+ let len = markdown.utf8.count
+ let options = Int32(CMARK_OPT_STRIKETHROUGH_DOUBLE_TILDE)
+
+ guard let buf = cmark_gfm_markdown_to_html(markdown, len, options) else { return "" }
+ let html = String(cString: buf)
+ free(buf)
+
+ return PreviewCodeHighlighter.shared.highlightCodeBlocks(in: html, isDarkMode: isDarkMode)
+ }
+
+ static func themeCSS(isDarkMode: Bool) -> String {
+ PreviewCodeHighlighter.shared.themeCSS(isDarkMode: isDarkMode)
+ }
+}
+
+private final class PreviewCodeHighlighter {
+ static let shared = PreviewCodeHighlighter()
+
+ private static let codeBlockRegex = try! NSRegularExpression(
+ pattern: #"<pre([^>]*)><code(?: class="([^"]*)")?>([\s\S]*?)</code></pre>"#,
+ options: []
+ )
+
+ private let bundle: Bundle?
+ private let hljs: JSValue?
+ private var cssCache: [String: String] = [:]
+
+ private init() {
+ self.bundle = Self.resourceBundle()
+
+ guard let jsContext = JSContext(),
+ let bundle,
+ let highlightPath = bundle.path(forResource: "highlight.min", ofType: "js"),
+ let highlightJS = try? String(contentsOfFile: highlightPath, encoding: .utf8) else {
+ self.hljs = nil
+ return
+ }
+
+ jsContext.evaluateScript(highlightJS)
+ self.hljs = jsContext.objectForKeyedSubscript("hljs")
+ }
+
+ func highlightCodeBlocks(in html: String, isDarkMode: Bool) -> String {
+ let nsHTML = html as NSString
+ let matches = Self.codeBlockRegex.matches(in: html, range: NSRange(location: 0, length: nsHTML.length))
+
+ guard !matches.isEmpty else { return html }
+
+ var rendered = ""
+ var currentLocation = 0
+
+ for match in matches {
+ let blockRange = match.range(at: 0)
+ rendered += nsHTML.substring(with: NSRange(location: currentLocation, length: blockRange.location - currentLocation))
+
+ let preAttributes = substring(in: nsHTML, range: match.range(at: 1))
+ let classNames = substring(in: nsHTML, range: match.range(at: 2))
+ let encodedCode = substring(in: nsHTML, range: match.range(at: 3)) ?? ""
+
+ let language = languageName(classNames: classNames, preAttributes: preAttributes)
+ let code = decodeHTML(encodedCode)
+
+ if let highlighted = highlightedHTML(for: code, language: language, isDarkMode: isDarkMode) {
+ rendered += highlighted
+ } else {
+ rendered += nsHTML.substring(with: blockRange)
+ }
+
+ currentLocation = blockRange.location + blockRange.length
+ }
+
+ rendered += nsHTML.substring(from: currentLocation)
+ return rendered
+ }
+
+ func themeCSS(isDarkMode: Bool) -> String {
+ let themeName = isDarkMode ? "atom-one-dark" : "atom-one-light"
+
+ if let cached = cssCache[themeName] {
+ return cached
+ }
+
+ guard let bundle,
+ let themePath = bundle.path(forResource: themeName + ".min", ofType: "css"),
+ let css = try? String(contentsOfFile: themePath, encoding: .utf8) else {
+ return ""
+ }
+
+ cssCache[themeName] = css
+ return css
+ }
+
+ private func highlightedHTML(for code: String, language: String?, isDarkMode: Bool) -> String? {
+ guard let hljs else { return nil }
+
+ let result: JSValue?
+ if let language, !language.isEmpty {
+ let highlighted = hljs.invokeMethod("highlight", withArguments: [language, code, false])
+ if highlighted?.isUndefined == false {
+ result = highlighted
+ } else {
+ result = hljs.invokeMethod("highlightAuto", withArguments: [code])
+ }
+ } else {
+ result = hljs.invokeMethod("highlightAuto", withArguments: [code])
+ }
+
+ guard let html = result?.objectForKeyedSubscript("value")?.toString() else {
+ return nil
+ }
+
+ let languageClass = language.map { " language-\($0)" } ?? ""
+ let themeClass = isDarkMode ? "dark" : "light"
+
+ return """
+ <pre class="highlighted-code \(themeClass)"><code class="hljs\(languageClass)">\(html)</code></pre>
+ """
+ }
+
+ private func languageName(classNames: String?, preAttributes: String?) -> String? {
+ if let classNames {
+ for className in classNames.split(separator: " ") {
+ if className.hasPrefix("language-") {
+ return String(className.dropFirst("language-".count))
+ }
+ if className.hasPrefix("lang-") {
+ return String(className.dropFirst("lang-".count))
+ }
+ }
+ }
+
+ guard let preAttributes else { return nil }
+
+ let pattern = #"lang="([^"]+)""#
+ guard let regex = try? NSRegularExpression(pattern: pattern),
+ let match = regex.firstMatch(in: preAttributes, range: NSRange(location: 0, length: (preAttributes as NSString).length)) else {
+ return nil
+ }
+
+ return substring(in: preAttributes as NSString, range: match.range(at: 1))
+ }
+
+ private func substring(in string: NSString, range: NSRange) -> String? {
+ guard range.location != NSNotFound else { return nil }
+ return string.substring(with: range)
+ }
+
+ private func decodeHTML(_ string: String) -> String {
+ string
+ .replacingOccurrences(of: "&amp;", with: "&")
+ .replacingOccurrences(of: "&lt;", with: "<")
+ .replacingOccurrences(of: "&gt;", with: ">")
+ .replacingOccurrences(of: "&quot;", with: "\"")
+ .replacingOccurrences(of: "&#39;", with: "'")
+ }
+
+ private static func resourceBundle() -> Bundle? {
+ let bundleName = "Highlightr_Highlightr"
+ let overrides: [URL]
+
+ if let override = ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_PATH"]
+ ?? ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_URL"] {
+ overrides = [URL(fileURLWithPath: override)]
+ } else {
+ overrides = []
+ }
+
+ let candidates = overrides + [
+ Bundle.main.resourceURL,
+ Bundle(for: ResourceBundleFinder.self).resourceURL,
+ Bundle.main.bundleURL
+ ]
+
+ for candidate in candidates {
+ let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
+ if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
+ return bundle
+ }
+ }
+
+ return nil
+ }
+}
+
+private final class ResourceBundleFinder {}
diff --git a/Chops/Views/Detail/SkillPreviewView.swift b/Chops/Views/Detail/SkillPreviewView.swift
index df56f69..e7577f6 100644
--- a/Chops/Views/Detail/SkillPreviewView.swift
+++ b/Chops/Views/Detail/SkillPreviewView.swift
@@ -1,54 +1,324 @@
import SwiftUI
-import MarkdownUI
+import WebKit
struct SkillPreviewView: View {
let content: String
var body: some View {
+ MarkdownWebView(content: content)
+ .frame(maxWidth: .infinity, maxHeight: .infinity)
+ }
+}
+
+// MARK: - WKWebView Wrapper
+
+private struct MarkdownWebView: NSViewRepresentable {
+ let content: String
+ @Environment(\.colorScheme) private var colorScheme
+
+ private var contentHash: Int {
+ var hasher = Hasher()
+ hasher.combine(content)
+ hasher.combine(colorScheme)
+ return hasher.finalize()
+ }
+
+ func makeCoordinator() -> Coordinator {
+ Coordinator()
+ }
+
+ func makeNSView(context: Context) -> WKWebView {
+ let config = WKWebViewConfiguration()
+ config.preferences.isElementFullscreenEnabled = false
+ config.defaultWebpagePreferences.allowsContentJavaScript = false
+ let webView = WKWebView(frame: .zero, configuration: config)
+ webView.navigationDelegate = context.coordinator
+ webView.underPageBackgroundColor = Self.dynamicBgColor
+ loadHTML(in: webView, context: context)
+ return webView
+ }
+
+ private static let dynamicBgColor = NSColor(name: nil) { appearance in
+ appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
+ ? NSColor(red: 0x1A/255.0, green: 0x1A/255.0, blue: 0x1A/255.0, alpha: 1)
+ : NSColor(red: 0xFA/255.0, green: 0xFA/255.0, blue: 0xFA/255.0, alpha: 1)
+ }
+
+ func updateNSView(_ webView: WKWebView, context: Context) {
+ webView.underPageBackgroundColor = Self.dynamicBgColor
+ if context.coordinator.lastContentHash != contentHash {
+ loadHTML(in: webView, context: context)
+ }
+ }
+
+ private func loadHTML(in webView: WKWebView, context: Context) {
+ context.coordinator.lastContentHash = contentHash
let parsed = RawFrontmatterParser.parse(content)
+ let isDarkMode = colorScheme == .dark
+ let markdownHTML = MarkdownRenderer.renderHTML(parsed?.content ?? content, isDarkMode: isDarkMode)
+ let themeCSS = MarkdownRenderer.themeCSS(isDarkMode: isDarkMode)
+
+ var bodyHTML = ""
+ if let fm = parsed?.frontmatter {
+ let escaped = fm
+ .replacingOccurrences(of: "&", with: "&amp;")
+ .replacingOccurrences(of: "<", with: "&lt;")
+ .replacingOccurrences(of: ">", with: "&gt;")
+ bodyHTML += "<pre class=\"frontmatter\">\(escaped)</pre>"
+ }
+ bodyHTML += markdownHTML
+
+ let html = """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; img-src data:;">
+ <style>\(themeCSS)
+ \(Self.css)</style>
+ </head>
+ <body>\(bodyHTML)</body>
+ </html>
+ """
+ webView.loadHTMLString(html, baseURL: nil)
+ }
+
+ final class Coordinator: NSObject, WKNavigationDelegate {
+ var lastContentHash: Int?
- ScrollView {
- VStack(alignment: .leading, spacing: 0) {
- if let frontmatter = parsed?.frontmatter {
- FrontmatterBlockView(frontmatter: frontmatter)
- .padding(.bottom, 24)
- }
-
- Markdown(parsed?.content ?? content)
- .markdownTheme(.clearly)
- .markdownCodeSyntaxHighlighter(HighlightrSyntaxHighlighter())
- .textSelection(.enabled)
+ func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
+ if navigationAction.navigationType == .linkActivated,
+ let url = navigationAction.request.url {
+ NSWorkspace.shared.open(url)
+ decisionHandler(.cancel)
+ } else {
+ decisionHandler(.allow)
}
- .padding(24)
- .frame(maxWidth: 672, alignment: .leading)
- .frame(maxWidth: .infinity)
}
- .background(Color.clearlyBackground)
- .frame(maxWidth: .infinity, maxHeight: .infinity)
}
-}
-// MARK: - Frontmatter Block
+ // MARK: - CSS
-private struct FrontmatterBlockView: View {
- let frontmatter: String
+ private static let css = """
+ * {
+ margin: 0;
+ padding: 0;
+ box-sizing: border-box;
+ }
- var body: some View {
- Text(frontmatter)
- .font(.system(size: 12, design: .monospaced))
- .foregroundStyle(Color.clearlyFmValue)
- .textSelection(.enabled)
- .frame(maxWidth: .infinity, alignment: .leading)
- .padding(.horizontal, 12)
- .padding(.vertical, 10)
- .frame(maxWidth: .infinity, alignment: .leading)
- .background(Color.clearlyFmBg)
- .overlay(
- RoundedRectangle(cornerRadius: 6)
- .stroke(Color.clearlyFmBorder, lineWidth: 1)
- )
- .clipShape(RoundedRectangle(cornerRadius: 6))
+ body {
+ font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
+ font-size: 16px;
+ line-height: 1.6;
+ max-width: 672px;
+ margin: 0 auto;
+ padding: 24px 24px 40px;
+ color: #222222;
+ background-color: #FAFAFA;
+ -webkit-font-smoothing: antialiased;
+ -webkit-user-select: text;
}
+
+ @media (prefers-color-scheme: dark) {
+ body {
+ color: #E0E0E0;
+ background-color: #1A1A1A;
+ }
+ a { color: #6699CC; }
+ code {
+ background-color: #2A2A2A !important;
+ color: #E07070 !important;
+ }
+ pre {
+ background-color: #2A2A2A !important;
+ border-color: #333333 !important;
+ color: #E0E0E0 !important;
+ }
+ pre code {
+ background: none !important;
+ color: #E0E0E0 !important;
+ }
+ blockquote {
+ border-left-color: #444444;
+ color: #999999;
+ }
+ table th {
+ background-color: #2A2A2A;
+ border-color: #444444;
+ }
+ table td {
+ border-color: #333333;
+ }
+ table tr:nth-child(even) {
+ background-color: #222222;
+ }
+ hr {
+ border-color: #333333;
+ }
+ pre.frontmatter {
+ color: #999999;
+ background-color: #222222;
+ border-color: #333333;
+ }
+ pre.highlighted-code code.hljs {
+ border-color: #333333;
+ }
+ }
+
+ h1, h2, h3, h4, h5, h6 {
+ font-weight: 700;
+ line-height: 1.3;
+ margin-top: 1.5em;
+ margin-bottom: 0.5em;
+ }
+
+ body > *:first-child {
+ margin-top: 0;
+ }
+
+ h1 { font-size: 2em; }
+ h2 { font-size: 1.5em; }
+ h3 { font-size: 1.25em; }
+ h4 { font-size: 1.1em; }
+
+ p {
+ margin-bottom: 1em;
+ }
+
+ a {
+ color: #3366AA;
+ text-decoration: none;
+ }
+ a:hover {
+ text-decoration: underline;
+ }
+
+ code {
+ font-family: "SF Mono", SFMono-Regular, Menlo, monospace;
+ font-size: 0.85em;
+ background-color: #F0F0F0;
+ color: #CC3333;
+ padding: 0.15em 0.35em;
+ border-radius: 3px;
+ }
+
+ pre {
+ background-color: #F5F5F5;
+ border: 1px solid #E0E0E0;
+ border-radius: 4px;
+ padding: 1em;
+ margin-bottom: 1em;
+ overflow-x: auto;
+ }
+
+ pre code {
+ background: none;
+ color: inherit;
+ padding: 0;
+ font-size: 0.85em;
+ }
+
+ pre.highlighted-code {
+ background: none;
+ border: none;
+ padding: 0;
+ }
+
+ pre.highlighted-code code.hljs {
+ border: 1px solid #E0E0E0;
+ border-radius: 4px;
+ font-size: 0.85em;
+ }
+
+ blockquote {
+ border-left: 3px solid #CCCCCC;
+ padding-left: 1em;
+ margin-left: 0;
+ margin-bottom: 1em;
+ color: #666666;
+ font-style: italic;
+ }
+
+ ul, ol {
+ margin-bottom: 1em;
+ padding-left: 1.5em;
+ }
+
+ li {
+ margin-bottom: 0.25em;
+ }
+
+ ul.contains-task-list {
+ list-style: none;
+ padding-left: 0;
+ }
+
+ li.task-list-item {
+ display: flex;
+ align-items: baseline;
+ gap: 0.5em;
+ }
+
+ li.task-list-item input[type="checkbox"] {
+ margin: 0;
+ }
+
+ table {
+ border-collapse: collapse;
+ width: 100%;
+ margin-bottom: 1em;
+ }
+
+ th, td {
+ text-align: left;
+ padding: 0.5em 0.75em;
+ }
+
+ th {
+ font-weight: 600;
+ background-color: #F5F5F5;
+ border-bottom: 2px solid #DDDDDD;
+ }
+
+ td {
+ border-bottom: 1px solid #EEEEEE;
+ }
+
+ tr:nth-child(even) {
+ background-color: #FAFAFA;
+ }
+
+ del {
+ text-decoration: line-through;
+ opacity: 0.6;
+ }
+
+ hr {
+ border: none;
+ border-top: 1px solid #DDDDDD;
+ margin: 2em 0;
+ }
+
+ img {
+ max-width: 100%;
+ height: auto;
+ }
+
+ pre.frontmatter {
+ font-family: "SF Mono", SFMono-Regular, Menlo, monospace;
+ font-size: 12px;
+ line-height: 1.5;
+ color: #333333;
+ background-color: #F0F0F0;
+ border: 1px solid transparent;
+ border-radius: 6px;
+ padding: 10px 12px;
+ margin-bottom: 24px;
+ white-space: pre-wrap;
+ word-wrap: break-word;
+ }
+ """
}
private enum RawFrontmatterParser {
@@ -82,200 +352,3 @@ private enum RawFrontmatterParser {
return nil
}
}
-
-// MARK: - Clearly Theme
-
-extension Theme {
- static let clearly = Theme()
- .text {
- ForegroundColor(.clearlyText)
- BackgroundColor(.clearlyBackground)
- FontSize(16)
- }
- .code {
- FontFamilyVariant(.monospaced)
- FontSize(.em(0.85))
- ForegroundColor(.clearlyCode)
- BackgroundColor(.clearlyInlineCodeBg)
- }
- .strong {
- FontWeight(.bold)
- }
- .link {
- ForegroundColor(.clearlyLink)
- }
- .heading1 { configuration in
- configuration.label
- .relativeLineSpacing(.em(0.15))
- .markdownMargin(top: .em(1.5), bottom: .em(0.5))
- .markdownTextStyle {
- FontWeight(.bold)
- FontSize(.em(2))
- }
- }
- .heading2 { configuration in
- configuration.label
- .relativeLineSpacing(.em(0.15))
- .markdownMargin(top: .em(1.5), bottom: .em(0.5))
- .markdownTextStyle {
- FontWeight(.bold)
- FontSize(.em(1.5))
- }
- }
- .heading3 { configuration in
- configuration.label
- .relativeLineSpacing(.em(0.15))
- .markdownMargin(top: .em(1.5), bottom: .em(0.5))
- .markdownTextStyle {
- FontWeight(.bold)
- FontSize(.em(1.25))
- }
- }
- .heading4 { configuration in
- configuration.label
- .relativeLineSpacing(.em(0.15))
- .markdownMargin(top: .em(1.5), bottom: .em(0.5))
- .markdownTextStyle {
- FontWeight(.bold)
- FontSize(.em(1.1))
- }
- }
- .heading5 { configuration in
- configuration.label
- .relativeLineSpacing(.em(0.15))
- .markdownMargin(top: .em(1.5), bottom: .em(0.5))
- .markdownTextStyle {
- FontWeight(.bold)
- }
- }
- .heading6 { configuration in
- configuration.label
- .relativeLineSpacing(.em(0.15))
- .markdownMargin(top: .em(1.5), bottom: .em(0.5))
- .markdownTextStyle {
- FontWeight(.bold)
- FontSize(.em(0.85))
- ForegroundColor(.clearlyBlockquoteText)
- }
- }
- .paragraph { configuration in
- configuration.label
- .fixedSize(horizontal: false, vertical: true)
- .relativeLineSpacing(.em(0.3))
- .markdownMargin(top: 0, bottom: 16)
- }
- .blockquote { configuration in
- HStack(spacing: 0) {
- RoundedRectangle(cornerRadius: 1.5)
- .fill(Color.clearlyBlockquoteBorder)
- .frame(width: 3)
- configuration.label
- .markdownTextStyle {
- ForegroundColor(.clearlyBlockquoteText)
- FontStyle(.italic)
- }
- .relativePadding(.horizontal, length: .em(1))
- }
- .fixedSize(horizontal: false, vertical: true)
- }
- .codeBlock { configuration in
- ScrollView(.horizontal) {
- configuration.label
- .fixedSize(horizontal: false, vertical: true)
- .relativeLineSpacing(.em(0.225))
- .markdownTextStyle {
- FontFamilyVariant(.monospaced)
- FontSize(.em(0.85))
- }
- .padding(16)
- }
- .background(Color.clearlyCodeBlockBg)
- .overlay(
- RoundedRectangle(cornerRadius: 6)
- .stroke(Color.clearlyCodeBlockBorder, lineWidth: 1)
- )
- .clipShape(RoundedRectangle(cornerRadius: 6))
- .markdownMargin(top: 0, bottom: 16)
- }
- .listItem { configuration in
- configuration.label
- .markdownMargin(top: .em(0.25))
- }
- .table { configuration in
- configuration.label
- .fixedSize(horizontal: false, vertical: true)
- .markdownTableBorderStyle(.init(color: .clearlyTableBorder))
- .markdownTableBackgroundStyle(
- .alternatingRows(Color.clearlyBackground, Color.clearlyTableAltRow)
- )
- .markdownMargin(top: 0, bottom: 16)
- }
- .tableCell { configuration in
- configuration.label
- .markdownTextStyle {
- if configuration.row == 0 {
- FontWeight(.semibold)
- }
- BackgroundColor(nil)
- }
- .fixedSize(horizontal: false, vertical: true)
- .padding(.vertical, 6)
- .padding(.horizontal, 13)
- .relativeLineSpacing(.em(0.25))
- }
- .thematicBreak {
- Divider()
- .overlay(Color.clearlyHr)
- .markdownMargin(top: .em(2), bottom: .em(2))
- }
-}
-
-// MARK: - Color Palette
-
-private extension Color {
- static let clearlyText = Color(
- light: Color(rgba: 0x2222_22ff), dark: Color(rgba: 0xE0E0_E0ff)
- )
- static let clearlyBackground = Color(
- light: Color(rgba: 0xFAFA_FAff), dark: Color(rgba: 0x1A1A_1Aff)
- )
- static let clearlyCode = Color(
- light: Color(rgba: 0xCC33_33ff), dark: Color(rgba: 0xE070_70ff)
- )
- static let clearlyCodeBlockBg = Color(
- light: Color(rgba: 0xF5F5_F5ff), dark: Color(rgba: 0x2A2A_2Aff)
- )
- static let clearlyCodeBlockBorder = Color(
- light: Color(rgba: 0xE0E0_E0ff), dark: Color(rgba: 0x3333_33ff)
- )
- static let clearlyInlineCodeBg = Color(
- light: Color(rgba: 0xF0F0_F0ff), dark: Color(rgba: 0x2A2A_2Aff)
- )
- static let clearlyLink = Color(
- light: Color(rgba: 0x3366_AAff), dark: Color(rgba: 0x6699_CCff)
- )
- static let clearlyBlockquoteText = Color(
- light: Color(rgba: 0x6666_66ff), dark: Color(rgba: 0x9999_99ff)
- )
- static let clearlyBlockquoteBorder = Color(
- light: Color(rgba: 0xCCCC_CCff), dark: Color(rgba: 0x4444_44ff)
- )
- static let clearlyHr = Color(
- light: Color(rgba: 0xDDDD_DDff), dark: Color(rgba: 0x3333_33ff)
- )
- static let clearlyTableBorder = Color(
- light: Color(rgba: 0xE0E0_E0ff), dark: Color(rgba: 0x3333_33ff)
- )
- static let clearlyTableAltRow = Color(
- light: Color(rgba: 0xF5F5_F5ff), dark: Color(rgba: 0x2222_22ff)
- )
- static let clearlyFmBg = Color(
- light: Color(rgba: 0xF0F0_F0ff), dark: Color(rgba: 0x2222_22ff)
- )
- static let clearlyFmBorder = Color(
- light: .clear, dark: Color(rgba: 0x3333_33ff)
- )
- static let clearlyFmValue = Color(
- light: Color(rgba: 0x3333_33ff), dark: Color(rgba: 0x9999_99ff)
- )
-}
diff --git a/project.yml b/project.yml
index 2f2e975..2bdf8e2 100644
--- a/project.yml
+++ b/project.yml
@@ -15,12 +15,12 @@ packages:
Sparkle:
url: https://github.com/sparkle-project/Sparkle
from: "2.6.0"
- MarkdownUI:
- url: https://github.com/gonzalezreal/swift-markdown-ui
- from: "2.4.0"
Highlightr:
url: https://github.com/raspu/Highlightr
from: "2.2.1"
+ cmark-gfm:
+ url: https://github.com/brokenhandsio/cmark-gfm.git
+ from: "2.1.0"
targets:
Chops:
@@ -33,8 +33,9 @@ targets:
- Info.plist
dependencies:
- package: Sparkle
- - package: MarkdownUI
- package: Highlightr
+ - package: cmark-gfm
+ product: cmark
settings:
base:
PRODUCT_BUNDLE_IDENTIFIER: com.joshpigford.Chops