Skip to content

feat(logs): add inline JSON syntax highlighting for log attributes#111730

Merged
JoshuaKGoldberg merged 1 commit intomasterfrom
JoshuaKGoldberg/feat/inline-json-syntax-highlighting
Mar 31, 2026
Merged

feat(logs): add inline JSON syntax highlighting for log attributes#111730
JoshuaKGoldberg merged 1 commit intomasterfrom
JoshuaKGoldberg/feat/inline-json-syntax-highlighting

Conversation

@JoshuaKGoldberg
Copy link
Copy Markdown
Member

@JoshuaKGoldberg JoshuaKGoldberg commented Mar 27, 2026

Add inline syntax highlighting for JSON embedded within log attribute values in the expanded attribute tree view.

Previously (LOGS-400), only attribute values that were entirely valid JSON got pretty-printed with StructuredEventData. Values like This is my JSON: {"it": "would be", "nice": ["to", "highlight"], "this": true} rendered as plain text. Now, the JSON portion gets Prism-based syntax colorizing inline — keys, strings, numbers, booleans, and punctuation each get their own color — without adding newlines or changing the layout.

Screenshot of a log with a myData field showing syntax-highlighted JSON object

The JSON extraction logic lives in a standalone utility (extractJsonFromText) that uses string-aware bracket matching to correctly handle braces inside quoted strings and escape sequences — unlike existing npm packages (balanced-match, extract-json-from-string) which break on these cases. I intentionally set this up so that it acts like a standalone importable utility. It'd be nifty if we could publish this as its own package...

Fixes LOGS-636

Made with Cursor

@github-actions github-actions bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Mar 27, 2026
@linear-code
Copy link
Copy Markdown

linear-code bot commented Mar 27, 2026

@JoshuaKGoldberg JoshuaKGoldberg force-pushed the JoshuaKGoldberg/feat/inline-json-syntax-highlighting branch from 29aea9b to 2884591 Compare March 27, 2026 18:11
@scttcper
Copy link
Copy Markdown
Member

not sure if it would help but we do have the https://npmx.dev/package/jsonrepair package

@JoshuaKGoldberg
Copy link
Copy Markdown
Member Author

Oh cool good to know! I don't think we can use that here, since we don't want to modify the log strings. The intent is just to syntax highlight any valid JSON if it's there.

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as ready for review March 27, 2026 19:14
@JoshuaKGoldberg JoshuaKGoldberg requested review from a team as code owners March 27, 2026 19:14
@JoshuaKGoldberg JoshuaKGoldberg requested a review from a team March 27, 2026 19:16
Copy link
Copy Markdown
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

if (matchEnd === -1) {
segments.push({type: 'text', value: text.slice(nextStart)});
break;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unmatched bracket aborts scanning, skipping later valid JSON

Medium Severity

When findMatchingBracket returns -1 (unmatched bracket), the scanner pushes all remaining text as a single text segment and breaks out of the loop. This causes any valid JSON appearing later in the string to be silently missed. For example, parse [ error {"status": 500} would treat everything from [ onward as plain text because [ has no matching ], even though {"status": 500} is valid extractable JSON. The JSON.parse failure path correctly advances just one character and continues scanning — the matchEnd === -1 path needs the same treatment instead of giving up on the rest of the input.

Fix in Cursor Fix in Web

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine. It's an edge case and syntax highlighting is not a critical UI feature.

</span>
))}
</Fragment>
))}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiline JSON loses newlines during Prism rendering

Low Severity

JsonSegment renders each Prism line inside a Fragment with no newline character between lines. usePrismTokens splits on \n and consumes the newline characters during tokenization (in splitTokenContentByLine). So when a JSON segment contains actual newlines (e.g. {\n "key": "value"\n}), the rendered output collapses them — displaying { "key": "value"} instead. The white-space: pre-wrap on InlineCode doesn't help because the \n characters are already gone. Other callers of usePrismTokens typically render a line break element between lines.

Fix in Cursor Fix in Web

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into this on Monday.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is already a bug on production.

