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

Autolinks - URls within HTML produces invalid HTML #1045

Closed
edent opened this issue Sep 3, 2024 · 2 comments
Closed

Autolinks - URls within HTML produces invalid HTML #1045

edent opened this issue Sep 3, 2024 · 2 comments

Comments

@edent
Copy link
Contributor

edent commented Sep 3, 2024

Version(s) affected

2.5

Description

In this HTML, the autolinker adds a nested <a> which is invalid.

HTML <a href="https://example.com">Visit https://example.com</a>

Becomes

<p>HTML <a href="https://example.com">Visit <a href="https://example.com">https://example.com</a></a></p>

I would expect the autolinker to ignore the URl inside the anchor. For example, this produces correct HTML:

[Visit https://example.com](https://example.com)

How to reproduce

As per https://commonmark.thephpleague.com/2.5/extensions/autolinks/

require_once 'vendor/autoload.php';

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Autolink\AutolinkExtension;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;

// Define your configuration, if needed
$config = [];

// Configure the Environment with all the CommonMark parsers/renderers
$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());

// Add this extension
$environment->addExtension(new AutolinkExtension());

// Instantiate the converter engine and start converting some Markdown!
$converter = new MarkdownConverter($environment);

echo $converter->convert('Correct [Visit https://example.com](https://example.com)');
echo $converter->convert('Broken <a href="https://example.com">Visit https://example.com</a>');
@colinodell
Copy link
Member

The Autolink extension follows the GFM spec which unfortunately doesn't make exceptions for autolinks found inside of HTML. You can see here that the official GFM parser behaves somewhat similarly, giving this output:

<p>
  HTML <a href="https://example.com" rel="nofollow">Visit 
  </a><a href="https://example.com" rel="nofollow">https://example.com
  </a>
</p>

So because this behavior matches both the spec and the official GFM parser I'm inclined to keep this as-is for now. But if they change this upstream in their specification I'd be glad to make this change here to remain aligned with them!

Thanks for the detailed report, though!

@colinodell colinodell closed this as not planned Won't fix, can't repro, duplicate, stale Sep 7, 2024
@edent
Copy link
Contributor Author

edent commented Sep 8, 2024

Thanks - that's pretty reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants