@notion-headless-cms/notion-katex 0.1.7 → 0.1.9

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/index.d.mts CHANGED
@@ -1,43 +1,30 @@
1
- import { BlockEnricher } from "@notion-headless-cms/notion-orm";
1
+ import { ContentExtension } from "@notion-headless-cms/notion-orm";
2
2
 
3
3
  //#region src/index.d.ts
4
4
  /** `notionKatex()` のオプション。KaTeX の renderToString に渡すオプションのサブセット。 */
5
5
  interface NotionKatexOptions {
6
6
  /** 数式をブロック表示(display mode)にする。デフォルト: true。 */
7
7
  displayMode?: boolean;
8
- /** レンダリングエラーを throw する。false の場合はエラー時に __cachedHtml を設定しない。デフォルト: false。 */
8
+ /** レンダリングエラーを throw する。false の場合はエラー時に数式をそのまま表示する。デフォルト: false。 */
9
9
  throwOnError?: boolean;
10
10
  /** KaTeX マクロ定義。 */
11
11
  macros?: Record<string, string>;
12
12
  }
13
- /** equation ブロックに `__cachedHtml` が付与された拡張型。 */
14
- type EquationBlockWithCachedHtml = {
15
- type: "equation";
16
- equation: {
17
- expression: string;
18
- __cachedHtml: string;
19
- };
20
- };
21
13
  /**
22
- * fetch 時に Notion の equation ブロックを KaTeX HTML 化し、
23
- * `block.equation.__cachedHtml` に埋め込む `BlockEnricher` を返す。
14
+ * Notion の equation ブロック・インライン数式を KaTeX で描画する `ContentExtension` を返す。
24
15
  *
25
- * `react-renderer` Equation スタブは `__cachedHtml` があれば
26
- * `dangerouslySetInnerHTML` で描画するため、Workers バンドルから katex を除外できる。
16
+ * markdown 戦略: `rehype-katex` unified パイプラインへ注入する。
17
+ * `remarkMath` はパイプラインに既存のため `rehypePlugins` のみ追加。
27
18
  *
28
19
  * @example
29
20
  * ```ts
30
21
  * import { notionKatex } from "@notion-headless-cms/notion-katex";
31
- * import { createNotionCollection } from "@notion-headless-cms/notion-orm";
32
22
  *
33
- * source: createNotionCollection({
34
- * token: process.env.NOTION_TOKEN,
35
- * dataSourceId: "...",
36
- * enrichers: [notionKatex({ displayMode: true })],
37
- * });
23
+ * // markdown 戦略
24
+ * <Renderer content={item.content} extensions={[notionKatex()]} />
38
25
  * ```
39
26
  */
40
- declare function notionKatex(opts?: NotionKatexOptions): BlockEnricher;
27
+ declare function notionKatex(opts?: NotionKatexOptions): ContentExtension;
41
28
  //#endregion
42
- export { EquationBlockWithCachedHtml, NotionKatexOptions, notionKatex };
29
+ export { NotionKatexOptions, notionKatex };
43
30
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,22 +1,17 @@
1
- import katex from "katex";
1
+ import rehypeKatex from "rehype-katex";
2
2
  //#region src/index.ts
3
3
  /**
4
- * fetch 時に Notion の equation ブロックを KaTeX HTML 化し、
5
- * `block.equation.__cachedHtml` に埋め込む `BlockEnricher` を返す。
4
+ * Notion の equation ブロック・インライン数式を KaTeX で描画する `ContentExtension` を返す。
6
5
  *
7
- * `react-renderer` Equation スタブは `__cachedHtml` があれば
8
- * `dangerouslySetInnerHTML` で描画するため、Workers バンドルから katex を除外できる。
6
+ * markdown 戦略: `rehype-katex` unified パイプラインへ注入する。
7
+ * `remarkMath` はパイプラインに既存のため `rehypePlugins` のみ追加。
9
8
  *
10
9
  * @example
11
10
  * ```ts
12
11
  * import { notionKatex } from "@notion-headless-cms/notion-katex";
13
- * import { createNotionCollection } from "@notion-headless-cms/notion-orm";
14
12
  *
15
- * source: createNotionCollection({
16
- * token: process.env.NOTION_TOKEN,
17
- * dataSourceId: "...",
18
- * enrichers: [notionKatex({ displayMode: true })],
19
- * });
13
+ * // markdown 戦略
14
+ * <Renderer content={item.content} extensions={[notionKatex()]} />
20
15
  * ```
21
16
  */
