npp:自己的记事本的位置-绝对位置;
targetDir:自己的hexo md文件的位置。
其他,就是要满足已经可以正常hexo d推送成功过一次。
具备nodejs 具备hexo。
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#ifdef _MSC_VER
#pragma comment(lib, "shell32.lib")
#endif
bool runCmd(const std::string& cmd)
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
std::string full = "cmd /c " + cmd;
if (!CreateProcessA(NULL, const_cast<char*>(full.c_str()),
NULL, NULL, FALSE, 0,
NULL, NULL, &si, &pi))
{
std::cerr << "CreateProcess failed: " << GetLastError() << "\n";
return false;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;
}
void waitEnter()
{
std::cin.sync();
std::cin.clear();
std::cout << "Press ENTER to continue...";
std::cin.get();
}
int main()
{
const char* targetDir = "D:\\ProgramData\\git\\blogs\\source\\_posts";
if (!SetCurrentDirectoryA(targetDir))
{
std::cerr << "ERROR: Cannot switch to " << targetDir << "\n";
return 1;
}
while (true)
{
std::cout << "\n=== Hexo Helper ===\n"
"1 -> hexo g\n"
"2 -> hexo d\n"
"3 -> hexo g, wait 6s, hexo d\n"
"5 -> open _posts folder\n"
"6 -> hexo new \"title\" and open with Notepad++\n"
"0 -> exit\n"
"Choice: ";
int choice;
if (!(std::cin >> choice)) break;
std::cin.sync();
switch (choice)
{
case 1:
std::cout << "Running hexo g ...\n";
runCmd("hexo g");
waitEnter();
break;
case 2:
std::cout << "Running hexo d ...\n";
runCmd("hexo d");
waitEnter();
break;
case 3:
std::cout << "Running hexo g ...\n";
runCmd("hexo g");
waitEnter();
std::cout << "Waiting 6 seconds ...\n";
Sleep(6000);
std::cout << "Running hexo d ...\n";
runCmd("hexo d");
waitEnter();
break;
case 5: // 新增:打开 _posts 目录
std::cout << "Opening _posts folder ...\n";
runCmd("explorer .");
waitEnter();
break;
case 6:
{
std::cout << "Enter post title (may contain spaces): ";
std::cin.sync();
std::cin.clear();
std::string title;
std::getline(std::cin, title);
if (title.empty())
{
std::cout << "Empty title, cancelled.\n";
break;
}
std::string cmd = "hexo new \"" + title + "\"";
std::cout << "Running: " << cmd << "\n";
runCmd(cmd);
std::string fileName;
for (std::string::size_type i = 0; i < title.size(); ++i)
{
char c = title[i];
if (c == ' ') fileName += '-';
else fileName += static_cast<char>(std::tolower(c));
}
fileName += ".md";
std::string npp = "\"D:\\7zip\\a.minitools\\notepadPP\\Notepad++\\notepad++.exe\"";
std::string openCmd = npp + " " + fileName;
std::cout << "Opening: " << fileName << "\n";
runCmd(openCmd);
waitEnter();
break;
}
case 0:
std::cout << "Bye.\n";
return 0;
default:
std::cout << "Invalid choice.\n";
break;
}
}
return 0;
}