Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Typing **@** in a composer opens developer suggestions. Use ↑/↓ to move, Ent

Typing **#123** in a composer looks up that exact issue or pull request in the /dev/null repository, `dev-null-sh/dev-null.sh`. The matching title appears beneath the number; click it or use Enter or Tab to insert its GitHub URL, just like choosing an emoji suggestion. A space or punctuation leaves the number as typed and dismisses the suggestion. Missing numbers and failed lookups leave the text unchanged. Code, URL fragments, and references qualified with another repository are not expanded.

Write `color(#ff8a4c)` or `color(#fff)` outside code to show the hex value with a small color swatch. The `color(...)` markup stays in the draft and is hidden in the rendered commit. It accepts 3, 4, 6 or 8 hex digits. A bare `#` followed by a letter is a hashtag. On the web, if the text of a commit, reply, quote, or Remotes message contains a hashtag that could be a color, the composer shows the swatch syntax and offers to apply it. Colors that start with a digit can still show a swatch without the markup: `#000`, `#000000` and `#333333` are colors, while `#123` and `#333` stay plain text. The full-width `#` also works for automatic swatches, as in `#fff`. A swatch written with `color(...)` does not add the commit to a guild.
Write `color(#ff8a4c)` or `color(#fff)` outside code to show the hex value with a small color swatch. The `color(...)` markup stays in the draft and is hidden in the rendered commit. It accepts 3, 4, 6 or 8 hex digits. A bare `#` followed by a letter is a hashtag. On the web, if the text of a commit, reply, quote, or Remotes message contains a hashtag that could be a color, the composer shows the swatch syntax and offers to apply it. Colors that start with a digit can still show a swatch without the markup: `#000`, `#000000` and `#333333` are colors, while `#123` and `#333` stay plain text. The full-width `#` also works for automatic swatches, as in `#fff`. A CSS color function such as `rgb(255 138 76)`, `hsl(20 100% 65%)` or `oklch(0.75 0.15 50)` shows a swatch without the markup too, and stays as written. It needs three numbers, separated by spaces with an optional `/ alpha`, or in `rgb()` and `hsl()` also by commas with an optional fourth. A swatch written with `color(...)` does not add the commit to a guild.

Sources: [body renderer](resources/js/components/commit-body.tsx), [emoji picker button](resources/js/components/emoji-picker.tsx), [colors](resources/js/lib/colors.ts), [code blocks](resources/js/components/code-block.tsx), [mention suggestions](app/Http/Controllers/MentionSuggestionController.php), [mention parsing](app/Services/Mentions.php).

Expand Down
6 changes: 3 additions & 3 deletions resources/js/components/linkified-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { GuildIcon } from '@/components/guild-icon';
import { UserPreview } from '@/components/user-preview';
import { typedProse, withoutCode } from '@/lib/code';
import type { Color } from '@/lib/colors';
import { colorsIn } from '@/lib/colors';
import { colorsIn, cssColor } from '@/lib/colors';
import { hashtagPattern } from '@/lib/hashtags';
import { mentionPattern } from '@/lib/mentions';
import { pasteisParts } from '@/lib/pasteis';
Expand Down Expand Up @@ -780,7 +780,7 @@ function withHashtags(
}

