4.0.2 ā€¢ Published 1 year ago

lit-ntml v4.0.2

Weekly downloads
1,426
License
MIT
Repository
github
Last release
1 year ago

tippin.me Follow me

Version Node version MIT License

Downloads Total downloads Packagephobia Bundlephobia

ci codecov

Code of Conduct

Lightweight and modern templating for SSR in Node.js, inspired by lit-html.

This module also gets featured in web-padawan/awesome-lit-html. Make sure to check the repo out for awesome things inspired by lit-html. šŸ‘šŸ’Æ

Table of contents

Features

  • await all tasks including Functions, Promises, and whatnot.
  • Compatible for ES Modules (import { html } from 'lit-ntml') and CommonJS (const { html } = require('lit-ntml');).
  • Parses PromiseList or List by default, without explicit joining.
  • Support HTML syntax highlighting + autocompletion with vscode-lit-html in JavaScript's template string.
  • Support native ES Module via .mjs.

Pre-requisite

Enable syntax highlighting when writing HTML with template literal

Visual Studio Code

  1. Install vscode-lit-html extension.
  2. If the extension does not provide that syntax highlighting and autocompletion, try writing your templates in .jsx file (or .tsx file if you're TypeScript user) . That should work.

Install

# Install via NPM
$ npm install lit-ntml

Usage

html()

import { html } from 'lit-ntml';

const peopleList = ['Cash Black', 'Vict Fisherman'];
const syncTask = () => `<h1>Hello, World!</h1>`;
const asyncLiteral = Promise.resolve('<h2>John Doe</h2>');
const asyncListTask = async () => `<ul>${peopleList.map(n => `<li>${n}</li>`)}</ul>`;

/** Assuming top-level await is enabled... */
await html`${syncTask}${asyncLiteral}${asyncListTask}`; /** <!DOCTYPE html><html><head></head><body><h1>Hello, World!</h1><h2>John Doe</h2><ul><li>Cash Black</li><li>Vict Fisherman</li></ul></body></html> */

htmlSync()

import { htmlSync as html } from 'lit-ntml';

const peopleList = ['Cash Black', 'Vict Fisherman'];
const syncTask = () => `<h1>Hello, World!</h1>`;

html`${syncTask}${peopleList}`;
/** <!DOCTYPE html><html><head></head><body><h1>Hello, World!</h1>Cash BlackVictFisherman[object Promise]</body></html> */

htmlFragment()

import { htmlFragment as html } from 'lit-ntml';

const syncTask = () => `<h1>Hello, World!</h1>`;
const externalStyleLiteral = `<style>body { margin: 0; padding: 0; box-sizing: border-box; }</style>`;

/** Assuming top-level await is enabled... */
await html`${externalStyleLiteral}${syncTask}`; /** <style>body { margin: 0; padding: 0; box-sizing: border-box; }</style><h1>Hello, World!</h1> */

htmlFragmentSync()

import { htmlFragmentSync as html } from 'lit-ntml';

const peopleList = ['Cash Black', 'Vict Fisherman'];
const syncTask = () => `<h1>Hello, World!</h1>`;
const asyncTask = Promise.resolve(1);

html`${syncTask}${peopleList}${asyncTask}`;
/** <h1>Hello, World!</h1>Cash BlackVictFisherman[object Promise] */

SSR with Express (Node.js)

Edit SSR with Express and LitNtml

Browser support

Only modern browsers with native ES Modules support requires no polyfills and transpilation needed.

<!doctype html>
<html>
  <head>
    <script type="module">
      import { html } from 'https://cdn.skypack.dev/lit-ntml@latest';

      // --snip
    </script>
  </head>
</html>

API Reference

html()

  • returns: <Promise<string>> Promise which resolves with rendered HTML document string.

htmlSync()

This method works the same as html() except that this is the synchronous version.

htmlFragment()

  • returns: <Promise<string>> Promise which resolves with rendered HTML document fragment string.

htmlFragmentSync()

This method works the same as htmlFragment() except that this is the synchronous version.

deno

šŸ‘‰ Check out the deno module at deno_mod/lit_ntml.

License

MIT License Ā© Rong Sen Ng

4.0.1-rc.0

1 year ago

4.0.1

1 year ago

4.0.2

1 year ago

4.0.2-rc.0

1 year ago

4.0.0

2 years ago

3.0.1-rc.4

2 years ago

3.0.1-rc.3

2 years ago

3.0.1-rc.2

2 years ago

3.0.3-rc.0

2 years ago

3.0.3-rc.1

2 years ago

3.0.3-rc.2

2 years ago

3.0.2-rc.0

2 years ago

3.0.6-rc.1

2 years ago

3.0.6-rc.0

2 years ago

3.0.5-rc.2

2 years ago

3.0.5-rc.1

2 years ago

3.0.5-rc.0

2 years ago

3.0.1-rc.0

2 years ago

3.0.1-rc.1

2 years ago

3.0.4-rc.0

2 years ago

3.0.4-rc.1

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

3.0.0

3 years ago

3.0.0-rc.1

3 years ago

3.0.0-rc.0

3 years ago

2.20.0

3 years ago

2.20.0-rc.0

3 years ago

2.19.0

3 years ago

2.18.2

3 years ago

2.18.1

3 years ago

2.18.0

3 years ago

2.18.1-rc.0

3 years ago

2.18.0-rc.0

3 years ago

2.17.0

4 years ago

2.16.1

4 years ago

2.16.0

4 years ago

2.16.0-rc.0

4 years ago

2.15.1

4 years ago

2.14.2-rc.3

4 years ago

2.14.2-rc.4

4 years ago

2.14.2-rc.5

4 years ago

2.15.0

4 years ago

2.14.2-rc.1

4 years ago

2.14.2-rc.2

4 years ago

2.14.1

4 years ago

2.14.0

4 years ago

2.13.0

4 years ago

2.12.1-rc.0

4 years ago

2.12.0

4 years ago

2.11.0

4 years ago

2.10.0

4 years ago

2.9.0

5 years ago

2.8.0

5 years ago

2.7.0

5 years ago

2.6.0

5 years ago

2.6.0-beta.0

5 years ago

2.5.0

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

2.0.0-0

5 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.5.0

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago