Skip to content

Commit

Permalink
Escape hash in link URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Aug 26, 2023
1 parent 489db5e commit 907a30a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/LinkRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer)

$out = $childRenderer->renderNodes($node->children());

$url = \str_replace('_', '\\_', $node->getUrl());
$replacements = [
'_' => '\\_',
'#' => '\#',
];
$url = \str_replace(\array_keys($replacements), $replacements, $node->getUrl());

$allowUnsafeLinks = $this->config->get('allow_unsafe_links');
if (! $allowUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($url)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/data/links.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Link with protocol: [label](https://example.org).
Link with protocol and fragment: [label](https://example.org/lorem#ipsum).

Relative link: [label](./example.html).

Expand Down
2 changes: 1 addition & 1 deletion tests/data/links.tex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Link with protocol: label\footnote{\url{https://example.org}}.
Link with protocol and fragment: label\footnote{\url{https://example.org/lorem\#ipsum}}.

Relative link: label\footnote{\url{./example.html}}.

Expand Down

0 comments on commit 907a30a

Please sign in to comment.