Skip to content

Commit

Permalink
new columns to tab transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
kmlbgn committed Dec 17, 2023
1 parent 5bccc3e commit 2ba658d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/plugins/ColumnListTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,41 @@ import { NotionToMarkdown } from "notion-to-md";
import { NotionBlock } from "../types";
import { IPlugin } from "./pluginTypes";

async function notionColumnListToMarkdown(
async function notionColumnListToTabs(
notionToMarkdown: NotionToMarkdown,
getBlockChildren: (id: string) => Promise<NotionBlock[]>,
block: NotionBlock
): Promise<string> {
// Enhance: The @notionhq/client, which uses the official API, cannot yet get at column formatting information (column_ratio)
// However https://github1s.com/NotionX/react-notion-x/blob/master/packages/react-notion-x/src/block.tsx#L528 can get it.
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
const { id, has_children } = block as any;

if (!has_children) return "";

const column_list_children = await getBlockChildren(id);
const columnListChildren = await getBlockChildren(id);

const column_list_promise = column_list_children.map(
async column => await notionToMarkdown.blockToMarkdown(column)
);
const tabItemsPromises = columnListChildren.map(async (column) => {
const columnChildren = await getBlockChildren(column.id);

const columns: string[] = await Promise.all(column_list_promise);
let label = "Tab";
if (columnChildren.length > 0 && columnChildren[0].type === 'heading_1') {
const richTextItems = columnChildren[0].heading_1.rich_text;

if (richTextItems.length > 0 && richTextItems[0].type === 'text') {
label = richTextItems[0].text.content; // Directly accessing the content of the first text item
}
}

return `<div class='notion-row'>\n${columns.join("\n\n")}\n</div>`;
const markdownContent = await Promise.all(
columnChildren.slice(1).map(
async child => await notionToMarkdown.blockToMarkdown(child)
)
);
const content = markdownContent.join("\n");

return `<TabItem value="${label.toLowerCase()}" label="${label}">\n${content}\n</TabItem>`;
});

const tabItems = await Promise.all(tabItemsPromises);
return `<Tabs>\n${tabItems.join("\n")}</Tabs>`;
}

export const standardColumnListTransformer: IPlugin = {
Expand All @@ -30,7 +45,7 @@ export const standardColumnListTransformer: IPlugin = {
{
type: "column_list",
getStringFromBlock: (context, block) =>
notionColumnListToMarkdown(
notionColumnListToTabs(
context.notionToMarkdown,
context.getBlockChildren,
block
Expand Down
6 changes: 6 additions & 0 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export async function getMarkdownFromNotionBlocks(
// Filter out child page blocks before converting to markdown because there is no case where we want the content of an actual child page to be appended to the index.md of it's category level index.md
const filteredBlocks = blocks.filter(block => block.type !== 'child_page');

const containsColumnList = filteredBlocks.some(block => block.type === 'column_list');
verbose(
`Column_list type detected? >> "${containsColumnList}" `
);


// changes to the blocks we get from notion API
doNotionBlockTransforms(filteredBlocks, config);

Expand Down

0 comments on commit 2ba658d

Please sign in to comment.