posthtml-import

0.9.0 • Public • Published

NPM Deps Tests Coverage Standard Code Style

Import Plugin

Hello World

npm i -D posthtml-import

Hello World

Options

root: Set base directory, defaults to process.cwd()

encoding: Set file encoding, defaults to utf8

<import src="file.html">
<!DOCTYPE html>
<html>
  <head>
    <import src="file.html">
  </head>
  <body>
    <import src="/path/to/file.html">
  </body>
</html>

Note that it's necessary to declare the import on its own line for whitespace detection and there could be only one import statement per tag.

<!-- Do -->
<div>
  <import src="file.html">
</div>
 
<!-- Don't -->
<div><import src="./path/to/file.html"></div>
<!-- Do -->
<header>
  <import src="file.html">
</header>
<main>
  <import src="file2.html">
</main>
 
<!-- Don't -->
<div>
  <import src="file.html">
  <import src="file2.html">
</div>

Hello World

const { readFileSync } = require('fs')
 
const posthtml = require('posthtml')
const imports  = require('posthtml-import')
 
const html = readFileSync('./index.html', 'utf8')
 
posthtml([ imports({root: './path/to/imports'}) ])
  .process(html)
  .then((result) => console.log(result.html))

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>PostHTML Import</title>
  </head>
  <body>
    <nav>
      <import src="nav.html">
    </nav>
    <header>
      <import src="header.html">
    </header>
    <main>
      <import src="main.html">
    </main>
    <footer>
      <import src="footer.html">
    </footer>
  </body>
</html>

nav.html

<ul class="nav">
  <li class="nav__link">
    <a href="#">Home</a>
  </li>
  <li class="nav__link">
    <a href="#">About</a>
  </li>
  <li class="nav__link">
    <a href="#">Contact</a>
  </li>
</ul>

header.html

<h1>Hello World</h1>

main.html

<section class="intro">...</section>
<section class="about">...</section>
<section class="outro">...</section>

footer.html

<div class="col-md-3">...</div>
<div class="col-md-3">...</div>
<div class="col-md-3">...</div>

result.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>PostHTML Import</title>
  </head>
  <body>
    <nav>
      <ul class="nav">
        <li class="nav__link">
          <a href="#">Home</a>
        </li>
        <li class="nav__link">
          <a href="#">About</a>
        </li>
        <li class="nav__link">
          <a href="#">Contact</a>
        </li>
      </ul>
    </nav>
    <header>
      <h1>Hello World</h1>
    </header>
    <main>
      <section class="intro">...</section>
      <section class="about">...</section>
      <section class="outro">...</section>
    </main>
    <footer>
      <div class="col-md-3"></div>
      <div class="col-md-3"></div>
      <div class="col-md-3"></div>
    </footer>
  </body>
</html>

LICENSE

MIT License (MIT)

Copyright (c) PostHTML Michael Ciniawsky michael.ciniawsky@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

Package Sidebar

Install

npm i posthtml-import

Weekly Downloads

14

Version

0.9.0

License

MIT

Last publish

Collaborators

  • michael-ciniawsky