Skip to main content

code_span

Function code_span 

Source
pub fn code_span(text: &str, in_table_cell: bool) -> String
Expand description

A code span holding text exactly as given: CommonMark reads it back as text, every line ending in it read as one space.

Nothing is escaped inside a code span — a backslash there is printed (CommonMark §6.1), so the text is written as it is and the span is fenced instead:

  • the fence is one backtick longer than the longest run of backticks in the text, so no run inside can close it;
  • one space pads each side when the text starts or ends with a backtick (it would join the fence) or both starts and ends with a space (a reader strips one from each side) — the reader strips the padding again;
  • a line ending becomes a space, so a blank line cannot end the paragraph mid-span;
  • inside a table cell | is escaped — a GFM table splits its cells before it reads code spans, and reads \| back as | inside them.

Empty text has no code span and gives an empty string.

use unparser_shared::markdown::code_span;

assert_eq!(code_span(r"C:\dir", false), r"`C:\dir`");
assert_eq!(code_span("a`b", false), "``a`b``");
assert_eq!(code_span("`x`", false), "`` `x` ``");
assert_eq!(code_span(" a ", false), "`  a  `");
assert_eq!(code_span("a | b", true), r"`a \| b`");
assert_eq!(code_span("", false), "");