Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

saber-plugin-query-posts

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

saber-plugin-query-posts

Query posts and inject them to specific pages. Useful when you're writing a blog.

  • 0.4.6
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

saber-plugin-query-posts

Query posts and inject them to specific pages. Useful when you're writing a blog.

Install

yarn add saber-plugin-query-posts

Usage

In your saber-config.yml:

plugins:
  - resolve: saber-plugin-query-posts

Then this plugin will inject all posts to pages whose attribute injectAllPosts is set to true, for instance you can create a pages/index.md:

---
layout: index
injectAllPosts: true
---

Welcome to my homepage.

Then in the layout component layouts/index.vue, page.posts and page.pagination will be available:

<template>
  <div>
    <slot name="default" />

    <ul>
      <li v-for="post in page.posts" :key="post.permalink">
        <a :href="post.permalink">
          {{ post.title }}
        </a>
      </li>
    </ul>

    <a :href="page.pagination.prevLink" v-if="page.pagination.hasPrev">
      ← Prev Page
    </a>
    <a :href="page.pagination.nextLink" v-if="page.pagination.hasNext">
      Next Page →
    </a>
  </div>
</template>

<script>
export default {
  props: ['page']
}
</script>

Draft Posts

Posts with front matter draft: true will be excluded in page.posts, but they will still be built and accessible via permalink.

Tags

This plugin will automatically generate tag pages at /tags/:tag when you're using tags in page data, e.g. in a Markdown post:

---
title: hello
date: 2019-01-01
tags:
  - life
  - random
---

hello

Data Injected in Post Pages

You can access the detailed info of the tags of a post page via this.page.tagsInfo, for instance:

tags:
  - life
  - random

gives you:

;[
  {
    name: 'life',
    permalink: '/tags/life'
  },
  {
    name: 'random',
    permalink: '/tags/random'
  }
]

Tag Layout

Tag pages will use the tag layout or fallback to default layout.

You can access the tag name in the layout component via this.page.tag.

Categories

This plugin will automatically generate category pages at /categories/:tag when you're using categories in page data, e.g. in a Markdown post:

---
title: hello
date: 2019-01-01
categories:
  - sports
---

hello

Then there will be a /categories/sports page,categories is an array, so you can assign the post to multiple categories.

Data Injected in Post Pages

You can access the detailed info of the categories of a post page via this.page.categoriesInfo, for instance:

categories:
  - sports/football
  - hobby

gives you:

;[
  {
    name: 'sports',
    permalink: '/categories/sports'
  },
  {
    name: 'football',
    permalink: '/categories/sports/football'
  },
  {
    name: 'hobby',
    permalink: '/categories/hobby'
  }
]

Nesting Category

It also support nesting categories:

categories:
  - sports/football

In this way, it will generate two pages: /categories/sports and /categories/sports/football and both of them will include this post.

Category Layout

Category pages will use the category layout or fallback to default layout.

You can access the category name in the layout component via this.page.category.

Pagination

By default we show at most 30 posts per page, but you can configure this globally using perPage option in saber-config.yml:

plugins:
  - resolve: saber-plugin-query-posts
    options:
      perPage: 6

It's also possible to configure this behavior for each page separately, in a page like pages/index.md:

---
injectAllPosts:
  perPage: 20
---

Options

perPage

  • Type: number
  • Default: 30

The limit of posts to show per page.

firstPageOnly

  • Type: boolean
  • Default: false

Only generate the first page of posts.

tagsMap

  • Type: { [name: string]: string }

Map tag name to permalink's :tag part, by default :tag will be the tag name.

For example:

{
  tagsMap: {
    'c++': 'cpp'
  }
}

categoriesMap

Same as tagsMap but for categories.

  • Type: { category?: string, tag?: string }
  • Default: { category: '/categories/:slug', tag: '/tags/:slug' }

The permalink templates for category and tag pages, available placeholders:

placeholderdescription
slugSlugified tag / category name.

License

MIT.

Keywords

FAQs

Package last updated on 04 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc