npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

papyre

v0.0.9

Published

Build tool for static websites. Bring your own rendering engine. Works well with React and Netlify CMS

Downloads

23

Readme

Overview

Papyre is a build tool for static sites. It handles watching, rebuilding, aggregating templates, parsing front matter; everything except actual rendering. Bring your own rendering engine (but it must be JS).

Papyre is JS-centric. It requires a JS entry file that exports rendering functions, and uses those to render templates. It also uses Webpack to watch the dependency graph and transpile JS. You can add React with a few lines of code, and reuse code between static layouts, client-side bundle, Netlify CMS previews, or whatever. Check the examples directory.

Works well with React and Netlify CMS.

New and immature. Feedback and suggestions are welcome.

TOC

Why

Static site generators tend to combine build tooling and an opinionated rendering engine. Also, React-based generators, like Gatsby, tend to have WAY too many concepts and API surface. Papyre is the missing link: a simple build tool that lets you bring your own rendering tool. You can add React, or whatever else, with just a few lines.

Usage

Install from NPM:

npm i papyre

This example uses React and involves three files: a build script, a markdown/HTML template, and a JS publics file with rendering functions.

╠═ build.js
╚═ src
   ╚═ templates
      ╠═ index.md
      ╚═ index.js

Here, build.js will be a standalone build script. Don't balk at the glue code; Papyre is not for useless 1-liner demos.

'use strict'

const pt = require('path')
const papyre = require('papyre')

const webpackConfig = {
  entry: pt.resolve('src/templates/index.js'),
  module: {rules: [{
    test: /\.jsx?$/,
    include: pt.resolve('src/templates'),
    use: {loader: 'babel-loader'},
  }]},
}

const [_exec, _file, cmd] = process.argv

if (cmd === 'build') {
  papyre.build(webpackConfig, (err, result) => {
    if (err) {
      console.error(err)
      process.exit(1)
    }
    else {
      console.info(result.timing)
      papyre.writeEntries('public', renameEntries(result.entries))
    }
  })
}
else if (cmd === 'watch') {
  papyre.watch(webpackConfig, (err, result) => {
    if (err) {
      console.error(err)
    }
    else {
      console.info(result.timing)
      papyre.writeEntries('public', renameEntries(result.entries))
    }
  })
}
else {
  throw Error(`Unrecognized or missing command: ${cmd}`)
}

function renameEntries(entries) {
  for (const entry of entries) {
    entry.path = entry.path.replace(/\.md$/, '.html')
  }
  return entries
}

index.md will be a template with a front matter. The latter also specifies which rendering function to use.

---
papyre: {fn: html, layout: Index}
---

# Home

**Hello world!**

index.js must export the rendering function html specified in the template. A rendering function receives a template with metadata and returns a string or a promise of a string. That's it. It could be making network calls on a meson uplink to the dark side of the Moon. Or it could use React:

import {createElement} from 'react'
import {renderToStaticMarkup} from 'react-dom/server'

export function html(props) {
  const {layout} = Object(props.entry.papyre)
  const Layout = exports[layout]
  if (typeof Layout !== 'function') {
    throw Error(`Expected to find layout function ${layout}, got ${Layout}`)
  }
  return `<!doctype html>${renderToStaticMarkup(<Layout {...props} />)}`
}

export function Index({entry, entries: __, tree: ___}) {
  return (
    <html>
      <head>
        <title>{entry.title}</title>
      </head>
      <body>
        {entry.body}
      </body>
    </html>
  )
}

Build once:

node build build

Watch and rebuild:

node build watch

The public dir should now contain the output:

╚═ public
   ╚═ index.html

API

Front Matter, Props, Render Functions

Rendering is done by user-defined functions with the following signature:

Props -> string | Promise<string>

Where props have the following shape:

interface Props {
  entry: Entry
  entries: [Entry]
  tree: EntryTree
}

interface Entry {
  path: string
  body: string
  ...
}

