-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebview.go
More file actions
313 lines (276 loc) · 9.12 KB
/
webview.go
File metadata and controls
313 lines (276 loc) · 9.12 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"bytes"
"encoding/base64"
"fmt"
"html/template"
"image/jpeg"
"image/png"
"log"
"regexp"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
// Default implementation that will be overridden by build-specific files
var isWebviewAvailableDefault = false
// Global image cache instance
var globalImageCache *ImageCache
// initImageCache initializes the global image cache
func initImageCache() {
if globalImageCache == nil {
var err error
globalImageCache, err = NewImageCache()
if err != nil {
log.Printf("Warning: Failed to initialize image cache: %v", err)
globalImageCache = nil
}
}
}
// IsWebviewAvailable returns true if webview is available
func IsWebviewAvailable() bool {
return isWebviewAvailableDefault
}
// IsWebviewRunning returns true if a webview is currently running
// This is a stub that will be overridden by build-specific files
// Only declare this for non-CGO builds
var isWebviewRunning = false
// OpenNoteInWebview opens a note in a webview window or falls back to browser
// This function signature will be implemented by build-specific files
func openNoteInWebview(title, htmlContent string) error {
if isWebviewAvailableDefault {
return OpenNoteInWebview(title, htmlContent)
}
// Fallback - should not be reached due to build-specific implementations
ShowWebviewUnavailableMessage()
return fmt.Errorf("webview not available")
}
// RenderNoteAsHTML converts a note's markdown content to HTML for webview display
func RenderNoteAsHTML(title, markdownContent string) (string, error) {
// Pre-process markdown to handle Google Drive images
processedMarkdown, err := preprocessMarkdownImages(markdownContent)
if err != nil {
return "", fmt.Errorf("failed to preprocess markdown images: %v", err)
}
// Convert markdown to HTML using goldmark
htmlContent := convertMarkdownToHTML(processedMarkdown)
// Wrap in basic HTML template
htmlTemplate := `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
}
h1, h2, h3, h4, h5, h6 {
color: #2c3e50;
}
code {
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
}
pre {
background-color: #f4f4f4;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
}
blockquote {
border-left: 4px solid #3498db;
margin: 0;
padding-left: 20px;
color: #7f8c8d;
}
img {
max-width: 100%;
height: auto;
border-radius: 5px;
}
table {
border-collapse: collapse;
width: 100%;
margin: 20px 0;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
</style>
</head>
<body>
<h1>{{.Title}}</h1>
<div id="content">
{{.Content}}
</div>
</body>
</html>`
tmpl, err := template.New("note").Parse(htmlTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse HTML template: %v", err)
}
var buf strings.Builder
err = tmpl.Execute(&buf, struct {
Title string
Content template.HTML
}{
Title: title,
Content: template.HTML(htmlContent),
})
if err != nil {
return "", fmt.Errorf("failed to execute HTML template: %v", err)
}
return buf.String(), nil
}
// detectImageFormat detects image format from base64 data magic bytes
func detectImageFormat(base64Data string) string {
// PNG signature: iVBORw0KG (base64 of 89 50 4E 47)
if strings.HasPrefix(base64Data, "iVBORw0KG") {
return "png"
}
// JPEG signature: /9j/ (base64 of FF D8 FF)
if strings.HasPrefix(base64Data, "/9j/") {
return "jpeg"
}
// GIF signature: R0lGOD (base64 of 47 49 46 38)
if strings.HasPrefix(base64Data, "R0lGOD") {
return "gif"
}
// Default to png if can't detect (most common format)
return "png"
}
// preprocessMarkdownImages processes Google Drive images in markdown before HTML conversion
func preprocessMarkdownImages(markdown string) (string, error) {
// Initialize cache if needed
initImageCache()
// Regular expression to find Google Drive URLs in markdown image syntax
// Matches both full URLs and gdrive: format
//  or 
googleDriveRegex := regexp.MustCompile(`!\[([^\]]*)\]\(((?:https://drive\.google\.com/file/d/[^)]+)|(?:gdrive:[a-zA-Z0-9_-]+))\)`)
// Find all Google Drive image references
matches := googleDriveRegex.FindAllStringSubmatch(markdown, -1)
processedMarkdown := markdown
for _, match := range matches {
fullMatch := match[0] // Full match: 
altText := match[1] // Alt text
googleURL := match[2] // Google Drive URL
var dataURI string
var err error
// Try cache first
if globalImageCache != nil {
if cachedData, found := globalImageCache.GetCachedImage(googleURL); found {
// Check if cached data is already a data URI or raw base64
if strings.HasPrefix(cachedData, "data:") {
// Already a proper data URI, use as-is
dataURI = cachedData
} else {
// Raw base64 from cache - convert to data URI
format := detectImageFormat(cachedData)
dataURI = fmt.Sprintf("data:image/%s;base64,%s", format, cachedData)
}
} else {
// Cache miss - download and convert to data URI
dataURI, err = convertGoogleDriveImageToDataURI(googleURL)
if err != nil {
// If we can't convert, leave the original URL
log.Printf("Warning: Could not convert Google Drive image %s: %v", googleURL, err)
continue
}
// Store in cache for future use
if cacheErr := globalImageCache.StoreCachedImage(googleURL, dataURI); cacheErr != nil {
log.Printf("Warning: Could not cache image %s: %v", googleURL, cacheErr)
// Continue anyway - we have the data URI
}
}
} else {
// No cache available - fallback to direct conversion
dataURI, err = convertGoogleDriveImageToDataURI(googleURL)
if err != nil {
// If we can't convert, leave the original URL
log.Printf("Warning: Could not convert Google Drive image %s: %v", googleURL, err)
continue
}
}
// Replace the Google Drive URL with the data URI
newImageTag := fmt.Sprintf("", altText, dataURI)
processedMarkdown = strings.Replace(processedMarkdown, fullMatch, newImageTag, 1)
}
return processedMarkdown, nil
}
// convertGoogleDriveImageToDataURI downloads a Google Drive image and converts it to a data URI
func convertGoogleDriveImageToDataURI(googleURL string) (string, error) {
// Download the image using the existing loadGoogleImage function
// Note: We'll use reasonable defaults for max width/height for web display
img, imgFmt, err := loadGoogleImage(googleURL, 1200, 800)
if err != nil {
return "", fmt.Errorf("failed to load Google Drive image: %v", err)
}
// Convert image to bytes
var buf bytes.Buffer
switch imgFmt {
case "png":
err = png.Encode(&buf, img)
case "jpeg", "jpg":
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: 90})
default:
// Default to PNG for unknown formats
//err = png.Encode(&buf, img)
//imgFmt = "png"
//heic caused panic with the png assumption
return "", fmt.Errorf("Unsupported image format: %s.")
}
if err != nil {
return "", fmt.Errorf("failed to encode image: %v", err)
}
// Convert to base64
base64Data := base64.StdEncoding.EncodeToString(buf.Bytes())
// Create data URI
dataURI := fmt.Sprintf("data:image/%s;base64,%s", imgFmt, base64Data)
return dataURI, nil
}
// convertMarkdownToHTML converts markdown to HTML using goldmark
func convertMarkdownToHTML(markdown string) string {
// Configure goldmark with common extensions
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM, // GitHub Flavored Markdown
extension.Table, // Tables
extension.Strikethrough, // Strikethrough text
extension.Linkify, // Auto-link URLs
extension.TaskList, // Task lists
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(), // Auto-generate heading IDs
),
goldmark.WithRendererOptions(
html.WithUnsafe(), // Allow raw HTML (needed for some markdown features)
),
)
var buf bytes.Buffer
if err := md.Convert([]byte(markdown), &buf); err != nil {
// Fallback to plain text if goldmark fails
return fmt.Sprintf("<pre>%s</pre>", markdown)
}
return buf.String()
}
// ShowWebviewNotAvailableMessage displays a user-friendly message
func ShowWebviewNotAvailableMessage() string {
return fmt.Sprintf("%sWebview not available - opening in default browser%s", YELLOW_BG, RESET)
}