Codice 1.6.0

repository·main·Indexed 41 days ago

https://github.com/huozhi/codice

A React package for displaying and editing syntax-highlighted code blocks. It provides a Code component for rendering with support for line highlighting, line numbers, and automatic language detection via file extensions (e.g., py, rs, css), as well as an Editor component that synchronizes a styled display with a textarea. Note: This project has moved to Sugar High.

Tokens
2K
Snippets
9
Records
11
Agent score
49%

What's inside Codice

  1. Use the Editor component

    main

    The Editor component is the primary entrypoint for the codice editor package. It is exported from the package root and can be imported to integrate the editor into your application.

    import { Editor } from './editor';
    
    // Usage of the Editor component
    function MyComponent() {
      return <Editor />;
    }
  2. Use the Editor component

    main

    The Editor component is a React component designed for code editing. It provides a synchronized view between a styled Code display and a functional textarea. It supports features like line numbers, title management, and language extension detection based on the title.

    Key Props

    • title: The title displayed in the header. If provided, the component attempts to infer the file extension using getExtension(title) if extension is not explicitly set.
    • value: The current code content.
    • onChange: Callback function triggered when the code content changes.
    • onChangeTitle: Callback function triggered when the title is edited via the header controls.
    • controls: Boolean to show/hide the header controls (defaults to true).
    • lineNumbers: Boolean to enable/disable line numbers (defaults to true).
    • extension: Explicitly set the language extension (e.g., 'ts', 'js').
    • fontSize / fontFamily: Controls the typography for both the display and the input area.
    • textareaRef: A ref to access the underlying HTMLTextAreaElement.
    • lineNumbersWidth: Custom width for the line numbers gutter.
    • padding: Custom padding for the editor content.
    import { Editor } from './editor'
    
    function MyComponent() {
      return (
        <Editor
          title="example.ts"
          value="console.log('hello world');"
          onChange={(newCode) => console.log(newCode)}
          onChangeTitle={(newTitle) => console.log(newTitle)}
          fontSize={14}
          lineNumbersWidth="40px"
        />
      )
    }
  3. Use the Code component

    main

    The Code component is the primary entrypoint for the codice package. It is used to render code blocks within your application. Import it from the package root to access its functionality.

    import { Code } from 'codice';
    
    // Usage of the Code component
  4. Use the CodeHeader component

    main

    The CodeHeader component renders a header for a code block, which can include a title and control placeholders. It is typically used as a child or companion to the Code component.

    • If controls is true, it renders placeholder spans for UI controls (e.g., copy, expand).
    • If onChangeTitle is provided, the title becomes an editable input field.
    import { CodeHeader } from 'codice/code';
    
    <CodeHeader 
      title="example.ts" 
      controls={true} 
      onChangeTitle={(newTitle) => console.log(newTitle)} 
    />
  5. Use the Code component to display syntax-highlighted code

    main

    The Code component is the primary way to render syntax-highlighted code blocks in React. It supports line highlighting, line numbers, and custom styling via props. It uses sugar-high for tokenization and automatically detects language presets based on the file extension provided in the title or the extension prop.

    Supported language extensions for automatic preset selection include:

    • CSS: sass, scss, less, css
    • Python: py
    • Rust: rs

    If the extension is not recognized, it falls back to default tokenization.

    import { Code } from 'codice/code';
    
    function MyComponent() {
      return (
        <Code
          title="script.py"
          highlightLines={[2, 4, [6, 8]]}
          lineNumbers
          fontSize="14px"
        >
          def hello():\n    print("world")\n    # This is line 3\n    return True\n\n    # Lines 6 to 8\n    x = 1\n    y = 2\n    z = x + y
        </Code>
      );
    }
  6. Access the Editor DOM via data attributes

    main

    The Editor component renders with specific data-* attributes to facilitate easy selection and state inspection in the DOM or via testing libraries.

    • data-codice="editor": Identifies the main editor container.
    • data-codice-editor: A secondary identifier for the editor.
    • data-codice-title="[title]": The current title of the editor.
    • data-codice-controls="true|false": Indicates if controls are visible.
    • data-codice-line-numbers="true|false": Indicates if line numbers are enabled.
    • data-codice-content: Identifies the container holding the Code display and the textarea.
    <!-- Example DOM structure -->
    <div data-codice="editor" data-codice-title="script.js" data-codice-controls="true" data-codice-line-numbers="true">
      <div data-codice-content>
        <!-- Code display and textarea reside here -->
      </div>
    </div>
  7. Configure Code component options

    main

    The Code component accepts the following props to customize the display:

    PropTypeDescription
    childrenstringThe raw code text to be displayed.
    titlestringThe title of the code block (used for file extension detection).
    extensionstringExplicitly set the language extension (e.g., py, rs, css).
    highlightLines([number, number] | number)[]An array of line numbers or line ranges [start, end] to highlight.
    lineNumbersbooleanWhether to display line numbers. Defaults to false.
    lineNumbersWidthstringThe width of the line number gutter.
    fontSizestring | numberThe font size of the code text.
    paddingstringCSS padding for the code container.
    preformattedbooleanIf true, wraps the code in <pre><code>. Defaults to true.
    asMarkupbooleanIf true, treats children as raw HTML/markup instead of processing it through the tokenizer. Defaults to false.
    controlsbooleanWhether to show the CodeHeader controls.

    Note: Code also accepts all standard React.HTMLAttributes<HTMLDivElement> props.

  8. Get the file extension from a title

    main

    The getExtension utility function extracts the file extension from a given title string. If the title is undefined or has no extension, it returns an empty string.

    import { getExtension } from 'codice/code';
    
    const ext = getExtension('main.py'); // returns 'py'
    const none = getExtension('README'); // returns ''