Giter VIP home page Giter VIP logo

next-pages's Introduction

@primer/next-pages

Next.js does not make it easy to list or iterate over all of the pages in your app. Maybe this package can help!

Installation

npm install @primer/next-pages

Usage

In your next.config.js:

const withPages = require('@primer/next-pages/plugin')

module.exports = withPages({
  /* your config */
})

In your app, import the page map from @primer/next-pages, and serialize it as JSON to see what's in it:

import {pageMap} from '@primer/next-pages'

export default props => (
  <pre {...props}>{JSON.stringify(pageMap, true, 2)}</pre>
)

Page map

The pageMap export from @primer/next-pages is a Map object whose keys are the URL paths and values are objects with the following keys:

  • path is the path relative to the pages directory, e.g. index.md for pages/index.md

  • file is the full file path, e.g. /Users/cool/projects/sweet/pages/index.md

  • meta is an object containing any page metadata

  • requirePath is the path that you would pass to the function returned by require.context(), as in:

    const requirePage = require.context('./pages', true, /\.md$/)
    const Page = requirePage(page.requirePath)

Page metadata

This module uses gray-matter to parse front matter from each file in the pages/ directory, and outputs that metadata in the JS bundle so that you can do things like:

  • Know the title of each page, a la Jekyll
  • Conditionally show or hide common content on some pages
  • Generate navigation links and lists automatically

๐Ÿšจ Note: this plugin does not handle outputting front matter in the JS bundle. It only reads it into the page map object so that you can use it elsewhere. You can make a loader that discards front matter in the houtput in just a couple of lines:

const matter = require('gray-matter')
module.exports = function removeFrontMatter(source) {
  const {content, data} = matter(source)
  return content
}

Ensuring that the above loader is executed after this module's is an exercise left to the reader.

next-pages's People

Contributors

shawnbot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.