Skip to content

Distribute and run LLMs with a single file.

Make some markdown improvements

Justine Tunney0965a4b3c699
diff --git a/llamafile/highlight_markdown.cpp b/llamafile/highlight_markdown.cpp
index f3f3c680..33c819f4 100644
--- a/llamafile/highlight_markdown.cpp
+++ b/llamafile/highlight_markdown.cpp
@@ -68,6 +68,8 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {
}
if (c == 0xFEFF)
continue; // utf-8 bom
+ if (!c)
+ c = 0xfffd; // replacement character
switch (t_) {
Normal:
diff --git a/llamafile/server/www/highlight_markdown.js b/llamafile/server/www/highlight_markdown.js
index dba9ca74..7372923d 100644
--- a/llamafile/server/www/highlight_markdown.js
+++ b/llamafile/server/www/highlight_markdown.js
@@ -42,6 +42,8 @@ class HighlightMarkdown extends Highlighter {
for (let i = 0; i < input.length; i += this.delta) {
this.delta = 1;
let c = input[i];
+ if (c == '\u0000')
+ c = '\ufffd';
switch (this.state) {
case HighlightMarkdown.NORMAL:
diff --git a/llamafile/server/www/render_markdown.js b/llamafile/server/www/render_markdown.js
index 07629899..d78643c5 100644
--- a/llamafile/server/www/render_markdown.js
+++ b/llamafile/server/www/render_markdown.js
@@ -223,11 +223,18 @@ class RenderMarkdown extends Highlighter {
const c = input[i];
if (c == '\r')
continue;
+ if (c == '\u0000')
+ c = '\ufffd';
switch (this.state) {
case RenderMarkdown.NORMAL:
- if (this.bol && this.spaces == 4 && this.newlines >= 2 &&
- isgraph(c) && c != '`' && !this.style.length) {
+ if (this.bol &&
+ this.spaces >= 4 &&
+ this.newlines >= 2 &&
+ isgraph(c) &&
+ c != '`' &&
+ (!this.style.length ||
+ this.spaces >= this.style[this.style.length - 1][1] + 4)) {
this.dedent = this.spaces;
this.spaces = 0;
this.bol = false;
@@ -290,7 +297,7 @@ class RenderMarkdown extends Highlighter {
++this.spaces;
this.append(c);
} else if (c == '\t') {
- this.spaces = (this.spaces + 3) & -4;
+ this.spaces = (this.spaces + 4) & -4;
this.append(c);
} else {
this.got();
@@ -306,7 +313,7 @@ class RenderMarkdown extends Highlighter {
} else if (c == ' ') {
++this.spaces;
} else if (c == '\t') {
- this.spaces = (this.spaces + 3) & -4;
+ this.spaces = (this.spaces + 4) & -4;
} else {
if (this.newlines >= 2) {
if (this.style.length &&
@@ -411,6 +418,10 @@ class RenderMarkdown extends Highlighter {
if (ispunct(c)) {
this.append(c);
this.state = RenderMarkdown.NORMAL;
+ } else if (c == '\n') {
+ this.push("BR", "");
+ this.pop();
+ this.state = RenderMarkdown.NORMAL;
} else {
this.append('\\');
this.epsilon(RenderMarkdown.NORMAL);
@@ -639,7 +650,7 @@ class RenderMarkdown extends Highlighter {
if (this.spaces <= this.dedent)
break;
} else if (c == '\t') {
- this.spaces = (this.spaces + 3) & -4;
+ this.spaces = (this.spaces + 4) & -4;
if (this.spaces <= this.dedent)
break;
} else {
@@ -773,7 +784,7 @@ class RenderMarkdown extends Highlighter {
if (this.spaces <= this.dedent)
break;
} else if (c == '\t') {
- this.spaces = (this.spaces + 3) & -4;
+ this.spaces = (this.spaces + 4) & -4;
if (this.spaces <= this.dedent)
break;
} else {
diff --git a/llamafile/server/www/test/test.md b/llamafile/server/www/test/test.md
index 662a44e1..1584a36d 100644
--- a/llamafile/server/www/test/test.md
+++ b/llamafile/server/www/test/test.md
@@ -166,6 +166,106 @@ This program is a simple example of how to use variables, input/output
operations, conditional statements, and loops in FORTRAN66. It
demonstrates basic input/output operations and error handling.
+## Code
+
+~~~
+I can't believe it's not butter!
+~~~
+
+## Tabs
+
+Tabs can make code blocks.
+
+ code block
+
+Tabs round up to 4 when mixed with spaces.
+
+ code block
+
+- list item
+
+ code block under li
+
+## Emphasis
+
+***strong emph***
+***strong** in emph*
+***emph* in strong**
+**in strong *emph***
+*in emph **strong***
+
+The following patterns are less widely supported, but the intent is
+clear and they are useful (especially in contexts like bibliography
+entries):
+
+*emph *with emph* in it*
+**strong **with strong** in it**
+
+Many implementations have also restricted intraword emphasis to
+the `*` forms, to avoid unwanted emphasis in words containing
+internal underscores. (It is best practice to put these in code
+spans, but users often do not.)
+
+internal emphasis: foo*bar*baz
+no emphasis: foo_bar_baz
+
+## Backslash escapes
+
+Any ASCII punctuation character may be backslash-escaped:
+
+\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
+
+```````````````````````````````` example
+<p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
+````````````````````````````````
+
+Backslashes before other characters are treated as literal
+backslashes:
+
+\→\A\a\ \3\φ\«
+
+```````````````````````````````` example
+<p>\→\A\a\ \3\φ\«</p>
+````````````````````````````````
+
+Escaped characters are treated as regular characters and do
+not have their usual Markdown meanings:
+
+\*not emphasized\*
+\<br/> not a tag
+\[not a link](/foo)
+\`not code\`
+1\. not a list
+\* not a list
+\# not a heading
+\[foo]: /url "not a reference"
+\&ouml; not a character entity
+
+```````````````````````````````` example
+<p>*not emphasized*
+&lt;br/&gt; not a tag
+[not a link](/foo)
+`not code`
+1. not a list
+* not a list
+# not a heading
+[foo]: /url &quot;not a reference&quot;
+&amp;ouml; not a character entity</p>
+````````````````````````````````
+
+A backslash at the end of the line is a [hard line break]:
+
+foo\
+bar
+
+```````````````````````````````` example
+foo\
+bar
+.
+<p>foo<br />
+bar</p>
+````````````````````````````````
+
## Rubby
```ruby