Skip to content

Copy as Markdown, HTML or text

Output in a terminal is cells. Pasted into an issue, a chat or a document, those cells are a block of box-drawing characters with the links gone. rich_ext::copy_as turns rendered output back into what it showed: a table into a Markdown or HTML table, a tree into a nested list, a link into a link. No feature flag is needed.

It starts from what is already rendered, rows of Segments, so it works on whatever a program has on screen: a pager's page, Console::render_lines, or a selection of a Screen's cells. When you hold the renderable itself and want Markdown for GitHub, Markdown for GitHub reads a table's columns rather than its drawing, and is exact.

Three formats

use rich::{Console, Table};
use rich_ext::copy_as::CopyAs;

let mut table = Table::new();
table.add_column("name");
table.add_column("site");
table.add_row(&["rich", "[link=https://example.com]docs[/link]"]);

let console = Console::builder().force_terminal(false).build();
let markdown = CopyAs::Markdown.renderable(&console, &table, 40);
assert_eq!(
    markdown,
    "| name | site |\n| --- | --- |\n| rich | [docs](https://example.com) |"
);

CopyAs::Text gives the lines as shown, trailing padding trimmed: what a terminal's own copy gives, without the spaces. CopyAs::Html gives an escaped HTML fragment (<table>, <ul>, <a href>, no <html> around it), ready for a rich-text clipboard or a page.

CopyAs::parse reads text, markdown (md) and html; next cycles through them, for a key that switches formats.

What comes back

Segments carry styles, not the names of the renderables that drew them, so structure is read from the drawing. Only what can be told apart reliably is recovered; anything else stays preformatted, never guessed into a wrong structure.

On screen Markdown HTML
a link [text](url) <a href>
bold, italic, strike **, *, ~~ <strong>, <em>, <s>
a boxed table a GFM table, the rows above its first divider the header <table>
a header, a rule, columns (SIMPLE_HEAD, rich's Markdown tables) a GFM table <table>
a box without columns (a panel) a blockquote, its title in bold, its body read again <blockquote>
tree guides (├──, └──; bold, double and ASCII too) a nested list nested <ul>
bullets and numbers list items, nested by indent <ul>, <ol>
rich's quote bar ▌ a blockquote <blockquote>
a rule, alone or across everything --- (its title in bold) <hr>
a line on its own, all underlined (rich's h1, h2) #, ## <h1>, <h2>
a code block (its background from the first column) a ```text fence <pre>
aligned columns, box drawing a ```text fence, its links listed after it <pre>, links kept
prose a paragraph <p>

A table cell wrapped onto more lines (its first column blank on the lines after) is one cell again. Colour, dim and underline elsewhere have no equivalent and are dropped.

So rich's own Markdown, rendered and copied, comes back as Markdown: headings, emphasis, links, tables, lists, code, quotes and rules.

Links are filtered. Only http, https, mailto, ftp, file and relative URLs are kept; a javascript: link (or any other scheme) becomes its text. Markdown is escaped where it would change meaning (*, _, |, a # at the start of a line), and HTML everywhere.

A selection of a screen

CopyAs::screen reads cells from a Screen, with a Selection:

  • Selection::All, every cell;
  • Selection::Rect(rect), a block: the same columns of each row;
  • Selection::Range { start, end }, a stream, as a mouse drag selects text: from start to the end of its row, the rows between, and up to end (inclusive). The two ends can come in either order.
use rich::{Segment, Style};
use rich_ext::copy_as::{CopyAs, Selection};
use rich_ext::surface::{Rect, Screen};

let mut screen = Screen::new(12, 2);
let bold = Style::parse("bold").unwrap();
screen.write_lines(
    Rect::new(0, 0, 12, 2),
    &[
        vec![Segment::new("one ", None), Segment::new("two", Some(bold))],
        vec![Segment::new("three four", None)],
    ],
);
let range = Selection::Range { start: (4, 0), end: (4, 1) };
assert_eq!(CopyAs::Markdown.screen(&screen, range), "**two**\nthree");

Wide characters and links survive: a cell keeps its interned Style.

In the pager

Pager, and so rich pager, copies to the terminal's clipboard: Ctrl+Y the page on screen, Alt+Y everything, in the format Alt+F cycles (text, Markdown, HTML). Pager::copy_as picks the format to start with. The status line says what was copied, or why it could not be (no OSC 52 support: see the clipboard).