hexo.on("new", function (post) { console.log(`New post created:\n${post.content}\n${post.path}`);});
当执行命令 hexo new 时会触发:
shell
hexo new test
INFO Validating config
New post created:
---
title: test
date: 2024-12-10 09:46:33
tags:
---
D:\XXX\blog\source\_posts\test.md
INFO Created: D:\XXX\blog\source\_posts\test.md
exit
注意
在 Hexo 结束前发布。
javascript
hexo.on("exit", function () { console.log('Bye!');});
hexo.on("generateBefore", function () { let posts = hexo.locals.get("posts"); console.log(posts); console.log("\n----------------\n");});hexo.on("generateAfter", function () { let posts = hexo.locals.get("posts"); console.log(posts); console.log("\n----------------\n");});
执行:
shell
hexo clhexo s
INFO Validating config
INFO Start processing
_Query {
data: [
_Document {
title: 'Hello World',
_content: 'Welcome to [Hexo](https://hexo.io/)! This is your very first post. Check [documentation](https://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](https://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n' +
'\n' +
'## Quick Start\n' +
'\n' +
'### Create a new post\n' +
'\n' +
'``` bash\n' +
'$ hexo new "My New Post"\n' +
'```\n' +
'\n' +
'More info: [Writing](https://hexo.io/docs/writing.html)\n' +
'\n' +
'### Run server\n' +
'\n' +
'``` bash\n' +
'$ hexo server\n' +
'```\n' +
'\n' +
'More info: [Server](https://hexo.io/docs/server.html)\n' +
'\n' +
'### Generate static files\n' +
'\n' +
'``` bash\n' +
'$ hexo generate\n' +
'```\n' +
'\n' +
'More info: [Generating](https://hexo.io/docs/generating.html)\n' +
'\n' +
'### Deploy to remote sites\n' +
'\n' +
'``` bash\n' +
'$ hexo deploy\n' +
'```\n' +
'\n' +
'More info: [Deployment](https://hexo.io/docs/one-command-deployment.html)\n',
source: '_posts/hello-world.md',
raw: '---\n' +
'title: Hello World\n' +
'---\n' +
'Welcome to [Hexo](https://hexo.io/)! This is your very first post. Check [documentation](https://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](https://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n' +
'\n' +
'## Quick Start\n' +
'\n' +
'### Create a new post\n' +
'\n' +
'``` bash\n' +
'$ hexo new "My New Post"\n' +
'```\n' +
'\n' +
'More info: [Writing](https://hexo.io/docs/writing.html)\n' +
'\n' +
'### Run server\n' +
'\n' +
'``` bash\n' +
'$ hexo server\n' +
'```\n' +
'\n' +
'More info: [Server](https://hexo.io/docs/server.html)\n' +
'\n' +
'### Generate static files\n' +
'\n' +
'``` bash\n' +
'$ hexo generate\n' +
'```\n' +
'\n' +
'More info: [Generating](https://hexo.io/docs/generating.html)\n' +
'\n' +
'### Deploy to remote sites\n' +
'\n' +
'``` bash\n' +
'$ hexo deploy\n' +
'```\n' +
'\n' +
'More info: [Deployment](https://hexo.io/docs/one-command-deployment.html)\n',
slug: 'hello-world',
published: true,
date: Moment<2024-12-10T08:49:57+08:00>,
updated: Moment<2024-12-10T08:49:57+08:00>,
comments: true,
layout: 'post',
photos: [],
_id: 'cm4htkdur0000g49j8dxq6tfg',
path: [Getter],
permalink: [Getter],
full_source: [Getter],
asset_dir: [Getter],
tags: [Getter],
categories: [Getter]
},
_Document {
title: 'test',
date: Moment<2024-12-10T09:54:41+08:00>,
_content: '',
source: '_posts/test.md',
raw: '---\ntitle: test\ndate: 2024-12-10 09:54:41\ntags:\n---\n',
slug: 'test',
published: true,
updated: Moment<2024-12-10T09:54:41+08:00>,
comments: true,
layout: 'post',
photos: [],
_id: 'cm4htkdux0001g49jbpbsc60s',
path: [Getter],
permalink: [Getter],
full_source: [Getter],
asset_dir: [Getter],
tags: [Getter],
categories: [Getter]
}
],
length: 2
}
----------------
_Query {
data: [
_Document {
title: 'Hello World',
_content: 'Welcome to [Hexo](https://hexo.io/)! This is your very first post. Check [documentation](https://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](https://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n' +
'\n' +
'## Quick Start\n' +
'\n' +
'### Create a new post\n' +
'\n' +
'``` bash\n' +
'$ hexo new "My New Post"\n' +
'```\n' +
'\n' +
'More info: [Writing](https://hexo.io/docs/writing.html)\n' +
'\n' +
'### Run server\n' +
'\n' +
'``` bash\n' +
'$ hexo server\n' +
'```\n' +
'\n' +
'More info: [Server](https://hexo.io/docs/server.html)\n' +
'\n' +
'### Generate static files\n' +
'\n' +
'``` bash\n' +
'$ hexo generate\n' +
'```\n' +
'\n' +
'More info: [Generating](https://hexo.io/docs/generating.html)\n' +
'\n' +
'### Deploy to remote sites\n' +
'\n' +
'``` bash\n' +
'$ hexo deploy\n' +
'```\n' +
'\n' +
'More info: [Deployment](https://hexo.io/docs/one-command-deployment.html)\n',
source: '_posts/hello-world.md',
raw: '---\n' +
'title: Hello World\n' +
'---\n' +
'Welcome to [Hexo](https://hexo.io/)! This is your very first post. Check [documentation](https://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](https://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).\n' +
'\n' +
'## Quick Start\n' +
'\n' +
'### Create a new post\n' +
'\n' +
'``` bash\n' +
'$ hexo new "My New Post"\n' +
'```\n' +
'\n' +
'More info: [Writing](https://hexo.io/docs/writing.html)\n' +
'\n' +
'### Run server\n' +
'\n' +
'``` bash\n' +
'$ hexo server\n' +
'```\n' +
'\n' +
'More info: [Server](https://hexo.io/docs/server.html)\n' +
'\n' +
'### Generate static files\n' +
'\n' +
'``` bash\n' +
'$ hexo generate\n' +
'```\n' +
'\n' +
'More info: [Generating](https://hexo.io/docs/generating.html)\n' +
'\n' +
'### Deploy to remote sites\n' +
'\n' +
'``` bash\n' +
'$ hexo deploy\n' +
'```\n' +
'\n' +
'More info: [Deployment](https://hexo.io/docs/one-command-deployment.html)\n',
slug: 'hello-world',
published: true,
date: Moment<2024-12-10T08:49:57+08:00>,
updated: Moment<2024-12-10T08:49:57+08:00>,
comments: true,
layout: 'post',
photos: [],
_id: 'cm4htkdur0000g49j8dxq6tfg',
content: '<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>\n' +
'<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p>\n' +
'<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p>\n' +
'<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p>\n' +
'<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>\n',
excerpt: '',
more: '<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>\n' +
'<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p>\n' +
'<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p>\n' +
'<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p>\n' +
'<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>\n' +
'\n' +
'<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>\n',
path: [Getter],
permalink: [Getter],
full_source: [Getter],
asset_dir: [Getter],
tags: [Getter],
categories: [Getter],
prev: [_Document],
__post: true
},
_Document {
title: 'test',
date: Moment<2024-12-10T09:54:41+08:00>,
_content: '',
source: '_posts/test.md',
raw: '---\ntitle: test\ndate: 2024-12-10 09:54:41\ntags:\n---\n',
slug: 'test',
published: true,
updated: Moment<2024-12-10T09:54:41+08:00>,
comments: true,
layout: 'post',
photos: [],
_id: 'cm4htkdux0001g49jbpbsc60s',
content: '',
excerpt: '',
more: '',
path: [Getter],
permalink: [Getter],
full_source: [Getter],
asset_dir: [Getter],
tags: [Getter],
categories: [Getter],
next: [_Document],
__post: true
}
],
length: 2
}
----------------
/* global hexo */'use strict';// Backward compatibility for v0.2.0if (hexo.config.hide_posts) { const config = hexo.config.hide_posts; if (config.public_generators && !config.allowlist_generators) { config.allowlist_generators = config.public_generators; }}// Load plugin confighexo.config.hide_posts = Object.assign({ enable: true, filter: 'hidden', allowlist_generators: [], blocklist_generators: ['*'], allowlist_function: null, acl_function_per_post: null, hexo_7_compatibility_patch: hexo.version.startsWith('7'), noindex: true, noindex_tag: '<meta name="robots" content="noindex">', html_flag: '<!-- flag of hidden posts -->'}, hexo.config.hide_posts);const config = hexo.config.hide_posts;if (!config.enable) { return;}// Ensure allowlist and blocklist are arraysif (config.allowlist_generators && !Array.isArray(config.allowlist_generators)) { config.allowlist_generators = [config.allowlist_generators];}if (config.blocklist_generators && !Array.isArray(config.blocklist_generators)) { config.blocklist_generators = [config.blocklist_generators];}// Prepare hidden postshexo.extend.filter.register('before_generate', require('./lib/prepareHiddenPosts'));// Apply patch for Hexo 7.0hexo.extend.filter.register('before_generate', require('./lib/applyPatch'));// Hook into generatorshexo.extend.filter.register('after_init', require('./lib/injectGenerators'));// Add a command to get a list of all hidden posts.// Usage: `$ hexo hidden:list`hexo.extend.console.register( 'hidden:list', 'Show a list of all hidden articles.', require('./lib/getHiddenList'));// Append a special HTML tag to render result of hidden posts for future use.hexo.extend.filter.register('after_post_render', data => { if (data[config.filter]) { data.content += config.html_flag; } return data;});// When `after_render:html` filter is triggered, no useful data but// only a bunch of messy HTML string will be passed as argument.// So we have to use the HTML flag set before in `after_post_render` filter// to recognize hidden posts, and manipulate whatever we want.if (config.noindex) { hexo.extend.filter.register('after_render:html', str => { if (str && str.includes(config.html_flag)) { str = str.replace('</title>', '</title>' + config.noindex_tag); } return str; });}