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

@jkob/node-html-renderer

v1.0.0

Published

Simple template-base node static html generator

Downloads

3

Readme

node-html-renderer

Simple static HTML render, using templates and variables

Instalation

npm install @jkob/node-html-renderer --save

Usage in code

const HtmlRenderer = require('node-html-renderer');

const htmlRenderer = new HtmlRenderer({
	outputDirectory: HtmlRenderer.realPath('./output'),
	templatesDirectory: HtmlRenderer.realPath('./templates'),
	resourcesUrl: '../public' // any path to be resolved in browser, relative to index.html
});

htmlRenderer
	.render('page', {
		title: 'Page Title',
		posts: [
			{ title: 'Post 1' },
			{ title: 'Post 2' },
		]
	})
	.save('index);

Above example will create index.html file with parsed content in ./output directory. Template page will be resolved to ./templates/page.html file

Template syntax

Templates are simple HTML files with extra syntax recognized by HTML Renderer.

Variables

Variables are defined with second argument to render method. Variable name is a key from object argument.

<div>
	#title
</div>

When using with render('', { title: 'Example Title' }), will render:

<div>
	Example Title
</div>

Templates

HTML Renderer has support for templates, and templates list.

Syntax is:

@[template_name]:[variable_name]

for example

@postList:posts

will use postList template with posts variable. If variable will be an array, template will be resolved as list.

Resource resolver

Rules

  • all occurences of !! will be changed to defined resourcesUrl
  • same rules as variables but prefix is !, for example !imageVariable. This will also use resourcesUrl to prefix resource with correct page. Use relative resource path here.

Full featured example

<!-- templates/image.html -->
<img src="!image" />
<!-- templates/post-content.html -->
<p>#text</p>
<!-- templates/post.html -->
<article>
	<h2>#title</h2>
	@image:* <!-- * is used to pass entire variables scope to template -->
	@post-content:content
</article>
<!-- templates/page.html -->
<html>
	<head>
		<title>#title</title>
		<link rel="stylesheet" href="!!/style.css" />
	</head>
	<body>
		<h1>#title</h1>
		<main>
			@post:posts
		</main>
	</body>
</html>
const htmlRenderer = new HtmlRenderer({
	outputDirectory: HtmlRenderer.realPath('./output'),
	templatesDirectory: HtmlRenderer.realPath('./templates'),
	resourcesUrl: '../public' // any path to be resolved in browser
});

htmlRenderer.render('page', {
	title: 'Blog',
	posts: [
		{ title: 'First post', image: 'happy.jpg', content: { text: 'happy' } },
		{ title: 'Second post', image: 'great.jpg', content: { text: 'great' } },
	]
}).save('index');
<!-- output/index.html -->
<html>
	<head>
		<title>Blog</title>
		<link rel="stylesheet" href="../public/style.css" />
	</head>
	<body>
		<h1>Blog</h1>
		<main>
			<article>
				<h2>First post</h2>
				<img src="../public/happy.jpg" />
				<p>happy</p>
			</article>
			<article>
				<h2>Second post</h2>
				<img src="../public/great.jpg" />
				<p>great</p>
			</article>
		</main>
	</body>
</html>