Skip to content

Distribute and run LLMs with a single file.

Further improve markdown to html

Justine Tunney89154323633a
diff --git a/llamafile/server/www/chatbot.css b/llamafile/server/www/chatbot.css
index 75d09dd9..27f7afc4 100644
--- a/llamafile/server/www/chatbot.css
+++ b/llamafile/server/www/chatbot.css
@@ -114,23 +114,23 @@ p {
}
.stop-button {
- padding: 0.75rem 1.5rem;
- background: #dc3545;
- color: white;
- border: none;
- border-radius: 6px;
- cursor: pointer;
- font-size: 1rem;
- transition: background-color 0.2s;
+ padding: 0.75rem 1.5rem;
+ background: #dc3545;
+ color: white;
+ border: none;
+ border-radius: 6px;
+ cursor: pointer;
+ font-size: 1rem;
+ transition: background-color 0.2s;
}
.stop-button:hover {
- background: #bb2d3b;
+ background: #bb2d3b;
}
.stop-button:disabled {
- background: #6c757d;
- cursor: not-allowed;
+ background: #6c757d;
+ cursor: not-allowed;
}
.message pre {
@@ -139,6 +139,31 @@ p {
padding: 0.5rem;
border-radius: 4px;
overflow-x: auto;
+ position: relative;
+}
+
+.copy-button {
+ position: absolute;
+ top: 0.5rem;
+ right: 0.5rem;
+ padding: 0.25rem;
+ background: #fff;
+ border: 1px solid #dee2e6;
+ border-radius: 4px;
+ cursor: pointer;
+ opacity: 0.8;
+ transition: opacity 0.2s;
+ z-index: 1;
+}
+
+.copy-button:hover {
+ opacity: 1;
+}
+
+.copy-button svg {
+ width: 16px;
+ height: 16px;
+ display: block;
}
.typing-indicator {
diff --git a/llamafile/server/www/clipboard.js b/llamafile/server/www/clipboard.js
new file mode 100644
index 00000000..56b4957b
--- /dev/null
+++ b/llamafile/server/www/clipboard.js
@@ -0,0 +1,40 @@
+// Copyright 2024 Mozilla Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+function copyTextToClipboard(text) {
+ // see https://stackoverflow.com/a/30810322/1653720
+ if (navigator.clipboard) {
+ navigator.clipboard.writeText(text).then(function() {
+ }, function(err) {
+ console.error('could not copy text: ', err);
+ });
+ } else {
+ var textArea = document.createElement('textarea');
+ textArea.value = text;
+ textArea.style.top = '0';
+ textArea.style.left = '0';
+ textArea.style.position = 'fixed';
+ document.body.appendChild(textArea);
+ textArea.focus();
+ textArea.select();
+ try {
+ if (!document.execCommand('copy')) {
+ console.log('document.execCommand failed');
+ }
+ } catch (err) {
+ console.error('document.execCommand raised: ', err);
+ }
+ document.body.removeChild(textArea);
+ }
+}
diff --git a/llamafile/server/www/highlight_js.js b/llamafile/server/www/highlight_js.js
index 28d5d6ce..004ff6be 100644
--- a/llamafile/server/www/highlight_js.js
+++ b/llamafile/server/www/highlight_js.js
@@ -222,11 +222,11 @@ class HighlightJs extends Highlighter {
this.push("span", "string");
this.append('`');
this.expect = HighlightJs.EXPECT_OPERATOR;
- } else if (c == '{' && this.nest) {
+ } else if (c == '{' && this.nest.length) {
this.expect = HighlightJs.EXPECT_VALUE;
this.append('{');
this.nest.push(HighlightJs.NORMAL);
- } else if (c == '}' && this.nest) {
+ } else if (c == '}' && this.nest.length) {
if ((this.state = this.nest.pop()) != HighlightJs.NORMAL)
this.push("span", "string");
this.append('}');
diff --git a/llamafile/server/www/highlight_markdown.js b/llamafile/server/www/highlight_markdown.js
index 129392c0..9ca50ad7 100644
--- a/llamafile/server/www/highlight_markdown.js
+++ b/llamafile/server/www/highlight_markdown.js
@@ -22,26 +22,28 @@ class HighlightMarkdown extends Highlighter {
static CODE_TICK = 5;
static CODE_TICK_TICK = 6;
static STAR = 7;
- static STRONG = 8;
- static STRONG_BACKSLASH = 9;
- static STRONG_STAR = 10;
- static BACKSLASH = 11;
- static INCODE = 12;
- static INCODE2 = 13;
- static INCODE2_TICK = 14;
- static EMPHASIS = 15;
- static EMPHASIS_BACKSLASH = 16;
- static LANG2 = 17;
- static NEWLINE = 18;
- static EAT_NEWLINE = 19;
- static INCODE2_TICK2 = 20;
- static LSB = 21;
- static LSB_BACKSLASH = 22;
- static LSB_RSB = 23;
- static LSB_RSB_LPAREN = 24;
- static LSB_RSB_LPAREN_BACKSLASH = 25;
- static LT = 26;
- static LT_BACKSLASH = 27;
+ static EMPHASIS_STAR = 8;
+ static STRONG_STAR = 9;
+ static BACKSLASH = 10;
+ static INCODE = 11;
+ static INCODE2 = 12;
+ static INCODE2_TICK = 13;
+ static LANG2 = 14;
+ static NEWLINE = 15;
+ static EAT_NEWLINE = 16;
+ static INCODE2_TICK2 = 17;
+ static LSB = 18;
+ static LSB_BACKSLASH = 19;
+ static LSB_RSB = 20;
+ static LSB_RSB_LPAREN = 21;
+ static LSB_RSB_LPAREN_BACKSLASH = 22;
+ static LAB = 23;
+ static LAB_BACKSLASH = 24;
+ static HYPHEN = 25;
+
+ static STYLE_STRONG = 0;
+ static STYLE_EMPHASIS = 1;
+ static STYLE_LIST = 2;
constructor(delegate) {
super(delegate);
@@ -50,8 +52,37 @@ class HighlightMarkdown extends Highlighter {
this.lang = '';
this.highlighter = null;
this.newlines = 0;
+ this.spaces = 0;
this.text = '';
this.href = '';
+ this.style = [];
+ }
+
+ inside(style) {
+ return this.style.length && this.style[this.style.length - 1] == style;
+ }
+
+ li(c) {
+ if (this.inside(HighlightMarkdown.STYLE_LIST)) {
+ this.pop();
+ this.push('li', '');
+ } else if (!this.style.length) {
+ this.style.push(HighlightMarkdown.STYLE_LIST);
+ this.push('li', '');
+ } else {
+ this.append(c);
+ }
+ this.bol = false;
+ }
+
+ got() {
+ if (this.bol) {
+ this.bol = false;
+ if (this.spaces < 2 && this.inside(HighlightMarkdown.STYLE_LIST)) {
+ this.pop();
+ this.style.pop();
+ }
+ }
}
feed(input) {
@@ -65,28 +96,38 @@ class HighlightMarkdown extends Highlighter {
this.state = HighlightMarkdown.TICK;
break;
} else if (c == '*') {
- this.state = HighlightMarkdown.STAR;
+ if (this.inside(HighlightMarkdown.STYLE_EMPHASIS)) {
+ this.state = HighlightMarkdown.EMPHASIS_STAR;
+ } else if (this.inside(HighlightMarkdown.STYLE_STRONG)) {
+ this.state = HighlightMarkdown.STRONG_STAR;
+ } else {
+ this.state = HighlightMarkdown.STAR;
+ }
break;
} else if (c == '[') {
this.state = HighlightMarkdown.LSB;
} else if (c == '<') {
- this.state = HighlightMarkdown.LT;
+ this.state = HighlightMarkdown.LAB;
} else if (c == '\\') {
- // handle \*\*not bold\*\* etc.
this.state = HighlightMarkdown.BACKSLASH;
- this.bol = false;
+ } else if (c == '-' && this.bol) {
+ this.state = HighlightMarkdown.HYPHEN;
} else if (c == '\n') {
this.bol = true;
this.tail = false;
this.state = HighlightMarkdown.NEWLINE;
this.newlines = 1;
+ this.spaces = 0;
break;
} else {
this.append(c);
}
this.tail = true;
- if (!isblank(c))
- this.bol = false;
+ if (isblank(c)) {
+ ++this.spaces;
+ } else {
+ this.got();
+ }
break;
case HighlightMarkdown.NEWLINE:
@@ -94,6 +135,10 @@ class HighlightMarkdown extends Highlighter {
++this.newlines;
} else {
if (this.newlines >= 2) {
+ if (this.inside(HighlightMarkdown.STYLE_LIST)) {
+ this.pop();
+ this.style.pop();
+ }
this.push('p', '');
this.pop();
} else {
@@ -103,6 +148,17 @@ class HighlightMarkdown extends Highlighter {
}
break;
+ case HighlightMarkdown.HYPHEN:
+ if (isblank(c)) {
+ this.li('-');
+ this.append(c);
+ this.state = HighlightMarkdown.NORMAL;
+ } else {
+ this.append('-');
+ this.epsilon(HighlightMarkdown.NORMAL);
+ }
+ break;
+
case HighlightMarkdown.BACKSLASH:
this.append(c);
this.state = HighlightMarkdown.NORMAL;
@@ -111,74 +167,53 @@ class HighlightMarkdown extends Highlighter {
case HighlightMarkdown.STAR:
if (c == '*') {
// handle **strong** text
- this.state = HighlightMarkdown.STRONG;
- this.push('span', 'bold');
+ this.push('strong', '');
+ this.state = HighlightMarkdown.NORMAL;
+ this.style.push(HighlightMarkdown.STYLE_STRONG);
} else if (this.bol && isblank(c)) {
- this.append('*');
+ // * handle list item
+ this.li('*');
this.append(c);
this.state = HighlightMarkdown.NORMAL;
} else {
- // handle *emphasized* text
- // inverted because \e[3m has a poorly supported western bias
- this.push('span', 'italic');
- if (c == '\\') {
- this.state = HighlightMarkdown.EMPHASIS_BACKSLASH;
- } else {
- this.state = HighlightMarkdown.EMPHASIS;
- this.append(c);
- }
+ // handle *emphasis* text
+ this.got();
+ this.push('em', '');
+ this.style.push(HighlightMarkdown.STYLE_EMPHASIS);
+ this.epsilon(HighlightMarkdown.NORMAL);
}
- this.bol = false;
break;
- case HighlightMarkdown.EMPHASIS:
- // this is for *emphasized* text
+ case HighlightMarkdown.EMPHASIS_STAR:
if (c == '*') {
+ // handle *italic **strong** text*
+ this.push('strong', '');
this.state = HighlightMarkdown.NORMAL;
- this.pop();
- } else if (c == '\\') {
- this.state = HighlightMarkdown.EMPHASIS_BACKSLASH;
+ this.style.push(HighlightMarkdown.STYLE_STRONG);
} else {
- this.append(c);
- }
- break;
-
- case HighlightMarkdown.EMPHASIS_BACKSLASH:
- // so we can say *unbroken \* italic* and have it work
- this.append(c);
- this.state = HighlightMarkdown.EMPHASIS;
- break;
-
- case HighlightMarkdown.STRONG:
- if (c == '*') {
- this.state = HighlightMarkdown.STRONG_STAR;
- } else if (c == '\\') {
- this.state = HighlightMarkdown.STRONG_BACKSLASH;
- } else {
- this.append(c);
+ // leave *italic* text
+ this.style.pop();
+ this.pop();
+ this.epsilon(HighlightMarkdown.NORMAL);
}
break;
- case HighlightMarkdown.STRONG_BACKSLASH:
- // so we can say **unbroken \*\* bold** and have it work
- this.append(c);
- this.state = HighlightMarkdown.STRONG;
- break;
-
case HighlightMarkdown.STRONG_STAR:
if (c == '*') {
this.state = HighlightMarkdown.NORMAL;
+ this.style.pop();
this.pop();
- } else if (c == '\n' && !this.tail) { // handle *** line break
+ } else if (c == '\n' && !this.tail) {
+ // handle *** line break
+ this.style.pop();
this.pop();
this.push('hr', '');
this.pop();
this.epsilon(HighlightMarkdown.NORMAL);
- } else if (c == '\\') {
- this.state = HighlightMarkdown.STRONG_BACKSLASH;
} else {
- this.append(c);
- this.state = HighlightMarkdown.STRONG;
+ this.push('em', '');
+ this.style.push(HighlightMarkdown.STYLE_EMPHASIS);
+ this.epsilon(HighlightMarkdown.NORMAL);
}
break;
@@ -195,7 +230,7 @@ class HighlightMarkdown extends Highlighter {
this.append(c);
this.state = HighlightMarkdown.INCODE;
}
- this.bol = false;
+ this.got();
break;
case HighlightMarkdown.INCODE:
@@ -259,7 +294,9 @@ class HighlightMarkdown extends Highlighter {
case HighlightMarkdown.LANG2:
if (c == "\n") {
this.flush();
- let hdom = new HighlightDom(this.push('pre', ''));
+ let pre = this.push('pre', '');
+ this.setupCodeBlock(pre);
+ let hdom = new HighlightDom(pre);
if (!(this.highlighter = Highlighter.create(this.lang, hdom)))
this.highlighter = Highlighter.create('txt', hdom);
this.state = HighlightMarkdown.CODE;
@@ -302,7 +339,7 @@ class HighlightMarkdown extends Highlighter {
}
break;
- case HighlightMarkdown.LT:
+ case HighlightMarkdown.LAB:
if (c == '>') {
let a = this.push('a', '');
a.innerText = this.href;
@@ -311,15 +348,20 @@ class HighlightMarkdown extends Highlighter {
this.href = '';
this.state = HighlightMarkdown.NORMAL;
} else if (c == '\\') {
- this.state = HighlightMarkdown.LT_BACKSLASH;
+ this.state = HighlightMarkdown.LAB_BACKSLASH;
} else {
this.href += c;
+ if (this.href.length <= 4 && !"http".startsWith(this.href)) {
+ this.append('<' + this.href);
+ this.href = '';
+ this.state = HighlightMarkdown.NORMAL;
+ }
}
break;
- case HighlightMarkdown.LT_BACKSLASH:
+ case HighlightMarkdown.LAB_BACKSLASH:
this.href += c;
- this.state = HighlightMarkdown.LT;
+ this.state = HighlightMarkdown.LAB;
break;
case HighlightMarkdown.LSB:
@@ -369,7 +411,7 @@ class HighlightMarkdown extends Highlighter {
break;
default:
- throw new Error('Invalid state');
+ throw new Error('Invalid state: ' + this.state);
}
}
}
@@ -392,11 +434,6 @@ class HighlightMarkdown extends Highlighter {
case HighlightMarkdown.INCODE2:
case HighlightMarkdown.INCODE2_TICK:
case HighlightMarkdown.INCODE2_TICK2:
- case HighlightMarkdown.STRONG:
- case HighlightMarkdown.STRONG_BACKSLASH:
- case HighlightMarkdown.STRONG_STAR:
- case HighlightMarkdown.EMPHASIS:
- case HighlightMarkdown.EMPHASIS_BACKSLASH:
this.pop();
break;
case HighlightMarkdown.CODE:
@@ -416,8 +453,8 @@ class HighlightMarkdown extends Highlighter {
this.highlighter = null;
this.pop();
break;
- case HighlightMarkdown.LT:
- case HighlightMarkdown.LT_BACKSLASH:
+ case HighlightMarkdown.LAB:
+ case HighlightMarkdown.LAB_BACKSLASH:
this.append('<' + this.href);
this.href = '';
break;
@@ -439,13 +476,37 @@ class HighlightMarkdown extends Highlighter {
default:
break;
}
+ while (this.style.length) {
+ this.style.pop();
+ this.pop();
+ }
this.state = HighlightMarkdown.NORMAL;
this.bol = true;
+ this.spaces = 0;
this.tail = false;
this.newlines = 0;
this.delegate.flush();
this.delta = 1;
}
+
+ setupCodeBlock(pre) {
+ const copyButton = document.createElement('button');
+ copyButton.className = 'copy-button';
+ copyButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>`;
+ copyButton.addEventListener('click', function() {
+ try {
+ copyTextToClipboard(pre.innerText);
+ const originalInnerHTML = copyButton.innerHTML;
+ copyButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="green" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>`;
+ setTimeout(() => {
+ copyButton.innerHTML = originalInnerHTML;
+ }, 2000);
+ } catch (err) {
+ console.error('Failed to copy text:', err);
+ }
+ });
+ pre.appendChild(copyButton);
+ }
}
Highlighter.REGISTRY['markdown'] = HighlightMarkdown;
diff --git a/llamafile/server/www/highlight_scala.js b/llamafile/server/www/highlight_scala.js
index a25d36f7..72b0bf19 100644
--- a/llamafile/server/www/highlight_scala.js
+++ b/llamafile/server/www/highlight_scala.js
@@ -107,10 +107,10 @@ class HighlightScala extends Highlighter {
this.append('"');
} else if (c == '@') {
this.state = HighlightScala.ANNOTATION;
- } else if (c == '{' && this.nest) {
+ } else if (c == '{' && this.nest.length) {
this.nest.push(HighlightScala.NORMAL);
this.append('{');
- } else if (c == '}' && this.nest) {
+ } else if (c == '}' && this.nest.length) {
if ((this.state = this.nest.pop()) != HighlightScala.NORMAL)
this.push("span", "string");
this.append('}');
diff --git a/llamafile/server/www/highlight_swift.js b/llamafile/server/www/highlight_swift.js
index 96a48b4a..ce2fb38c 100644
--- a/llamafile/server/www/highlight_swift.js
+++ b/llamafile/server/www/highlight_swift.js
@@ -1017,11 +1017,11 @@ class HighlightSwift extends Highlighter {
} else if (c == '#') {
this.state = HighlightSwift.HASH;
this.hash1 = 1;
- } else if (c == '(' && this.nest) {
+ } else if (c == '(' && this.nest.length) {
this.append('(');
this.nest.push(HighlightSwift.NORMAL);
this.hash.push(0);
- } else if (c == ')' && this.nest) {
+ } else if (c == ')' && this.nest.length) {
this.state = this.nest.pop();
this.hash1 = this.hash.pop();
if (this.state != HighlightSwift.NORMAL)
diff --git a/llamafile/server/www/highlight_typescript.js b/llamafile/server/www/highlight_typescript.js
index 0ba1cdf2..e8f403d2 100644
--- a/llamafile/server/www/highlight_typescript.js
+++ b/llamafile/server/www/highlight_typescript.js
@@ -141,11 +141,11 @@ class HighlightTypescript extends Highlighter {
this.push("span", "string");
this.append('`');
this.expect = HighlightTypescript.EXPECT_OPERATOR;
- } else if (c == '{' && this.nest) {
+ } else if (c == '{' && this.nest.length) {
this.expect = HighlightTypescript.EXPECT_VALUE;
this.append('{');
this.nest.push(HighlightTypescript.NORMAL);
- } else if (c == '}' && this.nest) {
+ } else if (c == '}' && this.nest.length) {
if ((this.state = this.nest.pop()) != HighlightTypescript.NORMAL)
this.push("span", "string");
this.append('}');
diff --git a/llamafile/server/www/index.html b/llamafile/server/www/index.html
index 75854d65..43cad884 100644
--- a/llamafile/server/www/index.html
+++ b/llamafile/server/www/index.html
@@ -28,6 +28,7 @@
</div>
<script src="ctype.js"></script>
+<script src="clipboard.js"></script>
<script src="highlight.js"></script>
<script src="highlight_txt.js"></script>
<script src="highlight_markdown.js"></script>