MCPcopy
hub / github.com/vercel/streamdown / CodeComponent

Function CodeComponent

packages/streamdown/lib/components.tsx:778–957  ·  view source on GitHub ↗
({
  node,
  className,
  children,
  ...props
}: DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> &
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: "Code component handles multiple rendering paths for inline code, code blocks, and mermaid diagrams"
  ExtraProps & { "data-block"?: string })

Source from the content-addressed store, hash-verified

776MemoSection.displayName = "MarkdownSection";
777
778const CodeComponent = ({
779 node,
780 className,
781 children,
782 ...props
783}: DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> &
784 // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: "Code component handles multiple rendering paths for inline code, code blocks, and mermaid diagrams"
785 ExtraProps & { "data-block"?: string }) => {
786 const cn = useCn();
787 // A code element is block-level when it was inside a <pre> element.
788 // The custom pre component marks its children with data-block.
789 const inline = !("data-block" in props);
790 const {
791 mermaid: mermaidContext,
792 controls: controlsConfig,
793 lineNumbers: contextLineNumbers,
794 } = useContext(StreamdownContext);
795 const mermaidPlugin = useMermaidPlugin();
796 const isBlockIncomplete = useIsCodeFenceIncomplete();
797
798 const match = className?.match(LANGUAGE_REGEX);
799 const language = match?.at(1) ?? "";
800 const customRenderer = useCustomRenderer(language);
801
802 if (inline) {
803 return (
804 <code
805 className={cn(
806 "rounded bg-muted px-1.5 py-0.5 font-mono text-sm",
807 className
808 )}
809 data-streamdown="inline-code"
810 {...props}
811 >
812 {children}
813 </code>
814 );
815 }
816
817 // Parse startLine from the code fence meta string (e.g. ```js startLine=10)
818 const metastring = node?.properties?.metastring;
819 const startLineMatch = metastring?.match(START_LINE_PATTERN);
820 const parsedStartLine = startLineMatch
821 ? Number.parseInt(startLineMatch[1], 10)
822 : undefined;
823 const startLine =
824 parsedStartLine !== undefined && parsedStartLine >= 1
825 ? parsedStartLine
826 : undefined;
827
828 // Parse noLineNumbers from meta string and derive effective lineNumbers
829 const metaNoLineNumbers = metastring
830 ? NO_LINE_NUMBERS_PATTERN.test(metastring)
831 : false;
832 const showLineNumbers = !metaNoLineNumbers && contextLineNumbers !== false;
833
834 // Extract code content from children safely
835 let code = "";

Callers

nothing calls this directly

Calls 8

useCnFunction · 0.90
useMermaidPluginFunction · 0.90
useIsCodeFenceIncompleteFunction · 0.90
useCustomRendererFunction · 0.90
shouldShowControlsFunction · 0.85
shouldShowMermaidControlFunction · 0.85
shouldShowCodeControlFunction · 0.85
cnFunction · 0.70

Tested by

no test coverage detected