vercel / vercel/examples

AI ChatGPT Template Code Snippets Not Rendered in Code Blocks in Chatbot Responses

Open
#764 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
5.2k
Forks
1.8k
Avg merge
3d 12h
Merged PRs (30d)
6

Description

Description:
Currently, when the chatbot provides a response that includes code examples enclosed within triple backticks ( ), the code appears as normal text instead of being rendered within a code block. This makes it difficult to distinguish code from regular text and hampers readability.

Steps to Reproduce:

  • Initiate a conversation with the chatbot.
  • Ask a question that prompts the chatbot to provide a code example in its response.
  • Observe that the code example is not formatted as a code block and appears as plain text.

Expected Behavior:
When the chatbot's response contains code examples enclosed within triple backticks ( ), the code should be rendered within a code block, preserving its formatting and improving readability.

Alternative solution:

const convertNewLines = (text: string) => {
  const lines = text.split('\n');
  const result = [];
  let codeBlock = false;
  let codeLines = [];

  for (let line of lines) {
    if (line.trim().startsWith('```')) {
      if (codeBlock) {
        // Close the code block
        result.push(
          <div key={result.length} className="codebox">
            <CodeBlock
              text={codeLines.join('\n')}
              language={'ts'}
              showLineNumbers={false}
              wrapLines
            />
          </div>
        );
        codeLines = [];
        codeBlock = false;
      } else {
        // Start a new code block
        codeBlock = true;
      }
    } else if (codeBlock) {
      // Inside a code block, just collect the lines
      codeLines.push(line);
    } else {
      // Regular text lines
      result.push(
        <span key={result.length}>
          {line}
          <br />
        </span>
      );
    }
  }

  // Check if there's an unclosed code block
  if (codeBlock && codeLines.length > 0) {
    result.push(
      <div key={result.length} className="codebox">
        <CodeBlock
              text={codeLines.join('\n')}
              language={'ts'}
              showLineNumbers={false}
              wrapLines
            />
      </div>
    );
  }

  return result;
};

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ChatLine.tsx component and its convertNewLines function, then review the proposed react-code-blocks approach in the issue. The work is done when responses containing triple-backtick examples render those sections as code blocks while surrounding text remains readable.

Written by the indexing model from the issue text.

Assessment

Tech stack
nextjs, react, typescript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.