Skip to content

Commit 3bdec3f

Browse files
committed
docs: update documentation
1 parent 907e90e commit 3bdec3f

1 file changed

Lines changed: 40 additions & 10 deletions

File tree

README.md

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![NPM][npm]][npm-url]
44

5-
`posthtml-hash` is a [PostHTML](https://github.com/posthtml/posthtml) plugin for hashing static assets to enable caching.
5+
`posthtml-hash` is a [PostHTML](https://github.com/posthtml/posthtml) plugin for hashing file names to enable caching.
66

77
```diff
88
<html>
@@ -29,7 +29,7 @@ npm i -D posthtml-hash
2929

3030
### Input
3131

32-
The plugin will only attempt to hash files with `[hash]` in the name.
32+
By default, the plugin will attempt to hash file names that contain `[hash]`. As a qualifier, only nodes with a `href` or `src` attribute are considered.
3333

3434
```html
3535
<html>
@@ -81,29 +81,59 @@ For convenience, you can add the post-build script to your package.json. The `po
8181
}
8282
```
8383

84+
### Custom Hash Length
85+
86+
Customize the hash length by specifying an integer after the `hash:{NUMBER}`. The default hash length is `20`.
87+
88+
**Note**: This only works for a pattern that uses square brackets and a colon separator. Use the `hashLength` option for other use cases.
89+
90+
```html
91+
<script src="src.[hash].js"></script>
92+
<!-- src.b0dcc67ffc1fd562f212.js -->
93+
94+
<script src="src.[hash:8].js"></script>
95+
<!-- src.b0dcc67f.js -->
96+
```
97+
8498
### Options
8599

86-
This plugin assumes that the file to process is in the same directory as the posthtml script. If not, specify the relative path to the html file in `options.path`:
100+
This plugin assumes that the file to process is in the same directory as the PostHTML script. If not, specify the relative path to the html file in `options.path`:
87101

88102
```js
89103
hash({
90104
/**
91105
* Relative path to processed HTML file
92106
*/
93107
path: "public", // default: ""
108+
109+
/**
110+
* File name pattern (regular expression) to match
111+
*/
112+
pattern: new RegExp(/\custom-file-pattern/), // default: new RegExp(/\[hash.*]/g)
113+
114+
/**
115+
* Custom hash length
116+
*/
117+
hashLength: 8, // default: 20
94118
});
95119
```
96120

97-
### Custom Hash Length
121+
## Recipes
98122

99-
Customize the hash length by specifying an integer after the `hash:{NUMBER}`. The default hash length is `20`.
123+
### Custom Pattern and Hash Length
100124

101-
```html
102-
<script src="src.[hash].js"></script>
103-
<!-- src.b0dcc67ffc1fd562f212.js -->
125+
```js
126+
hash({
127+
pattern: new RegExp(/\custom-file-pattern/),
128+
hashLength: 8,
129+
});
130+
```
104131

105-
<script src="src.[hash:8].js"></script>
106-
<!-- src.b0dcc67f.js -->
132+
Result:
133+
134+
```diff
135+
- <script src="script.custom-file-pattern.js"></script>
136+
+ <script src="script.b0dcc67f.js"></script>
107137
```
108138

109139
## [Examples](examples)

0 commit comments

Comments
 (0)