Skip to content

Commit 7245546

Browse files
authored
feat(viewer): render color swatches next to hex color codes (#562)
When a plan contains hex color values (#rgb, #rrggbb, #rgba, #rrggbbaa), the InlineMarkdown renderer now shows a small filled swatch inline to the left of the code so the actual color is immediately visible.
1 parent f1d3a88 commit 7245546

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

packages/ui/components/Viewer.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,25 @@ const InlineMarkdown: React.FC<{ text: string; onOpenLinkedDoc?: (path: string)
836836
continue;
837837
}
838838

839+
// Hex color swatch — 3/4-digit forms need an a-f letter to avoid matching issue refs like #123.
840+
match = remaining.match(/^(#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6}|(?=[0-9a-fA-F]*[a-fA-F])[0-9a-fA-F]{4}|(?=[0-9a-fA-F]*[a-fA-F])[0-9a-fA-F]{3}))(?![0-9a-fA-F\w])/);
841+
if (match) {
842+
const hex = match[1];
843+
parts.push(
844+
<span key={key++} className="inline-flex items-center gap-1 align-middle">
845+
<span
846+
className="inline-block w-3.5 h-3.5 rounded-sm border border-black/20 dark:border-white/20 flex-shrink-0"
847+
style={{ backgroundColor: hex }}
848+
title={hex}
849+
/>
850+
<code className="px-1.5 py-0.5 rounded bg-muted text-sm font-mono">{hex}</code>
851+
</span>
852+
);
853+
remaining = remaining.slice(match[0].length);
854+
previousChar = match[0][match[0].length - 1] || previousChar;
855+
continue;
856+
}
857+
839858
// Wikilinks: [[filename]] or [[filename|display text]]
840859
match = remaining.match(/^\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/);
841860
if (match) {
@@ -972,7 +991,7 @@ const InlineMarkdown: React.FC<{ text: string; onOpenLinkedDoc?: (path: string)
972991
}
973992

974993
// Find next special character or consume one regular character
975-
const nextSpecial = remaining.slice(1).search(/[\*_`\[!~\\<]/);
994+
const nextSpecial = remaining.slice(1).search(/[\*_`\[!~\\<#]/);
976995
if (nextSpecial === -1) {
977996
parts.push(remaining);
978997
previousChar = remaining[remaining.length - 1] || previousChar;

0 commit comments

Comments
 (0)