Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

refactor(mcp-host): read inline markdown in the dashboard and slides as the documents do

marcoteb5ebba0aceb3e
diff --git a/mcp-host/src/workflow/__tests__/internalTools.dashboard.escaping.test.ts b/mcp-host/src/workflow/__tests__/internalTools.dashboard.escaping.test.ts
index 7209d9164..9e8a43568 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.dashboard.escaping.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.dashboard.escaping.test.ts
@@ -136,6 +136,46 @@ describe('dashboard text outside the data', () => {
})
})
+describe('dashboard inline markdown', () => {
+ const narrative = async (content: string): Promise<string> => {
+ const result = await dashboard.execute(
+ {
+ filename: 'n.html',
+ data: { title: 'T', sections: [{ title: 'S', type: 'narrative', content }] },
+ },
+ dir
+ )
+ expect(result.success, result.error).toBe(true)
+ const html = fs.readFileSync(result.artifact!.path, 'utf8')
+ return html.slice(html.indexOf('class="narrative"'))
+ }
+
+ it('reads the markdown the PDF and DOCX text reads', async () => {
+ const html = await narrative('**Bold** *it* `code` ~~old~~ [docs](https://x.test/a)')
+ expect(html).toContain('<strong>Bold</strong>')
+ expect(html).toContain('<em>it</em>')
+ expect(html).toContain('<code>code</code>')
+ expect(html).toContain('<s>old</s>')
+ expect(html).toContain(
+ '<a href="https:&#x2F;&#x2F;x.test&#x2F;a" rel="noopener noreferrer">docs</a>'
+ )
+ })
+
+ it('prints an escaped mark as the mark, as the documents do', async () => {
+ const html = await narrative('Use \\*args\\* here')
+ expect(html).toContain('Use *args* here')
+ })
+
+ it('links only to web and mail targets, with the target escaped', async () => {
+ const html = await narrative(
+ '<a href="javascript:alert(1)">x</a> [y](https://x.test/"onmouseover=alert(1))'
+ )
+ expect(html).not.toMatch(/javascript:/i)
+ expect(html).not.toMatch(/"onmouseover/i)
+ expect(html).toContain('&quot;onmouseover')
+ })
+})
+
describe('escapeHtml', () => {
it('escapes every character that can open markup or end an attribute', () => {
expect(escapeHtml(`<a href="x" title='y'>&</a>`)).toBe(
diff --git a/mcp-host/src/workflow/__tests__/pptxInput.test.ts b/mcp-host/src/workflow/__tests__/pptxInput.test.ts
index 3b9d38307..b49c85849 100644
--- a/mcp-host/src/workflow/__tests__/pptxInput.test.ts
+++ b/mcp-host/src/workflow/__tests__/pptxInput.test.ts
@@ -10,6 +10,7 @@ import {
readKpis,
readStringList,
readTable,
+ readText,
} from '../pptxInput'
const context = (): ReadContext => ({ outputDir: '/nonexistent', warnings: [] })
@@ -65,6 +66,33 @@ describe('readStringList', () => {
})
})
+describe('readText', () => {
+ const noted = (text: string): boolean => {
+ const ctx = context()
+ readText(text, 'title', 200, ctx)
+ return ctx.warnings.some(w => w.includes('holds markdown or HTML'))
+ }
+
+ it('notes text the document tools would print otherwise', () => {
+ for (const text of [
+ '**Revenue**',
+ '`npm ci`',
+ '~~old~~',
+ '&amp; co',
+ '<b>x</b>',
+ '[d](https://x.test)',
+ ]) {
+ expect(noted(text), text).toBe(true)
+ }
+ })
+
+ it('leaves ordinary text, single asterisks included, alone', () => {
+ for (const text of ['2*3*4 = 24', '*approx*', 'snake_case', 'AT&T', 'a < b > c', 'C:\\dir']) {
+ expect(noted(text), text).toBe(false)
+ }
+ })
+})
+
describe('readEnum', () => {
it('accepts an allowed value and names the field of any other', () => {
expect(readEnum('a', 'f', ['a', 'b'] as const)).toBe('a')
diff --git a/mcp-host/src/workflow/dashboardRender.ts b/mcp-host/src/workflow/dashboardRender.ts
index 1f786ebd1..95900501a 100644
--- a/mcp-host/src/workflow/dashboardRender.ts
+++ b/mcp-host/src/workflow/dashboardRender.ts
@@ -19,6 +19,7 @@ import {
type DashboardThemeColors,
type ThemeName,
} from './dashboardThemes'
+import { inlineSpans } from './inlineMarkup'
import { choose, own } from './ownEntry'
import { headerText, normalizeTableRows } from './tableRows'
import type { InternalToolResult } from './types'
@@ -732,11 +733,25 @@ interface DashRender {
failures: string[]
}
+/**
+ * Inline markdown as HTML, read by the reader the PDF and DOCX text uses, so
+ * the page shows what the secret check before attaching it reads. Every text
+ * is escaped, and a link keeps only an http(s) or mailto target.
+ */
function renderInlineMd(s: string): string {
- return escapeHtml(s)
- .replace(/\*\*([\s\S]+?)\*\*(?!\*)/g, '<strong>$1</strong>')
- .replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, '<em>$1</em>')
- .replace(/`([^`]+)`/g, '<code>$1</code>')
+ return inlineSpans(s)
+ .map(span => {
+ let html = escapeHtml(span.text)
+ if (span.code) html = `<code>${html}</code>`
+ if (span.strike) html = `<s>${html}</s>`
+ if (span.italics) html = `<em>${html}</em>`
+ if (span.bold) html = `<strong>${html}</strong>`
+ if (span.link !== undefined) {
+ html = `<a href="${escapeHtml(span.link)}" rel="noopener noreferrer">${html}</a>`
+ }
+ return html
+ })
+ .join('')
}
function isDashRecord(value: unknown): value is Record<string, unknown> {
diff --git a/mcp-host/src/workflow/pptxInput.ts b/mcp-host/src/workflow/pptxInput.ts
index ba401bad1..392f084fe 100644
--- a/mcp-host/src/workflow/pptxInput.ts
+++ b/mcp-host/src/workflow/pptxInput.ts
@@ -8,6 +8,7 @@
* shortened to its length limit) are made here and reported as warnings.
*/
import { type EmbeddableImage, loadEmbeddableImage } from './embeddedImages'
+import { inlineSpans } from './inlineMarkup'
import { ChartSpecError, type NativeChart, readNativeChart } from './pptxCharts'
import { SLIDE_LAYOUTS, STATUSES } from './pptxVocabulary'
import { headerText, normalizeTableRows } from './tableRows'
@@ -114,12 +115,18 @@ export interface ReadContext {
}
/**
- * Markdown and HTML a model writes into text that a slide prints as it is:
- * **bold**, `code`, [label](url) and <b>, <i>, <strong>, <em> or <br>. A single
- * asterisk is left alone, since 2*3*4 is ordinary text.
+ * Whether `text` holds markdown or HTML a slide would print as written: the
+ * PDF and DOCX tools, which read it, would print something else. A single
+ * asterisk is left out, since 2*3*4 is ordinary text on a slide.
*/
-const MARKUP =
- /\*\*[^*\s][^*]*\*\*|`[^`\n]+`|\[[^[\]\n]+\]\((?:https?:|mailto:)[^)\s]+\)|<\/?(?:b|i|strong|em|br)\b[^<>]*>/i
+function holdsMarkup(text: string): boolean {
+ const t = text.replace(/(?<!\*)\*(?!\*)/g, '\u2217')
+ return (
+ inlineSpans(t)
+ .map(span => span.text)
+ .join('') !== t
+ )
+}
export function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value)
@@ -155,7 +162,7 @@ export function readText(
}
const text = raw.trim()
if (!text) return undefined
- if (!ctx.markupNoted && MARKUP.test(text)) {
+ if (!ctx.markupNoted && holdsMarkup(text)) {
ctx.markupNoted = true
ctx.warnings.push(
`${where} holds markdown or HTML, which slides print as written; PPTX text is plain, ` +