Spaces:
Running
Running
muralipala1504 commited on
Commit ·
cffbc30
1
Parent(s): 3456720
Add Prism.js syntax highlighting and copy button with improved styling
Browse files- app.js +53 -40
- index.html +43 -3
app.js
CHANGED
|
@@ -1,45 +1,58 @@
|
|
| 1 |
-
|
| 2 |
-
const
|
| 3 |
-
const
|
|
|
|
| 4 |
|
| 5 |
-
form.addEventListener('submit', async (e) => {
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
});
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
-
|
| 23 |
-
appendMessage('Deepshell', data.output || 'No response');
|
| 24 |
-
} catch (err) {
|
| 25 |
-
appendMessage('Error', `Network error: ${err.message}`);
|
| 26 |
-
}
|
| 27 |
-
});
|
| 28 |
|
| 29 |
-
function appendMessage(sender, message) {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
+
const form = document.getElementById('chat-form');
|
| 3 |
+
const input = document.getElementById('chat-input');
|
| 4 |
+
const output = document.getElementById('chat-output');
|
| 5 |
|
| 6 |
+
form.addEventListener('submit', async (e) => {
|
| 7 |
+
e.preventDefault();
|
| 8 |
+
const command = input.value.trim();
|
| 9 |
+
if (!command) return;
|
| 10 |
|
| 11 |
+
appendMessage('User', command);
|
| 12 |
+
input.value = '';
|
| 13 |
+
try {
|
| 14 |
+
const response = await fetch('/run-agent', {
|
| 15 |
+
method: 'POST',
|
| 16 |
+
headers: { 'Content-Type': 'application/json' },
|
| 17 |
+
body: JSON.stringify({ prompt: command }),
|
| 18 |
+
});
|
| 19 |
+
if (!response.ok) {
|
| 20 |
+
appendMessage('Error', `Server error: ${response.statusText}`);
|
| 21 |
+
return;
|
| 22 |
+
}
|
| 23 |
+
const data = await response.json();
|
| 24 |
+
appendMessage('Deepshell', data.output || 'No response');
|
| 25 |
+
Prism.highlightAll(); // Highlight newly added code blocks
|
| 26 |
+
} catch (err) {
|
| 27 |
+
appendMessage('Error', `Network error: ${err.message}`);
|
| 28 |
}
|
| 29 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
function appendMessage(sender, message) {
|
| 32 |
+
const msgDiv = document.createElement('div');
|
| 33 |
+
msgDiv.classList.add('message');
|
| 34 |
+
|
| 35 |
+
// Detect code block markdown ```lang ... ```
|
| 36 |
+
const codeBlockMatch = message.match(/```(\w+)?\n([\s\S]*?)```/);
|
| 37 |
+
if (codeBlockMatch) {
|
| 38 |
+
const lang = codeBlockMatch[1] || 'bash';
|
| 39 |
+
const code = codeBlockMatch[2];
|
| 40 |
+
msgDiv.innerHTML = `<strong>${sender}:</strong><pre><code class="language-${lang}">${escapeHtml(code)}</code></pre>`;
|
| 41 |
+
} else {
|
| 42 |
+
msgDiv.innerHTML = `<strong>${sender}:</strong> <pre>${escapeHtml(message)}</pre>`;
|
| 43 |
+
}
|
| 44 |
|
| 45 |
+
output.appendChild(msgDiv);
|
| 46 |
+
output.scrollTop = output.scrollHeight;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
function escapeHtml(text) {
|
| 50 |
+
return text.replace(/[&<>"']/g, (m) => ({
|
| 51 |
+
'&': '&',
|
| 52 |
+
'<': '<',
|
| 53 |
+
'>': '>',
|
| 54 |
+
'"': '"',
|
| 55 |
+
"'": ''',
|
| 56 |
+
})[m]);
|
| 57 |
+
}
|
| 58 |
+
});
|
index.html
CHANGED
|
@@ -4,10 +4,13 @@
|
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
<title>Deepshell Chat UI</title>
|
| 7 |
-
<!-- Updated CSS path -->
|
| 8 |
<link rel="stylesheet" href="/static/services.css" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
<style>
|
| 10 |
-
/* Minimal inline styles in case services.css is missing */
|
| 11 |
body {
|
| 12 |
font-family: Arial, sans-serif;
|
| 13 |
background: #f5f5f5;
|
|
@@ -60,6 +63,38 @@
|
|
| 60 |
button:hover {
|
| 61 |
background: #0056b3;
|
| 62 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
</style>
|
| 64 |
</head>
|
| 65 |
<body>
|
|
@@ -71,7 +106,12 @@
|
|
| 71 |
<button type="submit">Send</button>
|
| 72 |
</form>
|
| 73 |
</div>
|
| 74 |
-
|
| 75 |
<script src="/static/app.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
</body>
|
| 77 |
</html>
|
|
|
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
<title>Deepshell Chat UI</title>
|
|
|
|
| 7 |
<link rel="stylesheet" href="/static/services.css" />
|
| 8 |
+
|
| 9 |
+
<!-- Prism CSS -->
|
| 10 |
+
<link href="https://cdn.jsdelivr.net/npm/prismjs/themes/prism.css" rel="stylesheet" />
|
| 11 |
+
<link href="https://cdn.jsdelivr.net/npm/prismjs/plugins/toolbar/prism-toolbar.min.css" rel="stylesheet" />
|
| 12 |
+
|
| 13 |
<style>
|
|
|
|
| 14 |
body {
|
| 15 |
font-family: Arial, sans-serif;
|
| 16 |
background: #f5f5f5;
|
|
|
|
| 63 |
button:hover {
|
| 64 |
background: #0056b3;
|
| 65 |
}
|
| 66 |
+
|
| 67 |
+
/* Prism copy button styling - always visible */
|
| 68 |
+
.prism-toolbar {
|
| 69 |
+
background: #007bff !important;
|
| 70 |
+
border-radius: 4px;
|
| 71 |
+
padding: 4px 8px;
|
| 72 |
+
top: 8px !important;
|
| 73 |
+
right: 8px !important;
|
| 74 |
+
opacity: 1 !important;
|
| 75 |
+
transition: none !important;
|
| 76 |
+
z-index: 10;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
.prism-toolbar:hover {
|
| 80 |
+
opacity: 1 !important;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.prism-toolbar > button {
|
| 84 |
+
color: white !important;
|
| 85 |
+
font-weight: bold;
|
| 86 |
+
font-size: 0.9rem;
|
| 87 |
+
border: none;
|
| 88 |
+
background: transparent;
|
| 89 |
+
cursor: pointer;
|
| 90 |
+
padding: 0;
|
| 91 |
+
margin: 0;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
.prism-toolbar > button:focus {
|
| 95 |
+
outline: 2px solid #fff;
|
| 96 |
+
outline-offset: 2px;
|
| 97 |
+
}
|
| 98 |
</style>
|
| 99 |
</head>
|
| 100 |
<body>
|
|
|
|
| 106 |
<button type="submit">Send</button>
|
| 107 |
</form>
|
| 108 |
</div>
|
| 109 |
+
|
| 110 |
<script src="/static/app.js"></script>
|
| 111 |
+
|
| 112 |
+
<!-- Prism JS -->
|
| 113 |
+
<script src="https://cdn.jsdelivr.net/npm/prismjs/prism.js"></script>
|
| 114 |
+
<script src="https://cdn.jsdelivr.net/npm/prismjs/plugins/toolbar/prism-toolbar.min.js"></script>
|
| 115 |
+
<script src="https://cdn.jsdelivr.net/npm/prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
|
| 116 |
</body>
|
| 117 |
</html>
|