@notion-headless-cms/notion-katex 0.1.1

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) 2024 kjfsm
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.
@@ -0,0 +1,43 @@
1
+ import { BlockEnricher } from "@notion-headless-cms/notion-orm";
2
+
3
+ //#region src/index.d.ts
4
+ /** `notionKatex()` のオプション。KaTeX の renderToString に渡すオプションのサブセット。 */
5
+ interface NotionKatexOptions {
6
+ /** 数式をブロック表示(display mode)にする。デフォルト: true。 */
7
+ displayMode?: boolean;
8
+ /** レンダリングエラーを throw する。false の場合はエラー時に __cachedHtml を設定しない。デフォルト: false。 */
9
+ throwOnError?: boolean;
10
+ /** KaTeX マクロ定義。 */
11
+ macros?: Record<string, string>;
12
+ }
13
+ /** equation ブロックに `__cachedHtml` が付与された拡張型。 */
14
+ type EquationBlockWithCachedHtml = {
15
+ type: "equation";
16
+ equation: {
17
+ expression: string;
18
+ __cachedHtml: string;
19
+ };
20
+ };
21
+ /**
22
+ * fetch 時に Notion の equation ブロックを KaTeX で HTML 化し、
23
+ * `block.equation.__cachedHtml` に埋め込む `BlockEnricher` を返す。
24
+ *
25
+ * `react-renderer` の Equation スタブは `__cachedHtml` があれば
26
+ * `dangerouslySetInnerHTML` で描画するため、Workers バンドルから katex を除外できる。
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { notionKatex } from "@notion-headless-cms/notion-katex";
31
+ * import { createNotionCollection } from "@notion-headless-cms/notion-orm";
32
+ *
33
+ * source: createNotionCollection({
34
+ * token: process.env.NOTION_TOKEN,
35
+ * dataSourceId: "...",
36
+ * enrichers: [notionKatex({ displayMode: true })],
37
+ * });
38
+ * ```
39
+ */
40
+ declare function notionKatex(opts?: NotionKatexOptions): BlockEnricher;
41
+ //#endregion
42
+ export { EquationBlockWithCachedHtml, NotionKatexOptions, notionKatex };
43
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs ADDED
@@ -0,0 +1,47 @@
1
+ import katex from "katex";
2
+ //#region src/index.ts
3
+ /**
4
+ * fetch 時に Notion の equation ブロックを KaTeX で HTML 化し、
5
+ * `block.equation.__cachedHtml` に埋め込む `BlockEnricher` を返す。
6
+ *
7
+ * `react-renderer` の Equation スタブは `__cachedHtml` があれば
8
+ * `dangerouslySetInnerHTML` で描画するため、Workers バンドルから katex を除外できる。
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { notionKatex } from "@notion-headless-cms/notion-katex";
13
+ * import { createNotionCollection } from "@notion-headless-cms/notion-orm";
14
+ *
15
+ * source: createNotionCollection({
16
+ * token: process.env.NOTION_TOKEN,
17
+ * dataSourceId: "...",
18
+ * enrichers: [notionKatex({ displayMode: true })],
19
+ * });
20
+ * ```
21
+ */
22
+ function notionKatex(opts) {
23
+ const katexOpts = {
24
+ displayMode: opts?.displayMode ?? true,
25
+ throwOnError: opts?.throwOnError ?? false,
26
+ ...opts?.macros && { macros: opts.macros }
27
+ };
28
+ return async (blocks) => {
29
+ enrichBlocks(blocks, katexOpts);
30
+ return blocks;
31
+ };
32
+ }
33
+ function enrichBlocks(blocks, opts) {
34
+ for (const block of blocks) {
35
+ if (block.type === "equation") {
36
+ const eq = block.equation;
37
+ try {
38
+ eq.__cachedHtml = katex.renderToString(eq.expression, opts);
39
+ } catch {}
40
+ }
41
+ if (block.children?.length) enrichBlocks(block.children, opts);
42
+ }
43
+ }
44
+ //#endregion
45
+ export { notionKatex };
46
+
47
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type {\n BlockEnricher,\n NotionBlockTreeNode,\n} from \"@notion-headless-cms/notion-orm\";\nimport katex from \"katex\";\n\n/** `notionKatex()` のオプション。KaTeX の renderToString に渡すオプションのサブセット。 */\nexport interface NotionKatexOptions {\n /** 数式をブロック表示(display mode)にする。デフォルト: true。 */\n displayMode?: boolean;\n /** レンダリングエラーを throw する。false の場合はエラー時に __cachedHtml を設定しない。デフォルト: false。 */\n throwOnError?: boolean;\n /** KaTeX マクロ定義。 */\n macros?: Record<string, string>;\n}\n\n/** equation ブロックに `__cachedHtml` が付与された拡張型。 */\nexport type EquationBlockWithCachedHtml = {\n type: \"equation\";\n equation: { expression: string; __cachedHtml: string };\n};\n\n/**\n * fetch 時に Notion の equation ブロックを KaTeX で HTML 化し、\n * `block.equation.__cachedHtml` に埋め込む `BlockEnricher` を返す。\n *\n * `react-renderer` の Equation スタブは `__cachedHtml` があれば\n * `dangerouslySetInnerHTML` で描画するため、Workers バンドルから katex を除外できる。\n *\n * @example\n * ```ts\n * import { notionKatex } from \"@notion-headless-cms/notion-katex\";\n * import { createNotionCollection } from \"@notion-headless-cms/notion-orm\";\n *\n * source: createNotionCollection({\n * token: process.env.NOTION_TOKEN,\n * dataSourceId: \"...\",\n * enrichers: [notionKatex({ displayMode: true })],\n * });\n * ```\n */\nexport function notionKatex(opts?: NotionKatexOptions): BlockEnricher {\n const katexOpts = {\n displayMode: opts?.displayMode ?? true,\n throwOnError: opts?.throwOnError ?? false,\n ...(opts?.macros && { macros: opts.macros }),\n };\n\n return async (\n blocks: NotionBlockTreeNode[],\n ): Promise<NotionBlockTreeNode[]> => {\n enrichBlocks(blocks, katexOpts);\n return blocks;\n };\n}\n\ntype KatexRenderOptions = {\n displayMode: boolean;\n throwOnError: boolean;\n macros?: Record<string, string>;\n};\n\nfunction enrichBlocks(\n blocks: NotionBlockTreeNode[],\n opts: KatexRenderOptions,\n): void {\n for (const block of blocks) {\n if (block.type === \"equation\") {\n const eq = block.equation as {\n expression: string;\n __cachedHtml?: string;\n };\n try {\n eq.__cachedHtml = katex.renderToString(eq.expression, opts);\n } catch {\n // レンダリング失敗時は __cachedHtml を設定せず、react-renderer の <pre> フォールバックを使う\n }\n }\n if (block.children?.length) {\n enrichBlocks(block.children, opts);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,YAAY,MAA0C;CACpE,MAAM,YAAY;EAChB,aAAa,MAAM,eAAe;EAClC,cAAc,MAAM,gBAAgB;EACpC,GAAI,MAAM,UAAU,EAAE,QAAQ,KAAK,QAAQ;EAC5C;AAED,QAAO,OACL,WACmC;AACnC,eAAa,QAAQ,UAAU;AAC/B,SAAO;;;AAUX,SAAS,aACP,QACA,MACM;AACN,MAAK,MAAM,SAAS,QAAQ;AAC1B,MAAI,MAAM,SAAS,YAAY;GAC7B,MAAM,KAAK,MAAM;AAIjB,OAAI;AACF,OAAG,eAAe,MAAM,eAAe,GAAG,YAAY,KAAK;WACrD;;AAIV,MAAI,MAAM,UAAU,OAClB,cAAa,MAAM,UAAU,KAAK"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@notion-headless-cms/notion-katex",
3
+ "version": "0.1.1",
4
+ "description": "Notion equation ブロックを fetch 時に KaTeX で pre-render し、Workers バンドルから katex を除外する拡張パッケージ",
5
+ "keywords": [
6
+ "notion",
7
+ "headless-cms",
8
+ "katex",
9
+ "math",
10
+ "equation",
11
+ "pre-render",
12
+ "typescript"
13
+ ],
14
+ "license": "MIT",
15
+ "homepage": "https://github.com/kjfsm/notion-headless-cms#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/kjfsm/notion-headless-cms/issues"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/kjfsm/notion-headless-cms.git",
22
+ "directory": "packages/notion-katex"
23
+ },
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.mts",
29
+ "import": "./dist/index.mjs"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "engines": {
36
+ "node": ">=24"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "provenance": true
41
+ },
42
+ "peerDependencies": {
43
+ "katex": "^0.16.11",
44
+ "@notion-headless-cms/notion-orm": "0.1.23"
45
+ },
46
+ "devDependencies": {
47
+ "katex": "^0.16.11",
48
+ "@notion-headless-cms/notion-orm": "0.1.23"
49
+ },
50
+ "scripts": {
51
+ "build": "tsdown src/index.ts --format esm --dts --sourcemap --out-dir dist",
52
+ "typecheck": "tsc --noEmit",
53
+ "test": "vitest run",
54
+ "test:coverage": "vitest run --coverage --coverage.reporter=lcov --coverage.reporter=text",
55
+ "publint": "publint --strict",
56
+ "attw": "attw --pack . --profile esm-only"
57
+ }
58
+ }