hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
38.96k stars 4.78k forks source link

Open markdown file after running hexo new? #1007

Closed andrewboni closed 8 years ago

andrewboni commented 9 years ago

Hi, I'm wondering how to run some custom commands after doing a hexo new "My post". I'd like to immediately open the my-post.md file in my editor of choice (with a vi my-post.md). Anyone have a solution to this? Thanks!

tommy351 commented 9 years ago

You can try to listen to the new event. For example:

var spawn = require('child_process').spawn;

// Hexo 2.x
hexo.on('new', function(path){
  spawn('vi', [path]);
});

// Hexo 3
hexo.on('new', function(data){
  spawn('vi', [data.path]);
});
andrewboni commented 9 years ago

Thanks for the reply. Where should this live?

On Sun, Jan 25, 2015 at 7:47 PM, Tommy Chen notifications@github.com wrote:

You can try to listen to the new event. For example:

var spawn = require('child_process').spawn; // Hexo 2.x hexo.on('new', function(path){ spawn('vi', [path]); }); // Hexo 3 hexo.on('new', function(data){ spawn('vi', [data.path]); });

— Reply to this email directly or view it on GitHub https://github.com/hexojs/hexo/issues/1007#issuecomment-71411711.

tommy351 commented 9 years ago

You can add a JavaScript file to scripts folder. http://hexo.io/docs/plugins.html#Script

Xuanwo commented 8 years ago

Closed for no more update.

ArchitectBeaver commented 6 years ago

var spawn = require('child_process').spawn; // Hexo 3 hexo.on('new', function(data){ spawn('vi', [data.path]); });

For me, this failed to open Vim in terminal. I added { stdio: 'inherit' } option to let Vim use parent's stdios.

var spawn = require('child_process').spawn;
 // Hexo 3
hexo.on('new', function(data){
spawn('vim', [data.path], { stdio: 'inherit' });
});

It opens Vim in terminal now.

weilining commented 3 years ago

hexo 5 Not working

Nuevo009 commented 2 years ago

you can just write a function in your .profile e.g on windows using powershell with vscode

function hexoNew {
    param (
        [Object]$name
    )
    Set-Location path/to/your/hexo_blog_repo ; hexo new $name ; Set-Location ./source/_posts
    $new = Get-ChildItem | Where-Object { $_.CreationTime -gt (Get-Date).AddSeconds(-5) } | ForEach-Object {$_.Name} 
    code ./$new
}

*nix users can modify some commad and that's not hard for you.