|
| 1 | +<div align="center"> |
| 2 | + <h1>dioxus-code</h1> |
| 3 | + <p><strong>Syntax-highlighted code blocks for Dioxus.</strong></p> |
| 4 | +</div> |
| 5 | + |
| 6 | +<div align="center"> |
| 7 | + <a href="https://crates.io/crates/dioxus-code"> |
| 8 | + <img src="https://img.shields.io/crates/v/dioxus-code.svg?style=flat-square" alt="Crates.io version" /> |
| 9 | + </a> |
| 10 | + <a href="https://crates.io/crates/dioxus-code"> |
| 11 | + <img src="https://img.shields.io/crates/d/dioxus-code.svg?style=flat-square" alt="Downloads" /> |
| 12 | + </a> |
| 13 | + <a href="https://docs.rs/dioxus-code"> |
| 14 | + <img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square" alt="docs.rs" /> |
| 15 | + </a> |
| 16 | +</div> |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +A small Dioxus component for rendering source code with proper highlighting. Parsing is powered by [arborium](https://crates.io/crates/arborium); themes ship as scoped CSS so you can mix several on one page. |
| 21 | + |
| 22 | +Two ways to highlight: |
| 23 | + |
| 24 | +- **`code!()` macro** — parses at compile time. The runtime ships only the spans, no parser. Default. |
| 25 | +- **`SourceCode`** — parses at runtime. Opt in with the `runtime` feature when the source isn't known until the user types it. |
| 26 | + |
| 27 | +## Quick start |
| 28 | + |
| 29 | +```toml |
| 30 | +[dependencies] |
| 31 | +dioxus-code = "0.1" |
| 32 | +``` |
| 33 | + |
| 34 | +```rust |
| 35 | +use dioxus::prelude::*; |
| 36 | +use dioxus_code::{Code, Theme, code}; |
| 37 | + |
| 38 | +#[component] |
| 39 | +fn ReadMe() -> Element { |
| 40 | + rsx! { |
| 41 | + Code { |
| 42 | + src: code!("/snippets/demo.rs"), |
| 43 | + theme: Theme::TOKYO_NIGHT, |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +The path is resolved from the consumer's `CARGO_MANIFEST_DIR`. `concat!` and `env!` work too. |
| 50 | + |
| 51 | +## Runtime highlighting |
| 52 | + |
| 53 | +For editor-style use cases where the source isn't known at compile time: |
| 54 | + |
| 55 | +```toml |
| 56 | +[dependencies] |
| 57 | +dioxus-code = { version = "0.1", features = ["runtime"] } |
| 58 | +``` |
| 59 | + |
| 60 | +```rust |
| 61 | +use dioxus_code::{Code, SourceCode, Theme}; |
| 62 | + |
| 63 | +rsx! { |
| 64 | + Code { |
| 65 | + src: SourceCode::new(user_input).with_language("rust"), |
| 66 | + theme: Theme::GITHUB_LIGHT, |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Language can be set explicitly, inferred from a filename via `with_name("main.rs")`, or auto-detected from the source. The default `runtime` feature includes Rust; pass `lang-python`, `lang-toml`, or `all-languages` for the rest. |
| 72 | + |
| 73 | +## Editor |
| 74 | + |
| 75 | +`dioxus-code-editor` is a sibling crate that pairs the highlighter with a `contenteditable` input layer: |
| 76 | + |
| 77 | +```rust |
| 78 | +use dioxus_code_editor::CodeEditor; |
| 79 | +use dioxus_code::Theme; |
| 80 | + |
| 81 | +let mut source = use_signal(|| String::new()); |
| 82 | + |
| 83 | +rsx! { |
| 84 | + CodeEditor { |
| 85 | + value: source(), |
| 86 | + language: "rust", |
| 87 | + theme: Theme::TOKYO_NIGHT, |
| 88 | + oninput: move |value| source.set(value), |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +It is controlled — drive `value` from your own signal and update it inside `oninput`. |
| 94 | + |
| 95 | +## Themes |
| 96 | + |
| 97 | +Thirty-odd built-ins, including Tokyo Night, Catppuccin (all four), Dracula, GitHub Light/Dark, Gruvbox, Nord, One Dark, Rosé Pine, Solarized, the Rustdoc themes, and others. Each is exposed as a `Theme` constant and a CSS asset; pages with multiple themes render side-by-side without leaking styles. |
| 98 | + |
| 99 | +```rust |
| 100 | +Code { src: code!("/example.rs"), theme: Theme::CATPPUCCIN_MOCHA } |
| 101 | +``` |
| 102 | + |
| 103 | +## Examples |
| 104 | + |
| 105 | +```sh |
| 106 | +dx serve --example dioxus-code-basic # macro + runtime side by side |
| 107 | +dx serve --example dioxus-code-macro-only # compile-time only, no parser in the binary |
| 108 | +dx serve --example dioxus-code-live-input # textarea bound to runtime highlighter |
| 109 | +``` |
| 110 | + |
| 111 | +## Workspace layout |
| 112 | + |
| 113 | +| crate | purpose | |
| 114 | +|---|---| |
| 115 | +| `dioxus-code` | The `Code` component, themes, and runtime/macro entry points. | |
| 116 | +| `dioxus-code-editor` | Editable code surface built on `Code`. | |
| 117 | +| `dioxus-code-macro` | Implementation of `code!()`. Re-exported by `dioxus-code` under the `macro` feature. | |
| 118 | + |
| 119 | +## License |
| 120 | + |
| 121 | +[MIT](LICENSE). |
0 commit comments