Skip to content

Commit 22dd33a

Browse files
authored
feat: support [label]{@link URL} syntax #690 (#702)
1 parent 3786a2d commit 22dd33a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/html/jsdoc.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::rc::Rc;
1111

1212
lazy_static! {
1313
static ref JSDOC_LINK_RE: regex::Regex = regex::Regex::new(
14-
r"(?m)\{\s*@link(?P<modifier>code|plain)?\s+(?P<value>[^}]+)}"
14+
r"(?m)(?:\[(?P<label>[^]]+)])?\{\s*@link(?P<modifier>code|plain)?\s+(?P<value>[^}]+)}"
1515
)
1616
.unwrap();
1717
static ref LINK_RE: regex::Regex =
@@ -31,6 +31,7 @@ fn parse_links<'a>(
3131
.map_or("plain", |modifier_match| modifier_match.as_str())
3232
== "code";
3333
let value = captures.name("value").unwrap().as_str();
34+
let label = captures.name("label").map(|x| x.as_str());
3435

3536
let (link, mut title) = if let Some((link, title)) =
3637
value.split_once('|').or_else(|| value.split_once(' '))
@@ -39,6 +40,9 @@ fn parse_links<'a>(
3940
} else {
4041
(value, "".to_string())
4142
};
43+
if let Some(label) = label {
44+
title = label.trim().to_string();
45+
}
4246

4347
let link = if let Some(module_link_captures) = MODULE_LINK_RE.captures(link)
4448
{
@@ -631,6 +635,23 @@ mod test {
631635
),
632636
"foo [Example](https://example.com) bar"
633637
);
638+
assert_eq!(
639+
parse_links(
640+
"foo [Example]{@link https://example.com} bar",
641+
&render_ctx,
642+
false
643+
),
644+
"foo [Example](https://example.com) bar"
645+
);
646+
// [label] takes precedence - consistent with the default JSDoc behaviour
647+
assert_eq!(
648+
parse_links(
649+
"foo [Example (pre)]{@link https://example.com|Example (after)} bar",
650+
&render_ctx,
651+
false,
652+
),
653+
"foo [Example (pre)](https://example.com) bar"
654+
);
634655
assert_eq!(
635656
parse_links(
636657
"foo {@linkcode https://example.com Example} bar",
@@ -639,6 +660,14 @@ mod test {
639660
),
640661
"foo [`Example`](https://example.com) bar"
641662
);
663+
assert_eq!(
664+
parse_links(
665+
"foo [Example]{@linkcode https://example.com} bar",
666+
&render_ctx,
667+
false
668+
),
669+
"foo [`Example`](https://example.com) bar"
670+
);
642671

643672
assert_eq!(
644673
parse_links("foo {@link unknownSymbol} bar", &render_ctx, false),

0 commit comments

Comments
 (0)