image

When a log attribute value contains embedded JSON within surrounding
text (e.g. "This is my JSON: {"key": "value"}"), the JSON portions
are now syntax-highlighted inline using Prism tokenization while the
non-JSON text renders as-is.

This builds on the existing full-JSON pretty-printing from LOGS-400.
The JSON extraction logic is in a standalone, dependency-free utility
(extractJsonFromText) with string-aware bracket matching that correctly
handles braces inside quoted strings and escape sequences.

Refs LINEAR-LOGS-636

Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
Made-with: Cursor
@JoshuaKGoldberg JoshuaKGoldberg force-pushed the JoshuaKGoldberg/feat/inline-json-syntax-highlighting branch from e606674 to 32270cd Compare March 30, 2026 15:44
@JoshuaKGoldberg JoshuaKGoldberg changed the title feat(logs): Add inline JSON syntax highlighting for log attributes feat(logs): add inline JSON syntax highlighting for log attributes Mar 31, 2026
@JoshuaKGoldberg JoshuaKGoldberg merged commit 7fdffb3 into master Mar 31, 2026
74 checks passed
@JoshuaKGoldberg JoshuaKGoldberg deleted the JoshuaKGoldberg/feat/inline-json-syntax-highlighting branch March 31, 2026 12:54
jan-auer pushed a commit that referenced this pull request Apr 1, 2026
…111730)

Add inline syntax highlighting for JSON embedded within log attribute
values in the expanded attribute tree view.

Previously (LOGS-400), only attribute values that were *entirely* valid
JSON got pretty-printed with `StructuredEventData`. Values like `This is
my JSON: {"it": "would be", "nice": ["to", "highlight"], "this": true}`
rendered as plain text. Now, the JSON portion gets Prism-based syntax
colorizing inline — keys, strings, numbers, booleans, and punctuation
each get their own color — without adding newlines or changing the
layout.

<img width="838" height="245" alt="Screenshot of a log with a myData
field showing syntax-highlighted JSON object"
src="https://github.com/user-attachments/assets/7b78bb01-5be9-494e-9010-2008fde3b59c"
/>

The JSON extraction logic lives in a standalone utility
(`extractJsonFromText`) that uses string-aware bracket matching to
correctly handle braces inside quoted strings and escape sequences —
unlike existing npm packages (`balanced-match`,
`extract-json-from-string`) which break on these cases. I intentionally
set this up so that it acts like a standalone importable utility. It'd
be nifty if we could publish this as its own package...

Fixes LOGS-636

Made with [Cursor](https://cursor.com)

Co-authored-by: Claude Sonnet 4 <noreply@anthropic.com>
dashed pushed a commit that referenced this pull request Apr 1, 2026
…111730)

Add inline syntax highlighting for JSON embedded within log attribute
values in the expanded attribute tree view.

Previously (LOGS-400), only attribute values that were *entirely* valid
JSON got pretty-printed with `StructuredEventData`. Values like `This is
my JSON: {"it": "would be", "nice": ["to", "highlight"], "this": true}`
rendered as plain text. Now, the JSON portion gets Prism-based syntax
colorizing inline — keys, strings, numbers, booleans, and punctuation
each get their own color — without adding newlines or changing the
layout.

<img width="838" height="245" alt="Screenshot of a log with a myData
field showing syntax-highlighted JSON object"
src="https://github.com/user-attachments/assets/7b78bb01-5be9-494e-9010-2008fde3b59c"
/>

The JSON extraction logic lives in a standalone utility
(`extractJsonFromText`) that uses string-aware bracket matching to
correctly handle braces inside quoted strings and escape sequences —
unlike existing npm packages (`balanced-match`,
`extract-json-from-string`) which break on these cases. I intentionally
set this up so that it acts like a standalone importable utility. It'd
be nifty if we could publish this as its own package...

Fixes LOGS-636

Made with [Cursor](https://cursor.com)

Co-authored-by: Claude Sonnet 4 <noreply@anthropic.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants