hexo-featured-image
Advanced tools
Comparing version 0.2.1 to 0.3.0
30
index.js
var fs = require('hexo-fs'); | ||
var yaml = require('yaml-front-matter'); | ||
var fm = require('hexo-front-matter'); | ||
@@ -17,5 +17,6 @@ var contentJsonPath = hexo.public_dir + 'content.json'; | ||
hexo.extend.filter.register('before_post_render', function(data) { | ||
var featured_image = yaml.loadFront(data.raw).featured_image; | ||
if (featured_image){ | ||
var thumbnail = yaml.loadFront(data.raw).thumbnail; | ||
var front = fm.parse(data.raw); | ||
var featured_image = front.featured_image; | ||
if (featured_image) { | ||
var thumbnail = front.thumbnail; | ||
var imagePrefix = imagesPath; | ||
@@ -27,3 +28,6 @@ // Use post asset folder | ||
// Check if the featured image path is an absolute URI | ||
if (featured_image.indexOf('http') === 0 || featured_image.indexOf('/') === 0) { | ||
if ( | ||
featured_image.indexOf('http') === 0 || | ||
featured_image.indexOf('/') === 0 | ||
) { | ||
// Use no prefix since we have the full or relative URI | ||
@@ -45,8 +49,12 @@ imagePrefix = ''; | ||
// to work smoothly with hexo_generator_json_content | ||
var jsonContentCfg = hexo.config.hasOwnProperty('jsonContent') ? hexo.config.jsonContent : { | ||
meta: true | ||
}; | ||
var postsCfg = jsonContentCfg.hasOwnProperty('posts') ? jsonContentCfg.posts : {}; | ||
var jsonContentCfg = hexo.config.hasOwnProperty('jsonContent') | ||
? hexo.config.jsonContent | ||
: { | ||
meta: true, | ||
}; | ||
var postsCfg = jsonContentCfg.hasOwnProperty('posts') | ||
? jsonContentCfg.posts | ||
: {}; | ||
if ((postsCfg.featured_image) && fs.existsSync(contentJsonPath)) { | ||
if (postsCfg.featured_image && fs.existsSync(contentJsonPath)) { | ||
var postsObject = {}; | ||
@@ -64,3 +72,3 @@ var posts = hexo.locals.get('posts'); | ||
post.featured_image = fullPost.featured_image; | ||
if(postsCfg.thumbnail && fullPost.thumbnail){ | ||
if (postsCfg.thumbnail && fullPost.thumbnail) { | ||
post.thumbnail = fullPost.thumbnail; | ||
@@ -67,0 +75,0 @@ } |
{ | ||
"name": "hexo-featured-image", | ||
"description": "A plugin for the Hexo static site generator to add support for featured images in posts and json-content.", | ||
"author": "Daniel Skogly (http://www.pnkt.no)", | ||
"homepage": "https://github.com/poacher2k/hexo-featured-image", | ||
"keywords": [ | ||
"hexo", | ||
"featured", | ||
"image", | ||
"plugin" | ||
], | ||
"version": "0.2.1", | ||
"main": "index", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/poacher2k/hexo-featured-image.git" | ||
}, | ||
"dependencies": { | ||
"hexo-fs": "^0.2.2", | ||
"yaml-front-matter": "^3.4.0" | ||
}, | ||
"devDependencies": {} | ||
"name": "hexo-featured-image", | ||
"description": "A plugin for the Hexo static site generator to add support for featured images in posts and json-content.", | ||
"author": "Daniel Skogly (http://www.pnkt.no)", | ||
"homepage": "https://github.com/poacher2k/hexo-featured-image", | ||
"keywords": [ | ||
"hexo", | ||
"featured", | ||
"image", | ||
"plugin" | ||
], | ||
"version": "0.3.0", | ||
"main": "index", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/poacher2k/hexo-featured-image.git" | ||
}, | ||
"dependencies": { | ||
"hexo-fs": "^0.2.2", | ||
"hexo-front-matter": "^1.0.0" | ||
}, | ||
"devDependencies": {} | ||
} |
@@ -11,14 +11,13 @@ # Hexo Featured Image | ||
--- | ||
title: Cool post | ||
featured_image: my_img.png | ||
thumbnail: my_img_thumbnail.png | ||
--- | ||
What a cool blog I have! | ||
--- | ||
title: Cool post | ||
featured_image: my_img.png | ||
thumbnail: my_img_thumbnail.png | ||
--- | ||
What a cool blog I have! | ||
By using the Hexo Front Featured Image plugin, you can specify a post's featured image in its front matter. | ||
The absolute path to `my_img.png` will be available through `page.featured_image` in your templates. | ||
The absolute path to `my_img.png` will be available through `post.featured_image` in your templates. | ||
For example: | ||
@@ -28,5 +27,5 @@ | ||
... | ||
<% if (post.featured_image){ %> | ||
<img src="<%- post.featured_image %>"> | ||
... | ||
<% if (page.featured_image){ %> | ||
<img src="<%- page.featured_image %>"> | ||
<% } %> | ||
@@ -36,9 +35,18 @@ ... | ||
## Installation | ||
npm install --save hexo-featured-image | ||
npm install --save hexo-featured-image | ||
or | ||
yarn add hexo-featured-image | ||
## Usage | ||
This plugin will make automatically make `post.featured_image` available in your templates when you run `hexo server` or `hexo generate`. | ||
If you are using [hexo-generator-json-content](https://github.com/alexbruno/hexo-generator-json-content), it will automatically add the `featured_image` property to `content.json` when you run `hexo generate` and when you __exit__ `hexo server`. | ||
This plugin will make automatically make `page.featured_image` available in your templates when you run `hexo server` or `hexo generate`. | ||
If you are using [hexo-generator-json-content](https://github.com/alexbruno/hexo-generator-json-content), it will automatically add the `featured_image` property to `content.json` when you run `hexo generate` and when you **exit** `hexo server`. | ||
## Configuration | ||
### URL | ||
For this plugin to work correctly, you must set `url` to your URL in `_config.yml`. For example, if you are working locally using the default url (http://0.0.0.0:4000/), set it like this: | ||
@@ -48,4 +56,4 @@ | ||
... | ||
# URL | ||
... | ||
# URL | ||
url: http://0.0.0.0:4000/ | ||
@@ -55,2 +63,3 @@ ... | ||
### post_asset_folder | ||
This plugin works without configuration if you are using absolute or relative URI's, [post asset folders](https://hexo.io/docs/asset-folders.html), or you are storing your images in `source/images`. | ||
@@ -62,6 +71,6 @@ | ||
... | ||
# Directory | ||
... | ||
# Directory | ||
source_dir: source | ||
public_dir: public | ||
public_dir: public | ||
... | ||
@@ -71,7 +80,7 @@ image_dir: assets | ||
### [hexo-generator-json-content](https://github.com/alexbruno/hexo-generator-json-content) | ||
### [hexo-generator-json-content](https://github.com/alexbruno/hexo-generator-json-content) | ||
This plugin plays nicely with [hexo-generator-json-content](https://github.com/alexbruno/hexo-generator-json-content), and will output the absolute path of `featured_image` to `content.json` if `featured_image` has been set to `true` in the `jsonContent` configuration part of `_config.yml` like so: | ||
... | ||
... | ||
jsonContent: { | ||
@@ -78,0 +87,0 @@ ... |
Sorry, the diff of this file is not supported yet
7381
5
71
87
+ Addedhexo-front-matter@^1.0.0
+ Addedhexo-front-matter@1.0.0(transitive)
- Removedyaml-front-matter@^3.4.0
- Removedcommander@1.0.0(transitive)
- Removedyaml-front-matter@3.4.1(transitive)