@tin.so/core 0.1.0 → 0.3.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/README.md CHANGED
@@ -1,33 +1,123 @@
1
1
  # @tin.so/core
2
2
 
3
- Renders code images from a JSON spec. The pipeline is `spec → Shiki tokens → layout → SVG → PNG` and needs no browser: the layout places every character on a monospace grid with the metrics of the bundled fonts, and [resvg](https://github.com/linebender/resvg) (WebAssembly) rasterizes the SVG. The same code runs in Node and in the browser.
3
+ Renders code images from a JSON spec, to SVG and PNG. One image can show several snippets: windows next to each other or below each other, panes in a window, and popovers that point to a word. It has 14 editor themes such as GitHub, Dracula, Nord and Catppuccin, a default background for each theme and mode, backgrounds as CSS gradients, line highlights, text styles and colors, tabs with file icons, and fixed aspect ratios for social media.
4
+
5
+ The pipeline is `spec → Shiki tokens → layout → SVG → PNG` and needs no browser. The layout puts every character on a monospace grid with the metrics of the bundled fonts, and [resvg](https://github.com/linebender/resvg) (WebAssembly) rasterizes the SVG. The same code runs in Node and in the browser.
6
+
7
+ This package is the renderer of the [tin.so](https://tin.so) web editor and of the [`tin` CLI](https://www.npmjs.com/package/tin.so). To make images from the command line, use the CLI.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @tin.so/core
13
+ ```
14
+
15
+ Node 22.18 or later.
16
+
17
+ ## Usage
4
18
 
5
19
  ```ts
20
+ import { writeFile } from "node:fs/promises";
6
21
  import { render } from "@tin.so/core/node";
7
22
 
8
23
  const png = await render({
9
- theme: "vercel",
24
+ theme: "github",
10
25
  windows: [
11
26
  {
12
- titleBar: false,
13
27
  tabs: true,
14
- split: "row",
28
+ titleBar: false,
15
29
  panes: [
16
- { title: "main.ts", code: 'import plans from "./plans.toml";' },
30
+ {
31
+ title: "main.ts",
32
+ code: 'import plans from "./plans.toml";\n\nplans.free.price;',
33
+ highlight: [3],
34
+ annotations: [
35
+ { line: 1, match: "plans", code: "const plans: {\n free: { price: 0 };\n}" },
36
+ ],
37
+ },
17
38
  { title: "plans.toml", code: "[free]\nprice = 0" },
18
39
  ],
19
40
  },
20
41
  ],
21
42
  });
43
+
44
+ await writeFile("plans.png", png);
45
+ ```
46
+
47
+ `render(spec, { format: "svg" })` gives an SVG with the fonts inside it. `scale` sets the pixel ratio of a PNG (default 2). By default, the image contains the spec and a link that opens it in the web editor. `metadata: false` turns this off.
48
+
49
+ `createLink(spec)` gives that link without an image:
50
+
51
+ ```ts
52
+ import { createLink } from "@tin.so/core";
53
+
54
+ const link = await createLink(spec); // https://tin.so/#spec=…
55
+ ```
56
+
57
+ ## The spec
58
+
59
+ A spec is JSON with the image settings, its windows, the panes of each window, and the code of each pane. A spec needs only `windows`, `panes` and `code`.
60
+
61
+ ```json
62
+ {
63
+ "$schema": "https://tin.so/schema/v1.json",
64
+ "theme": "dracula",
65
+ "windows": [{ "panes": [{ "title": "main.ts", "code": "const a = 1;" }] }]
66
+ }
67
+ ```
68
+
69
+ | Level | Fields |
70
+ | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
71
+ | Image | `theme`, `mode`, `background`, `backgroundOpacity`, `padding`, `font`, `lineNumbers`, `direction`, `gap`, `aspectRatio`, `width`, `height`, `windows` |
72
+ | Window | `panes`, `split`, `titleBar`, `tabs`, `frame`, `controls`, `background`, `title`, `width`, `height`, `x`, `y` |
73
+ | Pane | `code`, `title`, `language`, `highlight`, `marks` (bold, italic, colors, highlighter fills), `annotations` (popovers, with an optional `background`), `icon`, `lineNumbers` |
74
+
75
+ The full spec, with a description, a default and the limits of each field, is the JSON Schema at [`https://tin.so/schema/v1.json`](https://tin.so/schema/v1.json). `specJsonSchema()` gives the same schema.
76
+
77
+ The `$schema` URL (`/schema/v1.json`) or the field `v` gives the version of the spec. A spec without both is version 1. A new version comes only with a change that can break a spec. New optional fields and new themes, fonts or languages come without one.
78
+
79
+ The renderer ignores keys that start with `x-` in every object, so a spec can carry data of other tools.
80
+
81
+ An invalid spec throws a `SpecError` that names each problem with its path, for example `windows.0.panes.0.annotations.0.match: "plnas" (occurrence 1) is not in line 1`.
82
+
83
+ ## API
84
+
85
+ | Function | What it does |
86
+ | -------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
87
+ | `render(spec, options?)` | Renders a spec to PNG or SVG bytes, in Node (`@tin.so/core/node`) |
88
+ | `parseSpec(input)` | Checks a spec and fills in the defaults. Throws a `SpecError` |
89
+ | `layout(spec, options?)` | Highlights the code and places the windows, panes and popovers. Gives a `Scene` |
90
+ | `toSvg(scene, options?)` | Writes the SVG of a scene, with or without embedded fonts and metadata |
91
+ | `initPng(load)`, `svgToPng(svg, fonts, options?)` | Loads the resvg WebAssembly once, then rasterizes an SVG |
92
+ | `loadSceneFonts(scene, loadFont)` | Loads the font files that a scene uses |
93
+ | `createLink(spec, appUrl?)`, `readLink(link)` | Writes a spec into an editor link, and reads it back |
94
+ | `encodeSpec(spec)`, `decodeSpec(encoded)` | The compressed spec of a link, without the URL |
95
+ | `readImageSpec(image)` | Reads the spec from a PNG or SVG that has the metadata. Gives `undefined` for other images |
96
+ | `specJsonSchema()`, `SPEC_SCHEMA_URL` | The JSON Schema of the spec, and its URL |
97
+ | `THEME_IDS`, `CODE_FONT_IDS`, `LANGUAGE_IDS`, `ICON_IDS` | The ids that the spec accepts |
98
+ | `BACKGROUNDS`, `isCssBackground` | The background presets of the editor, and the check of a CSS background |
99
+ | `themeBackground(theme, mode)` | The background preset that a theme uses in a mode when the spec sets no background |
100
+
101
+ ### In the browser
102
+
103
+ Import from `@tin.so/core`, and give the steps that read files a loader, because the browser has no file system:
104
+
105
+ ```ts
106
+ import { initPng, layout, loadSceneFonts, svgToPng, toSvg } from "@tin.so/core";
107
+
108
+ const scene = await layout(spec);
109
+ // loadFont gets a path in the fonts/ directory of this package, for example "fira-code/fira-code-400.ttf"
110
+ const fonts = await loadSceneFonts(scene, loadFont);
111
+ await initPng(() => fetch(wasmUrl)); // the index_bg.wasm file of @resvg/resvg-wasm
112
+ const png = await svgToPng(toSvg(scene), fonts.values());
22
113
  ```
23
114
 
24
115
  ## Scripts
25
116
 
26
- - `pnpm port-themes` fetches the ray.so themes at a pinned commit and writes `src/themes/*.json`.
117
+ - `pnpm port-editor-themes` writes `src/themes/editor-themes.json` from the editor themes of Shiki, and their licenses into `EDITOR_THEMES_NOTICES.md`. It also fetches the official light variants of Dracula and Tokyo Night, which Shiki does not have, from their repositories at a pinned commit, and writes them into `src/themes/textmate/`. The list of themes is in the script.
27
118
  - `pnpm download-fonts` downloads the fonts to `fonts/` and writes their metrics to `src/fonts/fonts.generated.json`.
119
+ - `pnpm generate-icons` writes the language icons from Simple Icons.
28
120
 
29
- ## Attribution
121
+ ## License
30
122
 
31
- - The themes, the token colors and the frame styles come from [ray.so](https://github.com/raycast/ray-so) (MIT).
32
- - The fonts are under the SIL Open Font License, except Commit Mono (MIT). Each font directory contains its license.
33
- - The file icon is from [Lucide](https://lucide.dev) (ISC), the language icons from [Simple Icons](https://simpleicons.org) (CC0).
123
+ MIT. The window style comes from ray.so (MIT), and the editor themes are under MIT or Apache 2.0 (see [EDITOR_THEMES_NOTICES.md](EDITOR_THEMES_NOTICES.md)), the file icon from Lucide (ISC), the language icons from Simple Icons (CC0), and the fonts are under the SIL Open Font License, except Commit Mono (MIT). See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
@@ -0,0 +1,65 @@
1
+ # Third-party notices
2
+
3
+ `@tin.so/core` contains work of other projects. Their licenses follow.
4
+
5
+ ## ray.so
6
+
7
+ The style of the window (its colors, border, shadow and title bar) comes from [ray.so](https://github.com/raycast/ray-so).
8
+
9
+ ```
10
+ MIT License
11
+
12
+ Copyright (c) 2024 Raycast Technologies Ltd
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all
22
+ copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ SOFTWARE.
31
+ ```
32
+
33
+ ## Editor themes
34
+
35
+ The editor themes come from their own projects, through [tm-themes](https://github.com/shikijs/textmate-grammars-themes) and Shiki. [EDITOR_THEMES_NOTICES.md](EDITOR_THEMES_NOTICES.md) lists each project with its license.
36
+
37
+ ## Lucide
38
+
39
+ The file icon comes from [Lucide](https://lucide.dev).
40
+
41
+ ```
42
+ ISC License
43
+
44
+ Copyright (c) 2026 Lucide Icons and Contributors
45
+
46
+ Permission to use, copy, modify, and/or distribute this software for any
47
+ purpose with or without fee is hereby granted, provided that the above
48
+ copyright notice and this permission notice appear in all copies.
49
+
50
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
51
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
52
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
53
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
54
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
55
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
56
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
57
+ ```
58
+
59
+ ## Simple Icons
60
+
61
+ The language icons come from [Simple Icons](https://simpleicons.org), under [CC0 1.0](https://github.com/simple-icons/simple-icons/blob/develop/LICENSE.md).
62
+
63
+ ## Fonts
64
+
65
+ The fonts in `fonts/` are under the SIL Open Font License 1.1, except Commit Mono, which is under the MIT License. Each font directory contains its license.