22
17
  function notionKatex(opts) {
@@ -25,21 +20,9 @@ function notionKatex(opts) {
25
20
  throwOnError: opts?.throwOnError ?? false,
26
21
  ...opts?.macros && { macros: opts.macros }
27
22
  };
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
- }
23
+ return { getMarkdownPlugins() {
24
+ return { rehypePlugins: [[rehypeKatex, katexOpts]] };
25
+ } };
43
26
  }
44
27
  //#endregion
45
28
  export { notionKatex };
@@ -1 +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,OAAO;CAC5C;CAEA,OAAO,OACL,WACmC;EACnC,aAAa,QAAQ,SAAS;EAC9B,OAAO;CACT;AACF;AAQA,SAAS,aACP,QACA,MACM;CACN,KAAK,MAAM,SAAS,QAAQ;EAC1B,IAAI,MAAM,SAAS,YAAY;GAC7B,MAAM,KAAK,MAAM;GAIjB,IAAI;IACF,GAAG,eAAe,MAAM,eAAe,GAAG,YAAY,IAAI;GAC5D,QAAQ,CAER;EACF;EACA,IAAI,MAAM,UAAU,QAClB,aAAa,MAAM,UAAU,IAAI;CAErC;AACF"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { ContentExtension } from \"@notion-headless-cms/notion-orm\";\nimport rehypeKatex from \"rehype-katex\";\n\n/** `notionKatex()` のオプション。KaTeX の renderToString に渡すオプションのサブセット。 */\nexport interface NotionKatexOptions {\n /** 数式をブロック表示(display mode)にする。デフォルト: true。 */\n displayMode?: boolean;\n /** レンダリングエラーを throw する。false の場合はエラー時に数式をそのまま表示する。デフォルト: false。 */\n throwOnError?: boolean;\n /** KaTeX マクロ定義。 */\n macros?: Record<string, string>;\n}\n\n/**\n * Notion の equation ブロック・インライン数式を KaTeX で描画する `ContentExtension` を返す。\n *\n * markdown 戦略: `rehype-katex` unified パイプラインへ注入する。\n * `remarkMath` はパイプラインに既存のため `rehypePlugins` のみ追加。\n *\n * @example\n * ```ts\n * import { notionKatex } from \"@notion-headless-cms/notion-katex\";\n *\n * // markdown 戦略\n * <Renderer content={item.content} extensions={[notionKatex()]} />\n * ```\n */\nexport function notionKatex(opts?: NotionKatexOptions): ContentExtension {\n const katexOpts = {\n displayMode: opts?.displayMode ?? true,\n throwOnError: opts?.throwOnError ?? false,\n ...(opts?.macros && { macros: opts.macros }),\n };\n return {\n getMarkdownPlugins() {\n return { rehypePlugins: [[rehypeKatex, katexOpts]] };\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA2BA,SAAgB,YAAY,MAA6C;CACvE,MAAM,YAAY;EAChB,aAAa,MAAM,eAAe;EAClC,cAAc,MAAM,gBAAgB;EACpC,GAAI,MAAM,UAAU,EAAE,QAAQ,KAAK,OAAO;CAC5C;CACA,OAAO,EACL,qBAAqB;EACnB,OAAO,EAAE,eAAe,CAAC,CAAC,aAAa,SAAS,CAAC,EAAE;CACrD,EACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notion-headless-cms/notion-katex",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Notion equation ブロックを fetch 時に KaTeX で pre-render し、Workers バンドルから katex を除外する拡張パッケージ",
5
5
  "keywords": [
6
6
  "notion",
@@ -41,11 +41,14 @@
41
41
  },
42
42
  "peerDependencies": {
43
43
  "katex": "^0.16.11",
44
- "@notion-headless-cms/notion-orm": "0.1.29"
44
+ "@notion-headless-cms/notion-orm": "0.1.31"
45
45
  },
46
46
  "devDependencies": {
47
- "katex": "^0.16.46",
48
- "@notion-headless-cms/notion-orm": "0.1.29"
47
+ "katex": "^0.16.47",
48
+ "@notion-headless-cms/notion-orm": "0.1.31"
49
+ },
50
+ "dependencies": {
51
+ "rehype-katex": "^7.0.1"
49
52
  },
50
53
  "scripts": {
51
54
  "build": "tsdown src/index.ts --format esm --dts --sourcemap --out-dir dist",