-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown_template.html
284 lines (253 loc) · 9.59 KB
/
markdown_template.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Live Preview</title>
<!-- Load the JavaScript Markdown library -->
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<!-- Load Prism.js for syntax highlighting -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<!-- Load additional languages as needed -->
<!-- For example, to support Python syntax highlighting: -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
<!-- Add more languages here -->
<!-- Load MathJax for rendering mathematical equations -->
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" id="MathJax-script"></script>
<style>
/* Base styles */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: #f8f8f2;
background-color: #1e1e1e;
padding: 15px;
margin: 0;
}
a {
color: #1e90ff;
font-weight: bold;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* Blockquote styling */
blockquote {
background-color: #2e2e2e;
color: #dddddd;
border-left: 5px solid #ffcc00;
padding: 10px 20px;
margin: 20px 0;
border-radius: 8px;
font-style: italic;
}
/* Code block styling */
pre {
position: relative; /* For positioning the copy button */
background-color: #2e2e2e;
color: #dddddd;
border-left: 5px solid #AAFF00;
padding: 10px 20px;
margin: 20px 0;
border-radius: 8px;
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
white-space: pre; /* Preserve formatting */
overflow: auto; /* Enable scrolling for long code */
}
/* Remove additional styling from code inside pre */
pre code {
background-color: transparent;
padding: 0;
color: inherit;
font-size: inherit;
}
/* Inline code styling */
code {
background-color: #2b2b2b;
color: #f8f8f2;
padding: 2px 4px;
border-radius: 4px;
font-size: 14px;
}
/* Copy button styling */
.copy-button {
position: absolute;
top: 10px;
right: 20px;
background-color: #444;
color: #fff;
border: none;
padding: 5px 8px;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
opacity: 0.7;
transition: opacity 0.2s;
}
.copy-button:hover {
opacity: 1;
}
/* Table styling */
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
background-color: #2b2b2b;
color: #f8f8f2;
}
th, td {
border: 1px solid #444;
padding: 8px 12px;
text-align: left;
}
th {
background-color: #333;
font-weight: bold;
}
/* Image styling */
img {
max-width: 100%;
height: auto;
}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: #1e1e1e;
}
::-webkit-scrollbar-thumb {
background-color: #444;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background-color: #555;
}
/* Tooltip styling */
.custom-tooltip {
position: absolute;
background-color: #333;
color: #fff;
padding: 5px;
border-radius: 5px;
font-size: 12px;
z-index: 1000;
pointer-events: none;
white-space: nowrap;
}
/* MathJax styling (if needed) */
.MathJax {
font-size: 100%;
}
</style>
</head>
<body>
<div id="content"></div>
<!-- JavaScript code -->
<script>
// Configure Marked.js to support syntax highlighting with Prism.js
marked.setOptions({
highlight: function(code, lang) {
var validLang = Prism.languages[lang] ? lang : 'plaintext';
return Prism.highlight(code, Prism.languages[validLang], validLang);
}
});
// Function to update the content with rendered Markdown
function updateContent(markdownText) {
console.log("updateContent called with:", markdownText);
// Use marked.js to parse Markdown to HTML
var htmlContent = marked.parse(markdownText);
// Set the rendered HTML to the content div
document.getElementById('content').innerHTML = htmlContent;
// Re-run Prism.js to highlight new content
Prism.highlightAllUnder(document.getElementById('content'));
// Typeset math equations using MathJax
if (typeof MathJax !== 'undefined') {
MathJax.typesetPromise();
}
// Add copy buttons to code blocks
addCopyButtons();
// Call the function to enable tooltips if needed
enableTooltips();
}
// Function to add copy buttons to code blocks
function addCopyButtons() {
var codeBlocks = document.querySelectorAll('pre');
codeBlocks.forEach(function(pre, index) {
// Create a unique ID
var codeBlockId = 'code-block-' + index;
pre.setAttribute('data-code-id', codeBlockId);
// Check if copy button already exists to avoid duplicates
if (!pre.querySelector('.copy-button')) {
// Create the copy button
var button = document.createElement('button');
button.className = 'copy-button';
button.setAttribute('data-copy-target', codeBlockId);
button.textContent = 'Copy';
// Position the button inside the pre element
pre.insertBefore(button, pre.firstChild);
}
});
// Attach event listeners for copy buttons
addCopyButtonListeners();
}
// Function to add event listeners to copy buttons
function addCopyButtonListeners() {
var copyButtons = document.querySelectorAll('.copy-button');
copyButtons.forEach(function(button) {
button.addEventListener('click', function() {
var targetId = this.getAttribute('data-copy-target');
var codeBlock = document.querySelector('pre[data-code-id="' + targetId + '"] code');
if (codeBlock) {
var codeText = codeBlock.textContent;
// Copy the code text to the clipboard
copyToClipboard(codeText);
// Optionally, provide feedback to the user
this.textContent = 'Copied!';
var that = this;
setTimeout(function() {
that.textContent = 'Copy';
}, 2000);
}
});
});
}
// Function to copy text to the clipboard with fallback
function copyToClipboard(text) {
if (navigator.clipboard && window.isSecureContext) {
// navigator.clipboard API method
navigator.clipboard.writeText(text).catch(function(err) {
console.error('Failed to copy!', err);
});
} else {
// Fallback method
let textArea = document.createElement("textarea");
textArea.value = text;
// Make the textarea out of viewport
textArea.style.position = "absolute";
textArea.style.left = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
}
// Function to enable tooltips (optional)
function enableTooltips() {
// Implement any tooltip functionality if required
}
// Example of how to use the updateContent function on page load
// You can remove this if not needed
document.addEventListener("DOMContentLoaded", function() {
updateContent('# Welcome to Markiva!\n\nStart typing your Markdown content!');
});
</script>
</body>
</html>