Package detail

posthtml-modules

canvaskisa4.9kMIT0.9.1

Posthtml modules processing

posthtml, posthtml-modules, parser, transform

readme

NPM Deps Tests Coverage XO Code Style

Modules Plugin

Import and process HTML Modules with PostHTML

Install

npm i -D posthtml-modules

Example

<!-- index.html -->
<html>
<body>
  <module href="./module.html">
    title
  </module>
</body>
</html>
<!-- module.html -->
<header>
  <h1>
    Test <content></content>
  </h1>
</header>
const { readFileSync } = require('fs')
const posthtml = require('posthtml')
const options = { /* see available options below */ }

posthtml()
  .use(require('posthtml-modules')(options))
  .process(readFileSync('index.html', 'utf8'))
  .then((result) => result)
  });
<html>
 <body>
   <header>
     <h1>Test title</h1>
   </header>
  </body>
</html>

Options

root

Type: string\ Default: ./

Root path for modules lookup.

plugins

Type: array | function\ Default: []

PostHTML plugins to apply for every parsed module.

If a function provided, it will be called with module's file path.

from

Type: string\ Default: ''

Root filename for processing apply, needed for path resolving (it's better to always provide it).

initial

Type: boolean\ Default: false

Apply plugins to root file after modules processing.

tag

Type: string\ Default: module

Use a custom tag name.

attribute

Type: string\ Default: href

Use a custom attribute name.

locals

Type: object\ Default: {}

Pass data to the module.

If present, the JSON object from the locals="" attribute will be merged on top of this, overwriting any existing values.

attributeAsLocals

Type: boolean\ Default: false

All attributes on <module></module> will be added to locals

parser

Type: object\ Default: {}

Options for the PostHTML parser.

By default, posthtml-parser is used.

expressions

Type: object\ Default: {}

Options to forward to posthtml-expressions, like custom delimiters for example. Available options can be found here.

Component options

locals

You can pass data to a module using a locals="" attribute.

Must be a valid JSON object.

Example:

<!-- module.html -->
<p>The foo is {{ foo }} in this one.</p>
<content></content>
<!-- index.html -->
<module href="./module.html" locals='{"foo": "strong"}'>
  <p>Or so they say...</p>
</module>

Result

<p>The foo is strong in this one.</p>
<p>Or so they say...</p>

attributeAsLocals

All attributes on <module></module> will be added to locals

Example:

<!-- module.html -->
<div class="{{ class }}" id="{{ id }}" style="{{ style }}">
  <content></content>
</div>
<!-- index.html -->
<module 
  href="module.html" 
  class="text-center uppercase" 
  id="example"
  style="display: flex; gap: 2;"
>
  Module content
</module>

Result

<div class="text-center uppercase" id="example" style="display: flex; gap: 2;">
  Module content
</div>

changelog

0.9.1 (2023-03-02)

0.9.0 (2022-05-04)

  • 0.9.0 (28c177a)
  • add plugin option to forward options to posthtml-expressions (0ce0a96)
  • add posthtmlExpressionsOptions option documentation to readme (a514a28)
  • rename posthtmlExpressionsOptions to expressions for consitency among other posthtml plugins (3bc25e0)
  • build(deps): bump minimist from 1.2.5 to 1.2.6 (828e484)
  • build(deps): bump posthtml-match-helper from 1.0.2 to 1.0.3 (fc9f834)
  • build(deps): bump shelljs from 0.8.4 to 0.8.5 (696e339)
  • build(deps): bump trim-off-newlines from 1.0.1 to 1.0.3 (90dd232)
  • build(deps): update dependencies (503c294)

0.8.1 (2022-03-30)

0.8.0 (2021-12-01)

  • 0.8.0 (9c168c5)
  • feat: add attribute/value to locals, close #76 (2b96b22), closes #76
  • docs: add attribute/value to locals, #76 (5a79b35), closes #76
  • test: add attribute/value to locals, #76 (2b49df4), closes #76

0.7.6 (2021-11-08)

0.7.5 (2021-11-08)

  • 0.7.5 (b34bb60)
  • fix: fix locals merge (8076414)
  • build(deps): bump browserslist from 4.16.3 to 4.16.6 (5c33e57)
  • build(deps): bump handlebars from 4.7.6 to 4.7.7 (161cbc6)
  • build(deps): bump hosted-git-info from 2.8.8 to 2.8.9 (2b71581)
  • build(deps): bump normalize-url from 4.5.0 to 4.5.1 (3a44749)
  • build(deps): bump path-parse from 1.0.6 to 1.0.7 (d397fef)

0.7.4 (2021-03-04)

0.7.3 (2021-01-29)

  • 0.7.3 (425bd89)
  • fix: locals option and no content passed (0b0f76a)
  • test: fails with locals option and no content passed (0865f5d)

0.7.2 (2021-01-28)

  • 0.7.2 (d2888d3)
  • fix: parse options locals in <content> (6d01304)
  • test: fails to parse options locals in <content> (724cc58)

0.7.1 (2021-01-27)

  • 0.7.1 (12c90fb)
  • fix: parse options locals when passed as content (5f928c8)

0.7.0 (2021-01-27)

  • 0.7.0 (1ebfb1b)
  • build: fix lint runer (6104a41)
  • build: update dep dev (b17cd71)
  • build(deps): bump ini from 1.3.5 to 1.3.8 (cd6ae66)
  • chore: update readme.md (985e6c4)
  • chore: update readme.md (a5db69a)
  • refactor: switch merging order (79aa010)
  • feat: pass locals as option (ce06f76)

0.6.4 (2020-11-17)

  • 0.6.4 (0a6b456)
  • revert: fix nesting randomly fails when locals are provided (36e2689)

0.6.3 (2020-11-17)

  • 0.6.3 (5e5dea9)
  • build: update build system (982ebf5)
  • build: update dep dev (c6e29e8)
  • build(deps): bump lodash from 4.17.15 to 4.17.19 (d65f7c4)
  • fix: nesting randomly fails when locals are provided, close #51 (249cda6), closes #51
  • test: nesting randomly fails when locals are provided, issue #51 (0f8a44e), closes #51

0.6.2 (2020-07-08)

  • 0.6.2 (6f7ff75)
  • build: update dep dev (72a3eca)
  • style: after lint (ce7b0db)
  • fix: fails to parse expressions in <content>, close #46 (38036c6), closes #46
  • test: fails to parse expressions in <content> (8081287)

0.6.1 (2020-06-25)

0.6.0 (2020-04-08)

  • 0.6.0 (4951039)
  • build: update dep dev (c34e99c)
  • chore: update .gitignore (d92797b)
  • revert(docs): install command should include -D flag (35aa1a5)
  • docs: rename LICENSE.md (b3865ce)
  • docs: update README.md (15b3f8e)
  • docs: update README.md (5a148c6)
  • refactor: define options fallback in function parameter (09b0491)
  • refactor: set tag name when initializing plugin (2f5100c)
  • feat: custom attribute name (3b6d316)
  • feat: custom tag name (42aeb7e)

0.5.0 (2020-03-25)

0.4.3 (2020-03-23)

  • 0.4.3 (8fe03e6)
  • Require at least Node 10. (751b872)
  • Revert Travis config update. (e2ad62f)
  • Update .editorconfig (ea091ef)
  • Update .travis.yml (6ca7b6c)
  • Update dependencies. (69b94a1)
  • Update package.json. (32bff45)
  • Update tests. (b431a1c)
  • Update xo config. (05f8374)
  • Use semicolon. (44d9868)
  • ci: perf configuretion (5510cb1)
  • chore(package): update ava to version 0.16.0 (03e4adb)
  • chore(package): update ava to version 0.17.0 (7634eca)
  • chore(package): update ava to version 0.18.1 (8eef6ab)
  • chore(package): update ava to version 0.18.2 (fb1f584)
  • chore(package): update ava v0.18.2...0.19.0 (#29) (f6e138d), closes #29
  • chore(package): update ava v0.19.0...0.19.1 (#30) (efd53a9), closes #30
  • chore(package): update nyc to version 10.0.0 (a380b65)
  • chore(package): update nyc to version 10.1.2 (ce19a6b)
  • chore(package): update nyc to version 9.0.1 (6a7aee4)
  • chore(package): update nyc v10.1.2...10.2.0 (#28) (418655b), closes #28
  • chore(package): update nyc v10.2.0...10.3.0 (#31) (0297e83), closes #31
  • chore(package): update nyc v10.3.0...10.3.2 (#32) (b6b7781), closes #32
  • chore(package): update posthtml to version 0.9.2 (aac79bd)
  • chore(package): update xo to version 0.17.1 (4dc8519)
  • chore(package): update xo v0.17.1...0.19.0 (#26) (458384e), closes #26

0.4.1 (2016-07-21)

  • v0.4.1 (e7ab3a3)
  • refactor(index): return incorrect object content instead content (acf24c4)
  • docs(README): change README style, update examples && LICENSE (764e935)

0.4.0 (2016-06-29)

  • Add initial options property. (a7d8970)
  • Add additinal option: initial. Simple, untested implementation. (5d8b186)
  • Move posthtml processing to its own function. (16d52b1)

0.3.2 (2016-06-23)

  • Remove support for oldest node versions. (0bd3c13)

0.3.1 (2016-06-23)

  • Fix build for nodev0.12.x. (3e38f8e)

0.3.0 (2016-06-23)

0.2.0 (2016-06-14)

  • Add naive implementation of modules paths resolving. (03cf8de)
  • Allow to use <content/> inside modules. (5659826)
  • Convert README.md tabs to spaces. Add badges. (7a9a3af)
  • Fix <content> inlining. (6572927)
  • Initial commit (8c85d13)
  • Move posthtml to devdeps instead of deps. (e6340f0)
  • Remove const usage for node v0.12.x. (7c5f839)
  • Update AVA version. (a4a11f5)
  • Update tests and styleguide for path resolving feature. (51bf4f1)