@teaui/code 1.0.0
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/CodeView.d.ts +24 -0
- package/.dist/CodeView.js +147 -0
- package/.dist/CodeView.js.map +1 -0
- package/.dist/formatter.d.ts +19 -0
- package/.dist/formatter.js +33 -0
- package/.dist/formatter.js.map +1 -0
- package/.dist/highlight.d.ts +7 -0
- package/.dist/highlight.js +28 -0
- package/.dist/highlight.js.map +1 -0
- package/.dist/index.d.ts +3 -0
- package/.dist/index.js +3 -0
- package/.dist/index.js.map +1 -0
- package/.dist/react.d.ts +11 -0
- package/.dist/react.js +8 -0
- package/.dist/react.js.map +1 -0
- package/LICENSE +24 -0
- package/package.json +64 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Viewport } from '@teaui/core';
|
|
2
|
+
import { View, Size } from '@teaui/core';
|
|
3
|
+
import type { ViewProps } from '@teaui/core';
|
|
4
|
+
interface CodeProps {
|
|
5
|
+
code: string;
|
|
6
|
+
language?: string;
|
|
7
|
+
showLineNumbers?: boolean;
|
|
8
|
+
highlightLines?: number[];
|
|
9
|
+
wrap?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export type Props = CodeProps & ViewProps;
|
|
12
|
+
/**
|
|
13
|
+
* A view that renders syntax-highlighted code. Uses cli-highlight (highlight.js)
|
|
14
|
+
* for colorization and renders the ANSI output using the core text rendering
|
|
15
|
+
* pipeline.
|
|
16
|
+
*/
|
|
17
|
+
export declare class CodeView extends View {
|
|
18
|
+
#private;
|
|
19
|
+
constructor(props: Props);
|
|
20
|
+
update(props: Props): void;
|
|
21
|
+
naturalSize(available: Size): Size;
|
|
22
|
+
render(viewport: Viewport): void;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import * as unicode from '@teaui/term';
|
|
2
|
+
import { View, Style, Point, Size } from '@teaui/core';
|
|
3
|
+
import { highlight } from './highlight.js';
|
|
4
|
+
/**
|
|
5
|
+
* A view that renders syntax-highlighted code. Uses cli-highlight (highlight.js)
|
|
6
|
+
* for colorization and renders the ANSI output using the core text rendering
|
|
7
|
+
* pipeline.
|
|
8
|
+
*/
|
|
9
|
+
export class CodeView extends View {
|
|
10
|
+
#code = '';
|
|
11
|
+
#language;
|
|
12
|
+
#showLineNumbers = false;
|
|
13
|
+
#highlightLines = new Set();
|
|
14
|
+
#wrap = false;
|
|
15
|
+
/**
|
|
16
|
+
* Cached highlighted lines: [ansiLine, printableWidth][]
|
|
17
|
+
*/
|
|
18
|
+
#lines = [];
|
|
19
|
+
#gutterWidth = 0;
|
|
20
|
+
constructor(props) {
|
|
21
|
+
super(props);
|
|
22
|
+
this.#update(props);
|
|
23
|
+
}
|
|
24
|
+
update(props) {
|
|
25
|
+
this.#update(props);
|
|
26
|
+
super.update(props);
|
|
27
|
+
}
|
|
28
|
+
#update({ code, language, showLineNumbers, highlightLines, wrap }) {
|
|
29
|
+
const codeChanged = code !== this.#code || language !== this.#language;
|
|
30
|
+
this.#code = code ?? '';
|
|
31
|
+
this.#language = language;
|
|
32
|
+
this.#showLineNumbers = showLineNumbers ?? false;
|
|
33
|
+
this.#highlightLines = new Set(highlightLines ?? []);
|
|
34
|
+
this.#wrap = wrap ?? false;
|
|
35
|
+
if (codeChanged) {
|
|
36
|
+
this.#highlight();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
#highlight() {
|
|
40
|
+
if (!this.#code) {
|
|
41
|
+
this.#lines = [];
|
|
42
|
+
this.#gutterWidth = 0;
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
let highlighted;
|
|
46
|
+
try {
|
|
47
|
+
highlighted = highlight(this.#code, {
|
|
48
|
+
language: this.#language,
|
|
49
|
+
ignoreIllegals: true,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
// Fall back to unhighlighted code if language detection fails
|
|
54
|
+
highlighted = this.#code;
|
|
55
|
+
}
|
|
56
|
+
const rawLines = highlighted.split('\n');
|
|
57
|
+
this.#lines = rawLines.map(line => {
|
|
58
|
+
const width = unicode.lineWidth(line);
|
|
59
|
+
return [line, width];
|
|
60
|
+
});
|
|
61
|
+
this.#gutterWidth = this.#showLineNumbers
|
|
62
|
+
? String(this.#lines.length).length + 1
|
|
63
|
+
: 0;
|
|
64
|
+
}
|
|
65
|
+
naturalSize(available) {
|
|
66
|
+
if (this.#lines.length === 0) {
|
|
67
|
+
return Size.zero;
|
|
68
|
+
}
|
|
69
|
+
const gutter = this.#gutterWidth;
|
|
70
|
+
const contentWidth = available.width - gutter;
|
|
71
|
+
return this.#lines.reduce((size, [, width]) => {
|
|
72
|
+
const lineWidth = width + gutter;
|
|
73
|
+
if (this.#wrap && contentWidth > 0) {
|
|
74
|
+
const lineHeight = Math.ceil(width / contentWidth);
|
|
75
|
+
size.width = Math.max(size.width, Math.min(lineWidth, available.width));
|
|
76
|
+
size.height += lineHeight;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
size.width = Math.max(size.width, lineWidth);
|
|
80
|
+
size.height += 1;
|
|
81
|
+
}
|
|
82
|
+
return size;
|
|
83
|
+
}, Size.zero.mutableCopy());
|
|
84
|
+
}
|
|
85
|
+
render(viewport) {
|
|
86
|
+
if (viewport.isEmpty) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const gutter = this.#gutterWidth;
|
|
90
|
+
const point = new Point(0, 0).mutableCopy();
|
|
91
|
+
for (let lineIndex = 0; lineIndex < this.#lines.length; lineIndex++) {
|
|
92
|
+
if (point.y >= viewport.contentSize.height) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
const [line] = this.#lines[lineIndex];
|
|
96
|
+
const lineNumber = lineIndex + 1;
|
|
97
|
+
const isHighlighted = this.#highlightLines.has(lineNumber);
|
|
98
|
+
// Draw highlight background for the entire line
|
|
99
|
+
if (isHighlighted) {
|
|
100
|
+
for (let x = 0; x < viewport.contentSize.width; x++) {
|
|
101
|
+
viewport.write(' ', new Point(x, point.y), HIGHLIGHT_STYLE);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Draw line number gutter
|
|
105
|
+
if (this.#showLineNumbers) {
|
|
106
|
+
const numStr = String(lineNumber).padStart(gutter - 1);
|
|
107
|
+
const gutterStyle = isHighlighted
|
|
108
|
+
? HIGHLIGHT_GUTTER_STYLE
|
|
109
|
+
: GUTTER_STYLE;
|
|
110
|
+
for (let i = 0; i < numStr.length; i++) {
|
|
111
|
+
viewport.write(numStr[i], new Point(i, point.y), gutterStyle);
|
|
112
|
+
}
|
|
113
|
+
viewport.write(GUTTER_SEP, new Point(gutter - 1, point.y), gutterStyle);
|
|
114
|
+
}
|
|
115
|
+
// Draw code content
|
|
116
|
+
const startingStyle = isHighlighted ? HIGHLIGHT_STYLE : Style.NONE;
|
|
117
|
+
viewport.usingPen(startingStyle, pen => {
|
|
118
|
+
let x = gutter;
|
|
119
|
+
for (const char of unicode.printableChars(line)) {
|
|
120
|
+
const charWidth = unicode.charWidth(char);
|
|
121
|
+
if (charWidth === 0) {
|
|
122
|
+
pen.mergePen(Style.fromSGR(char, startingStyle));
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (this.#wrap && x >= viewport.contentSize.width) {
|
|
126
|
+
x = gutter;
|
|
127
|
+
point.y += 1;
|
|
128
|
+
if (point.y >= viewport.contentSize.height) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (x >= viewport.visibleRect.minX() &&
|
|
133
|
+
x + charWidth - 1 < viewport.visibleRect.maxX()) {
|
|
134
|
+
viewport.write(char, new Point(x, point.y));
|
|
135
|
+
}
|
|
136
|
+
x += charWidth;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
point.y += 1;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const GUTTER_SEP = '│';
|
|
144
|
+
const GUTTER_STYLE = new Style({ dim: true });
|
|
145
|
+
const HIGHLIGHT_STYLE = new Style({ background: 'gray' });
|
|
146
|
+
const HIGHLIGHT_GUTTER_STYLE = new Style({ dim: true, background: 'gray' });
|
|
147
|
+
//# sourceMappingURL=CodeView.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeView.js","sourceRoot":"","sources":["../lib/CodeView.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,aAAa,CAAA;AAGtC,OAAO,EAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAC,MAAM,aAAa,CAAA;AAEpD,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAA;AAYxC;;;;GAIG;AACH,MAAM,OAAO,QAAS,SAAQ,IAAI;IAChC,KAAK,GAAW,EAAE,CAAA;IAClB,SAAS,CAAoB;IAC7B,gBAAgB,GAAY,KAAK,CAAA;IACjC,eAAe,GAAgB,IAAI,GAAG,EAAE,CAAA;IACxC,KAAK,GAAY,KAAK,CAAA;IAEtB;;OAEG;IACH,MAAM,GAAuB,EAAE,CAAA;IAC/B,YAAY,GAAW,CAAC,CAAA;IAExB,YAAY,KAAY;QACtB,KAAK,CAAC,KAAK,CAAC,CAAA;QACZ,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IACrB,CAAC;IAED,MAAM,CAAC,KAAY;QACjB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACnB,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACrB,CAAC;IAED,OAAO,CAAC,EAAC,IAAI,EAAE,QAAQ,EAAE,eAAe,EAAE,cAAc,EAAE,IAAI,EAAY;QACxE,MAAM,WAAW,GAAG,IAAI,KAAK,IAAI,CAAC,KAAK,IAAI,QAAQ,KAAK,IAAI,CAAC,SAAS,CAAA;QACtE,IAAI,CAAC,KAAK,GAAG,IAAI,IAAI,EAAE,CAAA;QACvB,IAAI,CAA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CAAC,IAAI,SAAS,CAAA;gBAChB,CAAC;YACH,CAAC,CAAC,CAAA;YAEF,KAAK,CAAC,CAAC,IAAI,CAAC,CAAA;QACd,CAAC;IACH,CAAC;CACF;AAED,MAAM,UAAU,GAAG,GAAG,CAAA;AACtB,MAAM,YAAY,GAAG,IAAI,KAAK,CAAC,EAAC,GAAG,EAAE,IAAI,EAAC,CAAC,CAAA;AAC3C,MAAM,eAAe,GAAG,IAAI,KAAK,CAAC,EAAC,UAAU,EAAE,MAAM,EAAC,CAAC,CAAA;AACvD,MAAM,sBAAsB,GAAG,IAAI,KAAK,CAAC,EAAC,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a formatting function suitable for use with Input's `format` prop.
|
|
3
|
+
* Returns a function that takes plain text and returns ANSI-colored text
|
|
4
|
+
* with syntax highlighting applied.
|
|
5
|
+
*
|
|
6
|
+
* @param language - Language for highlighting (auto-detected if omitted)
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import {Input} from '@teaui/core'
|
|
11
|
+
* import {codeHighlighter} from '@teaui/code'
|
|
12
|
+
*
|
|
13
|
+
* new Input({
|
|
14
|
+
* multiline: true,
|
|
15
|
+
* format: codeHighlighter('javascript'),
|
|
16
|
+
* })
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function codeHighlighter(language?: string): (text: string) => string;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { highlight } from './highlight.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a formatting function suitable for use with Input's `format` prop.
|
|
4
|
+
* Returns a function that takes plain text and returns ANSI-colored text
|
|
5
|
+
* with syntax highlighting applied.
|
|
6
|
+
*
|
|
7
|
+
* @param language - Language for highlighting (auto-detected if omitted)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import {Input} from '@teaui/core'
|
|
12
|
+
* import {codeHighlighter} from '@teaui/code'
|
|
13
|
+
*
|
|
14
|
+
* new Input({
|
|
15
|
+
* multiline: true,
|
|
16
|
+
* format: codeHighlighter('javascript'),
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function codeHighlighter(language) {
|
|
21
|
+
return (text) => {
|
|
22
|
+
try {
|
|
23
|
+
return highlight(text, {
|
|
24
|
+
language,
|
|
25
|
+
ignoreIllegals: true,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return text;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.js","sourceRoot":"","sources":["../lib/formatter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAA;AAExC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,eAAe,CAAC,QAAiB;IAC/C,OAAO,CAAC,IAAY,EAAU,EAAE;QAC9B,IAAI,CAAC;YACH,OAAO,SAAS,CAAC,IAAI,EAAE;gBACrB,QAAQ;gBACR,cAAc,EAAE,IAAI;aACrB,CAAC,CAAA;QACJ,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAA;QACb,CAAC;IACH,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { highlight as _highlight, DEFAULT_THEME } from 'cli-highlight';
|
|
2
|
+
/**
|
|
3
|
+
* Build a theme that always emits ANSI codes regardless of TTY/chalk color level.
|
|
4
|
+
* cli-highlight uses chalk instances which auto-disable in non-TTY environments.
|
|
5
|
+
* We extract the raw ANSI open/close codes from chalk's internal styler and
|
|
6
|
+
* build plain string-wrapping functions.
|
|
7
|
+
*/
|
|
8
|
+
function buildAnsiTheme() {
|
|
9
|
+
const theme = {};
|
|
10
|
+
for (const [key, value] of Object.entries(DEFAULT_THEME)) {
|
|
11
|
+
if (typeof value === 'function' && value._styler) {
|
|
12
|
+
const { open, close } = value._styler;
|
|
13
|
+
theme[key] = (text) => `${open}${text}${close}`;
|
|
14
|
+
}
|
|
15
|
+
else if (typeof value === 'function') {
|
|
16
|
+
theme[key] = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return theme;
|
|
20
|
+
}
|
|
21
|
+
const ANSI_THEME = buildAnsiTheme();
|
|
22
|
+
/**
|
|
23
|
+
* Syntax highlight with ANSI codes always enabled (regardless of TTY).
|
|
24
|
+
*/
|
|
25
|
+
export function highlight(code, options) {
|
|
26
|
+
return _highlight(code, { ...options, theme: ANSI_THEME });
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=highlight.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlight.js","sourceRoot":"","sources":["../lib/highlight.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,IAAI,UAAU,EAAE,aAAa,EAAC,MAAM,eAAe,CAAA;AAGpE;;;;;GAKG;AACH,SAAS,cAAc;IACrB,MAAM,KAAK,GAA6C,EAAE,CAAA;IAE1D,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC;QACzD,IAAI,OAAO,KAAK,KAAK,UAAU,IAAK,KAAa,CAAC,OAAO,EAAE,CAAC;YAC1D,MAAM,EAAC,IAAI,EAAE,KAAK,EAAC,GAAI,KAAa,CAAC,OAAO,CAAA;YAC5C,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,EAAE,CAAA;QACzD,CAAC;aAAM,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;YACvC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAiC,CAAA;QAChD,CAAC;IACH,CAAC;IAED,OAAO,KAAc,CAAA;AACvB,CAAC;AAED,MAAM,UAAU,GAAG,cAAc,EAAE,CAAA;AAEnC;;GAEG;AACH,MAAM,UAAU,SAAS,CACvB,IAAY,EACZ,OAAuD;IAEvD,OAAO,UAAU,CAAC,IAAI,EAAE,EAAC,GAAG,OAAO,EAAE,KAAK,EAAE,UAAU,EAAC,CAAC,CAAA;AAC1D,CAAC"}
|
package/.dist/index.d.ts
ADDED
package/.dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,eAAe,CAAA;AAEtC,OAAO,EAAC,eAAe,EAAC,MAAM,gBAAgB,CAAA"}
|
package/.dist/react.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Props as CodeViewProps } from './CodeView.js';
|
|
2
|
+
type CodeProps = CodeViewProps;
|
|
3
|
+
declare module 'react' {
|
|
4
|
+
namespace JSX {
|
|
5
|
+
interface IntrinsicElements {
|
|
6
|
+
'tui-code': CodeProps;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare function Code(props: CodeProps): JSX.Element;
|
|
11
|
+
export {};
|
package/.dist/react.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { registerElement } from '@teaui/react';
|
|
3
|
+
import { CodeView } from './CodeView.js';
|
|
4
|
+
registerElement('tui-code', (props) => new CodeView(props));
|
|
5
|
+
export function Code(props) {
|
|
6
|
+
return React.createElement("tui-code", { ...props });
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../lib/react.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAC,eAAe,EAAC,MAAM,cAAc,CAAA;AAC5C,OAAO,EAAC,QAAQ,EAAC,MAAM,eAAe,CAAA;AAKtC,eAAe,CAAC,UAAU,EAAE,CAAC,KAAgB,EAAE,EAAE,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;AAUtE,MAAM,UAAU,IAAI,CAAC,KAAgB;IACnC,OAAO,qCAAc,KAAK,GAAI,CAAA;AAChC,CAAC"}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
TeaUI
|
|
2
|
+
Copyright (c) 2023, Colin T.A. Gray
|
|
3
|
+
https://github.com/colinta/teaui
|
|
4
|
+
|
|
5
|
+
With code from multiple sources
|
|
6
|
+
see https://github.com/colinta/teaui/blob/master/packages/core/LICENSE
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
|
16
|
+
all copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
24
|
+
THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@teaui/code",
|
|
3
|
+
"description": "Syntax-highlighted code view for TeaUI",
|
|
4
|
+
"author": "Colin T.A. Gray <colinta@colinta.com>",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git://github.com/colinta/teaui.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/colinta/teaui",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./.dist/index.d.ts",
|
|
16
|
+
"import": "./.dist/index.js",
|
|
17
|
+
"default": "./.dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./react": {
|
|
20
|
+
"types": "./.dist/react.d.ts",
|
|
21
|
+
"import": "./.dist/react.js",
|
|
22
|
+
"default": "./.dist/react.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"main": ".dist/index.js",
|
|
26
|
+
"types": ".dist/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
".dist/"
|
|
29
|
+
],
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">= 18.12.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"cli-highlight": "^2.1.11",
|
|
35
|
+
"@teaui/term": "1.11.8",
|
|
36
|
+
"@teaui/core": "1.11.8"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": ">=18.0.0",
|
|
40
|
+
"@teaui/react": "1.11.8"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"@teaui/react": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"react": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"react": "^18.3.1",
|
|
52
|
+
"@types/react": "^18.3.1",
|
|
53
|
+
"vitest": "^3.0.0",
|
|
54
|
+
"@teaui/shared": "1.11.8",
|
|
55
|
+
"@teaui/react": "1.11.8"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"clean": "rm -rf .dist/",
|
|
59
|
+
"_build": "pnpm clean && pnpm tsc && pnpm tsc -p tsconfig.react.json",
|
|
60
|
+
"build": "node ../../shared/check.js",
|
|
61
|
+
"test": "vitest run",
|
|
62
|
+
"test:watch": "vitest"
|
|
63
|
+
}
|
|
64
|
+
}
|