Add support for more characters in links (#379)

This commit is contained in:
frozolotl 2023-03-30 21:26:43 +02:00 committed by GitHub
parent 5aa2ba1490
commit ed36ef3312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 8 deletions

View File

@ -264,16 +264,35 @@ impl Lexer<'_> {
}
fn link(&mut self) -> SyntaxKind {
let mut bracket_stack = Vec::new();
#[rustfmt::skip]
self.s.eat_while(|c: char| matches!(c,
| '0' ..= '9'
| 'a' ..= 'z'
| 'A' ..= 'Z'
| '~' | '/' | '%' | '?' | '#' | '&' | '+' | '='
| '\'' | '.' | ',' | ';'
));
self.s.eat_while(|c: char| {
match c {
| '0' ..= '9'
| 'a' ..= 'z'
| 'A' ..= 'Z'
| '!' | '#' | '$' | '%' | '&' | '*' | '+'
| ',' | '-' | '.' | '/' | ':' | ';' | '='
| '?' | '@' | '_' | '~' | '\'' => true,
'[' => {
bracket_stack.push(SyntaxKind::LeftBracket);
true
}
'(' => {
bracket_stack.push(SyntaxKind::LeftParen);
true
}
']' => bracket_stack.pop() == Some(SyntaxKind::LeftBracket),
')' => bracket_stack.pop() == Some(SyntaxKind::LeftParen),
_ => false,
}
});
if !bracket_stack.is_empty() {
return self.error_at_end("expected closing bracket in link");
}
if self.s.scout(-1) == Some('.') {
// Don't include the trailing characters likely to be part of another expression.
if matches!(self.s.scout(-1), Some('!' | ',' | '.' | ':' | ';' | '?' | '\'')) {
self.s.uneat();
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 90 KiB

View File

@ -22,6 +22,22 @@ Wahttp://link \
Nohttps:\//link \
Nohttp\://comment
---
// Verify that brackets are included in links.
https://[::1]:8080/ \
https://example.com/(paren) \
https://example.com/#(((nested))) \
---
// Check that unbalanced brackets are not included in links.
#[https://example.com/] \
https://example.com/)
---
// Verify that opening brackets without closing brackets throw an error.
// Error: 22-22 expected closing bracket in link
https://exam(ple.com/
---
// Styled with underline and color.
#show link: it => underline(text(fill: rgb("283663"), it))