-->

Show only partial blog post in Docpad, with “Read

2019-02-15 07:22发布

问题:

I need show only partial blog post... with a "Read more" link to full blog post.

HOME: List last 5 partial/intro posts with Read More.

This is possible in Docpad?

Thanks..

回答1:

May by

    getCuttedContent: (content) ->            
        i = content.search('<!-- Read more -->')
        if i >= 0
            content[0..i-1]                
        else
            content

    hasReadMore: (content) ->
        content.search('<!-- Read more -->') >= 0

more

and

        <% posts = @getCollection('posts') %>
        <% for i in [@document.page.startIdx...@document.page.endIdx]: %>
            <% document = posts.at(i).toJSON() %>
            <article class="post">
                <h3><span class="posts_date"><%= @formatDate(document.date) %></span> <a class="post_head" href="<%= document.url %>"><%= document.title %></a></h3>
                <div class="post-content"><%- @getCuttedContent(String(document.contentRenderedWithoutLayouts)) %></div>
                <% if @hasReadMore(String(document.contentRenderedWithoutLayouts)): %>
                <div class="read_more"><a href="<%= document.url %>"><strong>Читать далее &rarr;</strong></a></div>
                <% end %>
            </article>
        <% end %>

posts

and add to post

 <!-- Read more -->


回答2:

If you are wanting different pages for the before more and after more, you can use the paged plugin with their Splitting a Document into Multiple Pages example.

Something like:

---
title: 'Awesome Pages Post'
layout: 'default'
isPaged: true
pageCount: 2
pageSize: 1
---

<!-- Page Content -->
before more
if @document.page.number is 1: %>
    after more
<% end %>

<!-- Page Listing -->
<% if @document.page.number is 0: %>
    <!-- Read More Button -->
    <a href="<%= @getNextPage() %>">Read more!</a></li>
<% end %>

Should do the trick. Then you can just custoimse the logic to handle different use cases. For instance, ths will have the "before more" text shown on both pages. But you could wrap "before more" in a "is page 0" check to prevent that if you wished.



回答3:

If you are not wanting different pages for the before more and after more, but just wanting to use before more in the content listing. You can just put your before more stuff in a "description" meta data attribute like so:

--- cson
title: 'Awesome Pages Post"
layout: "default"
description: """
    Before more content goes here
    """
---

After more content (the actual page content) goes here.

Then you can display the description in your content listing by doing:

<%- post.description or post.contentRenderedWithoutLayouts  %>

Which will fallback to the full content if the description is not defined.

If you want to be able to have your description rendered, the text plugin has you covered. Change your meta data description to the following instead:

description: """
    <t render="markdown">With the text plugin **you can render anything providing you have the plugins installed!**</t>
    """


回答4:

Just as another way, I use the following method in docpad.coffee to truncate posts for display on a home page. It deals with links, which will make text seem longer and blockquotes which you might end up breaking in the middle

# Used for shortening a post
truncateText: (content,trimTo) ->
    trimTo = trimTo || 200
    output = content.substr(0,trimTo).trim()
    #remove anchor tags as they don't show up on the page
    nolinks = output.replace(/<a(\s[^>]*)?>.*?<\/a>/ig,"")
    #check if there is a difference in length - if so add this
    #difference to the trimTo length - add the text length that will not show
    #up in the rendered HTML
    diff = output.length - nolinks.length
    output = content.substr(0,trimTo + diff)
    #find the last space so that we don't break the text
    #in the middle of a word
    i = output.lastIndexOf(' ',output.length-1)
    output = output.substr(0,i)+"..."
    count1 = (output.match(/<blockquote>/g) || []).length
    count2 = (output.match(/<\/blockquote>/g) || []).length
    if count1 > count2
        output += "</blockquote>"
    return output


标签: docpad