-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_system.html
More file actions
398 lines (334 loc) · 13.1 KB
/
plugin_system.html
File metadata and controls
398 lines (334 loc) · 13.1 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SquibView - Plugin System Example</title>
<link rel="icon"
href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><circle cx='16' cy='16' r='14' fill='%23f57c00' stroke='%23000' stroke-width='1'/><path d='M2 16h28M16 2a14 14 0 0114 14 14 14 0 01-14 14 14 14 0 01-14-14A14 14 0 0116 2zm0 0v28M9 16a7 7 0 0014 0 7 7 0 00-14 0z' fill='none' stroke='%23000' stroke-width='1'/></svg>">
<!-- Load Required Libraries -->
<script src="https://cdn.jsdelivr.net/npm/markdown-it@13.0.1/dist/markdown-it.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/highlight.js@11.7.0/lib/highlight.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mermaid@9.4.0/dist/mermaid.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/papaparse@5.3.2/papaparse.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/turndown@7.1.1/dist/turndown.js"></script>
<script src="https://cdn.jsdelivr.net/npm/tiny-emitter@2.1.0/dist/tinyemitter.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/diff-match-patch@1.0.5/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.js"></script>
<!-- SquibView CSS -->
<link rel="stylesheet" href="../dist/squibview.css">
<!-- Shared example styles -->
<link rel="stylesheet" href="examples.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@11.7.0/styles/github.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.css">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
header {
background-color: #f5f5f5;
padding: 20px;
margin-bottom: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
margin-top: 0;
color: #333;
}
.description {
background-color: #e9f7fe;
border-left: 4px solid #3498db;
padding: 15px;
margin-bottom: 20px;
border-radius: 0 5px 5px 0;
}
.editor-container {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 10px;
margin-bottom: 20px;
min-height: 600px;
}
.examples-nav {
display: flex;
justify-content: space-between;
margin-top: 40px;
padding-top: 20px;
border-top: 1px solid #eee;
}
.nav-button {
padding: 10px 15px;
background-color: #4CAF50;
color: white;
text-decoration: none;
border-radius: 4px;
transition: background-color 0.2s;
}
.nav-button:hover {
background-color: #3e8e41;
}
.plugin-controls {
margin-bottom: 20px;
display: flex;
gap: 10px;
}
.plugin-button {
padding: 8px 15px;
background-color: #3498db;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}
.plugin-button:hover {
background-color: #2980b9;
}
#status-message {
padding: 10px;
margin-top: 10px;
border-radius: 4px;
background-color: #f8f9fa;
display: none;
}
.info {
background-color: #e9f7fe;
border-left: 4px solid #3498db;
}
.success {
background-color: #e9fef5;
border-left: 4px solid #2ecc71;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>SquibView - Plugin System Example</h1>
<p>This example demonstrates how to extend SquibView with custom renderers and operations using the new plugin architecture.</p>
</header>
<div class="description">
<h3>Custom Features Available:</h3>
<ol>
<li><strong>KaTeX Math Renderer:</strong> Adds support for mathematical expressions with KaTeX syntax</li>
<li><strong>Word Counter:</strong> Adds a button to count words, characters, and sentences</li>
<li><strong>Text Case Converter:</strong> Adds buttons to convert text to UPPERCASE or lowercase</li>
</ol>
<p>Use the buttons below to activate these plugins and extend SquibView's functionality.</p>
</div>
<div class="plugin-controls">
<button id="activate-math" class="plugin-button">Activate Math Plugin</button>
<button id="activate-wordcount" class="plugin-button">Activate Word Counter</button>
<button id="activate-case" class="plugin-button">Activate Case Converter</button>
</div>
<div id="status-message"></div>
<div class="editor-container">
<div id="editor" style="width: 100%; height: 500px;"></div>
</div>
<div class="examples-nav">
<a href="index.html" class="nav-button">Back to Examples</a>
</div>
</div>
<!-- Load SquibView UMD module -->
<script src="../dist/squibview.umd.min.js"></script>
<script>
// Initial sample content with various features to demonstrate plugins
const initialContent = `# Plugin System Demo
## Math Expressions (KaTeX Plugin)
After activating the KaTeX plugin, the following math expressions will render:
$$E = mc^2$$
Inline math: $a^2 + b^2 = c^2$
Matrix example:
$$
\\begin{pmatrix}
a & b \\\\
c & d
\\end{pmatrix}
$$
## Text Statistics (Word Counter Plugin)
This paragraph can be analyzed by the word counter plugin. It will count words, characters, and estimate reading time. The quick brown fox jumps over the lazy dog. This sentence has exactly ten words. How many words do you think are in this entire paragraph?
## Case Conversion
THESE WORDS ARE IN UPPERCASE.
these words are in lowercase.
Mixed Case Text Can Be Converted Too.
`;
// Initialize SquibView
document.addEventListener('DOMContentLoaded', function() {
// Create a variable to reference the editor
window.editor = new SquibView('#editor', {
initialContent: initialContent,
inputContentType: 'md',
showControls: true,
initialView: 'split'
});
// Get status message element
const statusMessage = document.getElementById('status-message');
// Show status message function
function showStatus(message, type = 'info') {
statusMessage.textContent = message;
statusMessage.className = type;
statusMessage.style.display = 'block';
setTimeout(() => {
statusMessage.style.display = 'none';
}, 5000);
}
// KaTeX Math Plugin
document.getElementById('activate-math').addEventListener('click', function() {
// Create KaTeX math renderer plugin
const mathRenderer = {
render: (source) => {
const html = editor.renderers.md.sourceToOutput(source);
// Custom rendering to process KaTeX math expressions
const div = document.createElement('div');
div.innerHTML = html;
// Process block math: $$...$$
const blockMathRegex = /\$\$([\s\S]+?)\$\$/g;
let content = div.innerHTML;
content = content.replace(blockMathRegex, (match, p1) => {
try {
return '<div class="math-block">' + katex.renderToString(p1, { displayMode: true }) + '</div>';
} catch (e) {
console.error('KaTeX error:', e);
return '<div class="math-error">Math rendering error</div>';
}
});
// Process inline math: $...$
const inlineMathRegex = /\$([^\$]+?)\$/g;
content = content.replace(inlineMathRegex, (match, p1) => {
try {
return katex.renderToString(p1, { displayMode: false });
} catch (e) {
console.error('KaTeX error:', e);
return '<span class="math-error">Math error</span>';
}
});
div.innerHTML = content;
editor.output.innerHTML = "<div contenteditable='true'>" + div.innerHTML + "</div>";
// Initialize mermaid diagrams
mermaid.init(undefined, editor.output.querySelectorAll('.mermaid'));
},
sourceToOutput: (source) => {
// This would be the same as the render function but returning the HTML instead
const html = editor.renderers.md.sourceToOutput(source);
// Same processing as in render
const div = document.createElement('div');
div.innerHTML = html;
// Process block math: $$...$$
const blockMathRegex = /\$\$([\s\S]+?)\$\$/g;
let content = div.innerHTML;
content = content.replace(blockMathRegex, (match, p1) => {
try {
return '<div class="math-block">' + katex.renderToString(p1, { displayMode: true }) + '</div>';
} catch (e) {
return '<div class="math-error">Math rendering error</div>';
}
});
// Process inline math: $...$
const inlineMathRegex = /\$([^\$]+?)\$/g;
content = content.replace(inlineMathRegex, (match, p1) => {
try {
return katex.renderToString(p1, { displayMode: false });
} catch (e) {
return '<span class="math-error">Math error</span>';
}
});
return content;
},
outputToSource: (output) => {
// Use the default Markdown converter
return editor.htmlToMarkdown(output);
}
};
// Override the markdown renderer
const origRenderer = editor.renderers.md;
editor.registerRenderer('md', {
...origRenderer,
render: mathRenderer.render,
sourceToOutput: mathRenderer.sourceToOutput,
outputToSource: mathRenderer.outputToSource
});
// Force re-render
editor.renderOutput();
showStatus('KaTeX Math Plugin activated! Math expressions will now render.', 'success');
});
// Word Counter Plugin
document.getElementById('activate-wordcount').addEventListener('click', function() {
// Word counter function
function countWordsAndStats(text) {
const wordCount = text.split(/\s+/).filter(word => word.length > 0).length;
const charCount = text.length;
const sentenceCount = text.split(/[.!?]+/).filter(s => s.trim().length > 0).length;
const readingTime = Math.ceil(wordCount / 200); // Average reading speed of 200 wpm
return {
words: wordCount,
chars: charCount,
sentences: sentenceCount,
readingTime: readingTime
};
}
// Get original renderer
const origRenderer = editor.renderers.md;
// Add word counter operation
const enhancedRenderer = {
...origRenderer,
operations: {
...origRenderer.operations,
countWords: (src) => {
const stats = countWordsAndStats(src);
alert(`Text Statistics:
- Words: ${stats.words}
- Characters: ${stats.chars}
- Sentences: ${stats.sentences}
- Estimated reading time: ${stats.readingTime} minute(s)`);
return src; // Return unchanged
}
},
buttons: [
...origRenderer.buttons,
{ label: 'Count Words', action: 'countWords', title: 'Count words, characters and sentences' }
]
};
// Register the enhanced renderer
editor.registerRenderer('md', enhancedRenderer);
showStatus('Word Counter Plugin activated! A "Count Words" button has been added.', 'success');
});
// Case Converter Plugin
document.getElementById('activate-case').addEventListener('click', function() {
// Get original renderer
const origRenderer = editor.renderers.md;
// Add case conversion operations
const enhancedRenderer = {
...origRenderer,
operations: {
...origRenderer.operations,
uppercase: (src) => {
return src.toUpperCase();
},
lowercase: (src) => {
return src.toLowerCase();
}
},
buttons: [
...origRenderer.buttons,
{ label: 'UPPERCASE', action: 'uppercase', title: 'Convert text to uppercase' },
{ label: 'lowercase', action: 'lowercase', title: 'Convert text to lowercase' }
]
};
// Register the enhanced renderer
editor.registerRenderer('md', enhancedRenderer);
showStatus('Case Converter Plugin activated! UPPERCASE and lowercase buttons have been added.', 'success');
});
});
</script>
</body>
</html>