php-markdown icon indicating copy to clipboard operation
php-markdown copied to clipboard

rel attribute

Open wintstar opened this issue 1 year ago • 8 comments

How to enter multiple attributes for rel?

php-markdown 2.0.0

$parser = new MarkdownExtra;
$parser->empty_element_suffix = ">";
$url = $parser->transform([Link](url){.link  target=_blank rel=noreferrer noopener});
echo $url;

This results <a href="url" class="link" target="_blank" rel="noreferrer">Link</a>

noopener missing.

wintstar avatar Feb 03 '23 16:02 wintstar

You can use quotes:

[Link](url){.link  target=_blank rel="noreferrer noopener"}

michelf avatar Feb 03 '23 18:02 michelf

Not work

[Link](url){.css target=_blank rel="noreferrer noopener"}

with quotes this result

<a href="url">Link</a>{.link target=_blank rel="noreferrer noopener"}

wintstar avatar Feb 03 '23 18:02 wintstar

I was confused. You're right: there's no way at the moment to have a space in a custom attribute. You can write the link in HTML form though (if the context allows HTML).

(Some work would need to be done in doExtraAttributes and the regular expressions above it to make that work.)

michelf avatar Feb 03 '23 18:02 michelf

For the css attribute the following works .css .link [Link](url){.css .link target=_blank rel="noreferrer noopener"}

result <a href="url" class="css link" target="_blank" rel="noreferrer">Link</a>

but not work with rel rel=noreferrer rel=noopener [Link](url){.css .link target=_blank rel="noreferrer rel=noopener"}

Could you do it like with the css attribute?

wintstar avatar Feb 03 '23 18:02 wintstar

My solution

https://github.com/michelf/php-markdown/blob/eb176f173fbac58a045aff78e55f833264b34e71/Michelf/MarkdownExtra.php#L244-L245

change

				$parts = explode('=', $element, 2);
				$attributes[] = $parts[0] . '="' . $parts[1] . '"';

to

				$parts = explode('=', $element, 2);
				$parts[1] = preg_replace('~_~', ' ', $parts[1]);
				$attributes[] = $parts[0] . '="' . $parts[1] . '"';

replace the space with underscore

[Link](url){.link target=_blank rel="noreferrer_noopener"}

result <a href="url" class="link" target="_blank" rel="noreferrer noopener">Link</a>

wintstar avatar Feb 03 '23 22:02 wintstar

Won't work, given the example {.class name=my_field} Now you have name="my field" when you were expecting my_field.

In theory, potentially a better solution would be to support {.class_name rel=att1 rel=att2}, this also better matches your first comment of utilizing the multiple class functionality.

This would require more than just a 1-line change though. I've done something very similiar with another project and if Michelf is interested I could probably whip up something to accommodate this.

cdp1337 avatar Feb 04 '23 00:02 cdp1337

@cdp1337

It works. Also with the img tag. With the tag title there are now also no more problems with the spaces.

![alt-tag](path/to/image){.w3-image .w3-border .w3-padding title=title_text_with_space width=800 height=613 loading=lazy}

result <img src="path/to/image" alt="alt-tag" class="w3-image w3-border w3-padding" title="title text with space" width="800" height="613" loading="lazy">

wintstar avatar Feb 04 '23 10:02 wintstar

It works using _, but it's not a great solution. People will expect things like target=_blank to work correctly too.

rel=att1 rel=att2 could be made to work, but it doesn't feel right since that's not how attributes work in HTML.

Adding support for quotes would be the best solution.

michelf avatar Feb 05 '23 00:02 michelf