-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcuemode-renderer.js
More file actions
280 lines (246 loc) · 8.72 KB
/
cuemode-renderer.js
File metadata and controls
280 lines (246 loc) · 8.72 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
const fs = require('fs');
const path = require('path');
// Import actual CueMode modules
const { MarkdownParser } = require('./out/utils/markdown');
const { ThemeManager } = require('./out/utils/theme');
const { generateMarkdownCSS } = require('./out/utils/markdownStyles');
const { generateWebViewCSS, generateDebugCSS } = require('./out/utils/webviewStyles');
/**
* CueMode Renderer - Using real extension logic
*/
class CueModeRenderer {
constructor(options = {}) {
this.config = {
fontSize: options.fontSize || 25,
lineHeight: options.lineHeight || 1,
maxWidth: options.maxWidth || 1200,
padding: options.padding || 10,
theme: options.theme || 'rose',
features: options.features || {
headers: true,
emphasis: true,
lists: true,
links: true,
code: true,
blockquotes: true,
tables: true,
taskLists: true,
strikethrough: true,
horizontalRule: true
}
};
}
/**
* Parse markdown blocks - Copied from webview.ts logic
*/
parseMarkdownBlocks(html) {
const blocks = [];
const blockElements = [
'h[1-6]', 'p', 'div', 'table', 'ul', 'ol', 'blockquote', 'pre'
];
const blockRegex = new RegExp(`(<(?:${blockElements.join('|')})[^>]*>.*?</(?:${blockElements.join('|')})>)`, 'gs');
let lastIndex = 0;
let match;
while ((match = blockRegex.exec(html)) !== null) {
if (match.index > lastIndex) {
const beforeText = html.slice(lastIndex, match.index).trim();
if (beforeText) {
// For plain text content, split by single newlines to create separate display blocks
// This allows each line to be shown independently in presentation mode
const lines = beforeText.split(/\n/).filter(line => line.trim());
lines.forEach(line => {
if (line.trim()) {
blocks.push(line.trim());
}
});
}
}
if (match[1]) {
blocks.push(match[1]);
}
lastIndex = match.index + match[0].length;
}
if (lastIndex < html.length) {
const remainingText = html.slice(lastIndex).trim();
if (remainingText) {
// For plain text content, split by single newlines to create separate display blocks
const lines = remainingText.split(/\n/).filter(line => line.trim());
lines.forEach(line => {
if (line.trim()) {
blocks.push(line.trim());
}
});
}
}
return blocks;
}
/**
* Process markdown content - Copied from webview.ts logic
*/
processMarkdownContent(content) {
try {
// Use actual MarkdownParser
const result = MarkdownParser.parse(content, this.config.features);
// Use actual block-level processing logic
const logicalBlocks = this.parseMarkdownBlocks(result.html);
const processedBlocks = logicalBlocks.map((block, index) => {
if (block.trim() === '') {
return '';
}
return `<div class="cue-line markdown-block" data-block="${index}">${block}</div>`;
});
const filteredBlocks = processedBlocks.filter(block => block !== '');
return {
content: `<div class="markdown-content">${filteredBlocks.join('')}</div>`,
stats: {
originalLength: result.html.length,
blockCount: logicalBlocks.length,
filteredCount: filteredBlocks.length
}
};
} catch (error) {
throw new Error(`Markdown parsing failed: ${error.message}`);
}
}
/**
* Generate complete CSS - Using actual theme and style modules
*/
generateCSS() {
const themeConfig = ThemeManager.getTheme(this.config.theme);
const themeCSS = ThemeManager.generateCSS(
this.config.theme,
this.config.fontSize,
this.config.lineHeight,
this.config.maxWidth,
this.config.padding
);
const markdownCSS = generateMarkdownCSS(themeConfig);
const webviewCSS = generateWebViewCSS();
const debugCSS = generateDebugCSS();
return {
theme: themeCSS,
markdown: markdownCSS,
webview: webviewCSS,
debug: debugCSS
};
}
/**
* Render markdown file to HTML
*/
renderFile(markdownPath, outputPath = null) {
if (!fs.existsSync(markdownPath)) {
throw new Error(`File does not exist: ${markdownPath}`);
}
const markdownContent = fs.readFileSync(markdownPath, 'utf8');
const processed = this.processMarkdownContent(markdownContent);
const css = this.generateCSS();
const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CueMode Debug - ${path.basename(markdownPath)}</title>
<style>
/* Basic style reset */
* { box-sizing: border-box; }
body { margin: 0; padding: 20px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; }
/* WebView styles */
${css.webview}
/* Theme CSS */
${css.theme}
/* Markdown base styles */
${css.markdown}
/* Debug-specific styles */
${css.debug}
/* Line break markers - show visual indicators at actual line breaks */
br {
position: relative;
}
br::after {
content: ' ↵';
color: var(--accent-color);
opacity: 0.7;
font-size: 1.2em;
font-weight: bold;
margin-left: 0.3em;
pointer-events: none;
text-shadow: 0 0 3px rgba(255, 255, 255, 0.3);
}
</style>
</head>
<body>
<div class="file-info">
<h3>� File Information</h3>
<p><strong>📁 File:</strong> \${filePath}</p>
<p><strong>Size:</strong> ${markdownContent.length} characters</p>
<p><strong>Config:</strong> ${this.config.fontSize}px, line height ${this.config.lineHeight}, padding ${this.config.padding}px</p>
<p><strong>Logical blocks:</strong> ${processed.stats.blockCount} → ${processed.stats.filteredCount} blocks</p>
</div>
<div class="debug-panel">
<h4>🔧 Debug Guide</h4>
<p>🔴 Red border: Logical block boundaries</p>
<p>🔵 Blue background: Header elements</p>
<p>🟡 Yellow background: Table elements</p>
<p>🟣 Purple background: Code blocks</p>
<p>🟢 Green background: List elements</p>
<p>💡 Click any block to view details</p>
<p>📏 Unordered list indent: 25px | Ordered list indent: 50px</p>
<p>📐 Paragraph spacing: 8px (optimized reading experience)</p>
<p>✂️ Paragraph splitting: Split by lines, displayed independently</p>
<p>🛠️ Use browser developer tools to debug CSS</p>
</div>
<div class="cue-container">
<div class="cue-content">
${processed.content}
</div>
</div>
<script>
// Debug interaction script
document.addEventListener('DOMContentLoaded', function() {
console.log('📊 CueMode Debug Info:');
console.log('File:', '${markdownPath}');
console.log('Total blocks:', document.querySelectorAll('.markdown-block').length);
console.log('Config:', ${JSON.stringify(this.config)});
// Add click event to each block
document.querySelectorAll('.markdown-block').forEach((block, index) => {
block.style.cursor = 'pointer';
block.addEventListener('click', function() {
console.log(\`Block \${index}:\`, {
content: this.textContent.trim().substring(0, 100) + '...',
innerHTML: this.innerHTML.substring(0, 200) + '...',
computedStyle: {
margin: getComputedStyle(this).margin,
padding: getComputedStyle(this).padding,
marginLeft: getComputedStyle(this).marginLeft
}
});
});
});
// Special highlighting for list elements
document.querySelectorAll('.markdown-block ul, .markdown-block ol').forEach(list => {
list.addEventListener('mouseenter', function() {
this.style.backgroundColor = 'rgba(0, 255, 0, 0.2)';
});
list.addEventListener('mouseleave', function() {
this.style.backgroundColor = 'rgba(0, 255, 0, 0.05)';
});
});
});
</script>
</body>
</html>`;
// Determine output filename
if (!outputPath) {
const baseName = path.basename(markdownPath, path.extname(markdownPath));
outputPath = `debug-${baseName}.html`;
}
fs.writeFileSync(outputPath, html, 'utf8');
return {
inputFile: markdownPath,
outputFile: outputPath,
stats: processed.stats,
config: this.config
};
}
}
module.exports = { CueModeRenderer };