# Ruby text

> Attach a pronunciation guide to text such as Japanese kanji.

Ruby text adds the `rubyText` mark. The mark stores a reading in `rt`. Attach `ruby_text_format` to display the reading above the base text in GPUI.

## Try it

[Try the ruby-text editor example](https://gpdoc.dev/docs/plugins/formatting/ruby-text/#ruby-text-example)

## Install

```sh
cargo add gpdoc-plugin-ruby-text
```

## Example

Register the ruby text formatter in the [Quickstart editor configuration](/docs/quickstart/#configure-the-editor). `ruby_text()` uses this formatter by default.

```rust
use gpdoc::{EditorConfig, PluginViewExt};
use gpdoc_plugin_ruby_text::{ruby_text, ruby_text_format};
use gpdoc_plugin_paragraph::paragraph;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(ruby_text().with_view(ruby_text_format))
    .build()
    .expect("configure editor");
```

Apply `setRubyText` to selected text with a reading such as `とうきょう`. The formatter displays the reading above the selection.

## Commands

The mark stores its reading in `rt`, a nullable string with default `null`. `editor.run` returns `Result<CommandOutcome, Error>`. Set and toggle can return `NotApplicable` where the schema excludes this mark.

### `setRubyText`

Attach a reading to the selected base text, or replace the reading already there. Pass the pronunciation as `rt`.

```rust
editor.run(gpdoc_plugin_ruby_text::Set { rt: Some("とうきょう".into()) })?;
```

### `toggleRubyText`

Remove a matching reading or apply the one supplied. At a caret inside an annotation, remove the whole contiguous reading.

```rust
editor.run(gpdoc_plugin_ruby_text::Toggle { rt: Some("とうきょう".into()) })?;
```

### `unsetRubyText`

Remove the reading and keep the base text. Place the caret inside a reading to clear its contiguous range in one command.

```rust
editor.run(gpdoc_plugin_ruby_text::Unset)?;
```