pi-syntax-highlight 0.1.0 → 0.2.1
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/CHANGELOG.md +81 -40
- package/DEVELOPER.md +100 -0
- package/LICENSE +21 -0
- package/README.md +42 -44
- package/extensions/index.ts +5 -10
- package/package.json +20 -5
- package/src/detect-language.ts +205 -8
- package/src/emphasis.ts +82 -1
- package/src/highlight.ts +25 -9
- package/src/lru-cache.ts +30 -0
- package/src/markdown-fence-lang.ts +20 -87
- package/src/render-bash.ts +29 -2
- package/src/render-edit.ts +0 -6
- package/src/render-generic.ts +10 -21
- package/src/render-read.ts +8 -149
- package/src/render-utils.ts +8 -17
- package/src/resolve-renderers.ts +181 -240
- package/src/tokens.ts +1 -34
- package/src/active-theme.ts +0 -26
- package/src/markdown-style.ts +0 -134
- package/src/markdown-theme.ts +0 -53
- package/src/render-path.ts +0 -54
- package/src/render-write.ts +0 -81
- package/test/unit/active-theme.test.ts +0 -38
- package/test/unit/bash-preview.test.ts +0 -57
- package/test/unit/detect-language.test.ts +0 -124
- package/test/unit/emphasis.test.ts +0 -113
- package/test/unit/extensions-index.test.ts +0 -116
- package/test/unit/grep-parse.test.ts +0 -125
- package/test/unit/highlight.test.ts +0 -107
- package/test/unit/markdown-fence-lang.test.ts +0 -131
- package/test/unit/markdown-pipeline.test.ts +0 -153
- package/test/unit/markdown-render-parity.test.ts +0 -127
- package/test/unit/markdown-style.test.ts +0 -152
- package/test/unit/markdown-theme.test.ts +0 -86
- package/test/unit/render-bash.test.ts +0 -194
- package/test/unit/render-edit.test.ts +0 -214
- package/test/unit/render-find.test.ts +0 -139
- package/test/unit/render-generic.test.ts +0 -152
- package/test/unit/render-grep.test.ts +0 -338
- package/test/unit/render-ls.test.ts +0 -132
- package/test/unit/render-read.test.ts +0 -375
- package/test/unit/render-utils.test.ts +0 -324
- package/test/unit/render-write.test.ts +0 -143
- package/test/unit/resolve-renderers.test.ts +0 -474
- package/test/unit/style-path.test.ts +0 -74
- package/tsconfig.json +0 -15
package/CHANGELOG.md
CHANGED
|
@@ -3,58 +3,99 @@
|
|
|
3
3
|
All notable changes to this project are documented in this file. The format
|
|
4
4
|
follows Keep a Changelog, and this project uses semantic versioning.
|
|
5
5
|
|
|
6
|
+
## [0.2.1] - 2026-11-16
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
|
|
10
|
+
- Manual GitHub Actions workflow to publish to npm.
|
|
11
|
+
- MIT `LICENSE` file.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- Only runtime files are published (43 → 22 files).
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- Stack traces and email headers are no longer detected as `makefile`. Targets starting with a capital letter (e.g. `Main:`) are no longer detected either.
|
|
20
|
+
- Code fences in user messages get their language label again.
|
|
21
|
+
|
|
22
|
+
## [0.2.0] - 2026-11-16
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
|
|
26
|
+
- Language detection for Dockerfile, Makefile, C, C++, PHP, Ruby, PowerShell, JSX, TSX, properties and HTML fragments.
|
|
27
|
+
- Tests that fail if pi changes the internals this package relies on.
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
30
|
+
|
|
31
|
+
- Requires pi 1.5.1 or newer.
|
|
32
|
+
- `read`, `write` and error results now use the stock pi renderers. Final `bash`/`powershell` results keep the styled body, even on failure.
|
|
33
|
+
- Fence labels also apply to thinking, custom and summary messages.
|
|
34
|
+
- Colors follow stock pi.
|
|
35
|
+
- The 400-line highlight limit applies to `bash` and `powershell` only.
|
|
36
|
+
- MCP tools use pi's own renderers.
|
|
37
|
+
|
|
38
|
+
### Removed
|
|
39
|
+
|
|
40
|
+
- Markdown styling of assistant messages. Only unlabeled fences get a label now.
|
|
41
|
+
- The `read` line-number gutter and the `write` content overlay.
|
|
42
|
+
- Fence info strings are copied as written, so `{.ts}` and `js title="a"` are no longer cleaned up.
|
|
43
|
+
|
|
44
|
+
### Fixed
|
|
45
|
+
|
|
46
|
+
- `bash` duration stops at the final result.
|
|
47
|
+
- `grep` no longer hangs on lines over 500 characters or risky patterns.
|
|
48
|
+
|
|
49
|
+
## [0.1.2] - 2026-10-26
|
|
50
|
+
|
|
51
|
+
### Added
|
|
52
|
+
|
|
53
|
+
- `bash` and `powershell` output in a known language (YAML, SQL, TOML, shell, Python, JS, TS, Go, Rust, XML, HTML) is highlighted as one block.
|
|
54
|
+
|
|
55
|
+
### Changed
|
|
56
|
+
|
|
57
|
+
- Detection reads the same lines the highlighter paints.
|
|
58
|
+
- README lists the detected languages and fixes the collapsed-call description.
|
|
59
|
+
|
|
60
|
+
## [0.1.1] - 2026-10-26
|
|
61
|
+
|
|
62
|
+
### Added
|
|
63
|
+
|
|
64
|
+
- Package icon for the gallery.
|
|
65
|
+
- `DEVELOPER.md` with the module map and design notes.
|
|
66
|
+
|
|
67
|
+
### Changed
|
|
68
|
+
|
|
69
|
+
- Added `license: "MIT"`; npm was listing the package as Proprietary.
|
|
70
|
+
- Better npm description and keywords.
|
|
71
|
+
- README rewritten for pi.dev readers.
|
|
72
|
+
|
|
6
73
|
## [0.1.0] - 2026-10-25
|
|
7
74
|
|
|
8
75
|
### Added
|
|
9
76
|
|
|
10
|
-
- `read`
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
header for skills and agent resource files, and a highlighted content preview
|
|
15
|
-
for `write` that is capped while the argument streams.
|
|
16
|
-
- `edit` result renderer: diff highlighted by file language, dimmed line numbers,
|
|
17
|
-
changed words in reverse video, error body in the error color. The diff fills
|
|
18
|
-
the body of the call's own box, so the header keeps its success or failure
|
|
19
|
-
background.
|
|
20
|
-
- Markdown styling for assistant messages: headings in the accent color with one
|
|
21
|
-
attribute per level, inline code with its backtick delimiters restored, and
|
|
22
|
-
blockquote text in the accent color. Sequences close the attribute they opened
|
|
23
|
-
and never cross a line; the visible text is unchanged apart from the restored
|
|
24
|
-
delimiters.
|
|
25
|
-
- `diff` dependency for the word-level highlight of an edited line.
|
|
77
|
+
- `read` results with line numbers and highlighting by file language.
|
|
78
|
+
- `read` and `write` headers with a path link and line range, plus a highlighted preview for `write`.
|
|
79
|
+
- `edit` results with a highlighted diff and word-level changes.
|
|
80
|
+
- Markdown styling for assistant messages: headings, inline code and blockquotes.
|
|
26
81
|
|
|
27
82
|
### Changed
|
|
28
83
|
|
|
29
|
-
- `read` numbers its body and
|
|
30
|
-
|
|
31
|
-
- `edit` no longer paints its diff through pi's `renderDiff`, which reads the
|
|
32
|
-
module-global theme and does not highlight code.
|
|
84
|
+
- `read` numbers its body and keeps the continuation notice out of the highlighted text.
|
|
85
|
+
- `edit` no longer uses pi's `renderDiff`.
|
|
33
86
|
|
|
34
87
|
### Fixed
|
|
35
88
|
|
|
36
|
-
- The highlight cache now
|
|
37
|
-
|
|
38
|
-
cache is keyed by the color sequences the highlighter actually reads.
|
|
39
|
-
- Tool failures are detected through the render context. The host passes no
|
|
40
|
-
`isError` inside the result, so the previous check was always false and
|
|
41
|
-
`grep`, `find`, `ls` and the generic overlay never painted an error.
|
|
89
|
+
- The highlight cache now refreshes when the theme changes.
|
|
90
|
+
- Tool errors are detected correctly, so `grep`, `find`, `ls` and generic results show them.
|
|
42
91
|
|
|
43
92
|
## [0.0.1] - 2026-10-24
|
|
44
93
|
|
|
45
94
|
### Added
|
|
46
95
|
|
|
47
|
-
- `grep`
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
- `
|
|
53
|
-
styled, collapsed preview with the expand hint, duration line.
|
|
54
|
-
- Generic result overlay for `codemode`, MCP and unknown tools that have no
|
|
55
|
-
renderer of their own: JSON and diff bodies highlighted, 10-line collapsed
|
|
56
|
-
preview.
|
|
57
|
-
- Markdown fence transformer: unlabeled fences get a detected language
|
|
58
|
-
injected; labeled fences and `mermaid`, `math`, `latex`, `katex` are left
|
|
59
|
-
untouched.
|
|
60
|
-
- `read`, `write` and `edit` are passed through unchanged.
|
|
96
|
+
- `grep` results with styled paths, line numbers and highlighted matches.
|
|
97
|
+
- `find` and `ls` results with dimmed directories and colored file names.
|
|
98
|
+
- `bash` and `powershell` results with styled diffs, JSON, paths and errors.
|
|
99
|
+
- Generic renderer for `codemode`, MCP and unknown tools.
|
|
100
|
+
- Markdown fences without a label get a detected language.
|
|
101
|
+
- `read`, `write` and `edit` pass through unchanged.
|
package/DEVELOPER.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# pi-syntax-highlight: Developer Guide
|
|
2
|
+
|
|
3
|
+
How the extension draws what it draws, and why a few things stay out of reach. For installation and user-facing behavior, see [README.md](./README.md).
|
|
4
|
+
|
|
5
|
+
Needs Node 24 or newer (the tests run through `--experimental-strip-types`) and pi 1.5.1 or newer.
|
|
6
|
+
|
|
7
|
+
## Layout
|
|
8
|
+
|
|
9
|
+
`extensions/index.ts` is the whole public surface: it registers the tool renderer resolver and one markdown transformer (messages `user`, `assistant`, `assistant-thinking`, `custom`, `summary`). Everything else under `src/` is internal.
|
|
10
|
+
|
|
11
|
+
- `resolve-renderers.ts` is the resolver; see below.
|
|
12
|
+
- `render-*.ts` turn a tool result into the text of a `Text` node: `grep`, `find`, `ls`, `bash`, `read`, `edit`, and `generic` (extension tools without a result renderer).
|
|
13
|
+
- `render-utils.ts` holds the pieces those builders share: footers, the expand hint, the truncation notice.
|
|
14
|
+
- `highlight.ts` wraps pi's `highlightCode` with a cache, and `tokens.ts` holds the SGR sequences the code reads directly.
|
|
15
|
+
- `style-path.ts` and `emphasis.ts` handle the small decisions a path or a grep line needs: which color a basename gets, where the match sits inside a line (with the length and backtracking guards).
|
|
16
|
+
- `grep-parse.ts` splits grep's `path:line: text` and `path-line- text` forms. `detect-language.ts` answers a language for a fence, and `detectOutputLanguage` answers one for a tool-output body.
|
|
17
|
+
- `markdown-fence-lang.ts` is the markdown pass: it labels unlabeled closed fences and nothing else.
|
|
18
|
+
|
|
19
|
+
## The resolver
|
|
20
|
+
|
|
21
|
+
`resolveToolRenderers` wraps the host chain: it calls `next()` and decides per tool.
|
|
22
|
+
|
|
23
|
+
- No base renderers: `undefined` passes through.
|
|
24
|
+
- Base with no `renderResult`: the generic renderer is installed.
|
|
25
|
+
- Otherwise the slot is overlaid only if it is the stock one. Detection is by identity against the host's own `create*ToolDefinition(cwd)` result, then by `toString()` equality, because the stock `bash` and `powershell` renderers are built per definition. Anything undecidable counts as foreign and is returned unchanged, so MCP tools and extensions that bring their own renderer keep it.
|
|
26
|
+
- Styled slots: `grep`, `find`, `ls`, `read` (result only), `edit` (the highlighted diff is swapped into the box stock filled), `bash`/`powershell` (final results only; partial results stay on stock, which owns the elapsed line and repaint interval).
|
|
27
|
+
- Every overlay is wrapped: if ours throws, stock renders the result with a fresh component; the generic renderer falls back to the plain body.
|
|
28
|
+
- `write`, and the `read`/`write` call headers, are stock. Error results render like stock; final `bash`/`powershell` results keep the styled body, including failures.
|
|
29
|
+
- The key hint comes from `context.keyHint`, with `rawKeyHint` as the fallback outside a session.
|
|
30
|
+
|
|
31
|
+
The overlays depend on host internals, and contract tests fail when pi renames them: `state.callComponent` (edit's call box, header first), `state.startedAt` and `state.interval` (shell clock).
|
|
32
|
+
|
|
33
|
+
## Theme handling
|
|
34
|
+
|
|
35
|
+
The theme reaches the renderers as a proxy of stable identity, so a reference comparison never sees a theme switch. The highlight cache is keyed by the color sequences the highlighter actually reads, which changes when the theme does. `syncHighlightTheme` does the comparison on every render. Nothing captures the theme for the markdown pass, which writes no colors.
|
|
36
|
+
|
|
37
|
+
Tool failure is read from the render context, not from the result: the host passes no `isError` field inside `AgentToolResult`, so a check there is always false.
|
|
38
|
+
|
|
39
|
+
Without an initialized theme the renderers fall back to the plain tool output color.
|
|
40
|
+
|
|
41
|
+
## How a bash body gets its language
|
|
42
|
+
|
|
43
|
+
Diff and JSON are recognized over the whole body. Anything else goes to `detectOutputLanguage`, which wraps the fence detector with an allowlist and two raised thresholds, because shell output is log-shaped far more often than code-shaped. Three false positives decided that design, all measured: two `key: value` lines are `git log`, `curl -I` headers are a YAML document to any count that ignores case, and "Update the docs in the table from last week" is SQL. A wrong label paints the body with a grammar that does not fit it, which reads worse than flat text.
|
|
44
|
+
|
|
45
|
+
Detection reads the first `HIGHLIGHT_LINE_BUDGET` lines, the same lines the tokenizer will paint, so nothing is detected in a region that would stay flat anyway. A classified line (error, warning, `path:line:`) replaces the highlighted one instead of being wrapped inside it: `theme.fg()` closes at the end of the string and cannot recolor a span of an already painted line.
|
|
46
|
+
|
|
47
|
+
`render-generic` does not use the detector. It recognizes JSON and diff only, and anything else keeps the plain content color.
|
|
48
|
+
|
|
49
|
+
## The markdown pass
|
|
50
|
+
|
|
51
|
+
`registerMarkdownTransformer` is a display-only `string => string` step applied before parsing. This package uses it for one thing: the info string of a closed fence with no label gets a detected language, so pi's own highlighter has a grammar. The pass is memoized (small LRU), copies an unclosed fence as it is because its body is still arriving, never rewrites an existing label or a body that already carries escape sequences, and falls back to the original markdown if it throws.
|
|
52
|
+
|
|
53
|
+
Labels are chosen by `detect-language.ts`, which never answers `markdown` or `plaintext`, and leaves `mermaid`, `math`, `latex`, and `katex` alone. There is no hook that replaces the `Markdown` component, so headings, quotes, inline code, tables and lists are outside this package.
|
|
54
|
+
|
|
55
|
+
## Known limits
|
|
56
|
+
|
|
57
|
+
- Fences inside lists or blockquotes are not recognized, so they are not labeled.
|
|
58
|
+
- A bash body is read once, from its head: a log that turns into YAML after the first `HIGHLIGHT_LINE_BUDGET` (400) lines keeps the flat paint. The cap applies to bash only.
|
|
59
|
+
- A lowercase SQL query with no terminator stays flat, which is the price of rejecting English prose that starts with "Update" or "Delete".
|
|
60
|
+
- `properties` is detected in fences only; it is not in the bash allowlist.
|
|
61
|
+
- HTML fragments need a known lowercase tag and a closing tag; JSX without `className=` or `={` can be detected as HTML.
|
|
62
|
+
- `grep` output whose path contains `:N: ` or `-N- ` is ambiguous; pi does not disambiguate it either. Emphasis is skipped for lines over 500 characters and for backtracking-prone patterns.
|
|
63
|
+
- The diff of an `edit` marks changed words only where one removed line is followed by one added line, the same rule pi applies.
|
|
64
|
+
- Only one markdown transformer is registered by this extension.
|
|
65
|
+
|
|
66
|
+
## Tests
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npm install
|
|
70
|
+
npm run typecheck
|
|
71
|
+
npm test
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Seams the tests rely on:
|
|
75
|
+
|
|
76
|
+
- `stock-parity.test.ts` renders the same input through stock pi and through the resolver and compares text and shape (errors except final shell results, read/write calls, generic fallback, edit diff).
|
|
77
|
+
- `resolve-renderers.test.ts` covers slot detection, fallback to stock on error, the shell clock, and the contract tests on `state.callComponent`, `state.startedAt` and `state.interval`.
|
|
78
|
+
- `markdown-fence-lang.test.ts` and `markdown-render-parity.test.ts` cover the fence labeling and that the visible text is unchanged.
|
|
79
|
+
- `extensions-index.test.ts` covers what is registered.
|
|
80
|
+
- `highlight.test.ts` covers the theme cache; builders take an injected highlighter because `highlightCode` reads the module-global theme.
|
|
81
|
+
- `emphasis.test.ts` includes the backtracking guard.
|
|
82
|
+
|
|
83
|
+
To try a working copy without installing it, run `pi -e .` from the package directory. With `node_modules` present the extension can import a second copy of pi next to the host's: `keyHint` therefore comes from the render context, never from a module-level import, and languages beyond the eager set (about 21) are registered asynchronously, so the first render of a rarer language can be flat.
|
|
84
|
+
|
|
85
|
+
## Release
|
|
86
|
+
|
|
87
|
+
The package is published from `.github/workflows/publish-syntax-highlight.yml`, which lives at the root of the
|
|
88
|
+
monorepo and runs with `working-directory: pi-syntax-highlight`. It is triggered by hand only
|
|
89
|
+
(`workflow_dispatch`), over OIDC with `npm stage publish`. There is no `NPM_TOKEN`: the npm CLI exchanges the
|
|
90
|
+
runner's OIDC identity for a short-lived publish token. The trust relationship is configured on npmjs.com and
|
|
91
|
+
names the exact filename of that workflow, so renaming it breaks publishing until the connection is deleted and
|
|
92
|
+
created again. The publish stays in stage and goes to production when a person approves it on npmjs.com.
|
|
93
|
+
|
|
94
|
+
The job order is `npm ci`, `npm run typecheck`, `npm test` and `npm stage publish`. The version published is the
|
|
95
|
+
one in `package.json` at that commit, so a release is a version bump committed to `main`, a push and then the
|
|
96
|
+
dispatch; a version already on the registry fails the last step and leaves nothing staged.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pablo Peraza
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,68 +1,66 @@
|
|
|
1
1
|
# pi-syntax-highlight
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Syntax highlighting for what pi prints: tool output, and code fences the assistant writes without a language. Rendering only: tool calls, results and settings stay exactly as they are.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://badge.fury.io/js/pi-syntax-highlight)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
7
|
|
|
7
|
-
##
|
|
8
|
+
## Install
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
| `find`, `ls` | Directories dimmed with a trailing slash, basenames colored by file family, limit footer preserved |
|
|
13
|
-
| `bash`, `powershell` | Completed output styled (diffs, JSON, paths, errors), collapsed preview with the expand hint, duration line |
|
|
14
|
-
| `read` | Call header with path and line range; expanded body numbered from the real line the tool read (`offset`), highlighted by file language, continuation and truncation notices in the warning color; a collapsed call stays silent like stock pi |
|
|
15
|
-
| `edit` | Diff highlighted by file language, line numbers dimmed, changed words in reverse video; the diff fills the body of the call's box, whose background follows success or failure |
|
|
16
|
-
| `write` | Header preview of the content, highlighted and capped while the argument is still arriving, with the stock line footer; failures in the error color |
|
|
17
|
-
| `codemode`, MCP tools, unknown tools | Only when the tool has no result renderer of its own: JSON and diff bodies highlighted, anything else in the plain content color, collapsed to 10 lines with the expand hint |
|
|
18
|
-
| markdown fences | A fence with no info string gets a detected language injected so the existing highlighter colors it; a fence that already carries a label is never rewritten. Detection never answers `markdown` or `plaintext`, and fences tagged `mermaid`, `math`, `latex`, or `katex` are left untouched |
|
|
19
|
-
| assistant markdown | Headings in the theme's accent color with one attribute per level, inline code with its backtick delimiters visible, and blockquote text in the accent color |
|
|
20
|
-
|
|
21
|
-
## How the markdown styling works
|
|
22
|
-
|
|
23
|
-
There is no hook for the markdown theme, so the styles are written into the markdown text itself as terminal sequences, through the same transformer that labels fences. Three rules keep that contained:
|
|
24
|
-
|
|
25
|
-
- Only `assistant` messages are touched. In a user message the line background and the preserved backslash escapes would fight the injected sequences; thinking blocks keep their own default style.
|
|
26
|
-
- Every sequence opens and closes inside its line and closes only the attribute it opened, never with a full reset, so the style of the surrounding construct survives the span.
|
|
27
|
-
- The visible text is unchanged: stripping the sequences from the render leaves the same characters as the render without the transformer, except the backtick delimiters of inline code, which the stock renderer drops.
|
|
10
|
+
```bash
|
|
11
|
+
pi install npm:pi-syntax-highlight
|
|
12
|
+
```
|
|
28
13
|
|
|
29
|
-
|
|
14
|
+
Start a new session and the colors are on. To try it without installing:
|
|
30
15
|
|
|
31
|
-
|
|
16
|
+
```bash
|
|
17
|
+
pi -e npm:pi-syntax-highlight
|
|
18
|
+
```
|
|
32
19
|
|
|
33
|
-
|
|
20
|
+
Needs pi 1.5.1 or newer. Node 24 or newer matters only for running the working copy, not for the install.
|
|
34
21
|
|
|
35
|
-
|
|
22
|
+
## What gets colored
|
|
36
23
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
24
|
+
| Tool | What you see |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `grep` | Paths and line numbers styled, each matched line highlighted in the language of its file, the match itself emphasized. The truncation footer stays. |
|
|
27
|
+
| `find`, `ls` | Directories dimmed with a trailing slash, basenames colored by file family. The limit footer stays. |
|
|
28
|
+
| `bash`, `powershell` | Finished output styled: diffs, JSON, paths, errors, and a body in a recognized language (YAML, SQL, TOML, shell, Python, JavaScript, TypeScript, JSX, TSX, Go, Rust, C, C++, PHP, Ruby, PowerShell, Dockerfile, Makefile, XML, HTML). Error, warning and `path:line:` lines keep their own color inside a highlighted body. A collapsed call shows its last lines, the expand hint, and the duration. Only the first 400 lines are tokenized. |
|
|
29
|
+
| `read` | The expanded body is highlighted by file language. No line numbers, and a collapsed read stays silent, as in stock pi. The call header, errors and image results are stock. |
|
|
30
|
+
| `edit` | The successful diff is highlighted by file language, line numbers dimmed, changed words in reverse video. A failure is drawn by stock pi. |
|
|
31
|
+
| `write` | Stock pi. |
|
|
32
|
+
| Extension tools with no result renderer | JSON and diff bodies get highlighted, anything else keeps the plain content color, and the body collapses to 10 lines with the expand hint, as the host's own fallback does. |
|
|
33
|
+
| MCP tools and tools that bring their own renderer | Left to pi's renderers. |
|
|
34
|
+
| Markdown fences | A closed fence with no label gets a detected language, so the block comes out colored. A fence that already carries a label is never rewritten, and `mermaid`, `math`, `latex` and `katex` are left alone. Applies to assistant messages, thinking blocks, custom messages and summaries. |
|
|
35
|
+
|
|
36
|
+
Nothing else in the markdown is restyled.
|
|
37
|
+
|
|
38
|
+
## Limits
|
|
39
|
+
|
|
40
|
+
- Fences inside lists and blockquotes are not labeled.
|
|
41
|
+
- A fence that is still streaming (not closed yet) is not labeled.
|
|
42
|
+
- A `grep` output whose own path contains `:N: ` is ambiguous, and pi does not disambiguate it either.
|
|
43
|
+
- Match emphasis in `grep` is skipped for lines longer than 500 characters and for patterns prone to catastrophic backtracking; the line is still shown.
|
|
44
|
+
- A bash body is identified and tokenized from its first 400 lines. A language that only appears after that line leaves the body flat. The 400-line limit applies to `bash` and `powershell` only.
|
|
45
|
+
- Java-style `.properties` content is detected only inside fences, not in bash output.
|
|
46
|
+
- HTML fragments are recognized by a list of known tags plus a closing tag; unknown tags stay flat. JSX without `className` or `={` can be detected as HTML.
|
|
47
|
+
- A tool whose result renderer this package does not recognize keeps its own look.
|
|
48
|
+
- No initialized theme, no color: the renderers fall back to plain tool output.
|
|
40
49
|
|
|
41
50
|
## Development
|
|
42
51
|
|
|
43
|
-
Requires Node 24+ and pi 1.4+.
|
|
44
|
-
|
|
45
52
|
```bash
|
|
46
53
|
npm install
|
|
47
54
|
npm run typecheck
|
|
48
55
|
npm test
|
|
49
56
|
```
|
|
50
57
|
|
|
51
|
-
Load
|
|
52
|
-
|
|
53
|
-
```bash
|
|
54
|
-
pi -e /home/pablo/LLMs/pi-syntax-highlight/extensions/index.ts
|
|
55
|
-
```
|
|
58
|
+
Load the working copy from the package directory with `pi -e .`.
|
|
56
59
|
|
|
57
|
-
##
|
|
60
|
+
## More
|
|
58
61
|
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
- Only one markdown transformer is allowed per extension; the pipeline inside this one is a single pass.
|
|
62
|
-
- A `read` body records the line number where each source line starts. A source line longer than the terminal wraps, and the wrapped continuation does not repeat the gutter.
|
|
63
|
-
- The diff of an `edit` marks changed words only where one removed line is followed by one added line, the same rule pi applies; several removed lines keep the per-line colors.
|
|
64
|
-
- Restoring the backticks of an inline code span widens a table cell, so the columns of that table are laid out again.
|
|
65
|
-
- Code spans delimited by more than one backtick, setext headings, and escaped backticks are copied as they are.
|
|
62
|
+
- [DEVELOPER.md](DEVELOPER.md): how the renderers and the fence labeler are built, and the tests.
|
|
63
|
+
- [CHANGELOG.md](CHANGELOG.md): what changed in each release.
|
|
66
64
|
|
|
67
65
|
## License
|
|
68
66
|
|
package/extensions/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { activeTheme, captureTheme } from "../src/active-theme.ts";
|
|
3
2
|
import { createMarkdownTransformer } from "../src/markdown-fence-lang.ts";
|
|
4
3
|
import { resolveToolRenderers } from "../src/resolve-renderers.ts";
|
|
5
4
|
|
|
@@ -7,16 +6,12 @@ import { resolveToolRenderers } from "../src/resolve-renderers.ts";
|
|
|
7
6
|
* pi-syntax-highlight entry point.
|
|
8
7
|
*
|
|
9
8
|
* The tool resolver wraps the host chain and overrides only the result slots
|
|
10
|
-
* this package styles; the markdown transformer labels the fences that
|
|
11
|
-
* without a language so the built-in highlighter has a grammar to paint with
|
|
12
|
-
* styles headings, blockquotes and inline code spans for the assistant message.
|
|
9
|
+
* this package styles; the markdown transformer labels the closed fences that
|
|
10
|
+
* arrive without a language so the built-in highlighter has a grammar to paint with.
|
|
13
11
|
*/
|
|
14
12
|
export default function piSyntaxHighlight(pi: ExtensionAPI): void {
|
|
15
13
|
pi.registerToolRenderer(resolveToolRenderers);
|
|
16
|
-
pi.registerMarkdownTransformer(createMarkdownTransformer({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// reference it captures keeps resolving against the theme in effect, so no later
|
|
20
|
-
// session event is needed.
|
|
21
|
-
pi.on("session_start", (_event, ctx) => captureTheme(ctx.ui.theme));
|
|
14
|
+
pi.registerMarkdownTransformer(createMarkdownTransformer(), {
|
|
15
|
+
messageTypes: ["user", "assistant", "assistant-thinking", "custom", "summary"],
|
|
16
|
+
});
|
|
22
17
|
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-syntax-highlight",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"private": false,
|
|
5
|
-
"description": "Syntax highlighting for pi tool output (grep, find, ls, bash, read, edit,
|
|
5
|
+
"description": "Syntax highlighting for pi: colors tool output (grep, find, ls, bash, read, edit), diffs and JSON, and detects the language of unlabeled code fences in assistant markdown.",
|
|
6
|
+
"license": "MIT",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "git+https://github.com/piousp/llm-skills.git",
|
|
9
10
|
"directory": "pi-syntax-highlight"
|
|
10
11
|
},
|
|
11
12
|
"keywords": [
|
|
12
|
-
"pi-package"
|
|
13
|
+
"pi-package",
|
|
14
|
+
"pi-extension",
|
|
15
|
+
"pi",
|
|
16
|
+
"syntax-highlighting",
|
|
17
|
+
"highlighting",
|
|
18
|
+
"markdown",
|
|
19
|
+
"code-fences",
|
|
20
|
+
"language-detection",
|
|
21
|
+
"terminal",
|
|
22
|
+
"tui",
|
|
23
|
+
"diff",
|
|
24
|
+
"tool-output",
|
|
25
|
+
"theme"
|
|
13
26
|
],
|
|
14
27
|
"pi": {
|
|
28
|
+
"image": "https://raw.githubusercontent.com/piousp/llm-skills/main/pi-syntax-highlight/assets/icon.png",
|
|
15
29
|
"extensions": [
|
|
16
30
|
"./extensions"
|
|
17
31
|
]
|
|
18
32
|
},
|
|
19
33
|
"type": "module",
|
|
34
|
+
"files": ["extensions", "src", "README.md", "CHANGELOG.md", "DEVELOPER.md", "LICENSE"],
|
|
20
35
|
"scripts": {
|
|
21
36
|
"test": "node --experimental-strip-types --test test/unit/*.test.ts",
|
|
22
37
|
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
@@ -30,8 +45,8 @@
|
|
|
30
45
|
"diff": "^8.0.4"
|
|
31
46
|
},
|
|
32
47
|
"devDependencies": {
|
|
33
|
-
"@earendil-works/pi-coding-agent": "^1.
|
|
34
|
-
"@earendil-works/pi-tui": "^1.
|
|
48
|
+
"@earendil-works/pi-coding-agent": "^1.5.1",
|
|
49
|
+
"@earendil-works/pi-tui": "^1.5.1",
|
|
35
50
|
"typescript": "^7.0.2"
|
|
36
51
|
}
|
|
37
52
|
}
|