hexo-photoswipe
What is hexo-photoswipe
When you use hexo to build an vanilla blog, you might want a fine gallery to exhibit photos you uploaded.
Hexo-photoswipe
is the one that would power you and your photos up.
photoswipe is a beautiful and easy-to-use gallery, but limited to the working theory of which, it require the width
and height
attributes to exhibit the images/photos, in which case, most iamges do not have these attributes natrually.
This plugin is built for photoswipe, and it would generate width
and height
attributes when you run hexo s
or hexo g
.
How to use
Note that, this is not an Out of the box plugin. To avoid adding another js
file in your final website (which makes your website load slow), you are required to copy and modify some code to your somejs file manually. I'll be thrilled if it inspired you some kind.
We'll get there later.
Note that, hexo-lazyload-image is recommanded to install, which will generate the data-original
attr in img
tag. However you can config not to use it if you don't need it.
Note that, this plugin detect the image size/resolution two ways.
- One is that images are stored in
yourTitle/someImage.format
which means images are introduced in {% asset_img foo.bar "foobar some text" %} way.
for these local images, just calculate the width and height
- The other is that images are quoted as http(s), which means images are introduced in ![foobar some text](http(s)://www.john.doe/foo.bar) way
for these online images, download synchronously first time, and cache them in your disk, and then calculate the width and height one by one
install
So, first you install hexo-lazyload-image
, or omit if you don't want lazyload feature.
$ npm install hexo-lazyload-image --save
Below are how you can config hexo-lazyload-image
First add configuration in _config.yml from your hexo project.
lazyload: enable: true onlypost: false loadingImg: # eg /images/loading.gif
onlypost
If true, only the images from post or page will support lazy-load. If false, the whole images of your site will use lazy-load, including > the images dist from your theme, but not including the background images >from CSS style.
loadingImg
If you keep the value nothing (by default), then it will use the default >loading image. If you want to customize the image, then you need to copy your loading > image to your current theme image folder and then change this path to > find it. Run hexo command.
$ hexo clean
Additionally, install hexo-photoswipe
:
$ npm install hexo-photoswipe --save
and boom, you have a div
-wrapped img
tag which also contains something like class="image-container" data-type="content-image" data-size="100x100"
in you final render result.
Finally, you write some code using photoswipe in your somefilename.js
If you have no idea what to do, please open an issue at github: https://github.com/HarborZeng/hexo-photoswipe/issues
Demo
for online preview, visit https://tellyouwhat.cn for details.
LICENSE
Tutorial
Suppose you have installed the dependencies above. Then you may proceed.
0. Enable the plugin
in your hexo blog's _config.yml
:
# <div class="image-container" data-type="content-image" data-size="100x100"><img src="xxx" data-original="yyy"></img></div>' # imgSrcIn: dataOriginal if you install hexo-lazyload-image, or src as default # dataType is a presered attr. It is usless now but you can custum in your own js with this unique selector. # className you can custom the class for div, in your own css file as you want. # imageFileBaseDir is the base directory where hexo-photoswipe would find images, eg, source/_posts/my-first-post/cover.jpg. The final pattern is {imageFileBaseDir}/{YourPostTitle}/{imageName}.{imageFormat}. photoswipe: enable: true imageFileBaseDir: source/_posts imgSrcIn: dataOriginal className: image-container dataType: content-image
1. include JS and CSS files
You can find them in dist/ folder of GitHub repository. Sass and uncompiled JS files are in folder src/.
It doesn't matter how and where will you include JS and CSS files. Code is executed only when you call new PhotoSwipe(). So feel free to defer loading of files if you don't need PhotoSwipe to be opened initially.
or
It's highly recommended to use cdn
pswp
codes into your layout page
2. add you can add the code below to your theme's main page
or layout.ejs
code
This code can be appended anywhere, but ideally before the closing </body>
. You may reuse it across multiple galleries (as long as you use same UI class). You can just copy the following code in case you make a typo or something.
<!-- Root element of PhotoSwipe. Must have class pswp. --> <!-- Background of PhotoSwipe. It's a separate element as animating opacity is faster than rgba(). --> <!-- Slides wrapper with overflow:hidden. --> <!-- Container that holds slides. PhotoSwipe keeps only 3 of them in the DOM to save memory. Don't modify these 3 pswp__item elements, data is added later on. --> <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. --> <!-- Controls are self-explanatory. Order can be changed. -->
3. Initialize in your own js file
let pswpElement = documentlet imgSrcItem =
4. Add css into your project (optional)