Skip to content

Commit

Permalink
Feat endpoint (#23)
Browse files Browse the repository at this point in the history
* feat: endpoint

* feat: highlight code

* feat: highlight code
  • Loading branch information
purr100 authored Nov 11, 2024
1 parent 14c8c2d commit 87eaf96
Show file tree
Hide file tree
Showing 306 changed files with 15,506 additions and 387 deletions.
155 changes: 12 additions & 143 deletions Chato.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"originHash" : "5102feaeeb04bae99d89c22d43001820d5aea29107d777c6cd614c8f4c28495d",
"originHash" : "5a366234925c8bb73a8041fb8478dacb7c9125c2b4584a5406f6bb8d162a0d49",
"pins" : [
{
"identity" : "bottomsheet",
"kind" : "remoteSourceControl",
"location" : "https://github.com/lucaszischka/BottomSheet",
"state" : {
"revision" : "720ddbd1371fef2a09cc1ab65fc503df37767f9c",
"version" : "3.1.1"
}
},
{
"identity" : "confettiswiftui",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -46,15 +37,6 @@
"version" : "0.2.6"
}
},
{
"identity" : "splash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/Splash.git",
"state" : {
"revision" : "7f4df436eb78fe64fe2c32c58006e9949fa28ad8",
"version" : "0.16.0"
}
},
{
"identity" : "swift-markdown-ui",
"kind" : "remoteSourceControl",
Expand Down
55 changes: 0 additions & 55 deletions Chato.xcodeproj/xcshareddata/xcschemes/Test.xcscheme

This file was deleted.

12 changes: 12 additions & 0 deletions Chato/Assets.xcassets/panda-syntax-dark.min.dataset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data" : [
{
"filename" : "panda-syntax-dark.min.css",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Chato/Service/Highlight.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Created for Chato in 2024

import Foundation
import Highlightr
import MarkdownUI
import SwiftUI

struct HI: CodeSyntaxHighlighter {
func highlightCode(_ code: String, language: String?) -> Text {
if let highlightedCode = highlightr.highlight(code, as: language) {
return convertToText(highlightedCode)
} else {
return Text("")
}
}

static var shared = HI()
let highlightr: Highlightr

private init() {
highlightr = Highlightr()!
if !highlightr.setTheme(to: "panda-syntax-dark") {
print("failed to load panda-syntax-dark.min.css")
}
}
}

private func convertToText(_ attributedString: NSAttributedString) -> Text {
var result = Text("") // Start with an empty Text
attributedString.enumerateAttributes(in: NSRange(location: 0, length: attributedString.length), options: []) { attributes, range, _ in
// Create a substring for the given range
let substring = attributedString.attributedSubstring(from: range).string

// Create a Text view with attributes applied
var text = Text(substring)

// Apply attributes to the Text
text = text.font(.callout) // Change font to SwiftUI Font
// if let font = attributes[.font] as? UIFont {
// text = text.font(.system(size: font.pointSize)) // Change font to SwiftUI Font
// }
if let color = attributes[.foregroundColor] as? UIColor {
text = text.foregroundColor(Color(color)) // Change color to SwiftUI Color
}
// Add more attributes as needed (e.g. underline, strikethrough, etc.)

// Combine the attributed Text fragments
result = result + text
}

return result.fontDesign(.monospaced)
}

This file was deleted.

45 changes: 0 additions & 45 deletions Chato/Theme+Effect/SyntaxHighlighter/TextOutputFormat.swift

This file was deleted.

97 changes: 3 additions & 94 deletions Chato/Views/MessageList/MessageTypes/MsgVM.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import MarkdownUI
import Splash
import SwiftUI

extension NormalMsgView {
Expand Down Expand Up @@ -30,7 +29,7 @@ extension NormalMsgView {
}
}
.background {
Color(Self.theme.backgroundColor)
Color(Color(red: 0.165, green: 0.173, blue: 0.173))
}
.clipShape(RoundedRectangle(cornerRadius: 8))
.markdownMargin(top: .zero, bottom: .em(0.8))
Expand Down Expand Up @@ -63,7 +62,7 @@ extension NormalMsgView {
.markdownBlockStyle(\.codeBlock) {
codeBlock2($0)
}
.markdownCodeSyntaxHighlighter(.splash(theme: pandalong()))
.markdownCodeSyntaxHighlighter(HI.shared)
}

@ViewBuilder
Expand Down Expand Up @@ -95,98 +94,8 @@ func codeBlock2(_ configuration: CodeBlockConfiguration) -> some View {
}
}
.background {
Color(pandalong().backgroundColor)
Color(Color(red: 0.165, green: 0.173, blue: 0.173))
}
.clipShape(RoundedRectangle(cornerRadius: 8))
.markdownMargin(top: .zero, bottom: .em(0.8))
}

func pandalong() -> Splash.Theme {
return Splash.Theme(
font: .init(size: 16),
plainTextColor: Splash.Color(
red: 0.902, green: 0.902, blue: 0.902, alpha: 1
),
tokenColors: [
.keyword: Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.string: Splash.Color(red: 0.098, green: 0.976, blue: 0.847, alpha: 1),
.type: Splash.Color(red: 0.098, green: 0.976, blue: 0.847, alpha: 1),
.call: Splash.Color(red: 0.098, green: 0.976, blue: 0.847, alpha: 1),
.number: Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.comment: Splash.Color(red: 0.733, green: 0.733, blue: 0.733, alpha: 1),
.property: Splash.Color(red: 0.271, green: 0.663, blue: 0.976, alpha: 1),
.dotAccess: Splash.Color(red: 0.271, green: 0.663, blue: 0.976, alpha: 1),
.preprocessing: Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("meta"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("operator"): Splash.Color(red: 0.691, green: 0.518, blue: 0.922, alpha: 1),
.custom("variable"): Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.custom("attr"): Splash.Color(red: 0.902, green: 0.902, blue: 0.902, alpha: 1),
.custom("punctuation"): Splash.Color(red: 0.902, green: 0.902, blue: 0.902, alpha: 1),
.custom("metaKeyword"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("name"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("selectorTag"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("charEscape"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("deletion"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("bultIn"): Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.custom("doctag"): Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.custom("link"): Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.custom("literal"): Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.custom("regexp"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("selectorAttr"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("selectorPseudo"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.custom("titleClass"): Splash.Color(red: 0.098, green: 0.976, blue: 0.847, alpha: 1),
.custom("titleFunction"): Splash.Color(red: 0.271, green: 0.663, blue: 0.976, alpha: 1),
.custom("titleClassInherited"): Splash.Color(red: 0.098, green: 0.976, blue: 0.847, alpha: 1),
.custom("variableLanguage"): Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1)
],
backgroundColor: Splash.Color(
red: 0.165, green: 0.173, blue: 0.173, alpha: 1
)
)
}

func panda(withFont font: Splash.Font) -> Splash.Theme {
return Splash.Theme(
font: font,
plainTextColor: Splash.Color(
red: 0.902, green: 0.902, blue: 0.902, alpha: 1
),
tokenColors: [
.keyword: Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1),
.string: Splash.Color(red: 0.098, green: 0.976, blue: 0.847, alpha: 1),
.type: Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.call: Splash.Color(red: 0.271, green: 0.663, blue: 0.976, alpha: 1),
.number: Splash.Color(red: 1.0, green: 0.718, blue: 0.424, alpha: 1),
.comment: Splash.Color(red: 0.733, green: 0.733, blue: 0.733, alpha: 1),
.property: Splash.Color(red: 0.271, green: 0.663, blue: 0.976, alpha: 1),
.dotAccess: Splash.Color(red: 0.271, green: 0.663, blue: 0.976, alpha: 1),
.preprocessing: Splash.Color(red: 1.0, green: 0.459, blue: 0.718, alpha: 1)
],
backgroundColor: Splash.Color(
red: 0.165, green: 0.173, blue: 0.173, alpha: 1
)
)
}

func lightTheme(withFont font: Splash.Font) -> Splash.Theme {
return Splash.Theme(
font: font,
plainTextColor: Splash.Color(
red: 0.0, green: 0.0, blue: 0.0, alpha: 1
),
tokenColors: [
.keyword: Splash.Color(red: 0.0, green: 0.0, blue: 1.0, alpha: 1),
.string: Splash.Color(red: 0.0, green: 0.5, blue: 0.0, alpha: 1),
.type: Splash.Color(red: 0.6, green: 0.2, blue: 0.8, alpha: 1),
.call: Splash.Color(red: 0.8, green: 0.2, blue: 0.2, alpha: 1),
.number: Splash.Color(red: 1.0, green: 0.5, blue: 0.0, alpha: 1),
.comment: Splash.Color(red: 0.5, green: 0.5, blue: 0.5, alpha: 1),
.property: Splash.Color(red: 0.2, green: 0.4, blue: 0.8, alpha: 1),
.dotAccess: Splash.Color(red: 0.2, green: 0.4, blue: 0.8, alpha: 1),
.preprocessing: Splash.Color(red: 0.6, green: 0.4, blue: 0.0, alpha: 1)
],
backgroundColor: Splash.Color(
red: 1.0, green: 1.0, blue: 1.0, alpha: 1
)
)
}
11 changes: 5 additions & 6 deletions Chato/Views/MessageList/MessageTypes/NormalMsgView.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Highlightr
import MarkdownUI
import SwiftData
import SwiftUI
import Translation

struct NormalMsgView: View {
static let theme = pandalong()
@EnvironmentObject var em: EM
@EnvironmentObject var pref: Pref
@Environment(\.modelContext) private var modelContext
Expand Down Expand Up @@ -83,11 +83,10 @@ struct NormalMsgView: View {
// don't highlight code on typing for better performance
if msg.status == .received || msg.status == .error {
Markdown(msg.message)
// .markdownBlockStyle(\.codeBlock) {
// codeBlock($0)
// }
// may crash due to bad address access
// .markdownCodeSyntaxHighlighter(.splash(theme: Self.theme))
.markdownBlockStyle(\.codeBlock) {
codeBlock($0)
}
.markdownCodeSyntaxHighlighter(HI.shared)
} else {
Markdown(msg.message)
}
Expand Down
Loading

0 comments on commit 87eaf96

Please sign in to comment.