Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support [label]{@link URL} syntax #690 #702

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::rc::Rc;

lazy_static! {
static ref JSDOC_LINK_RE: regex::Regex = regex::Regex::new(
r"(?m)\{\s*@link(?P<modifier>code|plain)?\s+(?P<value>[^}]+)}"
r"(?m)(?:\[(?P<label>[^]]+)])?\{\s*@link(?P<modifier>code|plain)?\s+(?P<value>[^}]+)}"
)
.unwrap();
static ref LINK_RE: regex::Regex =
Expand All @@ -26,6 +26,7 @@ fn parse_links<'a>(md: &'a str, ctx: &RenderContext) -> Cow<'a, str> {
.map_or("plain", |modifier_match| modifier_match.as_str())
== "code";
let value = captures.name("value").unwrap().as_str();
let label = captures.name("label").map(|x| x.as_str());

let (link, mut title) = if let Some((link, title)) =
value.split_once('|').or_else(|| value.split_once(' '))
Expand All @@ -34,6 +35,9 @@ fn parse_links<'a>(md: &'a str, ctx: &RenderContext) -> Cow<'a, str> {
} else {
(value, "".to_string())
};
if let Some(label) = label {
title = label.trim().to_string();
}

let link = if let Some(module_link_captures) = MODULE_LINK_RE.captures(link)
{
Expand Down Expand Up @@ -585,13 +589,32 @@ mod test {
parse_links("foo {@link https://example.com|Example} bar", &render_ctx),
"foo [Example](https://example.com) bar"
);
assert_eq!(
parse_links("foo [Example]{@link https://example.com} bar", &render_ctx),
"foo [Example](https://example.com) bar"
);
// [label] takes precedence - consistent with the default JSDoc behaviour
assert_eq!(
parse_links(
"foo [Example (pre)]{@link https://example.com|Exapmle (after)} bar",
&render_ctx
),
"foo [Example (pre)](https://example.com) bar"
);
assert_eq!(
parse_links(
"foo {@linkcode https://example.com Example} bar",
&render_ctx
),
"foo [`Example`](https://example.com) bar"
);
assert_eq!(
parse_links(
"foo [Example]{@linkcode https://example.com} bar",
&render_ctx
),
"foo [`Example`](https://example.com) bar"
);

assert_eq!(
parse_links("foo {@link unknownSymbol} bar", &render_ctx),
Expand Down