Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

Merge pull request #7049 from bytrangle/fix_inconsistent-code-block-formatting

Trang Lebee630c2100f
diff --git a/web-app/package.json b/web-app/package.json
index 9da679e12..4cd1a621c 100644
--- a/web-app/package.json
+++ b/web-app/package.json
@@ -96,6 +96,7 @@
"tw-animate-css": "1.2.8",
"ulidx": "2.4.1",
"unified": "11.0.5",
+ "unist-util-visit": "^5.0.0",
"use-stick-to-bottom": "^1.0.48",
"uuid": "11.1.0",
"vaul": "1.1.2",
@@ -114,9 +115,11 @@
"@types/istanbul-reports": "3.0.4",
"@types/lodash.clonedeep": "4.5.9",
"@types/lodash.debounce": "4.0.9",
+ "@types/mdast": "^4.0.4",
"@types/node": "22.14.1",
"@types/react": "19.1.2",
"@types/react-dom": "19.1.2",
+ "@types/unist": "^3.0.3",
"@vitejs/plugin-react": "4.4.1",
"@vitest/coverage-v8": "3.2.4",
"clsx": "2.1.1",
diff --git a/web-app/src/containers/RenderMarkdown.tsx b/web-app/src/containers/RenderMarkdown.tsx
index 472d3e137..62beb2a49 100644
--- a/web-app/src/containers/RenderMarkdown.tsx
+++ b/web-app/src/containers/RenderMarkdown.tsx
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Components } from 'react-markdown'
import { memo, useMemo } from 'react'
-import { cn } from '@/lib/utils'
+import { cn, disableIndentedCodeBlockPlugin } from '@/lib/utils'
// import 'katex/dist/katex.min.css'
import { defaultRehypePlugins, Streamdown } from 'streamdown'
import { cjk } from '@streamdown/cjk'
@@ -12,6 +12,7 @@ import { mermaid } from '@streamdown/mermaid'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import rehypeKatex from 'rehype-katex'
+import 'katex/dist/katex.min.css'
import { MermaidError } from '@/components/MermaidError'
interface MarkdownProps {
@@ -109,7 +110,7 @@ function RenderMarkdownComponent({
'size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0',
className
)}
- remarkPlugins={[remarkGfm, remarkMath]}
+ remarkPlugins={[remarkGfm, remarkMath, disableIndentedCodeBlockPlugin]}
rehypePlugins={[
rehypeKatex,
defaultRehypePlugins.harden,
diff --git a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
index a88402591..104cff3ea 100644
--- a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
+++ b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
@@ -129,6 +129,104 @@ describe('RenderMarkdown', () => {
})
})
+ it('does not format 4-space indented text as code blocks', () => {
+ const contentWithIndentedText = `Please explain this code block.
+
+ <!DOCTYPE html>
+ <html lang="en">
+ <body>
+ <header>
+ <h1>Welcome to My Website</h1>
+ <p>A sample HTML document showcasing various HTML elements</p>
+ </header>
+
+ <nav>
+ <a href="#home">Home</a>
+ <a href="#about">About</a>
+ <a href="#services">Services</a>
+ <a href="#contact">Contact</a>
+ </nav>
+
+ <div class="container">
+ <section id="home">
+ <h2>Home</h2>
+ <div class="card">
+ <p>This is a sample HTML document that demonstrates various HTML elements and their structure.</p>
+ <p>HTML (HyperText Markup Language) is the standard markup language for creating web pages.</p>
+ </div>
+ </section>
+
+ </div>
+
+ <footer>
+ <p>&copy; ${new Date().getFullYear()} My Sample Website. All rights reserved.</p>
+ </footer>
+ </body>
+ </html>
+ `
+ render(
+ <RenderMarkdown
+ content={contentWithIndentedText}
+ />
+ )
+ const markdownContainer = document.querySelector('.markdown')
+ const html = markdownContainer?.innerHTML || ''
+ expect(html).not.toContain('<pre>')
+ expect(html).toContain('<p>')
+ // Left and right brackets get escaped as &lt; and &gt; respectively
+ expect(html).toContain('&lt;!DOCTYPE html&gt;')
+ })
+
+ it('formats fenced code blocks correctly', async () => {
+ const contentWithFencedCodeBlock = `Please explain this code block.
+
+\`\`\`html
+<!DOCTYPE html>
+<html lang="en">
+<body>
+ <header>
+ <h1>Welcome to My Website</h1>
+ <p>A sample HTML document showcasing various HTML elements</p>
+ </header>
+
+ <nav>
+ <a href="#home">Home</a>
+ <a href="#about">About</a>
+ <a href="#services">Services</a>
+ <a href="#contact">Contact</a>
+ </nav>
+
+ <div class="container">
+ <section id="home">
+ <h2>Home</h2>
+ <div class="card">
+ <p>This is a sample HTML document that demonstrates various HTML elements and their structure.</p>
+ <p>HTML (HyperText Markup Language) is the standard markup language for creating web pages.</p>
+ </div>
+ </section>
+ </div>
+
+ <footer>
+ <p>&copy; ${new Date().getFullYear()} My Sample Website. All rights reserved.</p>
+ </footer>
+</body>
+</html>
+\`\`\`
+`
+ const { container, findByText} = render(
+ <RenderMarkdown
+ content={contentWithFencedCodeBlock}
+ />
+ )
+ // Wait for the code content to be rendered (async operation)
+ await findByText('<!DOCTYPE html>', { exact: false })
+ const markdownContainer = container.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
+ // Check that the code content is present
+ expect(text).toContain('<!DOCTYPE html>')
+ expect(text).toContain('<html lang="en">')
+ expect(text).toContain('Welcome to My Website')
+ })
describe('LaTeX normalization - display math', () => {
it('converts \\[...\\] to $$ display math', () => {
const content = 'Here is math:\n\\[\nx^2 + y^2\n\\]\nDone'
diff --git a/web-app/src/lib/utils.ts b/web-app/src/lib/utils.ts
index 6a4fc92ac..c7eb4af5e 100644
--- a/web-app/src/lib/utils.ts
+++ b/web-app/src/lib/utils.ts
@@ -1,5 +1,8 @@
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
+import type { Node, Position } from 'unist'
+import type { Code, Paragraph, Parent, Text } from 'mdast'
+import { visit } from 'unist-util-visit'
import { ExtensionManager } from './extension'
import path from "path"
@@ -23,6 +26,35 @@ export function basenameNoExt(filePath: string): string {
return base.slice(0, -path.extname(base).length);
}
+/**
+ * Remark plugin that disables indented code block syntax.
+ * Converts indented code blocks (without language specifier) to plain text paragraphs,
+ * while preserving fenced code blocks with backticks.
+ */
+export function disableIndentedCodeBlockPlugin() {
+ return (tree: Node) => {
+ visit(tree, 'code', (node: Code, index, parent: Parent | undefined) => {
+ // Convert indented code blocks (nodes without lang or meta property)
+ // to plain text
+ // Check if the parent exists so we can replace the node safely
+ if (!node.lang && !node.meta && parent && typeof index === 'number') {
+ const nodePosition: Position | undefined = node.position
+ const textNode: Text = {
+ type: 'text',
+ value: node.value,
+ position: nodePosition
+ }
+ const paragraphNode: Paragraph = {
+ type: 'paragraph',
+ children: [textNode],
+ position: nodePosition
+ }
+ parent.children[index] = paragraphNode
+ }
+ })
+ }
+}
+
/**
* Get the display name for a model, falling back to the model ID if no display name is set
*/
diff --git a/yarn.lock b/yarn.lock
index d60faaa09..baef5f6e8 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3609,6 +3609,7 @@ __metadata:
typescript-eslint: "npm:8.31.0"
ulidx: "npm:2.4.1"
unified: "npm:11.0.5"
+ unist-util-visit: "npm:^5.0.0"
uuid: "npm:11.1.0"
vaul: "npm:1.1.2"
vite: "npm:6.3.2"