Skip to content

Commit b963b6a

Browse files
committed
typescript
1 parent 5416cf9 commit b963b6a

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

docs/kit.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
The `type` attribute can have one of the following values:
114114

115115
- `module` - JavaScript
116+
- `text/x-typescript` - TypeScript
116117
- `text/markdown` - Markdown
117118
- `text/html` - HTML
118119
- `application/sql` - SQL
@@ -131,6 +132,15 @@
131132
<\/script>
132133
```
133134
</script>
135+
<script id="86" type="text/markdown">
136+
A TypeScript (`text/x-typescript`) cell:
137+
138+
```html
139+
<script type="text/x-typescript">
140+
let hello: string = "hello";
141+
<\/script>
142+
```
143+
</script>
134144
<script id="52" type="text/markdown">
135145
A Markdown (`text/markdown`) cell:
136146

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"jsdom": "^26.1.0",
6262
"markdown-it": "^14.1.0",
6363
"markdown-it-anchor": "^9.2.0",
64+
"typescript": "^5.8.3",
6465
"vite": "^7.0.0"
6566
},
6667
"devDependencies": {
@@ -77,7 +78,6 @@
7778
"postgres": "^3.4.7",
7879
"snowflake-sdk": "^2.1.3",
7980
"tsx": "^4.20.3",
80-
"typescript": "^5.8.3",
8181
"typescript-eslint": "^8.35.0",
8282
"vitest": "^3.2.4"
8383
},

src/javascript/transpile.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {ScriptTarget, transpile as transpileTypeScript} from "typescript";
12
import type {Cell} from "../lib/notebook.js";
23
import {toCell} from "../lib/notebook.js";
34
import {rewriteFileExpressions} from "./files.js";
@@ -55,14 +56,16 @@ export function transpile(
5556
input = cell.value;
5657
}
5758
const transpiled =
58-
mode === "ojs"
59-
? transpileObservable(input, options)
60-
: mode !== "js"
61-
? transpileJavaScript(transpileTemplate(cell), options)
62-
: transpileJavaScript(input, options);
59+
mode === "ts"
60+
? transpileJavaScript(transpileTypeScript(input, {target: ScriptTarget.ESNext}), options)
61+
: mode === "ojs"
62+
? transpileObservable(input, options)
63+
: mode !== "js"
64+
? transpileJavaScript(transpileTemplate(cell), options)
65+
: transpileJavaScript(input, options);
6366
if (transpiled.output === undefined) transpiled.output = cell.output;
6467
if (cell.hidden) transpiled.autodisplay = false;
65-
else if (mode !== "js" && mode !== "ojs") {
68+
else if (mode !== "js" && mode !== "ts" && mode !== "ojs") {
6669
transpiled.autodisplay = !!input;
6770
transpiled.autoview = mode === "sql" && transpiled.autodisplay && !!transpiled.output;
6871
if (transpiled.autoview) transpiled.output = `viewof$${transpiled.output}`;

src/lib/notebook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface CellSpec {
3939
/** the committed cell value; defaults to empty */
4040
value?: string;
4141
/** the mode; affects how the value is evaluated; defaults to js */
42-
mode?: "js" | "ojs" | "md" | "html" | "tex" | "dot" | "sql" | "node" | "python";
42+
mode?: "js" | "ts" | "ojs" | "md" | "html" | "tex" | "dot" | "sql" | "node" | "python";
4343
/** if true, the editor will stay open when not focused; defaults to false */
4444
pinned?: boolean;
4545
/** if true, implicit display will be suppressed; defaults to false */
@@ -120,5 +120,5 @@ function asDate(date: Date | string | number): Date {
120120
}
121121

122122
export function defaultPinned(mode: Cell["mode"]): boolean {
123-
return mode === "js" || mode === "sql" || isInterpreter(mode) || mode === "ojs";
123+
return mode === "js" || mode === "ts" || mode === "sql" || isInterpreter(mode) || mode === "ojs";
124124
}

src/lib/serialize.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ function serializeMode(mode: Cell["mode"]): string {
7373
return "text/x-python";
7474
case "ojs":
7575
return "application/vnd.observable.javascript";
76+
case "ts":
77+
return "text/x-typescript";
7678
default:
7779
return "module";
7880
}
@@ -96,6 +98,8 @@ function deserializeMode(mode: string | null): Cell["mode"] {
9698
return "python";
9799
case "application/vnd.observable.javascript":
98100
return "ojs";
101+
case "text/x-typescript":
102+
return "ts";
99103
default:
100104
return "js";
101105
}

0 commit comments

Comments
 (0)