user4815162342
user4815162342

Reputation: 2087

Is there any way to append text to the end of a paragraph in jekyll?

I'm using jekyll on github pages.

I want to add a "read more..." link to the end of post excerpts, and I want them to appear in the last paragraph of the excerpt. However, jekyll always puts them in a separate paragraph. I would like to be able to append it to the end of the last paragraph in the excerpt.

This is what i've got right now:

{{post.excerpt}}{% if post.excerpt != post.content %}<a class="read-more" href="{{ site.baseurl }}{{ post.url }}">Read more...</a>{% endif %}

But the "Read more..." link is added after the paragraph, instead of at the end:

<p>Excerpt from blog post</p>
<a class="read-more" href="posts/2023-01-09-todays-post.html">Read more...</a>

I have tried using the append filter, but it still adds a paragraph break to the end before the append. I've also tried the rstrip filter to clear newlines at the end, and that doesn't work. It behaves as if the markdown has already been converted to html by the time it gets to that point. Is there someway to delay that?

I'm using excerpt_separator: <!--more--> in my config to define excerpts in my posts. I've tried making sure the <!--more--> tag is inserted at the end of the last excerpt paragraph, with no line-break in between, in all of my posts.

Upvotes: 0

Views: 131

Answers (2)

user4815162342
user4815162342

Reputation: 2087

The question referenced by Christian's answer also had another answer which I could work with.

The following code overrides the automatic excerpt splitting, but it gives me more control. It doesn't account for setting the 'excerpt' in the frontmatter or posts not containing the excerpt_separator, but it's my own site and I can control that.

{{ post.content | split: site.excerpt_separator | first }} <a class="read-more" href="{{ site.baseurl }}{{ post.url }}">Read more...</a></p>

Upvotes: 1

Christian
Christian

Reputation: 5531

You can try this code, which was inspired by this SO post.

<ul>
  {% for post in site.posts %}
    <li>
      <a href="{{ post.url }}">{{ post.title }}</a>
      {{ post.excerpt | strip_html }}
      <a href="{{ site.baseurl }}{{ post.url }}">Read more</a>
    </li>
  {% endfor %}
</ul>

Using strip_html allows you to remove the HTMl from the excerpt, so the <p> tag is gone.

Upvotes: -1

Related Questions