interface EntryTree {
  [string]: Entry | EntryTree
}

entries is the collection of the parsed templates. tree is the tree of all entries matching the folder structure, for convenient lookup. It's especially useful for rendering "index" pages that display multiple items, such as blog posts.

Templates typically look like this:

---
(optional metadata in YAML format)
---

(body)

The --- ... --- part is called "front matter" and must be YAML.

Templates can also be JSON and YAML files. The top level data structure must be a dict:

// json
{
  "title": "Landing",
  "description": "Company Website"
}
# yaml
title: Landing
description: Company Website

Papyre renders those and only those templates that specify a rendering function, which must be exported by your main JS file.

---
papyre: {fn: myRenderingFunction}
---
export function myRenderingFunction(props) {
  return props.entry.body
}

Each template is parsed into an entry, which is the YAML front matter dict or the top-level data structure, with the remaining content added as body, plus the template's relative path.

Using the Tree

Suppose you want to render a page with multiple elements, say, blog posts. Say we have this structure:

╚═ src
   ╚═ templates
      ╠═ index.js
      ╠═ posts.md
      ╚═ posts
         ╠═ first.md
         ╚═ second.md

Suppose posts.md looks like this:

---
papyre: {fn: posts}
---

And first.md and second.md look like this:

---
papyre: {fn: post}
title: Post Title
---

(body)

Then posts would receive the following tree, and could use it to render multiple posts:

const _tree = {
  'index.js': {path: 'index.js', body: '(JS code)'},
  'posts.md': {path: 'posts.md', body: ''},
  'posts': {
    'first.md': {path: 'posts/first.md', title: 'Post Title', body: '(body)'},
    'second.md': {path: 'posts/second.md', title: 'Post Title', body: '(body)'},
  }
}

function posts({tree}) {
  // Should also sort these by date
  return Object.values(tree.posts).map(post => (
    `<div>${post.title}</div>`
  )).join('\n')
}

function post({entry: {title}}) {
  return `<div>${title}</div>`
}

build(webpackConfig, onDone)

Runs a single build cycle: compile JS, compile templates, trigger onDone when completed. See example in Usage.

The config is used to create a new Webpack compiler instance, with modifications:

  • store output in RAM
  • don't bundle libraries
  • compile for Node.js, without polyfills

The config must contain an entry, which must be a single string, a path to the JS file that exports rendering functions.

papyre.build({entry: './src/templates/index.js'}, () => {})

The template folder is assumed to be the entry file's directory; if the entry is './src/templates/index.js', the template folder is './src/templates'.

onDone receives either an error or the build result:

papyre.build({entry: './src/templates/index.js'}, (err, result) => {
  if (err) {
    console.error(err)
    process.exit(1)
  }
  else {
    papyre.writeEntries('public', result.entries)
  }
})

The result has the following shape:

interface Result {
  entries: [Entry]
  timing: string
}

interface Entry {
  path: string
  body: string
}

To write the result to disk, use writeEntries, see below.

When reusing a browser-oriented config, make sure to disable minification, i.e. webpack.optimize.UglifyJsPlugin, since it's expensive and pointless for a build-only bundle.

buildP(webpackConfig)

Same as build, but instead of accepting a callback, returns a promise. Convenient with coroutines:

async function build() {
  const result = await papyre.buildP(webpackConfig)
  await papyre.writeEntries('...', result.entries)
}

watch(webpackConfig, onDone)

Accepts the same configuration as build. Watches the templates directory and the dependency graph of the entry file. Triggers onDone on each rebuild. Returns a reference that can stop the watching:

const watch = papyre.watch({entry: './src/templates/index.js'}, () => {})

watch.deinit()

writeEntries(dir, entries)

Writes entries relative to dir, creating intermediary directories if necessary.

This:

writeEntries('public', [{path: 'index.html', body: ''}])

will create this:

╚═ public
   ╚═ index.html

Should be called in the build or watch callback; see the examples directory.

Misc

I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts