├── hexo-asset-image ├── package.json ├── README.md ├── .gitignore ├── LICENSE └── index.js /hexo-asset-image: -------------------------------------------------------------------------------- 1 | /Users/jinxinshe/other/hexo-asset-image -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hexo-asset-image", 3 | "version": "0.0.6", 4 | "description": "Give asset image in hexo a absolutely path automatically", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "hexo", 11 | "iamge", 12 | "asset", 13 | "path" 14 | ], 15 | "author": "xcodebuild", 16 | "license": "MIT", 17 | "dependencies": { 18 | "cheerio": "^0.19.0", 19 | "entities": "^1.1.2" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hexo-asset-image 2 | 3 | 4 | Give asset image in hexo a absolutely path automatically 5 | 6 | # Usege 7 | 8 | ```shell 9 | npm install hexo-asset-image --save 10 | ``` 11 | 12 | # Example 13 | 14 | ```shell 15 | MacGesture2-Publish 16 | ├── apppicker.jpg 17 | ├── logo.jpg 18 | └── rules.jpg 19 | MacGesture2-Publish.md 20 | ``` 21 | 22 | Make sure `post_asset_folder: true` in your `_config.yml`. 23 | 24 | Just use `![logo](logo.jpg)` to insert `logo.jpg`. 25 | 26 | # History 27 | 28 | 2018-04-18: support hexo-abbrlink 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .tern-port 2 | # Logs 3 | logs 4 | *.log 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 28 | node_modules 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 She Jinxin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var cheerio = require('cheerio'); 3 | 4 | // http://stackoverflow.com/questions/14480345/how-to-get-the-nth-occurrence-in-a-string 5 | function getPosition(str, m, i) { 6 | return str.split(m, i).join(m).length; 7 | } 8 | 9 | hexo.extend.filter.register('after_post_render', function(data){ 10 | var config = hexo.config; 11 | if(config.post_asset_folder){ 12 | var link = data.permalink; 13 | var beginPos = getPosition(link, '/', 3) + 1; 14 | var endPos = link.lastIndexOf('/'); 15 | var appendLink = ''; 16 | // In hexo 3.1.1, the permalink of "about" page is like ".../about/index.html". 17 | // if not with index.html endpos = link.lastIndexOf('.') + 1 support hexo-abbrlink 18 | if(/.*\/index\.html$/.test(link)) { 19 | // when permalink is end with index.html, for example 2019/02/20/xxtitle/index.html 20 | // image in xxtitle/ will go to xxtitle/index/ 21 | appendLink = 'index/'; 22 | } 23 | link = link.substring(beginPos, endPos) + '/' + appendLink; 24 | 25 | var toprocess = ['excerpt', 'more', 'content']; 26 | for(var i = 0; i < toprocess.length; i++){ 27 | var key = toprocess[i]; 28 | 29 | var $ = cheerio.load(data[key], { 30 | ignoreWhitespace: false, 31 | xmlMode: false, 32 | lowerCaseTags: false, 33 | decodeEntities: false 34 | }); 35 | 36 | $('img').each(function(){ 37 | if ($(this).attr('data-src')){ 38 | // For windows style path, we replace '\' to '/'. 39 | var src = $(this).attr('data-src').replace('\\', '/'); 40 | if(!(/http[s]*.*|\/\/.*/.test(src) 41 | || /^\s+\//.test(src) 42 | || /^\s*\/uploads|images\//.test(src))) { 43 | // For "about" page, the first part of "src" can't be removed. 44 | // In addition, to support multi-level local directory. 45 | var linkArray = link.split('/').filter(function(elem){ 46 | return elem != ''; 47 | }); 48 | var srcArray = src.split('/').filter(function(elem){ 49 | return elem != '' && elem != '.'; 50 | }); 51 | if(srcArray.length > 1) 52 | srcArray.shift(); 53 | src = srcArray.join('/'); 54 | 55 | $(this).attr('data-src', config.root + link + src); 56 | console.info&&console.info("update link as:-->"+config.root + link + src); 57 | } 58 | }else{ 59 | console.info&&console.info("no src attr, skipped..."); 60 | console.info&&console.info($(this)); 61 | } 62 | }); 63 | data[key] = $.html(); 64 | } 65 | } 66 | }); 67 | --------------------------------------------------------------------------------