@nodedk/page
page#
HTML templates and formatting helpers for server-rendered pages.
Install#
npm i @nodedk/page
Usage#
var { html, esc, num } = require('@nodedk/page')
function product(name, price) {
return html`
<article>
<h2>${esc(name)}</h2>
<p>${num(price, { style: 'currency', currency: 'USD' })}</p>
</article>
`
}
console.log(product('Coffee & tea', 12.5))
API#
The package re-exports @nodedk/template and @nodedk/ext. All methods are synchronous:
html(strings, ...values): tagged template returning a string; joins arrays with newlines and omits nullish and boolean values. Interpolated text is inserted unchanged.esc(value): escapes HTML special characters; preserves non-strings.raw(value): decodes the five entities emitted byesc; preserves non-strings.date(value, type = 'date'): local-time input formatting for a Date, date string, or millisecond timestamp. Types aredate,datetime-local,time,month, andweek; invalid input returns''.time(value, options): Date or date-string formatting. Options accept a locale string, orlang(default'en'),formattokens, andIntl.DateTimeFormatfields. Returns a string, or''for invalid input.num(value, options): number formatting. Numeric strings can contain underscores. Options accept a locale string, orlang(default'en') andIntl.NumberFormatfields. Returns a string.truncate(value = '', size = 32): shortens a string and adds' ...'beyondsizewhen needed.isImage(name): checks forgif,jpg,jpeg,tiff,png,bmp, orsvgfilename extensions, ignoring case.
See ext for formatting options and variations, and template for interpolation behavior.
Created by Vidar Eldøy