@@ -148,15 +148,48 @@ export async function render_content_markdown(
148
148
}
149
149
}
150
150
151
- return parse ( {
152
- body,
153
- type_links,
154
- code : ( raw , language , current ) => {
155
- const cached_snippet = SNIPPET_CACHE . get ( raw + language + current ) ;
151
+ const headings : string [ ] = [ ] ;
152
+
153
+ // this is a bit hacky, but it allows us to prevent type declarations
154
+ // from linking to themselves
155
+ let current = '' ;
156
+
157
+ return await transform ( body , {
158
+ text ( token ) {
159
+ // @ts -expect-error I think this is a bug in marked — some text tokens have children,
160
+ // but that's not reflected in the types. In these cases we can't just use `token.tokens`
161
+ // because that will result in e.g. `<code>` elements not being generated
162
+ if ( token . tokens ) {
163
+ // @ts -expect-error
164
+ return this . parser ! . parseInline ( token . tokens ) ;
165
+ }
166
+
167
+ return smart_quotes ( token . text , true ) ;
168
+ } ,
169
+ heading ( { tokens, depth, raw } ) {
170
+ const text = this . parser ! . parseInline ( tokens ) ;
171
+
172
+ const title = text
173
+ . replace ( / < \/ ? c o d e > / g, '' )
174
+ . replace ( / & q u o t ; / g, '"' )
175
+ . replace ( / & l t ; / g, '<' )
176
+ . replace ( / & g t ; / g, '>' ) ;
177
+ current = title ;
178
+ const normalized = normalizeSlugify ( raw ) ;
179
+ headings [ depth - 1 ] = normalized ;
180
+ headings . length = depth ;
181
+ const slug = headings . filter ( Boolean ) . join ( '-' ) ;
182
+ return `<h${ depth } id="${ slug } ">${ text . replace (
183
+ / < \/ ? c o d e > / g,
184
+ ''
185
+ ) } <a href="#${ slug } " class="permalink"><span class="visually-hidden">permalink</span></a></h${ depth } >`;
186
+ } ,
187
+ code ( { text, lang = 'js' } ) {
188
+ const cached_snippet = SNIPPET_CACHE . get ( text + lang + current ) ;
156
189
if ( cached_snippet . code ) return cached_snippet . code ;
157
190
158
- let { source, options } = parse_options ( raw , language ) ;
159
- source = adjust_tab_indentation ( source , language ) ;
191
+ let { source, options } = parse_options ( text , lang ) ;
192
+ source = adjust_tab_indentation ( source , lang ) ;
160
193
161
194
const converted = conversions . get ( source ) ;
162
195
@@ -182,7 +215,7 @@ export async function render_content_markdown(
182
215
html += syntax_highlight ( {
183
216
filename,
184
217
highlighter,
185
- language,
218
+ language : lang ,
186
219
source,
187
220
twoslashBanner,
188
221
options
@@ -192,7 +225,7 @@ export async function render_content_markdown(
192
225
html += syntax_highlight ( {
193
226
filename,
194
227
highlighter,
195
- language : language === 'js' ? 'ts' : language ,
228
+ language : lang === 'js' ? 'ts' : lang ,
196
229
source : converted ,
197
230
twoslashBanner,
198
231
options
@@ -225,7 +258,7 @@ export async function render_content_markdown(
225
258
226
259
return html ;
227
260
} ,
228
- codespan : ( text ) => {
261
+ codespan ( { text } ) {
229
262
return (
230
263
'<code>' +
231
264
( type_regex
@@ -238,61 +271,6 @@ export async function render_content_markdown(
238
271
: text ) +
239
272
'</code>'
240
273
) ;
241
- }
242
- } ) ;
243
- }
244
-
245
- async function parse ( {
246
- body,
247
- code,
248
- codespan
249
- } : {
250
- body : string ;
251
- type_links : Map < string , { relativeURL : string ; slug : string ; page : string } > | null ;
252
- code : ( source : string , language : string , current : string ) => string ;
253
- codespan : ( source : string ) => string ;
254
- } ) {
255
- const headings : string [ ] = [ ] ;
256
-
257
- // this is a bit hacky, but it allows us to prevent type declarations
258
- // from linking to themselves
259
- let current = '' ;
260
-
261
- return await transform ( body , {
262
- text ( token ) {
263
- // @ts -expect-error I think this is a bug in marked — some text tokens have children,
264
- // but that's not reflected in the types. In these cases we can't just use `token.tokens`
265
- // because that will result in e.g. `<code>` elements not being generated
266
- if ( token . tokens ) {
267
- // @ts -expect-error
268
- return this . parser ! . parseInline ( token . tokens ) ;
269
- }
270
-
271
- return smart_quotes ( token . text , true ) ;
272
- } ,
273
- heading ( { tokens, depth, raw } ) {
274
- const text = this . parser ! . parseInline ( tokens ) ;
275
-
276
- const title = text
277
- . replace ( / < \/ ? c o d e > / g, '' )
278
- . replace ( / & q u o t ; / g, '"' )
279
- . replace ( / & l t ; / g, '<' )
280
- . replace ( / & g t ; / g, '>' ) ;
281
- current = title ;
282
- const normalized = normalizeSlugify ( raw ) ;
283
- headings [ depth - 1 ] = normalized ;
284
- headings . length = depth ;
285
- const slug = headings . filter ( Boolean ) . join ( '-' ) ;
286
- return `<h${ depth } id="${ slug } ">${ text . replace (
287
- / < \/ ? c o d e > / g,
288
- ''
289
- ) } <a href="#${ slug } " class="permalink"><span class="visually-hidden">permalink</span></a></h${ depth } >`;
290
- } ,
291
- code ( { text, lang } ) {
292
- return code ( text , lang ?? 'js' , current ) ;
293
- } ,
294
- codespan ( { text } ) {
295
- return codespan ( text ) ;
296
274
} ,
297
275
blockquote ( token ) {
298
276
let content = this . parser ?. parse ( token . tokens ) ?? '' ;
0 commit comments