Skip to content

Commit

Permalink
feat: webui supports math in markdown (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Dec 5, 2024
1 parent 4399faf commit 5f7edc0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
30 changes: 17 additions & 13 deletions assets/arena.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<title>AIChat LLM Arena</title>
<link rel="stylesheet" href="//unpkg.com/[email protected]/dist/katex.min.css">
<link rel="stylesheet" href="//unpkg.com/[email protected]/github-markdown.css">
<link rel="stylesheet" href="//unpkg.com/@highlightjs/[email protected]/styles/github-dark.min.css"
media="screen and (prefers-color-scheme: dark)">
<link rel="stylesheet" href="//unpkg.com/@highlightjs/[email protected]/styles/github.min.css"
media="screen and (prefers-color-scheme: light)">
<script src="//unpkg.com/@highlightjs/[email protected]/highlight.min.js"></script>
<script src="//unpkg.com/[email protected]/lib/marked.umd.js" defer></script>
<script src="//unpkg.com/@highlightjs/[email protected]/highlight.min.js" defer></script>
<script src="//unpkg.com/[email protected]/lib/marked.umd.js" defer></script>
<script src="//unpkg.com/[email protected]/dist/katex.min.js" defer></script>
<script src="//unpkg.com/@sigodenjs/[email protected]/lib/index.umd.js" defer></script>
<script src="//unpkg.com/[email protected]/dist/cdn.min.js" defer></script>
<style>
:root {
Expand Down Expand Up @@ -989,24 +992,25 @@
}

function setupMarked() {
const renderer = new marked.Renderer();
renderer.code = (code, language) => {
const validLang = !!(language && hljs.getLanguage(language));

const highlighted = validLang
? hljs.highlight(code, { language }).value
: escapeForHTML(code);

return `<div class="code-block">
<pre><code class="hljs ${language}">${highlighted}</code></pre>
const renderer = {
code({ text, lang }) {
const validLang = !!(lang && hljs.getLanguage(lang));
const highlighted = validLang
? hljs.highlight(text, { language: lang }).value
: escapeForHTML(text);

return `<div class="code-block">
<pre><code class="hljs ${lang}">${highlighted}</code></pre>
<div class="copy-code-btn" @click="handleCopyCode" title="Copy code">
<svg fill="currentColor" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M4 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2zm2-1a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 5a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-1h1v1a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h1v1z"/>
</svg>
</div>
</div>`;
}
};
marked.setOptions({ renderer });
marked.use({ renderer });
marked.use(markedKatex({ throwOnError: false, inlineTolerantNoSpace: true }));
}

function escapeForHTML(input) {
Expand Down
30 changes: 17 additions & 13 deletions assets/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<title>AIChat LLM Playground</title>
<link rel="stylesheet" href="//unpkg.com/[email protected]/dist/katex.min.css">
<link rel="stylesheet" href="//unpkg.com/[email protected]/github-markdown.css">
<link rel="stylesheet" href="//unpkg.com/@highlightjs/[email protected]/styles/github-dark.min.css"
media="screen and (prefers-color-scheme: dark)">
<link rel="stylesheet" href="//unpkg.com/@highlightjs/[email protected]/styles/github.min.css"
media="screen and (prefers-color-scheme: light)">
<script src="//unpkg.com/@highlightjs/[email protected]/highlight.min.js"></script>
<script src="//unpkg.com/[email protected]/lib/marked.umd.js" defer></script>
<script src="//unpkg.com/@highlightjs/[email protected]/highlight.min.js" defer></script>
<script src="//unpkg.com/[email protected]/lib/marked.umd.js" defer></script>
<script src="//unpkg.com/[email protected]/dist/katex.min.js" defer></script>
<script src="//unpkg.com/@sigodenjs/[email protected]/lib/index.umd.js" defer></script>
<script src="//unpkg.com/[email protected]/dist/cdn.min.js" defer></script>
<style>
:root {
Expand Down Expand Up @@ -1470,24 +1473,25 @@
}

function setupMarked() {
const renderer = new marked.Renderer();
renderer.code = (code, language) => {
const validLang = !!(language && hljs.getLanguage(language));

const highlighted = validLang
? hljs.highlight(code, { language }).value
: escapeForHTML(code);

return `<div class="code-block">
<pre><code class="hljs ${language}">${highlighted}</code></pre>
const renderer = {
code({ text, lang }) {
const validLang = !!(lang && hljs.getLanguage(lang));
const highlighted = validLang
? hljs.highlight(text, { language: lang }).value
: escapeForHTML(text);

return `<div class="code-block">
<pre><code class="hljs ${lang}">${highlighted}</code></pre>
<div class="copy-code-btn" @click="handleCopyCode" title="Copy code">
<svg fill="currentColor" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M4 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2zm2-1a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 5a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-1h1v1a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h1v1z"/>
</svg>
</div>
</div>`;
}
};
marked.setOptions({ renderer });
marked.use({ renderer });
marked.use(markedKatex({ throwOnError: false, inlineTolerantNoSpace: true }));
}

function escapeForHTML(input) {
Expand Down

0 comments on commit 5f7edc0

Please sign in to comment.