@@ -4,60 +4,62 @@ import { PostHTML } from "posthtml";
44import hasha from "hasha" ;
55
66const DEFAULT_HASH_LENGTH = 20 ;
7- const REGEX_HASH = new RegExp ( / \[ h a s h .* ] / g) ;
7+ const DEFAULT_PATTERN = new RegExp ( / \[ h a s h .* ] / g) ;
88
9- export function replaceHash ( str : string , buffer : Buffer ) {
10- const match = str . match ( REGEX_HASH ) ;
9+ export function replaceHash (
10+ str : string ,
11+ buffer : Buffer ,
12+ exp : RegExp ,
13+ hashLength : number
14+ ) {
15+ const match = str . match ( exp ) ;
1116 const [ _ , len ] = match ! [ 0 ] . replace ( / \[ | ] / g, "" ) . split ( ":" ) ;
1217
13- return str . replace (
14- REGEX_HASH ,
15- hasha ( buffer ) . slice ( 0 , Number ( len ) || DEFAULT_HASH_LENGTH )
16- ) ;
18+ return str . replace ( exp , hasha ( buffer ) . slice ( 0 , Number ( len ) || hashLength ) ) ;
1719}
1820
1921type NodeWithHashRegex = { attrs : { href ?: string ; src ?: string } } ;
2022
21- function plugin ( options ?: { path ?: string } ) {
23+ function plugin ( options ?: {
24+ path ?: string ;
25+ hashLength ?: number ;
26+ pattern ?: RegExp ;
27+ } ) {
2228 return function posthtmlHash ( tree : PostHTML . Node ) {
23- tree . match (
24- [ { attrs : { href : REGEX_HASH } } , { attrs : { src : REGEX_HASH } } ] ,
25- ( node ) => {
26- const _node = ( node as unknown ) as NodeWithHashRegex ;
27- const { href, src } = _node . attrs ;
29+ const exp = options ?. pattern || DEFAULT_PATTERN ;
30+ const hashLength = options ?. hashLength || DEFAULT_HASH_LENGTH ;
2831
29- let fileName = "" ;
32+ tree . match ( [ { attrs : { href : exp } } , { attrs : { src : exp } } ] , ( node ) => {
33+ const _node = ( node as unknown ) as NodeWithHashRegex ;
34+ const { href, src } = _node . attrs ;
3035
31- if ( href ) {
32- fileName = href ;
33- } else if ( src ) {
34- fileName = src ;
35- }
36+ let fileName = "" ;
3637
37- const pathToFile = options ?. path || "" ;
38- const file = path . join ( process . cwd ( ) , pathToFile , fileName ) ;
38+ if ( href ) {
39+ fileName = href ;
40+ } else if ( src ) {
41+ fileName = src ;
42+ }
3943
40- if ( fs . existsSync ( file ) ) {
41- const buffer = fs . readFileSync ( file ) ;
42- const hashedFileName = replaceHash ( fileName , buffer ) ;
43- const hashedFile = path . join (
44- process . cwd ( ) ,
45- pathToFile ,
46- hashedFileName
47- ) ;
44+ const pathToFile = options ?. path || "" ;
45+ const file = path . join ( process . cwd ( ) , pathToFile , fileName ) ;
4846
49- fs . renameSync ( file , hashedFile ) ;
47+ if ( fs . existsSync ( file ) ) {
48+ const buffer = fs . readFileSync ( file ) ;
49+ const hashedFileName = replaceHash ( fileName , buffer , exp , hashLength ) ;
50+ const hashedFile = path . join ( process . cwd ( ) , pathToFile , hashedFileName ) ;
5051
51- if ( href ) {
52- _node . attrs . href = hashedFileName ;
53- } else if ( src ) {
54- _node . attrs . src = hashedFileName ;
55- }
56- }
52+ fs . renameSync ( file , hashedFile ) ;
5753
58- return node ;
54+ if ( href ) {
55+ _node . attrs . href = hashedFileName ;
56+ } else if ( src ) {
57+ _node . attrs . src = hashedFileName ;
58+ }
5959 }
60- ) ;
60+
61+ return node ;
62+ } ) ;
6163 } ;
6264}
6365
0 commit comments