2.0.1 • Published 5 months ago

metalsmith-html-relative v2.0.1

Weekly downloads
13
License
GPL-3.0-or-later
Repository
github
Last release
5 months ago

metalsmith-html-relative

npm: version npm: downloads

Snyk: vulnerabilities codecov: coverage license

A Metalsmith plugin to convert to relative paths within HTML.

This will change href, src, and other tag attributes that reference local files (pages, CSS, JavaScript, images, etc.) to use relative links (e.g. ../static/css/styles.css) rather than absolute links (e.g. /static/css/styles.css). This allows your website to be more portable, it can exist in any kind of subdomain or subdirectory.

Installation

npm install --save metalsmith-html-relative

JavaScript Usage

const Metalsmith = require('metalsmith');
const relative   = require('metalsmith-html-relative');

Metalsmith(__dirname)
    .use(relative({
        // options here
    }))
    .build((err) => {
        if (err) {
            throw err;
        }
    });

Options

html (optional)

Type: string Default: "**/*.html"

A micromatch glob pattern to find HTML files.

tags (optional)

Type: object Default:

{
    "a": "href",
    "img": ["src", "data-src"],
    "link": "href",
    "script": "src",
    "form": "action"
}

An object of what tags and attributes to process for glob patterns.

Example HTML

Example Input

Given a file tree:

.
├── contact
│   └── index.html
├── index.html
└── static
    ├── css
    │   └── styles.css
    └── js
        └── scripts.js

And the contents of contact/index.html are:

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="/static/css/styles.css">
    </head>
    <body>
        <script src="/static/js/scripts.js"></script>
    </body>
</html>

Example Output

After this plugin is run, the output of contact/index.html will be:

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="../static/css/styles.css">
    </head>
    <body>
        <script src="../static/js/scripts.js"></script>
    </body>
</html>

Changelog

Changelog

2.0.1

5 months ago

1.0.0

6 months ago

0.4.9

7 months ago

0.4.8

7 months ago

2.0.0

6 months ago

0.4.10

7 months ago

0.4.11

7 months ago

0.4.7

7 months ago

0.5.0

6 months ago

0.4.5

1 year ago

0.4.6

1 year ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago