@@ -97,6 +97,39 @@ export function setupCustomTransformers(
97
97
numberedListTransformer ( notionToMarkdown , notionClient , block )
98
98
) ;
99
99
100
+ const headingCustomTransformer = async (
101
+ block : ListBlockChildrenResponseResult
102
+ ) => {
103
+ // This is the other half of the horrible hack in pull.ts which sets the type
104
+ // of every heading_n to my_heading_n. We have to do this because if
105
+ // we simply set a custom transformer to heading_n, it will keep
106
+ // recursively calling this code, with blockToMarkdown using the custom transformer
107
+ // over and over. Instead, we want blockToMarkdown to give us the normal
108
+ // result, to which we will append the block ID to enable heading links.
109
+ ( block as any ) . type = ( block as any ) . type . replace ( "my_" , "" ) ;
110
+
111
+ const unmodifiedMarkdown = await notionToMarkdown . blockToMarkdown ( block ) ;
112
+ // For some reason, inline links come in without the dashes, so we have to strip
113
+ // dashes here to match them.
114
+ const blockIdSansDashes = block . id . replaceAll ( "-" , "" ) ;
115
+ // To make heading links work in docusaurus, you make them look like:
116
+ // ### Hello World {#my-explicit-id}
117
+ // See https://docusaurus.io/docs/markdown-features/toc#heading-ids.
118
+ return `${ unmodifiedMarkdown } {#${ blockIdSansDashes } }` ;
119
+ } ;
120
+ notionToMarkdown . setCustomTransformer (
121
+ "my_heading_1" ,
122
+ headingCustomTransformer
123
+ ) ;
124
+ notionToMarkdown . setCustomTransformer (
125
+ "my_heading_2" ,
126
+ headingCustomTransformer
127
+ ) ;
128
+ notionToMarkdown . setCustomTransformer (
129
+ "my_heading_3" ,
130
+ headingCustomTransformer
131
+ ) ;
132
+
100
133
// Note: Pull.ts also adds an image transformer, but has to do that for each
101
134
// page so we don't do it here.
102
135
}
0 commit comments