piranha / gostatic

Fast static site generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[support] What's the best wat to get excerpt of page?

opened this issue · comments

Hi,

In my blog, I use excerpt of pages. I get this with this code:

<ul class="blocks-3">
{{ range .Site.Pages.Children "posts/" }}

  <li class="marc">
    <div class="marc-content">{{ markdown (truncate 330 .Raw) }}...</div>
    <div class="marc-title">
     <a href="{{ $.Rel .Url }}">{{ .Title }}</a>
     <div class="meta">{{ template "date" .Date }}</div>
    </div>
  </li>

{{ end }}
</ul>

but it does not convince me at all. I want to get something like this.

How can be done?
Thanks in advance,

So you want to truncate, but by words, right?

Yes. But I want to mantain the syntax of the markdown. If I truncate the raw code, then perhaps I get broken url: [foo instead of [foo](bla). Or broken emph, for example *foo instead of *foo*.

The same applies to html code.

Yeah, I don't have any good solutions for this right now. That needs to be context-aware truncater...

But how wordpress themes do that? (see example above)

I'm quite sure they do some fancy processing in there. Saying 'I don't have any good solutions` I mean that I don't really have time to sit and code an algorithm which will do that. I would gladly accept pull requests implementing that, though, since it sounds quite useful.

On the other hand, I looked at what people do, and they use simpler approach: you render your content, strip tags and then truncate. This way you won't have any markup truncated in a middle.

OK. Looking it up, I found this which is simple but very capable. There is a explaining post

The best way is to run

{{ truncate (pandoc --to plain .Raw) }}

but for this I need a new release version with #51 . I reopen this in waiting for that.

I'd say best way would be to do {{ .Raw | markdown | strip_html | truncate 300 }}, this way you're not calling out to shell and have no external dependencies.

Is it a viable option to introduce a template function, e.g. wordify or whatever which does that logic?

Could be, but is markdown|strip|truncate not enough?

Yeah I guess, but it doesn't break on words, but on characters, right?

Right, function for breaking on words would be nice to have. Not sure wordify is good name for that though. :)

👍 perhaps excerpt is a good name

Sounds good to me!

So, could anyone implement this? I have no this level to do that.

It's in pull request #56 .

@krpors : fantastic. Is this tested against markdown syntax. I'm thinking about *foo* or [goo](google.com).

@somenxavier Er, no. That requires a whole new level of parsing and things... I guess I misinterpreted your requirement. What the excerpt function does however, is truncating text based on spaces so it doesn't just break of in the middle of a word. If you now chain the function as follows:

{{ excerpt (.Raw | markdown | strip_html) 7 }}

It will produce simply text, maximum of 7 words. You can maybe play a bit without strip_html to see if you can produce correct results. If not, we may have to look for another solution.

@krpors I mean that if you write

Hi. This is a *test*. Do you see this [web](google.com)

the the excerpt could produce something like:

Hi, This is a test. Do you see this web

But your solution is good enough

That is correct. The function chain will parse the markdown of your page, strip the html from it (so only text remains), and then it will generate an excerpt out of it.