├── .npmrc
├── .gitattributes
├── .gitignore
├── examples
├── index.js
└── highlight.js
├── media
└── example.png
├── test
├── snapshots
│ ├── test.js.snap
│ └── test.js.md
└── test.js
├── tsconfig.json
├── .editorconfig
├── .github
└── workflows
│ └── test.yml
├── source
└── index.tsx
├── license
├── readme.md
└── package.json
/.npmrc:
--------------------------------------------------------------------------------
1 | package-lock=false
2 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text=auto
2 | *.js text eol=lf
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | yarn.lock
2 | build
3 | node_modules
4 |
--------------------------------------------------------------------------------
/examples/index.js:
--------------------------------------------------------------------------------
1 | require('import-jsx')('./highlight');
2 |
--------------------------------------------------------------------------------
/media/example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vsashyn/ink-syntax-highlight/HEAD/media/example.png
--------------------------------------------------------------------------------
/test/snapshots/test.js.snap:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vsashyn/ink-syntax-highlight/HEAD/test/snapshots/test.js.snap
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "@sindresorhus/tsconfig",
3 | "compilerOptions": {
4 | "outDir": "build",
5 | "target": "es2018",
6 | "lib": ["es2018"]
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = tab
5 | end_of_line = lf
6 | charset = utf-8
7 | trim_trailing_whitespace = true
8 | insert_final_newline = true
9 |
10 | [*.yml]
11 | indent_style = space
12 | indent_size = 2
13 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import {render} from 'ink-testing-library';
3 | import test from 'ava';
4 | import SyntaxHighlight from '..';
5 |
6 | test('render code as in snapshot', t => {
7 | const {frames} = render(
8 |
9 | );
10 | t.snapshot(frames);
11 | });
12 |
--------------------------------------------------------------------------------
/test/snapshots/test.js.md:
--------------------------------------------------------------------------------
1 | # Snapshot report for `test/test.js`
2 |
3 | The actual snapshot is saved in `test.js.snap`.
4 |
5 | Generated by [AVA](https://avajs.dev).
6 |
7 | ## render spinner
8 |
9 | > Snapshot 1
10 |
11 | [
12 | 'const hello: string = \'world\'',
13 | ]
14 |
15 | ## render code as in snapshot
16 |
17 | > Snapshot 1
18 |
19 | [
20 | 'const hello: string = \'world\'',
21 | ]
22 |
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | name: test
2 | on: [push, pull_request]
3 |
4 | jobs:
5 | test:
6 | name: Node.js ${{ matrix.node_version }}
7 | runs-on: ubuntu-latest
8 | strategy:
9 | matrix:
10 | node_version: [18, 20]
11 |
12 | steps:
13 | - uses: actions/checkout@master
14 | - name: Use Node.js ${{ matrix.node_version }}
15 | uses: actions/setup-node@v1
16 | with:
17 | node-version: ${{ matrix.node_version }}
18 | - run: npm install
19 | - run: npm test
20 |
--------------------------------------------------------------------------------
/source/index.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import {Text} from 'ink';
3 | import {highlight, Theme} from 'cli-highlight';
4 |
5 | export interface Props {
6 | readonly code: string;
7 | readonly language?: string;
8 | readonly theme?: Theme;
9 | }
10 | /**
11 | * SyntaxHighlight.
12 | */
13 | const SyntaxHighlight: React.FC = ({code, language, theme}) => {
14 | const highlightedCode = React.useMemo(() => {
15 | return highlight(code, {language, theme});
16 | }, [code, language, theme]);
17 |
18 | return {highlightedCode};
19 | };
20 |
21 | export default SyntaxHighlight;
22 |
--------------------------------------------------------------------------------
/examples/highlight.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import {render} from 'ink';
3 | import SyntaxHighlight from '../build';
4 |
5 | const Syntax = () => {
6 | return (
7 | {
13 | const [counter, setCounter] = useState(0);
14 |
15 | useEffect(() => {
16 | const timer = setInterval(() => {
17 | setCounter(previousCounter => previousCounter + 1);
18 | }, 100);
19 |
20 | return () => {
21 | clearInterval(timer);
22 | };
23 | }, []);
24 |
25 | return {counter} tests passed;
26 | };
27 |
28 | render();`}
29 | />
30 | );
31 | };
32 |
33 | render();
34 |
--------------------------------------------------------------------------------
/license:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Vitalii Sashyn (github.com/vsashyn)
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # ink-syntax-highlight 
2 |
3 | > Synax highlight component for [Ink](https://github.com/vadimdemedes/ink).
4 |
5 | 
6 |
7 | ## Install
8 |
9 | ```
10 | $ npm install ink-syntax-highlight
11 | ```
12 |
13 | ## Usage
14 |
15 | ```jsx
16 | import React from 'react';
17 | import {render, Text} from 'ink';
18 | import SyntaxHighlight from 'ink-syntax-highlight';
19 |
20 | render();
21 | ```
22 |
23 | ## Props
24 |
25 | ### code
26 |
27 | Type: `string`
28 |
29 | Source code to highlight.
30 |
31 | ### language
32 |
33 | Type: `string`
34 |
35 | Language of the source code. If you don't set it yourself, this component will try to auto-detect it. [All languages of highlight.js](https://github.com/highlightjs/highlight.js/blob/master/SUPPORTED_LANGUAGES.md#supported-languages) are supported.
36 |
37 | ### theme
38 |
39 | Type: [Theme](https://github.com/felixfbecker/cli-highlight/blob/main/src/theme.ts#L280)
40 |
41 | You can write your own theme and pass it as prop. [More info regarding custom theme](https://github.com/felixfbecker/cli-highlight/blob/main/README.md#themes).
42 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ink-syntax-highlight",
3 | "version": "2.0.2",
4 | "description": "Synax highlight component for Ink",
5 | "license": "MIT",
6 | "repository": "vsashyn/ink-syntax-highlight",
7 | "author": {
8 | "name": "Vitalii Sashyn",
9 | "email": "sashyn.v@gmail.com",
10 | "url": "github.com/vsashyn"
11 | },
12 | "type": "module",
13 | "exports": "./build/index.js",
14 | "types": "build/index.d.ts",
15 | "engines": {
16 | "node": ">=18"
17 | },
18 | "scripts": {
19 | "test": "tsc && xo",
20 | "build": "tsc && xo",
21 | "prepare": "tsc"
22 | },
23 | "files": [
24 | "build"
25 | ],
26 | "keywords": [
27 | "ink",
28 | "code",
29 | "syntax",
30 | "component",
31 | "jsx",
32 | "react"
33 | ],
34 | "dependencies": {
35 | "cli-highlight": "^2.1.9"
36 | },
37 | "devDependencies": {
38 | "@ava/babel": "^2.0.0",
39 | "@babel/preset-react": "^7.26.3",
40 | "@sindresorhus/tsconfig": "^7.0.0",
41 | "@types/react": "^18.3.18",
42 | "@vdemedes/prettier-config": "^1.0.0",
43 | "ava": "^6.2.0",
44 | "eslint-config-xo-react": "^0.23.0",
45 | "eslint-plugin-prettier": "^3.4.1",
46 | "eslint-plugin-react": "^7.20.0",
47 | "eslint-plugin-react-hooks": "^4.0.4",
48 | "husky": "^4.2.5",
49 | "import-jsx": "^4.0.0",
50 | "ink": "^5.1.0",
51 | "ink-testing-library": "^2.0.0",
52 | "prettier": "^2.0.5",
53 | "pretty-quick": "^2.0.1",
54 | "react": "^18.3.1",
55 | "typescript": "^5.7.2",
56 | "xo": "^0.32.0"
57 | },
58 | "peerDependencies": {
59 | "ink": "^5.1.0",
60 | "react": "^18.3.1"
61 | },
62 | "ava": {
63 | "babel": {
64 | "testOptions": {
65 | "presets": [
66 | "@babel/preset-react"
67 | ]
68 | }
69 | }
70 | },
71 | "xo": {
72 | "extends": "xo-react",
73 | "prettier": true,
74 | "rules": {
75 | "react/prop-types": "off"
76 | },
77 | "ignores": [
78 | "./build"
79 | ]
80 | },
81 | "prettier": "@vdemedes/prettier-config",
82 | "husky": {
83 | "hooks": {
84 | "pre-commit": "pretty-quick --staged"
85 | }
86 | }
87 | }
88 |
--------------------------------------------------------------------------------