Giter Club home page Giter Club logo

elderjs-plugin-blog-pagination's Introduction

Elder.js Plugin: elderjs-plugin-blog-pagination

Generate pagination from @elderjs/plugin-markdown

Prerequisite

Currently this plugin only support post generate by @elderjs/plugin-markdown

npm install --save @elderjs/plugin-markdown

Install

npm i -s elderjs-plugin-blog-pagination

Config

Once installed, open your elder.config.js and configure the plugin by adding elderjs-plugin-blog-pagination to your plugin object.

plugins: {
  // other plugins
  '@elderjs/plugin-markdown': {
    routes: ['blog'],
    // Your other settings
  },
  'elderjs-plugin-blog-pagination': {
    routes: ['blog'], // change to your blog route same as plugin-markdown route
    postPerPage: 5, // change to your preferred post per apge
    indexTemplate: 'BlogIndex' // change to your own Index.svelte template but without `.svelte`
  },
}

Blog route.js

As of current state, i have no idea how to override the permalink function, so you have to copy and paste this code into your route.js. If you figure out how, do make a pull request or create a new issue.

module.exports = {
  template: 'Blog.svelte',
  permalink: ({ request, settings }) => {
    const route = 'blog';
    if (request.slug.includes(route))
      return `${request.slug}`; // index pagination permalink
    return `/${route}/${request.slug}/`; // markdown blog permalink
  },
  all: () => [],
  data: {},
};

Using inside your BlogIndex.svelte and Pagination.svelte

<!-- BlogIndex.svelte -->
<script>
  import Pagination from '../../components/Pagination.svelte';
  export let data, request, helpers;
  let blogPost = data.markdown.blog.slice(request.postStart, request.postEnd);
</script>

<ul>
  {#each blogPost as blog}
    <PostList {blog} {helpers}/>
  {/each}
</ul>
<Pagination {data} {request} {helpers} />
<!-- Pagination.svelte -->
<script>
  export let request, helpers;
</script>

<div class="pagination">
  {#if request.hasPrevious }
    <a href="{helpers.permalinks.blog(request.previousPage)}" class="prev">&lsaquo;</a>
  {/if}
  Page {request.page} / {request.lastPage}
  {#if request.hasNext}
    <a href="{helpers.permalinks.blog(request.nextPage)}" class="next">&rsaquo;</a>
  {/if}
</div>

Pull Request

If you have any idea on how to support other than @elderjs/plugin-markdown output, feel free to create pull request.

TODO

  • add unit test
  • add github action

elderjs-plugin-blog-pagination's People

Contributors

noxasch avatar

Stargazers

 avatar

Watchers

 avatar  avatar

elderjs-plugin-blog-pagination's Issues

Add per route config support and fallback to default config

This will allow different post count and template for different index route

config: {
    routes: ['blog'],
    postPerPage: 5,
    indexTemplate: 'BlogIndex',
    routeConfig: [ // default is routeConfig: [],
      {
        routes: ['blog'],
        postPerPage: 5,
        indexTemplate: 'BlogIndex', // if not supply use config.indexTemplate
      },
      {
        routes: ['travel'],
        postPerPage: 5,
        indexTemplate: 'TravelIndex',
      },
    ]
  },

Request: Tests for the markdown plugin

Hey man, great work on the pagination plugin. As current markdown plugin doesn't have tests I'm afraid the integration may be brittle. If you're up for it, I wouldn't turn down tests to make sure the format stays consistent.

No pressure, just a note more than anything.

Great work. ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.