Anton Medvedev
Anton Medvedev

Reputation: 3668

How to strip newlines in jekyll?

I know about strip_newlines but it doesn't doing what I want:

{% capture string %}
Hello
there
{% endcapture %}

{{ string | strip_newlines }}

Outputs Hellothere but I need Hello there insead.

Replace doesn't working too, because you can't put newline char.

{{ string | replace: '\n', ' ' }}

How to replace all newlines with space? For example for usage in meta tags.

Upvotes: 2

Views: 1716

Answers (1)

Anton Medvedev
Anton Medvedev

Reputation: 3668

Here is trick:

{% assign description = page.excerpt | newline_to_br | strip_newlines | replace: '<br />', ' ' | strip_html | strip |  truncatewords: 30 %}

<meta name="description" content="{{ description }}">

Replace newlines with br tag, then strip newlines, and then replace <br /> with space. Strip html and truncate for usage in meta description.

Upvotes: 10

Related Questions