Skip to content

Commit c79f23d

Browse files
committed
fix(html): unified ids
1 parent d643242 commit c79f23d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+677
-341
lines changed

examples/ddoc/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ fn generate_docs_directory(
275275
deno_doc::html::comrak::COMRAK_STYLESHEET_FILENAME
276276
)
277277
})),
278+
id_prefix: None,
278279
};
279280
let ctx = GenerateCtx::create_basic(options, doc_nodes_by_url)?;
280281
let html = deno_doc::html::generate(ctx)?;

src/html/jsdoc.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ lazy_static! {
1919
regex::Regex::new(r"^\[(\S+)\](?:\.(\S+)|\s|)$").unwrap();
2020
}
2121

22-
fn parse_links<'a>(md: &'a str, ctx: &RenderContext) -> Cow<'a, str> {
22+
fn parse_links<'a>(
23+
md: &'a str,
24+
ctx: &RenderContext,
25+
strip: bool,
26+
) -> Cow<'a, str> {
2327
JSDOC_LINK_RE.replace_all(md, |captures: &regex::Captures| {
2428
let code = captures
2529
.name("modifier")
@@ -111,7 +115,9 @@ fn parse_links<'a>(md: &'a str, ctx: &RenderContext) -> Cow<'a, str> {
111115
(title, link)
112116
};
113117

