Skip to content

Commit b3f3a5f

Browse files
committed
use a fallback markdown render when ext like KaTex fail
1 parent c7b3644 commit b3f3a5f

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

llms/ui/ctx.mjs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,15 @@ export class ExtensionScope {
126126
}
127127

128128
export class AppContext {
129-
constructor({ app, routes, ai, fmt, utils, marked }) {
129+
constructor({ app, routes, ai, fmt, utils, marked, markedFallback }) {
130130
this.app = app
131131
this.routes = routes
132132
this.ai = ai
133133
this.fmt = fmt
134134
this.utils = utils
135135
this._components = {}
136136
this.marked = marked
137+
this.markedFallback = markedFallback
137138

138139
this.state = reactive({})
139140
this.events = new EventBus()
@@ -397,7 +398,18 @@ export class AppContext {
397398
const header = content.substring(3, headerEnd).trim()
398399
content = '<div class="frontmatter">' + header + '</div>\n' + content.substring(headerEnd + 3)
399400
}
400-
const html = this.marked.parse(content || '')
401+
let html = content || ''
402+
try {
403+
html = this.marked.parse(content || '')
404+
} catch (e) {
405+
console.log('Failed to parse markdown, using fallback', e)
406+
try {
407+
html = this.markedFallback.parse(content || '')
408+
} catch (e2) {
409+
console.log('Failed to parse markdown, using raw content', e2)
410+
html = content || ''
411+
}
412+
}
401413
return sanitizeHtml(html)
402414
}
403415

llms/ui/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import ChatModule from './modules/chat/index.mjs'
99
import ModelSelectorModule from './modules/model-selector.mjs'
1010
import IconsModule from './modules/icons.mjs'
1111
import { utilsFunctions, utilsFormatters } from './utils.mjs'
12-
import { marked } from './markdown.mjs'
12+
import { marked, markedFallback } from './markdown.mjs'
1313
import { AppContext } from './ctx.mjs'
1414

1515
const Components = {
@@ -35,7 +35,7 @@ export async function createContext() {
3535
const utils = Object.assign({}, utilsFunctions())
3636
const routes = []
3737

38-
const ctx = new AppContext({ app, routes, ai, fmt, utils, marked })
38+
const ctx = new AppContext({ app, routes, ai, fmt, utils, marked, markedFallback })
3939
app.provide('ctx', ctx)
4040
await ctx.init()
4141

llms/ui/markdown.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function createMarked() {
2323
return ret
2424
}
2525

26-
export const marked = createMarked();
26+
export const marked = createMarked()
27+
export const markedFallback = createMarked()
2728

2829
export function renderMarkdown(content) {
2930
if (Array.isArray(content)) {

0 commit comments

Comments
 (0)