@nast/notion2typst 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Martí Pardo
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 ADDED
@@ -0,0 +1,9 @@
1
+ # Notion to Typst
2
+ This package is simply a wrapper over the following packages:
3
+
4
+ 1. notion2nbt
5
+ 2. nbt2nast
6
+ 3. nast2typst
7
+ 4. nast-fetch-images
8
+
9
+ It uses the other packages to provide a more direct conversion: given a notion pageId and notion token, you get the typst code and the images in the page.
@@ -0,0 +1,79 @@
1
+ import { Notion2NBT, NotionBlock } from "@nast/notion2nbt";
2
+ import { nbt2nast } from "@nast/nbt2nast";
3
+ import { nast2typst } from "@nast/nast2typst";
4
+ import { fetchImagesFromBlocks } from "@nast/nast-fetch-images";
5
+ import { DownloadedImage, DownloadedImage as DownloadedImage$1, NASTNode, NASTRoot, NASTRoot as NASTRoot$1, NBTBlock } from "@nast/types";
6
+
7
+ //#region src/index.d.ts
8
+
9
+ /**
10
+ * Image data from the fetchImagesFromBlocks function
11
+ * @deprecated Use DownloadedImage from @nast/types instead
12
+ */
13
+ type ImageData = DownloadedImage$1;
14
+ /**
15
+ * Options for the notion2typst function
16
+ */
17
+ interface Notion2TypstOptions {
18
+ /** Notion API token */
19
+ notionToken: string;
20
+ /** Notion page ID to convert */
21
+ pageId: string;
22
+ /** Whether to fetch images from the page (default: true) */
23
+ fetchImages?: boolean;
24
+ /** Whether to preserve block IDs in NAST (default: false) */
25
+ preserveBlockId?: boolean;
26
+ /** Whether to enable caching in Notion2NBT (default: false) */
27
+ enableCache?: boolean;
28
+ }
29
+ /**
30
+ * Result of the notion2typst conversion
31
+ */
32
+ interface Notion2TypstResult {
33
+ /** The generated Typst code */
34
+ typstCode: string;
35
+ /** Downloaded images (if fetchImages was true) */
36
+ images: DownloadedImage$1[];
37
+ /** Image fetch statistics (if fetchImages was true) */
38
+ imageStats: {
39
+ /** Total number of image blocks found */
40
+ totalImages: number;
41
+ /** Number of successfully downloaded images */
42
+ downloaded: number;
43
+ /** Number of expired images */
44
+ expired: number;
45
+ /** Number of external images */
46
+ external: number;
47
+ /** Number of file images */
48
+ file: number;
49
+ };
50
+ /** The intermediate NAST representation */
51
+ nast: NASTRoot$1;
52
+ }
53
+ /**
54
+ * Main notion2typst function - converts a Notion page to Typst code
55
+ *
56
+ * This is a wrapper that combines:
57
+ * 1. notion2nbt - fetch page from Notion API
58
+ * 2. nbt2nast - convert Notion blocks to NAST
59
+ * 3. nast2typst - convert NAST to Typst code
60
+ * 4. fetchImagesFromBlocks - download images (optional)
61
+ *
62
+ * @param options - Configuration options
63
+ * @returns Typst code and downloaded images
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * const result = await notion2typst({
68
+ * notionToken: 'your-token',
69
+ * pageId: 'your-page-id',
70
+ * fetchImages: true
71
+ * });
72
+ *
73
+ * console.log(result.typstCode);
74
+ * console.log(`Downloaded ${result.images.length} images`);
75
+ * ```
76
+ */
77
+ declare function notion2typst(options: Notion2TypstOptions): Promise<Notion2TypstResult>;
78
+ //#endregion
79
+ export { type DownloadedImage, ImageData, type NASTNode, type NASTRoot, type NBTBlock, Notion2NBT, Notion2TypstOptions, Notion2TypstResult, type NotionBlock, fetchImagesFromBlocks, nast2typst, nbt2nast, notion2typst };
package/dist/index.js ADDED
@@ -0,0 +1,76 @@
1
+ import { Notion2NBT } from "@nast/notion2nbt";
2
+ import { nbt2nast } from "@nast/nbt2nast";
3
+ import { nast2typst } from "@nast/nast2typst";
4
+ import { fetchImagesFromBlocks } from "@nast/nast-fetch-images";
5
+
6
+ //#region src/index.ts
7
+ /**
8
+ * Main notion2typst function - converts a Notion page to Typst code
9
+ *
10
+ * This is a wrapper that combines:
11
+ * 1. notion2nbt - fetch page from Notion API
12
+ * 2. nbt2nast - convert Notion blocks to NAST
13
+ * 3. nast2typst - convert NAST to Typst code
14
+ * 4. fetchImagesFromBlocks - download images (optional)
15
+ *
16
+ * @param options - Configuration options
17
+ * @returns Typst code and downloaded images
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const result = await notion2typst({
22
+ * notionToken: 'your-token',
23
+ * pageId: 'your-page-id',
24
+ * fetchImages: true
25
+ * });
26
+ *
27
+ * console.log(result.typstCode);
28
+ * console.log(`Downloaded ${result.images.length} images`);
29
+ * ```
30
+ */
31
+ async function notion2typst(options) {
32
+ const { notionToken, pageId, fetchImages = true, preserveBlockId = false, enableCache = false } = options;
33
+ console.log("Fetching page from Notion...");
34
+ const nbtPage = await new Notion2NBT({
35
+ auth: notionToken,
36
+ enableCache
37
+ }).getPageV2(pageId);
38
+ console.log("✓ Page fetched successfully");
39
+ console.log("Converting NBT to NAST...");
40
+ const nast = nbt2nast(nbtPage, { preserveBlockId });
41
+ console.log("✓ NBT converted to NAST");
42
+ console.log("Converting NAST to Typst...");
43
+ const typstCode = nast2typst(nast);
44
+ console.log("✓ NAST converted to Typst");
45
+ let images = [];
46
+ let imageStats = {
47
+ totalImages: 0,
48
+ downloaded: 0,
49
+ expired: 0,
50
+ external: 0,
51
+ file: 0
52
+ };
53
+ if (fetchImages) {
54
+ console.log("Fetching images...");
55
+ const imageResult = await fetchImagesFromBlocks(nast.children);
56
+ images = imageResult.images;
57
+ imageStats = {
58
+ totalImages: imageResult.imageCount,
59
+ downloaded: images.length,
60
+ expired: imageResult.expiredCount,
61
+ external: imageResult.imageExternalCount,
62
+ file: imageResult.imageFileCount
63
+ };
64
+ console.log(`✓ Downloaded ${images.length}/${imageResult.imageCount} images (${imageResult.expiredCount} expired)`);
65
+ }
66
+ console.log("Done!");
67
+ return {
68
+ typstCode,
69
+ images,
70
+ imageStats,
71
+ nast
72
+ };
73
+ }
74
+
75
+ //#endregion
76
+ export { Notion2NBT, fetchImagesFromBlocks, nast2typst, nbt2nast, notion2typst };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@nast/notion2typst",
3
+ "type": "module",
4
+ "version": "0.2.0",
5
+ "description": "A wrapper around nbt2nast and nast2typst to convert Notion content directly to Typst",
6
+ "keywords": [
7
+ "notion",
8
+ "ast",
9
+ "nast",
10
+ "typst"
11
+ ],
12
+ "author": "Mapaor <pardo.marti@gmail.com>",
13
+ "license": "MIT",
14
+ "homepage": "https://github.com/Mapaor/nast/tree/main/packages/notion2typst",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/Mapaor/nast.git",
18
+ "directory": "packages/notion2typst"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/Mapaor/nast/issues"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "exports": {
27
+ ".": "./dist/index.js",
28
+ "./package.json": "./package.json"
29
+ },
30
+ "main": "./dist/index.js",
31
+ "module": "./dist/index.js",
32
+ "types": "./dist/index-CJVMSA--.d.ts",
33
+ "files": [
34
+ "dist",
35
+ "README.md"
36
+ ],
37
+ "dependencies": {
38
+ "@nast/nbt2nast": "0.4.0",
39
+ "@nast/nast-fetch-images": "0.2.0",
40
+ "@nast/nast2typst": "0.3.0",
41
+ "@nast/notion2nbt": "0.6.0",
42
+ "@nast/types": "0.1.0"
43
+ },
44
+ "scripts": {
45
+ "build": "tsdown",
46
+ "dev": "tsdown --watch",
47
+ "test": "vitest",
48
+ "typecheck": "tsc --noEmit",
49
+ "example": "node --env-file=.env.local --import tsx scripts/example.ts"
50
+ }
51
+ }