# {{page.excerpt}} does not work if first element is not \<p\>

**URL:** <https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224>\
**Category:** Help\
**Created:** [December 4, 2017, 2:36am UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224 "2017-12-04T02:36:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdelarioja](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mdelarioja/32/619_2.png) [@mdelarioja](https://talk.jekyllrb.com/u/mdelarioja)\
**Post date:** [December 4, 2017, 2:36am UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/1 "2017-12-04T02:36:57Z")

</div>

As the title says, if I’m using {{ page.excerpt | strip\_html | normalize\_whitespace | truncate: 160 | escape }} to generate meta descriptions. However, if a posts begins with an image or anything other than `<p>` it simply returns " ".

Also, it seems this only picks up whatever is in the first `<p>` it finds, so even if the words within the first `<p>` are less than 160 characters, it still only displays that content.

Is there a way around this?

---

<div class="post-metadata">

**Author:** ![lsrdg](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/lsrdg/32/431_2.png) [@lsrdg](https://talk.jekyllrb.com/u/lsrdg)\
**Post date:** [December 4, 2017, 12:58pm UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/2 "2017-12-04T12:58:07Z")

</div>

From [Posts | Jekyll • Simple, blog-aware, static sites](https://jekyllrb.com/docs/posts/#post-excerpts) :

> Each post automatically takes the first block of **text** , from the beginning of the content to the first occurrence of excerpt\_separator, and sets it as the post.excerpt.

So, I suppose that after your image there is a `excerpt_separator`, and then it stops there. Maybe it works if you define your own `excerpt_separator`, maybe a `</p>` ?

---

<div class="post-metadata">

**Author:** ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)\
**Post date:** [December 4, 2017, 1:10pm UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/3 "2017-12-04T13:10:43Z")

</div>

You can also override the autogenerated excerpt by setting `page.excerpt` yourself in the YAML Front Matter.

```auto
---
title: Your Post
excerpt: Your custom excerpt.
---

```

---

<div class="post-metadata">

**Author:** ![mdelarioja](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mdelarioja/32/619_2.png) [@mdelarioja](https://talk.jekyllrb.com/u/mdelarioja)\
**Post date:** [December 4, 2017, 7:42pm UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/4 "2017-12-04T19:42:55Z")

</div>

Thank you. However, I am using this as a fallback for when a post does not have a description.

---

<div class="post-metadata">

**Author:** ![mdelarioja](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mdelarioja/32/619_2.png) [@mdelarioja](https://talk.jekyllrb.com/u/mdelarioja)\
**Post date:** [December 4, 2017, 7:47pm UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/5 "2017-12-04T19:47:34Z")

</div>

Makes sense. I guess I just assumed jekyll would scan the post’s content until it found the first paragraph and take the excerpt form there, regardless of whether the first element is an image or not. Could this be a bug?

---

<div class="post-metadata">

**Author:** ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)\
**Post date:** [December 4, 2017, 7:57pm UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/6 "2017-12-04T19:57:18Z")

</div>

I don’t think it’s a bug. The way Kramdown parses an image eg. `![your image](image.jpg)` is to wrap it in a paragraph.

So Jekyll’s auto-generating excerpt feature is doing the write thing and grabbing it.

I think your best bet is to use an excerpt separator as @lsrdg mentioned above.

Drop in `excerpt_separator: <!--more-->` in your post’s front matter, then manually insert `<!--more-->` after whatever text you want Jekyll to consider as the excerpt.

---

<div class="post-metadata">

**Author:** ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)\
**Post date:** [December 4, 2017, 8:03pm UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/7 "2017-12-04T20:03:58Z")

</div>

You could also try wrapping your image Markdown in `div` or `figure` element. I believe that will stop Kramdown from wrapping it with `<p></p>`.

```auto
<div>![your image](image.jpg)</div>

```

---

<div class="post-metadata">

**Author:** ![mdelarioja](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mdelarioja/32/619_2.png) [@mdelarioja](https://talk.jekyllrb.com/u/mdelarioja)\
**Post date:** [December 5, 2017, 4:24am UTC](https://talk.jekyllrb.com/t/page-excerpt-does-not-work-if-first-element-is-not-p/1224/8 "2017-12-05T04:24:46Z")

</div>

Thanks. They actually are wrapped using figure tags. I think the problem nay actually stem from the fact that I am using Json-ld to add schema data, so the script is the first thing that Kramdown sees. Not sure moving it below the image will make a difference, so I’ll have to look into using an excerpt separator.

BTW, big fan of your work; your blog has been an invaluable resource for learning.
