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

@sphido/core

v2.0.26

Published

A rocket 🚀 fast, lightweight, static site generator

Downloads

47

Readme

@sphido/core

Sphido core package contains two most important function getPages() and allPages(). The getPages() function scans directories for all *.md and *.html files. Second function allPages() is generator that allow to iterate over all pages.

const pages = await getPages({path: 'content'}, /* ...exteners */);

Returned structure is very simple and looks like follow:

[
  {
    "name": "Main page",
    "path": "content/Main page.md"
  },
  {
    "name": "Directory",
    "children": [
      {
        "name": "Subpage one",
        "path": "content/Directory/Subpage one.md"
      },
      {
        "name": "Subpage two",
        "path": "content/Directory/Subpage two.md"
      }
    ]
  }
]

Then iterate over pages like follow:

for (const page of allPages(pages)) {
  console.log(page);
}

Extending page object

Every single page object inside structure can be modified with extender. Extenders are set as additional parameters of the getPages() function. There are two types of extenders:

Callback extenders

Callback extender is a function that is called during recursion over each page with three parameters passed to the function page, path and dirent.

const callbackExtender = (page, path, dirent) => {
  // do anything with page object
}

const pages = await getPages({path: 'content'}, callbackExtender);

Object extenders

This extender is just a simple JavaScript object that is combined with the page object using the Object.assign() function.

const objectExtender = {
  author: 'Roman Ožana'
}

const pages = await getPages({path: 'content'}, objectExtender);

There is no limit to the number of extenders, you can combine as many as you want. Let's have the following code:

const extenders = [

	// callback extenders will be called during iteration ony by one

	(page) => {
		// add property
		page.title = `${page.name} | my best website`;

		// or function
		page.getDate = () => new Date();

		// or something else 
		page.counter = 1;
	},

	// callback extenders are called in the series
  
	(page) => {
		page.counter++;
	},

	// object extender will be merged with page object
  
	{
		"author": "Roman Ožana",
		"getLink": function () {
			return this.path.replace('content', 'public');
		}
	}
];

const pages = getPages({path: 'content'}, ...extenders);

then you get this structure:

[
  {
    "name": "Main page",
    "path": "content/Main page.md",
    "title": "Main page | my best website",
    "counter": 2,
    "author": "Roman Ožana",
    "getDate": "[Function: getDate]",
    "getLink": "[Function: getLink]"
  }
]

Installation

yarn add @sphido/core

Example

Following example read all *.md files in content directory and process them with marked to HTML files

#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages, readFile, writeFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';

const pages = await getPages({path: 'content'}, // ... extenders
  (page) => {
    page.slug = slugify(page.name) + '.html';
    page.dir = dirname(page.path);
  });

for (const page of allPages(pages)) {
  page.output = join('public', relative('content', page.dir), page.slug);
  page.content = marked(await readFile(page.path));

  await writeFile(page.output, `<!DOCTYPE html>
		<html lang="en" dir="ltr">
		<head>
			<meta charset="UTF-8">
			<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
			<title>${page.name} | Sphido Example</title>
		</head>
		<body class="prose mx-auto my-6">${page.content}</body>
		<!-- Generated by Sphido from ${page.path} -->
		</html>
  `);
}

Source codes

@sphido/core