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.

fix: render code block without lang spec correcty (#7741)

cs-cateff5a089d5d0
diff --git a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
index 104cff3ea..d0dcd87ac 100644
--- a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
+++ b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
@@ -222,11 +222,67 @@ describe('RenderMarkdown', () => {
await findByText('<!DOCTYPE html>', { exact: false })
const markdownContainer = container.querySelector('.markdown')
const text = markdownContainer?.textContent || ''
+ const codeBlock = container.querySelector('[data-streamdown="code-block"]')
// 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')
+ expect(codeBlock).toBeTruthy()
})
+
+ it('formats fenced code blocks without lang spec correctly', async () => {
+ const contentWithFencedCodeBlock = `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>
+\`\`\`
+`
+ 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 || ''
+ const codeBlock = container.querySelector('[data-streamdown="code-block"]')
+ // 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')
+ expect(codeBlock).toBeTruthy()
+ })
+
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 992c8b273..d9df6eef3 100644
--- a/web-app/src/lib/utils.ts
+++ b/web-app/src/lib/utils.ts
@@ -5,6 +5,7 @@ import type { Code, Paragraph, Parent, Text } from 'mdast'
import { visit } from 'unist-util-visit'
import { ExtensionManager } from './extension'
import path from 'path'
+import type { VFile } from 'vfile'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
@@ -27,28 +28,30 @@ export function basenameNoExt(filePath: string): string {
/**
* Remark plugin that disables indented code block syntax.
- * Converts indented code blocks (without language specifier) to plain text paragraphs,
+ * Converts indented code blocks to plain text paragraphs,
* while preserving fenced code blocks with backticks.
*/
export function disableIndentedCodeBlockPlugin() {
- return (tree: Node) => {
+ return (tree: Node, file: VFile) => {
visit(tree, 'code', (node: Code, index, parent: Parent | undefined) => {
- // Convert indented code blocks (nodes without lang or meta property)
- // to plain text
+ // Convert indented code blocks (nodes without lang / meta property,
+ // and are not surrounded by backticks) to plain text
// Check if the parent exists so we can replace the node safely
- if (!node.lang && !node.meta && parent && typeof index === 'number') {
+ if (node.lang === null && node.meta === null && parent && typeof index === 'number') {
const nodePosition: Position | undefined = node.position
- const textNode: Text = {
- type: 'text',
- value: node.value,
- position: nodePosition,
+ if (nodePosition !== undefined && file.value.at(nodePosition.start.offset!) !== '`') {
+ const textNode: Text = {
+ type: 'text',
+ value: node.value,
+ position: nodePosition,
+ }
+ const paragraphNode: Paragraph = {
+ type: 'paragraph',
+ children: [textNode],
+ position: nodePosition,
+ }
+ parent.children[index] = paragraphNode
}
- const paragraphNode: Paragraph = {
- type: 'paragraph',
- children: [textNode],
- position: nodePosition,
- }
- parent.children[index] = paragraphNode
}
})
}