Porter Pan 摘要
本文主要介绍,网页空间压缩的办法,去除html或其他脚本中的空格和无用的换行符,注释等,以减少网页文件的响应速度,实现web的快速响应。
安装和配置
1
| npm install hexo-neat --save
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
neat_enable: true
neat_html: enable: true exclude: - '**/index.h'
neat_css: enable: true exclude: - '**/*.min.css' - '**/*cntl.css'
neat_js: enable: true mangle: true output: compress: exclude: - '**/*.min.js' - '**/*.cntl.js' - '**/jquery.fancybox.pack.js' - '**/index.js' - '**/love.js'
|
这里exlude是排除的选项,根据自己的来合理调整
具体的配置语法,见hexo-neat
错误解决
通过上面的配置后,我们还是会编译出错,常见错误及解决办法如下
错误现象
1 2 3 4 5
| FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html Error: Parse Error: <--> C2: Cool label</p> <ul> <li>具体使用<a href="https://github.com/knsv/mermaid">帮助文档请移步github</a></li> </ul>
|
解决办法
查找这个文件node_modules/hexo-neat/index.js
1
| /GitHub_Projects/porterpan.github.io/node_modules/hexo-neat/index.js
|
1 2 3
| ignoreCustomComments: [/^\s*more/],
ignoreCustomFragments: [/<[^>]*>/,/<img[^>]*>/],
|
ignoreCustomFragments: [/<[>]*>/,/<img[>]*>/],是新加的。忽略尖括号和图像链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| var assign = require('object-assign');
if (true === hexo.config.neat_enable) { hexo.config.neat_html = assign({ enable: true, exclude: [], ignoreCustomComments: [/^\s*more/], ignoreCustomFragments: [/<[^>]*>/,/<img[^>]*>/], removeComments: true, removeCommentsFromCDATA: true, collapseWhitespace: true, collapseBooleanAttributes: true, removeEmptyAttributes: true, minifyJS: true, minifyCSS: true, }, hexo.config.neat_html);
hexo.config.neat_css = assign({ enable: true, exclude: ['*.min.css'] }, hexo.config.neat_css);
hexo.config.neat_js = assign({ enable: true, mangle: true, output: {}, compress: {}, exclude: ['*.min.js'] }, hexo.config.neat_js, { fromString: true });
var filter = require('./lib/filter'); hexo.extend.filter.register('after_render:html', filter.logic_html); hexo.extend.filter.register('after_render:css', filter.logic_css); hexo.extend.filter.register('after_render:js', filter.logic_js); }
|
附录
常见的正则表达式及注释
1 2 3 4 5 6 7 8 9 10
| protected void Page_Load(object sender, EventArgs e) { string regexstr = @"<(?!img|br|p|/p).*?>"; str = Regex.Replace(str, regexstr, string.Empty, RegexOptions.IgnoreCase); }
|