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
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>高级 Markdown 编辑器</title> <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <style> :root { --primary-color: #4285f4; --border-color: #dadce0; } body { margin: 0; padding: 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; height: 100vh; display: grid; grid-template-rows: auto 1fr auto; } .toolbar { padding: 8px; border-bottom: 1px solid var(--border-color); display: flex; gap: 5px; flex-wrap: wrap; } .toolbar button { background: white; border: 1px solid var(--border-color); border-radius: 4px; padding: 6px 10px; cursor: pointer; } .editor-container { display: grid; grid-template-columns: 1fr 10px 1fr; height: 100%; } .editor-pane { overflow: auto; display: flex; flex-direction: column; } #editor { width: 100%; height: 100%; border: none; padding: 15px; font-family: 'Consolas', monospace; resize: none; outline: none; font-size: 14px; line-height: 1.5; } #preview { padding: 15px; overflow: auto; } .resize-handle { background: #f1f3f4; cursor: col-resize; } .status-bar { padding: 5px 10px; border-top: 1px solid var(--border-color); font-size: 12px; } #file-input, #image-upload { display: none; } </style> </head> <body> <div class="toolbar"> <button onclick="insertText('**', '**')"><i class="fas fa-bold"></i></button> <button onclick="insertText('*', '*')"><i class="fas fa-italic"></i></button> <button onclick="insertText('# ', '')"><i class="fas fa-heading"></i></button> <button onclick="insertText('[', '](url)')"><i class="fas fa-link"></i></button> <button onclick="document.getElementById('image-upload').click()"><i class="fas fa-image"></i></button> <button onclick="insertText('```\n', '\n```')"><i class="fas fa-code"></i></button> <div style="flex-grow: 1;"></div> <button onclick="saveAsMd()"><i class="fas fa-save"></i> 保存为.md</button> <button onclick="document.getElementById('file-input').click()"><i class="fas fa-folder-open"></i> 打开</button> </div> <div class="editor-container"> <div class="editor-pane"> <textarea id="editor"># Markdown 编辑器
这是一个左右分屏的Markdown编辑器。
- 左边编辑源代码 - 右边实时预览
**功能特点**: 1. 可保存为.md文件 2. 图片上传功能 3. 语法高亮</textarea> </div> <div class="resize-handle" id="resize-handle"></div> <div class="editor-pane"> <div id="preview"></div> </div> </div> <div class="status-bar"> <span id="status">就绪</span> </div> <input type="file" id="file-input" accept=".md,.markdown,.txt"> <input type="file" id="image-upload" accept="image/*"> <script> const editor = document.getElementById('editor'); const preview = document.getElementById('preview'); const resizeHandle = document.getElementById('resize-handle'); const fileInput = document.getElementById('file-input'); const imageUpload = document.getElementById('image-upload'); const statusEl = document.getElementById('status'); // 配置marked marked.setOptions({ breaks: true, gfm: true, highlight: (code, lang) => { if (Prism.languages[lang]) { return Prism.highlight(code, Prism.languages[lang], lang); } return code; } }); // 初始渲染 updatePreview(); // 实时预览 editor.addEventListener('input', updatePreview); // 分屏调整 let isResizing = false; resizeHandle.addEventListener('mousedown', () => { isResizing = true; document.body.style.cursor = 'col-resize'; }); document.addEventListener('mousemove', (e) => { if (!isResizing) return; const container = document.querySelector('.editor-container'); const containerRect = container.getBoundingClientRect(); const leftWidth = e.clientX - containerRect.left; container.style.gridTemplateColumns = `${leftWidth}px 10px 1fr`; }); document.addEventListener('mouseup', () => { isResizing = false; document.body.style.cursor = ''; }); // 打开文件 fileInput.addEventListener('change', (e) => { const file = e.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = (event) => { editor.value = event.target.result; updatePreview(); showStatus(`已加载: ${file.name}`); }; reader.readAsText(file); }); // 图片上传处理 let imageUploadPath = ''; // 在实际应用中替换为您的图片上传API imageUpload.addEventListener('change', (e) => { const file = e.target.files[0]; if (!file) return; // 实际应用中应该上传到服务器并返回URL // 这里模拟上传后返回固定URL const fileName = `image-${Date.now()}.${file.name.split('.').pop()}`; const imageUrl = imageUploadPath ? `${imageUploadPath}/${fileName}` : fileName; insertText(``); showStatus(`已插入图片: ${fileName}`); imageUpload.value = ''; }); // 保存为.md文件 function saveAsMd() { const content = editor.value; const blob = new Blob([content], { type: 'text/markdown' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `markdown-${new Date().toISOString().slice(0,10)}.md`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); showStatus('已保存为.md文件'); } // 更新预览 function updatePreview() { preview.innerHTML = marked.parse(editor.value); } // 插入文本 function insertText(prefix, suffix = '') { const start = editor.selectionStart; const end = editor.selectionEnd; const selected = editor.value.substring(start, end); const before = editor.value.substring(0, start); const after = editor.value.substring(end); editor.value = before + prefix + selected + suffix + after; editor.focus(); editor.selectionStart = start + prefix.length; editor.selectionEnd = end + prefix.length; updatePreview(); } // 显示状态 function showStatus(message) { statusEl.textContent = message; setTimeout(() => statusEl.textContent = '就绪', 3000); } </script> </body> </html>
|