114-
if LINK_RE.is_match(&link) {
118+
if strip {
119+
title
120+
} else if LINK_RE.is_match(&link) {
115121
if code {
116122
format!("[`{title}`]({link})")
117123
} else {
@@ -122,7 +128,7 @@ fn parse_links<'a>(md: &'a str, ctx: &RenderContext) -> Cow<'a, str> {
122128
if code {
123129
format!("`{title}`")
124130
} else {
125-
title.to_string()
131+
title
126132
}
127133
}
128134
})
@@ -149,7 +155,7 @@ pub struct MarkdownToHTMLOptions {
149155
pub type MarkdownStripper = std::rc::Rc<dyn (Fn(&str) -> String)>;
150156

151157
pub fn strip(render_ctx: &RenderContext, md: &str) -> String {
152-
let md = parse_links(md, render_ctx);
158+
let md = parse_links(md, render_ctx, true);
153159

154160
(render_ctx.ctx.markdown_stripper)(&md)
155161
}
@@ -200,7 +206,7 @@ pub fn markdown_to_html(
200206
anchorizer.as_ref(),
201207
);
202208

203-
let md = parse_links(md, render_ctx);
209+
let md = parse_links(md, render_ctx, false);
204210

205211
let file = render_ctx.get_current_resolve().get_file().cloned();
206212

@@ -281,7 +287,7 @@ pub(crate) fn jsdoc_examples(
281287
#[derive(Debug, Serialize, Clone)]
282288
pub struct ExampleCtx {
283289
pub anchor: AnchorCtx,
284-
pub id: String,
290+
pub id: Id,
285291
pub title: String,
286292
pub markdown_title: String,
287293
markdown_body: String,
@@ -291,7 +297,11 @@ impl ExampleCtx {
291297
pub const TEMPLATE: &'static str = "example";
292298

293299
pub fn new(render_ctx: &RenderContext, example: &str, i: usize) -> Self {
294-
let id = name_to_id("example", &i.to_string());
300+
// Using the context-aware builder with the Example kind
301+
let id = IdBuilder::new(render_ctx.ctx)
302+
.kind(IdKind::Example)
303+
.index(i)
304+
.build();
295305

296306
let (maybe_title, body) = split_markdown_title(example);
297307
let title = if let Some(title) = maybe_title {
@@ -305,8 +315,8 @@ impl ExampleCtx {
305315
render_markdown(render_ctx, body.unwrap_or_default(), true);
306316

307317
ExampleCtx {
308-
anchor: AnchorCtx { id: id.to_string() },
309-
id: id.to_string(),
318+
anchor: AnchorCtx { id: id.clone() },
319+
id,
310320
title,
311321
markdown_title,
312322
markdown_body,
@@ -368,7 +378,9 @@ impl ModuleDocCtx {
368378
render_ctx.clone(),
369379
Some(SectionHeaderCtx {
370380
title: title.clone(),
371-
anchor: AnchorCtx { id: title },
381+
anchor: AnchorCtx {
382+
id: super::util::Id::new(title),
383+
},
372384
href: None,
373385
doc: None,
374386
}),
@@ -381,7 +393,7 @@ impl ModuleDocCtx {
381393
Self {
382394
deprecated,
383395
sections: super::SymbolContentCtx {
384-
id: "module_doc".to_string(),
396+
id: Id::new("module_doc"),
385397
docs: html,
386398
sections,
387399
},
@@ -490,6 +502,7 @@ mod test {
490502
),
491503
markdown_stripper: Rc::new(crate::html::comrak::strip),
492504
head_inject: None,
505+
id_prefix: None,
493506
},
494507
Default::default(),
495508
Default::default(),
@@ -569,65 +582,78 @@ mod test {
569582
);
570583

571584
assert_eq!(
572-
parse_links("foo {@link https://example.com} bar", &render_ctx),
585+
parse_links("foo {@link https://example.com} bar", &render_ctx, false),
573586
"foo [https://example.com](https://example.com) bar"
574587
);
575588
assert_eq!(
576-
parse_links("foo {@linkcode https://example.com} bar", &render_ctx),
589+
parse_links(
590+
"foo {@linkcode https://example.com} bar",
591+
&render_ctx,
592+
false
593+
),
577594
"foo [`https://example.com`](https://example.com) bar"
578595
);
579596

580597
assert_eq!(
581-
parse_links("foo {@link https://example.com Example} bar", &render_ctx),
598+
parse_links(
599+
"foo {@link https://example.com Example} bar",
600+
&render_ctx,
601+
false
602+
),
582603
"foo [Example](https://example.com) bar"
583604
);
584605
assert_eq!(
585-
parse_links("foo {@link https://example.com|Example} bar", &render_ctx),
606+
parse_links(
607+
"foo {@link https://example.com|Example} bar",
608+
&render_ctx,
609+
false
610+
),
586611
"foo [Example](https://example.com) bar"
587612
);
588613
assert_eq!(
589614
parse_links(
590615
"foo {@linkcode https://example.com Example} bar",
591-
&render_ctx
616+
&render_ctx,
617+
false,
592618
),
593619
"foo [`Example`](https://example.com) bar"
594620
);
595621

596622
assert_eq!(
597-
parse_links("foo {@link unknownSymbol} bar", &render_ctx),
623+
parse_links("foo {@link unknownSymbol} bar", &render_ctx, false),
598624
"foo unknownSymbol bar"
599625
);
600626
assert_eq!(
601-
parse_links("foo {@linkcode unknownSymbol} bar", &render_ctx),
627+
parse_links("foo {@linkcode unknownSymbol} bar", &render_ctx, false),
602628
"foo `unknownSymbol` bar"
603629
);
604630

605631
#[cfg(not(target_os = "windows"))]
606632
{
607633
assert_eq!(
608-
parse_links("foo {@link bar} bar", &render_ctx),
634+
parse_links("foo {@link bar} bar", &render_ctx, false),
609635
"foo [bar](../../.././/a.ts/~/bar.html) bar"
610636
);
611637
assert_eq!(
612-
parse_links("foo {@linkcode bar} bar", &render_ctx),
638+
parse_links("foo {@linkcode bar} bar", &render_ctx, false),
613639
"foo [`bar`](../../.././/a.ts/~/bar.html) bar"
614640
);
615641

616642
assert_eq!(
617-
parse_links("foo {@link [b.ts]} bar", &render_ctx),
643+
parse_links("foo {@link [b.ts]} bar", &render_ctx, false),
618644
"foo [b.ts](../../.././/b.ts/index.html) bar"
619645
);
620646
assert_eq!(
621-
parse_links("foo {@linkcode [b.ts]} bar", &render_ctx),
647+
parse_links("foo {@linkcode [b.ts]} bar", &render_ctx, false),
622648
"foo [`b.ts`](../../.././/b.ts/index.html) bar"
623649
);
624650

625651
assert_eq!(
626-
parse_links("foo {@link [b.ts].baz} bar", &render_ctx),
652+
parse_links("foo {@link [b.ts].baz} bar", &render_ctx, false),
627653
"foo [b.ts baz](../../.././/b.ts/~/baz.html) bar"
628654
);
629655
assert_eq!(
630-
parse_links("foo {@linkcode [b.ts].baz} bar", &render_ctx),
656+
parse_links("foo {@linkcode [b.ts].baz} bar", &render_ctx, false),
631657
"foo [`b.ts baz`](../../.././/b.ts/~/baz.html) bar"
632658
);
633659
}

src/html/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod pages;
1818
mod parameters;
1919
pub mod partition;
2020
mod render_context;
21-
mod search;
21+
pub mod search;
2222
mod symbols;
2323
mod types;
2424
mod usage;
@@ -267,6 +267,7 @@ pub struct GenerateOptions {
267267
pub markdown_renderer: jsdoc::MarkdownRenderer,
268268
pub markdown_stripper: jsdoc::MarkdownStripper,
269269
pub head_inject: Option<HeadInject>,
270+
pub id_prefix: Option<String>,
270271
}
271272

272273
#[non_exhaustive]
@@ -286,6 +287,7 @@ pub struct GenerateCtx {
286287
pub markdown_renderer: jsdoc::MarkdownRenderer,
287288
pub markdown_stripper: jsdoc::MarkdownStripper,
288289
pub head_inject: Option<HeadInject>,
290+
pub id_prefix: Option<String>,
289291
}
290292

291293
impl GenerateCtx {
@@ -422,6 +424,7 @@ impl GenerateCtx {
422424
markdown_renderer: options.markdown_renderer,
423425
markdown_stripper: options.markdown_stripper,
424426
head_inject: options.head_inject,
427+
id_prefix: options.id_prefix,
425428
})
426429
}
427430

src/html/pages.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ impl IndexCtx {
284284
short_path.as_resolve_kind(),
285285
)),
286286
title: title.to_string(),
287-
anchor: AnchorCtx { id: anchor },
287+
anchor: AnchorCtx {
288+
id: util::Id::new(anchor),
289+
},
288290
doc,
289291
}),
290292
content: util::SectionContentCtx::Empty,
@@ -300,7 +302,7 @@ impl IndexCtx {
300302
});
301303

302304
Some(SymbolContentCtx {
303-
id: String::new(),
305+
id: util::Id::empty(),
304306
sections,
305307
docs: None,
306308
})
@@ -336,7 +338,9 @@ impl IndexCtx {
336338
UrlResolveKind::Category { category: &title },
337339
)),
338340
title,
339-
anchor: AnchorCtx { id: anchor },
341+
anchor: AnchorCtx {
342+
id: util::Id::new(anchor),
343+
},
340344
doc,
341345
}),
342346
content: util::SectionContentCtx::Empty,
@@ -345,7 +349,7 @@ impl IndexCtx {
345349
.collect::<Vec<_>>();
346350

347351
Some(SymbolContentCtx {
348-
id: String::new(),
352+
id: util::Id::empty(),
349353
sections,
350354
docs: None,
351355
})
@@ -404,7 +408,9 @@ impl IndexCtx {
404408
(
405409
render_ctx.clone(),
406410
Some(SectionHeaderCtx {
407-
anchor: AnchorCtx { id: title.clone() },
411+
anchor: AnchorCtx {
412+
id: util::Id::new(title.clone()),
413+
},
408414
title,
409415
href: None,
410416
doc,
@@ -436,7 +442,7 @@ impl IndexCtx {
436442
html_head_ctx,
437443
module_doc: None,
438444
overview: Some(SymbolContentCtx {
439-
id: String::new(),
445+
id: util::Id::empty(),
440446
sections,
441447
docs: None,
442448
}),
@@ -485,7 +491,7 @@ impl AllSymbolsCtx {
485491
AllSymbolsCtx {
486492
html_head_ctx,
487493
content: SymbolContentCtx {
488-
id: String::new(),
494+
id: util::Id::empty(),
489495
sections,
490496
docs: None,
491497
},

src/html/render_context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl Default for HeadingToCAdapter {
365365

366366
lazy_static! {
367367
static ref REJECTED_CHARS: regex::Regex =
368-
regex::Regex::new(r"[^\p{L}\p{M}\p{N}\p{Pc} -]").unwrap();
368+
regex::Regex::new(r"[^\p{L}\p{M}\p{N}\p{Pc} -_/]").unwrap();
369369
}
370370

371371
impl HeadingToCAdapter {
@@ -603,6 +603,7 @@ mod test {
603603
),
604604
markdown_stripper: Rc::new(crate::html::comrak::strip),
605605
head_inject: None,
606+
id_prefix: None,
606607
},
607608
None,
608609
Default::default(),

0 commit comments

Comments
 (0)