-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.hbt
194 lines (148 loc) · 6.29 KB
/
README.hbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Metalsmith Link Globs
=====================
Repeat your `<script>`, `<link>`, `<img>` and `<a>` tags in your HTML by using a wildcard pattern in your `src=` or `href=` attributes.
[![npm version][npm-badge]][npm-link]
[![Build Status][travis-badge]][travis-link]
[![Dependencies][dependencies-badge]][dependencies-link]
[![Dev Dependencies][devdependencies-badge]][devdependencies-link]
[![codecov.io][codecov-badge]][codecov-link]
Overview
--------
When building HTML files that should link to a bunch of JavaScript files, one can hardcode each link, but that is tedious. It also doesn't work well when you want to use the same HTML but link to JavaScript that has not been concatenated nor minified. Why not just pull the list of files from Metalsmith?
<html>
<head>
<title> BEFORE THE PLUGIN </title>
<script src="third-party/angular.js"></script>
<script src="third-party/angular-ui.js"></script>
<script src="third-party/sha512.js"></script>
<script src="my-app.js"></script>
<script src="other-thing.js"></script>
<link rel="stylesheet" href="third-party/normalize.css">
<link rel="stylesheet" href="third-party/angular-ui.css">
<link rel="stylesheet" href="themes/clean-looks.css">
<link rel="stylesheet" href="themes/base.css">
<link rel="stylesheet" href="site.css">
<link rel="stylesheet" href="atomic.css">
This can be reduced to the following:
<html>
<head>
<title> WITH THE PLUGIN </title>
<script src="third-party/**/*.js"></script>
<script src="!(third-party)**/*.js"></script>
<link rel="stylesheet" href="**/*.css">
You would simply add this plugin to your `.use()` chain in Metalsmith.
var linkGlobs = require("metalsmith-link-globs");
// ...
.use(linkGlobs);
If the default settings don't work for you, there are options you can use to tailor how the library works.
Installation
------------
Use `npm` to install this package easily.
$ npm install --save metalsmith-link-globs
Alternately you may edit your `package.json` and add this to your `dependencies` object:
{
...
"dependencies": {
...
"metalsmith-link-globs": "*"
...
}
...
}
Usage
-----
Include this plugin the same as any other Metalsmith plugin. This first example shows how you would add it using a JSON configuration. It also shows the default settings. These are described later.
{
"plugins": {
"metalsmith-link-globs": {
"elementMatchOptions": {},
"encoding": "utf8",
"match": "**/*.html",
"matchOptions": {},
"nodes": [
{
"element": "a",
"property": "href"
},
{
"element": "img",
"property": "src"
},
{
"element": "link",
"property": "href"
},
{
"element": "script",
"property": "src"
}
]
}
}
}
This is a JavaScript example, which also includes a brief explanation of the options.
var linkGlobs = require("metalsmith-link-globs");
// Then in your list of plugins you use it.
.use(linkGlobs());
// Alternately, you can specify options. The values shown here are
// the defaults.
.use(linkGlobs({
// Matching options for the glob patterns that are used within
// elements.
"elementMatchOptions": {},
// How buffers are decoded into text and encoded again. Only
// affects the files being changed.
"encoding": "utf8",
// What files to target.
"match": "**/*.html",
// Options to select what files should be targeted.
"matchOptions": {},
// A list of HTML element and property names that should be
// processed.
"nodes": [
{
"element": "a",
"property": "href"
},
{
"element": "img",
"property": "src"
},
{
"element": "link",
"property": "href"
},
{
"element": "script",
"property": "src"
}
]
});
This uses [metalsmith-plugin-kit] to match files. The `.matchOptions` and `.elementMatchOptions` objects are just options passed directly to that library for matching files.
If you want to see what files are processed, what elements are found and the resulting list of matching files, enable debugging.
DEBUG=metalsmith-link-globs metalsmith
API
---
{{>main}}
Development
-----------
This uses Jasmine, Istanbul and ESLint for tests.
# Install all of the dependencies
npm install
# Run the tests
npm run test
License
-------
This software is licensed under a [MIT license][LICENSE] that contains additional non-advertising and patent-related clauses. [Read full license terms][LICENSE]
[codecov-badge]: https://img.shields.io/codecov/c/github/connected-world-services/metalsmith-link-globs/master.svg
[codecov-link]: https://codecov.io/github/connected-world-services/metalsmith-link-globs?branch=master
[dependencies-badge]: https://img.shields.io/david/connected-world-services/metalsmith-link-globs.svg
[dependencies-link]: https://david-dm.org/connected-world-services/metalsmith-link-globs
[devdependencies-badge]: https://img.shields.io/david/dev/connected-world-services/metalsmith-link-globs.svg
[devdependencies-link]: https://david-dm.org/connected-world-services/metalsmith-link-globs#info=devDependencies
[LICENSE]: LICENSE.md
[metalsmith-plugin-kit]: https://github.com/fidian/metalsmith-plugin-kit
[npm-badge]: https://img.shields.io/npm/v/metalsmith-link-globs.svg
[npm-link]: https://npmjs.org/package/metalsmith-link-globs
[travis-badge]: https://img.shields.io/travis/connected-world-services/metalsmith-link-globs/master.svg
[travis-link]: http://travis-ci.org/connected-world-services/metalsmith-link-globs