Skip to content

Commit 6221ae9

Browse files
authored
Merge pull request #25 from sillsdev/RefactorTransformers
Refactor transformers
2 parents 9bd43e0 + 07f66e0 commit 6221ae9

File tree

9 files changed

+247
-218
lines changed

9 files changed

+247
-218
lines changed

src/CustomTranformers.ts

Lines changed: 0 additions & 215 deletions
This file was deleted.

src/pull.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "./images";
1212

1313
import { tweakForDocusaurus } from "./DocusaurusTweaks";
14-
import { setupCustomTransformers } from "./CustomTranformers";
14+
import { setupCustomTransformers } from "./transformers/CustomTransformers";
1515
import * as Path from "path";
1616
import { error, heading, info, logDebug, verbose, warning } from "./log";
1717
import { convertInternalLinks } from "./links";
@@ -235,7 +235,7 @@ async function outputPage(page: NotionPage) {
235235
);
236236

237237
// One half of a horrible hack to make heading links work.
238-
// See the other half and explanation in CustomTransformers.ts => headingCustomTransformer.
238+
// See the other half and explanation in HeadingTransformer.ts.
239239
for (const block_t of blocks) {
240240
const block = block_t as any;
241241
if (block.type.startsWith("heading"))
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { NotionToMarkdown } from "notion-to-md";
22
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types";
33
import { Client } from "@notionhq/client";
4-
import { getBlockChildren } from "./CustomTranformers";
4+
import { getBlockChildren } from "./CustomTransformers";
55

6+
// In Notion, you can make a callout and change its emoji. We map 5 of these
7+
// to the 5 Docusaurus admonition styles.
8+
// This is mostly a copy of the callout code from notion-to-md. The change is to output docusaurus
9+
// admonitions instead of emulating a callout with markdown > syntax.
10+
// Note: I haven't yet tested this with any emoji except "💡"/"tip", nor the case where the
11+
// callout has-children. Not even sure what that would mean, since the document I was testing
12+
// with has quite complex markup inside the callout, but still takes the no-children branch.
613
export async function notionCalloutToAdmonition(
714
notionToMarkdown: NotionToMarkdown,
815
notionClient: Client,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Client } from "@notionhq/client";
2+
import { NotionToMarkdown } from "notion-to-md";
3+
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types";
4+
import { getBlockChildren } from "./CustomTransformers";
5+
6+
export async function notionColumnListToMarkdown(
7+
notionToMarkdown: NotionToMarkdown,
8+
notionClient: Client,
9+
block: ListBlockChildrenResponseResult
10+
): Promise<string> {
11+
// Enhance: The @notionhq/client, which uses the official API, cannot yet get at column formatting information (column_ratio)
12+
// However https://github1s.com/NotionX/react-notion-x/blob/master/packages/react-notion-x/src/block.tsx#L528 can get it.
13+
const { id, has_children } = block as any; // "any" because the notion api type system is complex with a union that don't know how to help TS to cope with
14+
15+
if (!has_children) return "";
16+
17+
const column_list_children = await getBlockChildren(notionClient, id, 100);
18+
19+
const column_list_promise = column_list_children.map(
20+
async column => await notionToMarkdown.blockToMarkdown(column)
21+
);
22+
23+
const columns: string[] = await Promise.all(column_list_promise);
24+
25+
return `<div class='notion-row'>\n${columns.join("\n\n")}\n</div>`;
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Client } from "@notionhq/client";
2+
import { NotionToMarkdown } from "notion-to-md";
3+
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types";
4+
import { getBlockChildren } from "./CustomTransformers";
5+
6+
export async function notionColumnToMarkdown(
7+
notionToMarkdown: NotionToMarkdown,
8+
notionClient: Client,
9+
block: ListBlockChildrenResponseResult
10+
): Promise<string> {
11+
//console.log(JSON.stringify(block));
12+
const { id, has_children } = block as any; // "any" because the notion api type system is complex with a union that don't know how to help TS to cope with
13+
14+
if (!has_children) return "";
15+
16+
const children = await getBlockChildren(notionClient, id, 100);
17+
18+
const childrenPromise = children.map(
19+
async column => await notionToMarkdown.blockToMarkdown(column)
20+
);
21+
22+
const childrenStrings: string[] = await Promise.all(childrenPromise);
23+
24+
// note: it would look better in the markup with \n, but that
25+
// causes notion-to-md to give us ":::A" instead of \n for some reason.
26+
return `<div class='notion-column'>\n\n${childrenStrings.join(
27+
"\n\n"
28+
)}\n\n</div>`;
29+
}

0 commit comments

Comments
 (0)