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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| const fs = require('fs'); const path = require('path'); const readline = require('readline'); const { promisify } = require('util');
const mkdir = promisify(fs.mkdir); const writeFile = promisify(fs.writeFile); const readdir = promisify(fs.readdir); const readFile = promisify(fs.readFile);
hexo.extend.console.register('newp', 'Create new post with path (e.g., first/second/title)', { options: [ { name: '--auto-complete', desc: 'Enable tab completion' } ] }, async function(args) { const log = hexo.log || console.log; if (args.autoComplete) { return this.tabComplete(args); } const fullPath = args._[0]; if (!fullPath) { log.error('Usage: hexo newp <path/levels/title>'); return; }
const normalizedPath = fullPath.replace(/\\/g, '/'); const parts = normalizedPath.split('/'); const title = parts.pop(); const category = parts.join('/'); const lastDir = parts[parts.length - 1] || '';
const folderName = `【${lastDir}】${title}`; const fileName = `${folderName}.md`; const baseDir = path.join(hexo.source_dir, '_posts', category); const folderPath = path.join(baseDir, folderName); const filePath = path.join(baseDir, fileName); try { await mkdir(folderPath, { recursive: true }); const templatePath = path.join(hexo.scaffold_dir, 'post.md'); let templateContent; try { templateContent = await readFile(templatePath, 'utf8'); } catch (e) { templateContent = [ '---', 'title: {{ title }}', 'date: {{ date }}', 'categories:', 'tags:', '- private', 'description: 声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由用户承担全部法律及连带责任,文章作者不承担任何法律及连带责任。', 'top:', 'comments: true', '---' ].join('\n'); } const content = templateContent .replace(/{{ title }}/g, folderName) await writeFile(filePath, content); log.info(`Created folder: ${folderPath}`); log.info(`Created file: ${filePath}`); } catch (error) { log.error(`Error creating post: ${error.message}`); } });
hexo.extend.console.tabComplete = async function(args) { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, completer: async (line) => { const partial = line.trim().replace(/\\/g, '/'); const baseDir = path.join(hexo.source_dir, '_posts'); const matches = await this.findMatches(baseDir, partial); if (matches.length === 1) { const completed = matches[0] + '/'; return [[completed], completed]; } else if (matches.length > 1) { console.log('\n' + matches.join('\n')); } return [matches, line]; } });
rl.question('Enter post path: ', (line) => { if (line.trim()) { hexo.call('newp', { _: [line.trim()] }, () => { rl.close(); }); } else { rl.close(); } }); };
hexo.extend.console.findMatches = async function(baseDir, partialPath) { const parts = partialPath.split('/'); let currentDir = baseDir; let existingPath = []; for (const part of parts.slice(0, -1)) { if (!part) continue; const testDir = path.join(currentDir, part); try { if (!fs.existsSync(testDir)) break; const stat = fs.statSync(testDir); if (!stat.isDirectory()) break; currentDir = testDir; existingPath.push(part); } catch (e) { break; } } const lastPartial = parts[parts.length - 1] || ''; let dirContents = []; try { dirContents = await readdir(currentDir, { withFileTypes: true }); } catch (e) { return []; } return dirContents .filter(dirent => dirent.isDirectory()) .map(dirent => dirent.name) .filter(name => name.startsWith(lastPartial)) .map(name => [...existingPath, name].join('/')); };
|