Arrow keys: ← previous · → next 21 of 35
@nodedk/page
0.2.4 stable

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 by esc; preserves non-strings.
  • date(value, type = 'date'): local-time input formatting for a Date, date string, or millisecond timestamp. Types are date, datetime-local, time, month, and week; invalid input returns ''.
  • time(value, options): Date or date-string formatting. Options accept a locale string, or lang (default 'en'), format tokens, and Intl.DateTimeFormat fields. Returns a string, or '' for invalid input.
  • num(value, options): number formatting. Numeric strings can contain underscores. Options accept a locale string, or lang (default 'en') and Intl.NumberFormat fields. Returns a string.
  • truncate(value = '', size = 32): shortens a string and adds ' ...' beyond size when needed.
  • isImage(name): checks for gif, jpg, jpeg, tiff, png, bmp, or svg filename extensions, ignoring case.

See ext for formatting options and variations, and template for interpolation behavior.

Created by Vidar Eldøy