/**
* The hex colors found in the whole text that fall in this slice of it, as their value with a swatch of the color.
* The colors found in the whole text that fall in this slice of it, as their value with a swatch of the color.
*/
function withColors(
text: string,
Expand Down Expand Up @@ -816,7 +816,7 @@ function withColors(
<span
aria-hidden="true"
className="mr-0.5 inline-block size-[0.75em] rounded-sm border border-border align-[-0.05em]"
style={{ backgroundColor: `#${color.value}` }}
style={{ backgroundColor: cssColor(color) }}
/>
{color.label}
</span>,
Expand Down
26 changes: 22 additions & 4 deletions resources/js/lib/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ const hexColorPattern = new RegExp(`^#(?:${hexColor})$`, 'i');
// A bare ASCII "#" followed by a letter marks a hashtag. The lookahead on short hex keeps "#123" an issue reference.
// Bare colors start at whitespace or a quote boundary and may end in punctuation before the next space.
const bareColorPattern = String.raw`(?:(?<!\S)|(?<=\n>))(?:#(?=[0-9])|\uFF03)(${longHex}|(?=[0-9]*[a-f]|0)${shortHex})(?=\p{P}*(?:[\s\u00AD\u2060\u200A-\u200D\u20E2]|$))`;

// A channel of a color function, like "255", "0.75", "65%" or "50deg".
const channel = String.raw`[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:%|deg)?`;

// A CSS color function like "rgb(255 138 76)", "hsl(20 100% 65%)" or "oklch(0.75 0.15 50 / 50%)", a whole word like a bare
// color. Its three channels are on one line, separated by spaces with an optional "/ alpha" or, in rgb() and hsl() only, by
// commas with an optional fourth, so "rgb(1, 2 3)", "lab(1, 2, 3)" and "hsl(1 2 3 4)" stay text.
const colorFunctionPattern = String.raw`(?:(?<!\S)|(?<=\n>))(?:(?:rgba?|hsla?|(?:ok)?lab|(?:ok)?lch)\([ \t]*${channel}[ \t]+${channel}[ \t]+${channel}(?:[ \t]*/[ \t]*${channel})?|(?:rgba?|hsla?)\([ \t]*${channel}[ \t]*,[ \t]*${channel}[ \t]*,[ \t]*${channel}(?:[ \t]*,[ \t]*${channel})?)[ \t]*\)(?=\p{P}*(?:[\s\u00AD\u2060\u200A-\u200D\u20E2]|$))`;
const colorPattern = new RegExp(
`${explicitColorPattern}|${bareColorPattern}`,
`${explicitColorPattern}|${bareColorPattern}|${colorFunctionPattern}`,
'giu',
);

Expand All @@ -28,6 +36,9 @@ export function isTypingExplicitColor(text: string): boolean {
return typingExplicitColorPattern.test(text);
}

/**
* A hex color's digits, without the "#", or a whole color function, as written.
*/
export type Color = {
value: string;
label: string;
Expand All @@ -36,18 +47,25 @@ export type Color = {
};

/**
* The hex colors in a run of prose, with their display labels and source ranges. When the prose starts a whole body,
* its start is the start of a line, so a ">" there starts a quoted line.
* The hex colors and color functions in a run of prose, with their display labels and source ranges. When the prose
* starts a whole body, its start is the start of a line, so a ">" there starts a quoted line.
*/
export function colorsIn(text: string, startsText = false): Color[] {
const start = startsText ? 1 : 0;

return [...(startsText ? `\n${text}` : text).matchAll(colorPattern)].map(
(match) => ({
value: match[1] ?? match[2],
value: match[1] ?? match[2] ?? match[0],
label: match[1] === undefined ? match[0] : `#${match[1]}`,
index: match.index - start,
length: match[0].length,
}),
);
}

/**
* What a color's swatch is painted with: a hex color's digits with their "#", or a color function as written.
*/
export function cssColor(color: Color): string {
return color.value.endsWith(')') ? color.value : `#${color.value}`;
}
51 changes: 50 additions & 1 deletion tests/Unit/Colors.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { colorsIn } from '../../resources/js/lib/colors.ts';
import { colorsIn, cssColor } from '../../resources/js/lib/colors.ts';

const values = (text: string) => colorsIn(text).map((color) => color.value);

Expand Down Expand Up @@ -69,3 +69,52 @@ await test('ends a hex color at a zero-width character, as a hashtag ends', () =
assert.deepEqual(values('#0ff\u200Bnext and #0bc\u2060'), ['0ff', '0bc']);
assert.deepEqual(values('#0ff\u200Cx'), ['0ff']);
});

await test('finds CSS color functions as whole words, in order with hex colors', () => {
assert.deepEqual(
values(
'Use rgb(255 138 76), color(#fff), rgba(255, 138, 76, 0.5), hsl(20, 100%, 65%) or HSL(20 100% 65% / 50%)!\n>oklch(0.75 0.15 50deg) oklab(.7 -.1 .1) lab(52% 40 59) lch(52% 72 56)',
),
[
'rgb(255 138 76)',
'fff',
'rgba(255, 138, 76, 0.5)',
'hsl(20, 100%, 65%)',
'HSL(20 100% 65% / 50%)',
'oklch(0.75 0.15 50deg)',
'oklab(.7 -.1 .1)',
'lab(52% 40 59)',
'lch(52% 72 56)',
],
);
});

await test('ignores what only looks like a CSS color function', () => {
for (const text of [
'rgb()',
'rgb(1 2)',
'rgb(a b c)',
'rgb(1, 2 3)',
'lab(52%, 40, 59)',
'oklch(0.75, 0.15, 50)',
'hsl(1 2 3 4)',
'rgb(1 2 3)px',
'xrgb(1 2 3)',
'(rgb(1 2 3))',
'color(srgb 1 0 0)',
'rgb(1\n2 3)',
'rgba(\n0, 0, 0, 0.5\n)',
]) {
assert.deepEqual(values(text), [], text);
}
});

await test('paints a swatch with the color as written', () => {
const colors = colorsIn('color(#ff8a4c) and oklch(0.75 0.15 50)');

assert.deepEqual(colors.map(cssColor), ['#ff8a4c', 'oklch(0.75 0.15 50)']);
assert.deepEqual(
colors.map((color) => color.label),
['#ff8a4c', 'oklch(0.75 0.15 50)'],
);
});
12 changes: 12 additions & 0 deletions tests/Unit/LinkifiedText.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,18 @@ test('shows the hex value and swatch without the color markup', () => {
assert.equal(html.replace(/<[^>]*>/g, ''), 'Brand orange is #FF8A4C.');
});

test('shows a CSS color function as written with a swatch of it', () => {
const text = 'Brand orange is oklch(0.75 0.15 50) or rgb(255, 138, 76).';
const html = renderToStaticMarkup(
<LinkifiedText text={text} mentions={null} />,
);

assert.equal(html.match(/data-test="color"/g)?.length, 2);
assert.ok(html.includes('background-color:oklch(0.75 0.15 50)'));
assert.ok(html.includes('background-color:rgb(255, 138, 76)'));
assert.equal(html.replace(/<[^>]*>/g, ''), text);
});

test('shows no swatch for a color in code when given a whole body', () => {
const html = renderToStaticMarkup(
<LinkifiedText
Expand Down