@@ -16,6 +16,8 @@ import {
1616import * as md from "./utils/md" ;
1717import { getBlockChildren } from "./utils/notion" ;
1818
19+ type LinkTransformer = ( text : string , href : string ) => string ;
20+
1921/**
2022 * Converts a Notion page to Markdown.
2123 */
@@ -44,6 +46,14 @@ export class NotionToMarkdown {
4446 return this ;
4547 }
4648
49+ setLinkTransformer ( fn : LinkTransformer ) {
50+ this . linkTransformer = fn ;
51+ }
52+
53+ linkTransformer ( text : string , href : string ) {
54+ return md . link ( text , href ) ;
55+ }
56+
4757 /**
4858 * Converts Markdown Blocks to string
4959 * @param {MdBlock[] } mdBlocks - Array of markdown blocks
@@ -120,8 +130,9 @@ export class NotionToMarkdown {
120130 mdOutput [ pageIdentifier ] = mdOutput [ pageIdentifier ] || "" ;
121131 if ( mdstr [ childPageTitle ] ) {
122132 // child page heading followed by child page content
123- mdOutput [ pageIdentifier ] +=
124- `\n${ childPageTitle } \n${ mdstr [ childPageTitle ] } ` ;
133+ mdOutput [
134+ pageIdentifier
135+ ] += `\n${ childPageTitle } \n${ mdstr [ childPageTitle ] } ` ;
125136 }
126137 }
127138 } else if ( mdBlocks . type === "toggle" ) {
@@ -159,8 +170,7 @@ export class NotionToMarkdown {
159170 mdOutput [ pageIdentifier ] += "\n" ;
160171 } else if ( mdBlocks . type === "callout" ) {
161172 // do nothing the callout block is already processed
162- }
163- else {
173+ } else {
164174 let mdstr = this . toMarkdownString (
165175 mdBlocks . children ,
166176 pageIdentifier ,
@@ -375,7 +385,7 @@ export class NotionToMarkdown {
375385 title = matches ? matches [ 0 ] : type ;
376386 }
377387
378- return md . link ( title , link ) ;
388+ return this . linkTransformer ( title , link ) ;
379389 }
380390 }
381391 break ;
@@ -399,7 +409,8 @@ export class NotionToMarkdown {
399409 } ;
400410 }
401411
402- if ( blockContent ) return md . link ( title , blockContent . url ) ;
412+ if ( blockContent )
413+ return this . linkTransformer ( title , blockContent . url ) ;
403414 }
404415 break ;
405416
@@ -430,7 +441,7 @@ export class NotionToMarkdown {
430441 const tableRows = await getBlockChildren ( this . notionClient , id , 100 ) ;
431442 let rowsPromise = tableRows ?. map ( async ( row ) => {
432443 const { type } = row as any ;
433- if ( type !== ' table_row' ) return
444+ if ( type !== " table_row" ) return ;
434445 const cells = ( row as any ) . table_row [ "cells" ] ;
435446
436447 /**
@@ -490,7 +501,7 @@ export class NotionToMarkdown {
490501 plain_text = this . annotatePlainText ( plain_text , annotations ) ;
491502
492503 if ( content [ "href" ] )
493- plain_text = md . link ( plain_text , content [ "href" ] ) ;
504+ plain_text = this . linkTransformer ( plain_text , content [ "href" ] ) ;
494505
495506 parsedData += plain_text ;
496507 } ) ;
@@ -500,9 +511,11 @@ export class NotionToMarkdown {
500511 switch ( type ) {
501512 case "code" :
502513 {
503- const codeContent = block . code . rich_text . map ( ( t : any ) => t . plain_text ) . join ( "\n" ) ;
504- const language = block . code . language || "plaintext" ;
505- parsedData = md . codeBlock ( codeContent , language ) ;
514+ const codeContent = block . code . rich_text
515+ . map ( ( t : any ) => t . plain_text )
516+ . join ( "\n" ) ;
517+ const language = block . code . language || "plaintext" ;
518+ parsedData = md . codeBlock ( codeContent , language ) ;
506519 }
507520 break ;
508521
0 commit comments