1
1
use super :: render_context:: RenderContext ;
2
2
use super :: util:: * ;
3
3
use crate :: html:: usage:: UsagesCtx ;
4
+ use crate :: html:: ShortPath ;
4
5
use crate :: js_doc:: JsDoc ;
5
6
use crate :: js_doc:: JsDocTag ;
6
7
use crate :: DocNodeKind ;
@@ -9,7 +10,6 @@ use comrak::nodes::AstNode;
9
10
use comrak:: nodes:: NodeHtmlBlock ;
10
11
use comrak:: nodes:: NodeValue ;
11
12
use comrak:: Arena ;
12
- use deno_ast:: ModuleSpecifier ;
13
13
use serde:: Serialize ;
14
14
use std:: borrow:: Cow ;
15
15
use std:: cell:: RefCell ;
@@ -79,6 +79,8 @@ fn split_markdown_title(md: &str) -> (Option<&str>, &str) {
79
79
80
80
let index = newline. min ( codeblock) . min ( md. len ( ) ) ;
81
81
82
+ dbg ! ( md. split_at( index) ) ;
83
+
82
84
match md. split_at ( index) {
83
85
( "" , body) => ( None , body) ,
84
86
( title, "" ) => ( None , title) ,
@@ -88,14 +90,14 @@ fn split_markdown_title(md: &str) -> (Option<&str>, &str) {
88
90
89
91
#[ cfg( feature = "ammonia" ) ]
90
92
struct AmmoniaRelativeUrlEvaluator {
91
- current_specifier : Option < ModuleSpecifier > ,
93
+ current_file : Option < ShortPath > ,
92
94
url_rewriter : URLRewriter ,
93
95
}
94
96
95
97
#[ cfg( feature = "ammonia" ) ]
96
98
impl ammonia:: UrlRelativeEvaluate for AmmoniaRelativeUrlEvaluator {
97
99
fn evaluate < ' a > ( & self , url : & ' a str ) -> Option < Cow < ' a , str > > {
98
- Some ( ( self . url_rewriter ) ( self . current_specifier . as_ref ( ) , url) . into ( ) )
100
+ Some ( ( self . url_rewriter ) ( self . current_file . as_ref ( ) , url) . into ( ) )
99
101
}
100
102
}
101
103
@@ -241,7 +243,7 @@ fn render_node<'a>(
241
243
String :: from_utf8 ( bw. into_inner ( ) . unwrap ( ) ) . unwrap ( )
242
244
}
243
245
244
- #[ derive( Debug , Serialize , Clone ) ]
246
+ #[ derive( Debug , Serialize , Clone , Default ) ]
245
247
pub struct Markdown {
246
248
pub html : String ,
247
249
pub toc : Option < String > ,
@@ -252,7 +254,7 @@ pub fn markdown_to_html(
252
254
md : & str ,
253
255
summary : bool ,
254
256
render_toc : bool ,
255
- ) -> Markdown {
257
+ ) -> Option < Markdown > {
256
258
// TODO(bartlomieju): this should be initialized only once
257
259
let mut options = comrak:: Options :: default ( ) ;
258
260
options. extension . autolink = true ;
@@ -281,12 +283,16 @@ pub fn markdown_to_html(
281
283
let md = parse_links ( md, render_ctx) ;
282
284
283
285
let md = if summary {
284
- let ( title, body ) = split_markdown_title ( md . as_ref ( ) ) ;
285
- title. unwrap_or ( body )
286
+ let ( title, _body ) = split_markdown_title ( & md ) ;
287
+ title. unwrap_or_default ( )
286
288
} else {
287
289
md. as_ref ( )
288
290
} ;
289
291
292
+ if md. is_empty ( ) {
293
+ return None ;
294
+ }
295
+
290
296
let class_name = if summary {
291
297
"markdown_summary"
292
298
} else {
@@ -356,7 +362,7 @@ pub fn markdown_to_html(
356
362
ammonia:: UrlRelative :: PassThrough ,
357
363
|url_rewriter| {
358
364
ammonia:: UrlRelative :: Custom ( Box :: new ( AmmoniaRelativeUrlEvaluator {
359
- current_specifier : render_ctx. get_current_specifier ( ) . cloned ( ) ,
365
+ current_file : render_ctx. get_current_resolve ( ) . get_file ( ) . cloned ( ) ,
360
366
url_rewriter : url_rewriter. clone ( ) ,
361
367
} ) )
362
368
} ,
@@ -407,21 +413,25 @@ pub fn markdown_to_html(
407
413
None
408
414
} ;
409
415
410
- Markdown {
416
+ Some ( Markdown {
411
417
html : format ! ( r#"<div class="{class_name} flex-1">{html}</div>"# ) ,
412
418
toc,
413
- }
419
+ } )
414
420
}
415
421
416
422
pub ( crate ) fn render_markdown_summary (
417
423
render_ctx : & RenderContext ,
418
424
md : & str ,
419
425
) -> String {
420
- markdown_to_html ( render_ctx, md, true , false ) . html
426
+ markdown_to_html ( render_ctx, md, true , false )
427
+ . unwrap_or_default ( )
428
+ . html
421
429
}
422
430
423
431
pub ( crate ) fn render_markdown ( render_ctx : & RenderContext , md : & str ) -> String {
424
- markdown_to_html ( render_ctx, md, false , false ) . html
432
+ markdown_to_html ( render_ctx, md, false , false )
433
+ . unwrap_or_default ( )
434
+ . html
425
435
}
426
436
427
437
pub ( crate ) fn jsdoc_body_to_html (
@@ -430,11 +440,7 @@ pub(crate) fn jsdoc_body_to_html(
430
440
summary : bool ,
431
441
) -> Option < String > {
432
442
if let Some ( doc) = js_doc. doc . as_deref ( ) {
433
- if doc. is_empty ( ) {
434
- None
435
- } else {
436
- Some ( markdown_to_html ( ctx, doc, summary, false ) . html )
437
- }
443
+ markdown_to_html ( ctx, doc, summary, false ) . map ( |markdown| markdown. html )
438
444
} else {
439
445
None
440
446
}
@@ -461,10 +467,10 @@ pub(crate) fn jsdoc_examples(
461
467
. collect :: < Vec < ExampleCtx > > ( ) ;
462
468
463
469
if !examples. is_empty ( ) {
464
- Some ( SectionCtx {
465
- title : "Examples" . to_string ( ) ,
466
- content : SectionContentCtx :: Example ( examples) ,
467
- } )
470
+ Some ( SectionCtx :: new (
471
+ "Examples" ,
472
+ SectionContentCtx :: Example ( examples) ,
473
+ ) )
468
474
} else {
469
475
None
470
476
}
@@ -513,13 +519,13 @@ pub struct ModuleDocCtx {
513
519
impl ModuleDocCtx {
514
520
pub fn new (
515
521
render_ctx : & RenderContext ,
516
- specifier : & ModuleSpecifier ,
522
+ short_path : & ShortPath ,
517
523
doc_nodes_by_url : & super :: ContextDocNodesByUrl ,
518
524
) -> Self {
519
- let module_doc_nodes = doc_nodes_by_url. get ( specifier) . unwrap ( ) ;
525
+ let module_doc_nodes = doc_nodes_by_url. get ( & short_path . specifier ) . unwrap ( ) ;
520
526
521
527
let title = if !render_ctx. ctx . hide_module_doc_title {
522
- Some ( render_ctx . ctx . url_to_short_path ( specifier ) . to_name ( ) )
528
+ Some ( short_path . display_name ( ) )
523
529
} else {
524
530
None
525
531
} ;
@@ -546,7 +552,7 @@ impl ModuleDocCtx {
546
552
. js_doc
547
553
. doc
548
554
. as_ref ( )
549
- . map ( |doc| markdown_to_html ( render_ctx, doc, false , true ) )
555
+ . and_then ( |doc| markdown_to_html ( render_ctx, doc, false , true ) )
550
556
{
551
557
( Some ( markdown. html ) , markdown. toc )
552
558
} else {
@@ -558,18 +564,25 @@ impl ModuleDocCtx {
558
564
( None , None , None )
559
565
} ;
560
566
561
- if !render_ctx
562
- . ctx
563
- . main_entrypoint
564
- . as_ref ( )
565
- . is_some_and ( |main_entrypoint| main_entrypoint == specifier)
566
- {
567
+ if short_path. is_main {
567
568
let partitions_by_kind =
568
569
super :: partition:: partition_nodes_by_kind ( module_doc_nodes, true ) ;
569
570
570
571
sections. extend ( super :: namespace:: render_namespace (
571
572
render_ctx,
572
- partitions_by_kind,
573
+ partitions_by_kind
574
+ . into_iter ( )
575
+ . map ( |( title, nodes) | {
576
+ (
577
+ SectionHeaderCtx {
578
+ title,
579
+ href : None ,
580
+ doc : None ,
581
+ } ,
582
+ nodes,
583
+ )
584
+ } )
585
+ . collect ( ) ,
573
586
) ) ;
574
587
}
575
588
0 commit comments