@sybilion/uilib 1.8.9 → 1.8.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/src/components/ui/Chat/ChatMessage/AgentMessageContent.helpers.js +23 -1
- package/dist/esm/src/components/ui/InteractiveContent/CodeBlock.js +60 -0
- package/dist/esm/src/components/ui/InteractiveContent/InteractiveContent.styl.js +2 -2
- package/dist/esm/src/index.js +1 -0
- package/dist/esm/types/src/components/ui/Chat/ChatMessage/AgentMessageContent.helpers.d.ts +11 -1
- package/dist/esm/types/src/components/ui/InteractiveContent/CodeBlock.d.ts +14 -0
- package/dist/esm/types/src/components/ui/InteractiveContent/CodeBlock.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/InteractiveContent/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/ui/Chat/ChatMessage/AgentMessageContent.helpers.tsx +26 -0
- package/src/components/ui/InteractiveContent/AGENT.md +3 -2
- package/src/components/ui/InteractiveContent/CodeBlock.test.tsx +60 -0
- package/src/components/ui/InteractiveContent/CodeBlock.tsx +93 -0
- package/src/components/ui/InteractiveContent/InteractiveContent.styl +42 -0
- package/src/components/ui/InteractiveContent/InteractiveContent.styl.d.ts +5 -0
- package/src/components/ui/InteractiveContent/index.ts +1 -0
- package/src/docs/pages/InteractiveContentPage.tsx +10 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
2
|
import { shouldRenderAutolinkPill, AutolinkUrl } from '../../InteractiveContent/AutolinkUrl.js';
|
|
3
|
+
import { CodeBlock } from '../../InteractiveContent/CodeBlock.js';
|
|
3
4
|
import logger from '../../../../lib/logger.js';
|
|
4
5
|
import S from './ChatMessage.styl.js';
|
|
5
6
|
import S$1 from '../../InteractiveContent/InteractiveContent.styl.js';
|
|
@@ -40,6 +41,25 @@ const injectHTMLCodeTags = (content) => {
|
|
|
40
41
|
length: matches[0].length,
|
|
41
42
|
};
|
|
42
43
|
};
|
|
44
|
+
/**
|
|
45
|
+
* Fenced code block: ```lang ... ``` on its own lines, any language.
|
|
46
|
+
* An unclosed fence (e.g. while streaming) runs to the end of the text.
|
|
47
|
+
* Swallows one newline on each side so the block does not add blank lines.
|
|
48
|
+
*/
|
|
49
|
+
const injectFencedCodeBlock = (content) => {
|
|
50
|
+
const regex = /(^|\n)[ \t]*```[ \t]*([^\s`]*)[^\n]*\n([\s\S]*?)(?:\n?[ \t]*```[ \t]*(?=\n|$)|$)/;
|
|
51
|
+
const matches = content.match(regex);
|
|
52
|
+
if (!matches || matches.index === undefined)
|
|
53
|
+
return null;
|
|
54
|
+
let length = matches[0].length;
|
|
55
|
+
if (content[matches.index + length] === '\n')
|
|
56
|
+
length += 1;
|
|
57
|
+
return {
|
|
58
|
+
elem: jsx(CodeBlock, { code: matches[3], language: matches[2] || undefined }),
|
|
59
|
+
index: matches.index,
|
|
60
|
+
length,
|
|
61
|
+
};
|
|
62
|
+
};
|
|
43
63
|
const injectBacktickCode = (content) => {
|
|
44
64
|
const regex = /`([^`\n]+?)`/;
|
|
45
65
|
const matches = content.match(regex);
|
|
@@ -453,6 +473,7 @@ const injectHeaders = (content) => {
|
|
|
453
473
|
};
|
|
454
474
|
};
|
|
455
475
|
const applyFormatting = (text) => runFormattingPipeline(text, [
|
|
476
|
+
injectFencedCodeBlock,
|
|
456
477
|
injectHeaders,
|
|
457
478
|
injectAnchor,
|
|
458
479
|
injectMarkdownLink,
|
|
@@ -466,6 +487,7 @@ const applyFormatting = (text) => runFormattingPipeline(text, [
|
|
|
466
487
|
injectNewlines,
|
|
467
488
|
]);
|
|
468
489
|
const applyFormattingInline = (text) => runFormattingPipeline(text, [
|
|
490
|
+
injectFencedCodeBlock,
|
|
469
491
|
injectAnchor,
|
|
470
492
|
injectMarkdownLink,
|
|
471
493
|
injectHTMLTags,
|
|
@@ -477,4 +499,4 @@ const applyFormattingInline = (text) => runFormattingPipeline(text, [
|
|
|
477
499
|
injectNewlines,
|
|
478
500
|
]);
|
|
479
501
|
|
|
480
|
-
export { applyFormatting, applyFormattingInline, convertMarkdownTableToHTML, injectAnchor, injectBold, injectBullet, injectHeaders, injectItalic, injectNewlines };
|
|
502
|
+
export { applyFormatting, applyFormattingInline, convertMarkdownTableToHTML, injectAnchor, injectBold, injectBullet, injectFencedCodeBlock, injectHeaders, injectItalic, injectNewlines };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import S from './InteractiveContent.styl.js';
|
|
3
|
+
|
|
4
|
+
const JSON_LANGUAGES = new Set(['json', 'jsonc', 'json5']);
|
|
5
|
+
const isJsonLanguage = (language) => !!language && JSON_LANGUAGES.has(language.toLowerCase());
|
|
6
|
+
/** Pretty-print JSON when the block is JSON (by language tag, or untagged and parseable). */
|
|
7
|
+
function formatCodeBlock(code, language) {
|
|
8
|
+
const trimmed = code.trim();
|
|
9
|
+
const looksLikeJson = /^[[{]/.test(trimmed);
|
|
10
|
+
if (isJsonLanguage(language) || (!language && looksLikeJson)) {
|
|
11
|
+
try {
|
|
12
|
+
return {
|
|
13
|
+
code: JSON.stringify(JSON.parse(trimmed), null, 2),
|
|
14
|
+
isJson: true,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return { code, isJson: isJsonLanguage(language) };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return { code, isJson: false };
|
|
22
|
+
}
|
|
23
|
+
const JSON_TOKEN_RE = /("(?:\\.|[^"\\])*")(\s*:)?|\b(true|false|null)\b|(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/g;
|
|
24
|
+
/** Light JSON highlighting: keys, strings, numbers, and literals. */
|
|
25
|
+
function highlightJson(code) {
|
|
26
|
+
const nodes = [];
|
|
27
|
+
let last = 0;
|
|
28
|
+
let match;
|
|
29
|
+
JSON_TOKEN_RE.lastIndex = 0;
|
|
30
|
+
while ((match = JSON_TOKEN_RE.exec(code)) !== null) {
|
|
31
|
+
if (match.index > last)
|
|
32
|
+
nodes.push(code.slice(last, match.index));
|
|
33
|
+
const [whole, str, colon, literal, num] = match;
|
|
34
|
+
const key = nodes.length;
|
|
35
|
+
if (str !== undefined) {
|
|
36
|
+
nodes.push(jsx("span", { className: colon ? S.tokenKey : S.tokenString, children: str }, key));
|
|
37
|
+
if (colon)
|
|
38
|
+
nodes.push(colon);
|
|
39
|
+
}
|
|
40
|
+
else if (literal !== undefined) {
|
|
41
|
+
nodes.push(jsx("span", { className: S.tokenLiteral, children: literal }, key));
|
|
42
|
+
}
|
|
43
|
+
else if (num !== undefined) {
|
|
44
|
+
nodes.push(jsx("span", { className: S.tokenNumber, children: num }, key));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
nodes.push(whole);
|
|
48
|
+
}
|
|
49
|
+
last = match.index + whole.length;
|
|
50
|
+
}
|
|
51
|
+
if (last < code.length)
|
|
52
|
+
nodes.push(code.slice(last));
|
|
53
|
+
return nodes;
|
|
54
|
+
}
|
|
55
|
+
function CodeBlock({ code, language }) {
|
|
56
|
+
const formatted = formatCodeBlock(code, language);
|
|
57
|
+
return (jsx("pre", { className: S.codeBlock, "data-language": language || undefined, children: jsx("code", { children: formatted.isJson ? highlightJson(formatted.code) : formatted.code }) }));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { CodeBlock, formatCodeBlock, highlightJson };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import styleInject from 'style-inject';
|
|
2
2
|
|
|
3
|
-
var css_248z = ".InteractiveContent_root__FHnlY{box-sizing:border-box;display:inline-block;max-width:100%;min-width:0;overflow-wrap:anywhere;word-break:break-word}.InteractiveContent_root__FHnlY strong{font-weight:600}.InteractiveContent_root__FHnlY em{font-style:italic}.InteractiveContent_root__FHnlY code{background-color:var(--background);border-radius:var(--p-1);box-shadow:0 0 1px var(--foreground);color:var(--foreground);font-family:ui-monospace,SF Mono,SFMono-Regular,Menlo,Monaco,Consolas,monospace;font-size:.875em;padding:.2em .35em}.InteractiveContent_textLink__Ubh4i{text-decoration:underline;text-underline-offset:2px}.InteractiveContent_textLink__Ubh4i:hover{color:var(--link-color-hover);text-decoration:none}.InteractiveContent_autolinkUrl__W0tQH{align-items:center;border-radius:var(--p-2);box-sizing:border-box;color:var(--link-color);display:inline-flex;font-size:var(--text-xs);gap:6px;line-height:1.4;margin:0 1px;max-width:100%;padding:2px 8px 2px 6px;position:relative;text-decoration:none;vertical-align:middle}.InteractiveContent_autolinkUrl__W0tQH:before{background-color:var(--link-color);border-radius:inherit;content:\"\";display:inline-block;height:100%;left:0;opacity:.1;position:absolute;top:0;width:100%}.InteractiveContent_autolinkUrl__W0tQH:hover:before{opacity:.2}.InteractiveContent_autolinkUrlIcon__QoqlQ{flex-shrink:0}.InteractiveContent_autolinkUrlLabel__CcJOG{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}";
|
|
4
|
-
var S = {"root":"InteractiveContent_root__FHnlY","textLink":"InteractiveContent_textLink__Ubh4i","autolinkUrl":"InteractiveContent_autolinkUrl__W0tQH","autolinkUrlIcon":"InteractiveContent_autolinkUrlIcon__QoqlQ","autolinkUrlLabel":"InteractiveContent_autolinkUrlLabel__CcJOG"};
|
|
3
|
+
var css_248z = ".InteractiveContent_root__FHnlY{box-sizing:border-box;display:inline-block;max-width:100%;min-width:0;overflow-wrap:anywhere;word-break:break-word}.InteractiveContent_root__FHnlY strong{font-weight:600}.InteractiveContent_root__FHnlY em{font-style:italic}.InteractiveContent_root__FHnlY code{background-color:var(--background);border-radius:var(--p-1);box-shadow:0 0 1px var(--foreground);color:var(--foreground);font-family:ui-monospace,SF Mono,SFMono-Regular,Menlo,Monaco,Consolas,monospace;font-size:.875em;padding:.2em .35em}.InteractiveContent_textLink__Ubh4i{text-decoration:underline;text-underline-offset:2px}.InteractiveContent_textLink__Ubh4i:hover{color:var(--link-color-hover);text-decoration:none}.InteractiveContent_autolinkUrl__W0tQH{align-items:center;border-radius:var(--p-2);box-sizing:border-box;color:var(--link-color);display:inline-flex;font-size:var(--text-xs);gap:6px;line-height:1.4;margin:0 1px;max-width:100%;padding:2px 8px 2px 6px;position:relative;text-decoration:none;vertical-align:middle}.InteractiveContent_autolinkUrl__W0tQH:before{background-color:var(--link-color);border-radius:inherit;content:\"\";display:inline-block;height:100%;left:0;opacity:.1;position:absolute;top:0;width:100%}.InteractiveContent_autolinkUrl__W0tQH:hover:before{opacity:.2}.InteractiveContent_autolinkUrlIcon__QoqlQ{flex-shrink:0}.InteractiveContent_autolinkUrlLabel__CcJOG{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.InteractiveContent_codeBlock__96NmS{background-color:var(--muted);border-radius:var(--p-2);box-sizing:border-box;color:var(--foreground);display:block;font-family:ui-monospace,SF Mono,SFMono-Regular,Menlo,Monaco,Consolas,monospace;font-size:.875em;line-height:1.5;margin:var(--p-2) 0;overflow-wrap:anywhere;padding:var(--p-3) var(--p-4);-moz-tab-size:2;-o-tab-size:2;tab-size:2;text-align:left;white-space:pre-wrap;width:100%;word-break:break-word}.InteractiveContent_codeBlock__96NmS code,.InteractiveContent_codeBlock__96NmS span{font-family:inherit}.InteractiveContent_codeBlock__96NmS code{background:none;border-radius:0;box-shadow:none;color:inherit;font:inherit;padding:0}.InteractiveContent_tokenKey__NK8-9{color:var(--sb-cyan-500)}.InteractiveContent_tokenString__-xrEK{color:var(--sb-green-500)}.InteractiveContent_tokenNumber__JlmGy{color:var(--sb-yellow-600)}.InteractiveContent_tokenLiteral__wlHkH{color:var(--sb-purple-500)}";
|
|
4
|
+
var S = {"root":"InteractiveContent_root__FHnlY","textLink":"InteractiveContent_textLink__Ubh4i","autolinkUrl":"InteractiveContent_autolinkUrl__W0tQH","autolinkUrlIcon":"InteractiveContent_autolinkUrlIcon__QoqlQ","autolinkUrlLabel":"InteractiveContent_autolinkUrlLabel__CcJOG","codeBlock":"InteractiveContent_codeBlock__96NmS","tokenKey":"InteractiveContent_tokenKey__NK8-9","tokenString":"InteractiveContent_tokenString__-xrEK","tokenNumber":"InteractiveContent_tokenNumber__JlmGy","tokenLiteral":"InteractiveContent_tokenLiteral__wlHkH"};
|
|
5
5
|
styleInject(css_248z);
|
|
6
6
|
|
|
7
7
|
export { S as default };
|
package/dist/esm/src/index.js
CHANGED
|
@@ -51,6 +51,7 @@ export { ImageWithFallback } from './components/ui/ImageWithFallback/ImageWithFa
|
|
|
51
51
|
export { Input } from './components/ui/Input/Input.js';
|
|
52
52
|
export { InteractiveContent } from './components/ui/InteractiveContent/InteractiveContent.js';
|
|
53
53
|
export { AutolinkUrl, displayUrlForAutolink } from './components/ui/InteractiveContent/AutolinkUrl.js';
|
|
54
|
+
export { CodeBlock } from './components/ui/InteractiveContent/CodeBlock.js';
|
|
54
55
|
export { Label } from './components/ui/Label/Label.js';
|
|
55
56
|
export { LabeledInput } from './components/ui/LabeledInput/LabeledInput.js';
|
|
56
57
|
export { AltKeyProvider } from './components/ui/LabelWithId/AltKeyProvider.js';
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Fenced code block: ```lang ... ``` on its own lines, any language.
|
|
4
|
+
* An unclosed fence (e.g. while streaming) runs to the end of the text.
|
|
5
|
+
* Swallows one newline on each side so the block does not add blank lines.
|
|
6
|
+
*/
|
|
7
|
+
declare const injectFencedCodeBlock: (content: string) => {
|
|
8
|
+
elem: import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
index: number;
|
|
10
|
+
length: number;
|
|
11
|
+
};
|
|
2
12
|
declare const injectBold: (content: string) => {
|
|
3
13
|
elem: import("react/jsx-runtime").JSX.Element;
|
|
4
14
|
index: number;
|
|
@@ -30,4 +40,4 @@ declare const injectAnchor: Injector;
|
|
|
30
40
|
declare const injectHeaders: Injector;
|
|
31
41
|
declare const applyFormatting: (text: string) => React.ReactNode[];
|
|
32
42
|
declare const applyFormattingInline: (text: string) => React.ReactNode[];
|
|
33
|
-
export { injectHeaders, injectAnchor, injectBold, injectItalic, injectBullet, injectNewlines, convertMarkdownTableToHTML, applyFormatting, applyFormattingInline, };
|
|
43
|
+
export { injectFencedCodeBlock, injectHeaders, injectAnchor, injectBold, injectItalic, injectBullet, injectNewlines, convertMarkdownTableToHTML, applyFormatting, applyFormattingInline, };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type CodeBlockProps = {
|
|
3
|
+
code: string;
|
|
4
|
+
language?: string;
|
|
5
|
+
};
|
|
6
|
+
/** Pretty-print JSON when the block is JSON (by language tag, or untagged and parseable). */
|
|
7
|
+
export declare function formatCodeBlock(code: string, language?: string): {
|
|
8
|
+
code: string;
|
|
9
|
+
isJson: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** Light JSON highlighting: keys, strings, numbers, and literals. */
|
|
12
|
+
export declare function highlightJson(code: string): React.ReactNode[];
|
|
13
|
+
export declare function CodeBlock({ code, language }: CodeBlockProps): import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
autolinkStyles,
|
|
6
6
|
shouldRenderAutolinkPill,
|
|
7
7
|
} from '#uilib/components/ui/InteractiveContent/AutolinkUrl';
|
|
8
|
+
import { CodeBlock } from '#uilib/components/ui/InteractiveContent/CodeBlock';
|
|
8
9
|
import logger from '#uilib/lib/logger';
|
|
9
10
|
|
|
10
11
|
import S from './ChatMessage.styl';
|
|
@@ -60,6 +61,28 @@ const injectHTMLCodeTags = (content: string) => {
|
|
|
60
61
|
};
|
|
61
62
|
};
|
|
62
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Fenced code block: ```lang ... ``` on its own lines, any language.
|
|
66
|
+
* An unclosed fence (e.g. while streaming) runs to the end of the text.
|
|
67
|
+
* Swallows one newline on each side so the block does not add blank lines.
|
|
68
|
+
*/
|
|
69
|
+
const injectFencedCodeBlock = (content: string) => {
|
|
70
|
+
const regex =
|
|
71
|
+
/(^|\n)[ \t]*```[ \t]*([^\s`]*)[^\n]*\n([\s\S]*?)(?:\n?[ \t]*```[ \t]*(?=\n|$)|$)/;
|
|
72
|
+
const matches = content.match(regex);
|
|
73
|
+
|
|
74
|
+
if (!matches || matches.index === undefined) return null;
|
|
75
|
+
|
|
76
|
+
let length = matches[0].length;
|
|
77
|
+
if (content[matches.index + length] === '\n') length += 1;
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
elem: <CodeBlock code={matches[3]} language={matches[2] || undefined} />,
|
|
81
|
+
index: matches.index,
|
|
82
|
+
length,
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
63
86
|
const injectBacktickCode = (content: string) => {
|
|
64
87
|
const regex = /`([^`\n]+?)`/;
|
|
65
88
|
const matches = content.match(regex);
|
|
@@ -603,6 +626,7 @@ const injectHeaders: Injector = (content: string) => {
|
|
|
603
626
|
|
|
604
627
|
const applyFormatting = (text: string): React.ReactNode[] =>
|
|
605
628
|
runFormattingPipeline(text, [
|
|
629
|
+
injectFencedCodeBlock,
|
|
606
630
|
injectHeaders,
|
|
607
631
|
injectAnchor,
|
|
608
632
|
injectMarkdownLink,
|
|
@@ -618,6 +642,7 @@ const applyFormatting = (text: string): React.ReactNode[] =>
|
|
|
618
642
|
|
|
619
643
|
const applyFormattingInline = (text: string): React.ReactNode[] =>
|
|
620
644
|
runFormattingPipeline(text, [
|
|
645
|
+
injectFencedCodeBlock,
|
|
621
646
|
injectAnchor,
|
|
622
647
|
injectMarkdownLink,
|
|
623
648
|
injectHTMLTags,
|
|
@@ -630,6 +655,7 @@ const applyFormattingInline = (text: string): React.ReactNode[] =>
|
|
|
630
655
|
]);
|
|
631
656
|
|
|
632
657
|
export {
|
|
658
|
+
injectFencedCodeBlock,
|
|
633
659
|
injectHeaders,
|
|
634
660
|
injectAnchor,
|
|
635
661
|
injectBold,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
InteractiveContent renders assistant-style plain text with inline formatting: bold and italic emphasis, inline code via <code> tags or backticks, markdown and HTML links, and bare URLs converted into clickable links. The inline variant omits block-level features such as headings and markdown bullets.
|
|
1
|
+
InteractiveContent renders assistant-style plain text with inline formatting: bold and italic emphasis, inline code via <code> tags or backticks, fenced code blocks (```lang … ```), markdown and HTML links, and bare URLs converted into clickable links. The inline variant omits block-level features such as headings and markdown bullets.
|
|
2
2
|
|
|
3
3
|
## When to use
|
|
4
4
|
|
|
@@ -8,7 +8,7 @@ InteractiveContent renders assistant-style plain text with inline formatting: bo
|
|
|
8
8
|
|
|
9
9
|
## When not to use
|
|
10
10
|
|
|
11
|
-
- For trusted, authored content with rich structure such as
|
|
11
|
+
- For trusted, authored content with rich structure such as nested lists, blockquotes, or images; use a full markdown component.
|
|
12
12
|
- For static UI copy that needs no formatting; render the text directly.
|
|
13
13
|
|
|
14
14
|
## Do
|
|
@@ -19,3 +19,4 @@ InteractiveContent renders assistant-style plain text with inline formatting: bo
|
|
|
19
19
|
- and renders each item inside a list — do not repeat
|
|
20
20
|
- inside the item string.
|
|
21
21
|
- Wrap inline code with <code>…</code> or backticks (`…`); both render as monospace code.
|
|
22
|
+
- Wrap multi-line code in a ``` fence on its own lines, with an optional language tag (```json). Any language renders as a monospace, pre-wrapped block with its contents untouched; JSON is pretty-printed and lightly highlighted.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { render } from '@testing-library/react';
|
|
2
|
+
|
|
3
|
+
import { InteractiveContent } from './InteractiveContent';
|
|
4
|
+
import { formatCodeBlock } from './CodeBlock';
|
|
5
|
+
|
|
6
|
+
describe('InteractiveContent fenced code blocks', () => {
|
|
7
|
+
it('renders a ```json block as a pretty-printed code block', () => {
|
|
8
|
+
const { container } = render(
|
|
9
|
+
<InteractiveContent
|
|
10
|
+
text={'Done.\n\n```json\n{"pushed": true, "items": [1, null]}\n```\nAfter.'}
|
|
11
|
+
/>,
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const pre = container.querySelector('pre');
|
|
15
|
+
expect(pre).not.toBeNull();
|
|
16
|
+
expect(pre?.getAttribute('data-language')).toBe('json');
|
|
17
|
+
expect(pre?.textContent).toBe(
|
|
18
|
+
'{\n "pushed": true,\n "items": [\n 1,\n null\n ]\n}',
|
|
19
|
+
);
|
|
20
|
+
expect(container.textContent).not.toContain('```');
|
|
21
|
+
expect(container.textContent).toContain('Done.');
|
|
22
|
+
expect(container.textContent).toContain('After.');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('keeps other languages verbatim and leaves markdown inside untouched', () => {
|
|
26
|
+
const code = 'const a = **b**;\n // https://example.com\n- item';
|
|
27
|
+
const { container } = render(
|
|
28
|
+
<InteractiveContent text={'```ts\n' + code + '\n```'} />,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const pre = container.querySelector('pre');
|
|
32
|
+
expect(pre?.textContent).toBe(code);
|
|
33
|
+
expect(pre?.querySelector('strong, a, br')).toBeNull();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('renders an untagged fence and an unclosed fence', () => {
|
|
37
|
+
const { container } = render(
|
|
38
|
+
<InteractiveContent text={'```\nplain\n```\n\n```sh\nnpm i'} />,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const blocks = container.querySelectorAll('pre');
|
|
42
|
+
expect(blocks).toHaveLength(2);
|
|
43
|
+
expect(blocks[0].textContent).toBe('plain');
|
|
44
|
+
expect(blocks[1].textContent).toBe('npm i');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('works in the inline variant too', () => {
|
|
48
|
+
const { container } = render(
|
|
49
|
+
<InteractiveContent variant="inline" text={'```\nx\n```'} />,
|
|
50
|
+
);
|
|
51
|
+
expect(container.querySelector('pre')?.textContent).toBe('x');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('keeps invalid JSON as written', () => {
|
|
55
|
+
expect(formatCodeBlock('{"a": 1,', 'json')).toEqual({
|
|
56
|
+
code: '{"a": 1,',
|
|
57
|
+
isJson: true,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import S from './InteractiveContent.styl';
|
|
4
|
+
|
|
5
|
+
type CodeBlockProps = {
|
|
6
|
+
code: string;
|
|
7
|
+
language?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const JSON_LANGUAGES = new Set(['json', 'jsonc', 'json5']);
|
|
11
|
+
|
|
12
|
+
const isJsonLanguage = (language?: string) =>
|
|
13
|
+
!!language && JSON_LANGUAGES.has(language.toLowerCase());
|
|
14
|
+
|
|
15
|
+
/** Pretty-print JSON when the block is JSON (by language tag, or untagged and parseable). */
|
|
16
|
+
export function formatCodeBlock(
|
|
17
|
+
code: string,
|
|
18
|
+
language?: string,
|
|
19
|
+
): { code: string; isJson: boolean } {
|
|
20
|
+
const trimmed = code.trim();
|
|
21
|
+
const looksLikeJson = /^[[{]/.test(trimmed);
|
|
22
|
+
|
|
23
|
+
if (isJsonLanguage(language) || (!language && looksLikeJson)) {
|
|
24
|
+
try {
|
|
25
|
+
return {
|
|
26
|
+
code: JSON.stringify(JSON.parse(trimmed), null, 2),
|
|
27
|
+
isJson: true,
|
|
28
|
+
};
|
|
29
|
+
} catch {
|
|
30
|
+
return { code, isJson: isJsonLanguage(language) };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return { code, isJson: false };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const JSON_TOKEN_RE =
|
|
38
|
+
/("(?:\\.|[^"\\])*")(\s*:)?|\b(true|false|null)\b|(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/g;
|
|
39
|
+
|
|
40
|
+
/** Light JSON highlighting: keys, strings, numbers, and literals. */
|
|
41
|
+
export function highlightJson(code: string): React.ReactNode[] {
|
|
42
|
+
const nodes: React.ReactNode[] = [];
|
|
43
|
+
let last = 0;
|
|
44
|
+
let match: RegExpExecArray | null;
|
|
45
|
+
JSON_TOKEN_RE.lastIndex = 0;
|
|
46
|
+
|
|
47
|
+
while ((match = JSON_TOKEN_RE.exec(code)) !== null) {
|
|
48
|
+
if (match.index > last) nodes.push(code.slice(last, match.index));
|
|
49
|
+
|
|
50
|
+
const [whole, str, colon, literal, num] = match;
|
|
51
|
+
const key = nodes.length;
|
|
52
|
+
|
|
53
|
+
if (str !== undefined) {
|
|
54
|
+
nodes.push(
|
|
55
|
+
<span key={key} className={colon ? S.tokenKey : S.tokenString}>
|
|
56
|
+
{str}
|
|
57
|
+
</span>,
|
|
58
|
+
);
|
|
59
|
+
if (colon) nodes.push(colon);
|
|
60
|
+
} else if (literal !== undefined) {
|
|
61
|
+
nodes.push(
|
|
62
|
+
<span key={key} className={S.tokenLiteral}>
|
|
63
|
+
{literal}
|
|
64
|
+
</span>,
|
|
65
|
+
);
|
|
66
|
+
} else if (num !== undefined) {
|
|
67
|
+
nodes.push(
|
|
68
|
+
<span key={key} className={S.tokenNumber}>
|
|
69
|
+
{num}
|
|
70
|
+
</span>,
|
|
71
|
+
);
|
|
72
|
+
} else {
|
|
73
|
+
nodes.push(whole);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
last = match.index + whole.length;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (last < code.length) nodes.push(code.slice(last));
|
|
80
|
+
return nodes;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function CodeBlock({ code, language }: CodeBlockProps) {
|
|
84
|
+
const formatted = formatCodeBlock(code, language);
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<pre className={S.codeBlock} data-language={language || undefined}>
|
|
88
|
+
<code>
|
|
89
|
+
{formatted.isJson ? highlightJson(formatted.code) : formatted.code}
|
|
90
|
+
</code>
|
|
91
|
+
</pre>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
@@ -72,3 +72,45 @@
|
|
|
72
72
|
overflow hidden
|
|
73
73
|
text-overflow ellipsis
|
|
74
74
|
white-space nowrap
|
|
75
|
+
|
|
76
|
+
.codeBlock
|
|
77
|
+
display block
|
|
78
|
+
width 100%
|
|
79
|
+
box-sizing border-box
|
|
80
|
+
margin var(--p-2) 0
|
|
81
|
+
padding var(--p-3) var(--p-4)
|
|
82
|
+
border-radius var(--p-2)
|
|
83
|
+
background-color var(--muted)
|
|
84
|
+
color var(--foreground)
|
|
85
|
+
font-family ui-monospace, 'SF Mono', SFMono-Regular, Menlo, Monaco, Consolas, monospace
|
|
86
|
+
font-size 0.875em
|
|
87
|
+
line-height 1.5
|
|
88
|
+
white-space pre-wrap
|
|
89
|
+
overflow-wrap anywhere
|
|
90
|
+
word-break break-word
|
|
91
|
+
tab-size 2
|
|
92
|
+
text-align left
|
|
93
|
+
|
|
94
|
+
// base.css resets font-family on every element; keep tokens monospace
|
|
95
|
+
code, span
|
|
96
|
+
font-family inherit
|
|
97
|
+
|
|
98
|
+
code
|
|
99
|
+
font inherit
|
|
100
|
+
padding 0
|
|
101
|
+
border-radius 0
|
|
102
|
+
background none
|
|
103
|
+
box-shadow none
|
|
104
|
+
color inherit
|
|
105
|
+
|
|
106
|
+
.tokenKey
|
|
107
|
+
color var(--sb-cyan-500)
|
|
108
|
+
|
|
109
|
+
.tokenString
|
|
110
|
+
color var(--sb-green-500)
|
|
111
|
+
|
|
112
|
+
.tokenNumber
|
|
113
|
+
color var(--sb-yellow-600)
|
|
114
|
+
|
|
115
|
+
.tokenLiteral
|
|
116
|
+
color var(--sb-purple-500)
|
|
@@ -4,8 +4,13 @@ interface CssExports {
|
|
|
4
4
|
'autolinkUrl': string;
|
|
5
5
|
'autolinkUrlIcon': string;
|
|
6
6
|
'autolinkUrlLabel': string;
|
|
7
|
+
'codeBlock': string;
|
|
7
8
|
'root': string;
|
|
8
9
|
'textLink': string;
|
|
10
|
+
'tokenKey': string;
|
|
11
|
+
'tokenLiteral': string;
|
|
12
|
+
'tokenNumber': string;
|
|
13
|
+
'tokenString': string;
|
|
9
14
|
}
|
|
10
15
|
export const cssExports: CssExports;
|
|
11
16
|
export default cssExports;
|
|
@@ -35,6 +35,16 @@ const SHOWCASE_ROWS: ShowcaseRow[] = [
|
|
|
35
35
|
source: 'Pass `?prompt=` in the query string.',
|
|
36
36
|
variant: 'inline',
|
|
37
37
|
},
|
|
38
|
+
{
|
|
39
|
+
label: 'Code block (JSON, pretty-printed)',
|
|
40
|
+
source:
|
|
41
|
+
'Result:\n```json\n{"pushed": true, "commit": "360b2a9", "items": [{"id": "rc-1", "action": "fix", "ticket": null, "count": 2}]}\n```',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
label: 'Code block (any language)',
|
|
45
|
+
source:
|
|
46
|
+
'```ts\nexport function add(a: number, b: number) {\n return a + b; // **not bold**\n}\n```',
|
|
47
|
+
},
|
|
38
48
|
{
|
|
39
49
|
label: 'Markdown link',
|
|
40
50
|
source: 'Read the [release notes](/releases) for details.',
|