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

Paragraphs are not stripped but turn into line-breaks #80

Closed
rbrv opened this issue Nov 18, 2023 · 4 comments
Closed

Paragraphs are not stripped but turn into line-breaks #80

rbrv opened this issue Nov 18, 2023 · 4 comments

Comments

@rbrv
Copy link

rbrv commented Nov 18, 2023

When SEOMate outputs <title> and <meta desc>, tags like <br>, <strong>, '<em>, ... are properly stripped.
Paragraphs, however, turn into line-breaks, both for <title> and <meta desc>.
This is particularly problematic when using a rich text fields.

Example

Content of variable altTitle (a rich text field):

<p>A page title with<p>
<p>a new <strong>paragraph</strong></p>

outputs

<title>A page title with
a new paragraph</title>

config/SEOMate.php

...
'fieldProfiles' => [
            'standard' => [
                'title' => ['seoTitle', 'altTitle', 'title'],
                'description' => ['seoDescription', 'firstParagraph']
            ],
],
...
@mmikkel
Copy link
Member

mmikkel commented Nov 18, 2023

Those linebreaks are newline characters (i.e. \n) added by Redactor after every </p> tag, and they persist because the strip_tags() function doesn't remove control characters.

FWIW, newline characters in <title> or <meta> tags isn't considered invalid HTML, and Google/Facebook/whatever seems to handle them just fine, too. Generally, newlines simply render as a single space where appropriate, which is actually a good thing – because if we would simply remove them, the <title> in your example would render as A page title witha new paragraph 😬

We have debated if SEOMate should replace closing/opening paragraph tags with literal spaces, but so far we've opted to just keep the newlines and avoid the potential edge cases involved in doing that.

That said, at this point Redactor isn't the only field type outputting rich text, and it's not even the recommended, first party one anymore. I took a look at CKEditor, and it turns out that CK doesn't produce the newlines after paragraphs... which means that if a CKEditor field is used for SEOMate metadata, paragraphs will be joined together without spaces – which IMO is a bigger problem than Redactor fields having newlines retained. But in any case, perhaps it's time to revisit the problem and figure out a solution that works well for any and all rich text field types.

TL;DR – maybe the best call after all, is if SEOMate a) removes any newlines and b) adds a literal space between stripped paragraphs.

@rbrv
Copy link
Author

rbrv commented Nov 18, 2023

Thanks for clarifying!

And you are right, as it turns out, the <p> tags weren't the problem, but rather the accompanying \n inserted by redactor.

I was able to work around this by adding something like this to config/seomate.php:

'tagTemplateMap' => [
    'title' => '<title>{{ value|replace({"\n": "", "\r": ""}) }}</title>',
]

@mmikkel
Copy link
Member

mmikkel commented Jan 18, 2024

We've decided to address this by replacing all control characters (including newlines) by a literal space.

This change will be in the next SEOMate release.

@mmikkel mmikkel closed this as completed Jan 18, 2024
@mmikkel
Copy link
Member

mmikkel commented Jan 19, 2024

SEOMate v. 2.2.0 is out now with this change.

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