Skip to content

Commit 5f2adad

Browse files
committed
tpl/images: Format the QR hashes as hex
Slightly shorter, and it looks more like a ... hash.
1 parent a837976 commit 5f2adad

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

common/hashing/hashing.go

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ func HashString(vs ...any) string {
8383
return strconv.FormatUint(hash, 10)
8484
}
8585

86+
// HashStringHex returns a hash from the given elements as a hex encoded string.
87+
// See HashString for more information.
88+
func HashStringHex(vs ...any) string {
89+
hash := HashUint64(vs...)
90+
return strconv.FormatUint(hash, 16)
91+
}
92+
8693
var hashOptsPool = sync.Pool{
8794
New: func() any {
8895
return &hashstructure.HashOptions{

tpl/images/images.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (ns *Namespace) QR(options any) (images.ImageResource, error) {
169169
return nil, errors.New("scale must be an integer greater than or equal to 2")
170170
}
171171

172-
targetPath := path.Join(opts.TargetDir, fmt.Sprintf("qr_%s.png", hashing.HashString(opts)))
172+
targetPath := path.Join(opts.TargetDir, fmt.Sprintf("qr_%s.png", hashing.HashStringHex(opts)))
173173

174174
r, err := ns.createClient.FromOpts(
175175
create.Options{

tpl/images/images_integration_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term']
7979

8080
b := hugolib.Test(t, files)
8181
b.AssertFileContent("public/index.html",
82-
`<img data-id="0" data-img-hash="6ccacf8056c41475" data-level="" data-scale="" data-targetDir="" src="/qr_3891606335523452031.png">`,
83-
`<img data-id="1" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="" data-targetDir="" src="/qr_3891606335523452031.png">`,
84-
`<img data-id="2" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="4" data-targetDir="" src="/qr_3891606335523452031.png">`,
85-
`<img data-id="3" data-img-hash="c29338c3d105b156" data-level="low" data-scale="2" data-targetDir="" src="/qr_2532593520456080065.png">`,
86-
`<img data-id="4" data-img-hash="8f7a639cea917b0e" data-level="medium" data-scale="3" data-targetDir="" src="/qr_6630115934366238432.png">`,
87-
`<img data-id="5" data-img-hash="2d15d6dcb861b5da" data-level="quartile" data-scale="5" data-targetDir="" src="/qr_14167719016579496966.png">`,
88-
`<img data-id="6" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="" src="/qr_1700475212608554630.png">`,
89-
`<img data-id="7" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="foo/bar" src="/foo/bar/qr_12381230670040458986.png">`,
82+
`<img data-id="0" data-img-hash="6ccacf8056c41475" data-level="" data-scale="" data-targetDir="" src="/qr_3601c357f288f47f.png">`,
83+
`<img data-id="1" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="" data-targetDir="" src="/qr_3601c357f288f47f.png">`,
84+
`<img data-id="2" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="4" data-targetDir="" src="/qr_3601c357f288f47f.png">`,
85+
`<img data-id="3" data-img-hash="c29338c3d105b156" data-level="low" data-scale="2" data-targetDir="" src="/qr_232594637b3d9ac1.png">`,
86+
`<img data-id="4" data-img-hash="8f7a639cea917b0e" data-level="medium" data-scale="3" data-targetDir="" src="/qr_5c02e7507f8e86e0.png">`,
87+
`<img data-id="5" data-img-hash="2d15d6dcb861b5da" data-level="quartile" data-scale="5" data-targetDir="" src="/qr_c49dd961bcc47c06.png">`,
88+
`<img data-id="6" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="" src="/qr_17994d3244e3c686.png">`,
89+
`<img data-id="7" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="foo/bar" src="/foo/bar/qr_abd2f7b221eee6ea.png">`,
9090
)
9191

9292
files = strings.ReplaceAll(files, "low", "foo")

tpl/tplimpl/tplimpl_integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ https://gohugo.io"
730730
b := hugolib.Test(t, files)
731731

732732
b.AssertFileContent("public/index.html",
733-
`<img src="/codes/qr_1933601158373371382.png" width="148" height="148" alt="QR code linking to https://gohugo.io" class="my-class" id="my-id" title="My Title">`,
734-
`<img src="/qr_8288684942309665993.png" width="132" height="132">`,
733+
`<img src="/codes/qr_1ad588024620edf6.png" width="148" height="148" alt="QR code linking to https://gohugo.io" class="my-class" id="my-id" title="My Title">`,
734+
`<img src="/qr_7307530a297f2cc9.png" width="132" height="132">`,
735735
)
736736
}

0 commit comments

Comments
 (0)