a message's quotes, marked in backticks as Markdown's inline code, read apart
npx shadcn add @ag/code-spansAlready installed? Preview an update
npx shadcn add @ag/code-spans --dry-runYour installed source stays unchanged until you update it. How updates work
Latest changes · 2026-11-16
- New: words that quote things mark them as Markdown's inline code, in backticks (`
two columns are namedto), andcodeSpans(text)reads a message into its pieces, words and quotes, as CommonMark reads code spans; only code spans, so a*or a_is never emphasis.codeSpan(text)` writes one, fenced longer than any backticks it holds. The Cells engine's messages use it.
the demo above, verbatim
"use client"
import { Sample } from "@/app/samples"
import { codeSpans } from "@/registry/base-nova/lib/code-spans"
import { InlineCode } from "@/registry/base-nova/ui/code-inline"
// #region messages
const MESSAGES = [
"two columns are named `to`",
"row 2, `cost`: `8x.50` is not a number",
"`note` is blank: its type `text` has no `?`",
"a value holding a backtick: `` a`b ``",
"a backtick never closed stays as it is: ` and *stars* stay words",
]
// #endregion
// each quote in the registry's inline code (`InlineCode`), the words around it as written
function Said({ text }: { text: string }) {
return (
<span>
{codeSpans(text).map((piece, i) =>
piece.code ? (
// biome-ignore lint/suspicious/noArrayIndexKey: the pieces of one fixed string, in order
<InlineCode key={i}>{piece.text}</InlineCode>
) : (
piece.text
),
)}
</span>
)
}
export default function CodeSpansDemo() {
return (
<Sample
name="messages"
title="a message's quotes as code"
with="messages"
glance="wide"
>
<ul className="flex flex-col gap-1.5 text-muted-foreground text-sm">
{MESSAGES.map((m) => (
<li key={m}>
<Said text={m} />
</li>
))}
</ul>
</Sample>
)
}the source, every file an install writes
// Words that quote things: a message a program says to a person ("two columns are named
// `to`", "`8x.50` is not a number") marks what it quotes as Markdown's inline code, in
// backticks, so every reader can set it apart: a terminal prints the backticks, a page
// sets them as code. Only code spans are read: a `*` or an `_` in the words is never
// emphasis. The twin of swift-utils' `AttributedString(inkMarked:)`; framework-free.
/** A piece of a message: words as they are, or what it quotes (`code`). */
export interface CodeSpanPiece {
code: boolean
text: string
}
/** `text` quoted as a code span, as the engine writes one (`cells::say::code`): fenced by
* a run one longer than its longest run of backticks, one space inside each end when
* it begins or ends with a backtick or with a space (a reader strips them back). */
export function codeSpan(text: string): string {
let longest = 0
for (const run of text.match(/`+/g) ?? [])
longest = Math.max(longest, run.length)
const fence = "`".repeat(longest + 1)
const pad = /^[` ]|[` ]$/.test(text) && text.trim() !== "" ? " " : ""
return `${fence}${pad}${text}${pad}${fence}`
}
/** A message's pieces, its code spans read as CommonMark reads them: a run of
* backticks, the text, a run of exactly as many (`` `to` ``, ``` `` a`b `` ``` for one
* holding a backtick); one space each side is the fence's, not the span's, when both
* are there and the span is not spaces alone. A run never closed is words. */
export function codeSpans(text: string): CodeSpanPiece[] {
const out: CodeSpanPiece[] = []
let words = ""
let at = 0
while (at < text.length) {
if (text[at] !== "`") {
words += text[at]
at++
continue
}
let run = 0
while (text[at + run] === "`") run++
// the close: the next run of exactly as many backticks
let close = -1
let from = at + run
while (from < text.length) {
const next = text.indexOf("`", from)
if (next === -1) break
let length = 0
while (text[next + length] === "`") length++
if (length === run) {
close = next
break
}
from = next + length
}
if (close === -1) {
words += "`".repeat(run)
at += run
continue
}
let quoted = text.slice(at + run, close)
if (
quoted.length > 1 &&
quoted.startsWith(" ") &&
quoted.endsWith(" ") &&
quoted.trim() !== ""
)
quoted = quoted.slice(1, -1)
if (words) out.push({ code: false, text: words })
words = ""
out.push({ code: true, text: quoted })
at = close + run
}
if (words) out.push({ code: false, text: words })
return out
}