posthtml-import
Import for PostHTML
Last updated 6 years ago by michael-ciniawsky .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install posthtml-import 
SYNC missed versions from official npm registry.

NPM Deps Tests Coverage Standard Code Style

Import Plugin

Install

npm i -D posthtml-import

Usage

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>

Example

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.

Current Tags

  • 0.9.0                                ...           latest (6 years ago)

4 Versions

  • 0.9.0                                ...           6 years ago
  • 0.8.2                                ...           6 years ago
  • 0.8.1                                ...           6 years ago
  • 0.8.0                                ...           6 years ago
Maintainers (1)
Downloads
Total 304
Today 0
This Week 4
This Month 5
Last Day 0
Last Week 0
Last Month 4
Dependencies (1)
Dev Dependencies (5)

Copyright 2014 - 2021 © npmmirror.com |