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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
| const fs = require('fs'); const path = require('path'); const readline = require('readline'); const https = require('https'); const { exec } = require('child_process'); const { promisify } = require('util'); const execPromise = promisify(exec);
let config = {}
const configFilePath = process.env.GITALK_CONFIG_FILE || path.join(__dirname, 'gitalk_init.conf');
if (fs.existsSync(configFilePath)) { const configContent = fs.readFileSync(configFilePath).toString('utf-8');
const configLines = configContent.split('\n'); for (const line of configLines) { if (line.trim() === '' || line.trim().startsWith('#')) { continue; }
const [key, ...valueParts] = line.split('='); if (key && valueParts.length > 0) { const value = valueParts.join('=').trim();
const reg = /{process\.env\.[a-zA-Z_\-]}*/gm; const match = value.match(reg);
if (match) { match.forEach(matchItem => { const envKey = matchItem.substring(13, matchItem.length - 1); if (process.env[envKey]) { config[key.trim()] = value.replace(matchItem, process.env[envKey]); } else { config[key.trim()] = value; } }); } else { config[key.trim()] = value; }
if (config[key.trim()] === 'true') { config[key.trim()] = true; } else if (config[key.trim()] === 'false') { config[key.trim()] = false; } else if (!isNaN(config[key.trim()]) && config[key.trim()].trim() !== '') { config[key.trim()] = Number(config[key.trim()]); } } }
console.log(''); console.log(`\x1b[32m%s\x1b[0m`, `使用配置文件: ${configFilePath}`); } else { console.log(`\x1b[33m%s\x1b[0m`, `配置文件不存在: ${configFilePath},将使用默认配置或环境变量`);
config = { username: process.env.GITHUB_REPOSITORY_OWNER,
repo: process.env.GITALK_INIT_REPO,
token: process.env.GITALK_TOKEN,
enableCache: process.env.GITALK_INIT_CACHE !== undefined ? process.env.GITALK_INIT_CACHE === 'true' : true, cacheFile: process.env.GITALK_INIT_CACHE_FILE || path.join(__dirname, './public/gitalk-init-cache.json'),
cacheRemote: process.env.GITALK_INIT_CACHE_REMOTE,
enableGithubApiCheck: process.env.GITALK_ENABLE_GITHUB_API_CHECK !== undefined ? process.env.GITALK_ENABLE_GITHUB_API_CHECK === 'true' : true,
githubApiTimeout: process.env.GITALK_GITHUB_API_TIMEOUT || 5000,
postsDir: process.env.GITALK_INIT_POSTS_DIR || 'source/_posts',
hexoUrl: process.env.HEXO_URL,
hexoPermalink: process.env.HEXO_PERMALINK || ':abbrlink.html',
openInBrowser: process.env.GITALK_OPEN_IN_BROWSER !== undefined ? process.env.GITALK_OPEN_IN_BROWSER === 'true' : false,
browserWaitTime: process.env.GITALK_BROWSER_WAIT_TIME || 3000,
mouseActiveTime: process.env.GITALK_MOUSE_ACTIVE_TIME || 2000,
enableMouseMovement: process.env.GITALK_ENABLE_MOUSE_MOVEMENT !== undefined ? process.env.GITALK_ENABLE_MOUSE_MOVEMENT === 'true' : true,
maxPagesToOpen: process.env.GITALK_MAX_PAGES_TO_OPEN || 0,
butterflyConfigPath: process.env.BUTTERFLY_CONFIG_PATH || '_config.butterfly.yml' }; }
function configInit(config) { if (config.repo === undefined) { config.repo = `${config.username}.github.io` }
if (config.cacheRemote === undefined) { config.cacheRemote = `https://${config.repo}/gitalk-init-cache.json` }
if (config.postsDir === undefined) { config.postsDir = 'source/_posts' }
if (config.cacheFile === undefined) { config.cacheFile = path.join(__dirname, './public/gitalk-init-cache.json') }
if (config.enableCache === undefined) { config.enableCache = true }
if (config.enableGithubApiCheck === undefined) { config.enableGithubApiCheck = true }
if (config.githubApiTimeout === undefined) { config.githubApiTimeout = 5000 }
if (!config.hexoUrl) { console.warn('未设置Hexo URL,将使用默认值'); config.hexoUrl = `https://${config.repo}`; }
if (!config.hexoPermalink) { console.warn('未设置Hexo Permalink,将使用默认值'); config.hexoPermalink = ':abbrlink.html'; }
if (config.openInBrowser === undefined) { config.openInBrowser = false; }
if (config.browserWaitTime === undefined) { config.browserWaitTime = 3000; }
if (config.mouseActiveTime === undefined) { config.mouseActiveTime = 2000; }
if (config.enableMouseMovement === undefined) { config.enableMouseMovement = true; }
if (config.maxPagesToOpen === undefined || config.maxPagesToOpen < 0) { config.maxPagesToOpen = 0; }
if (config.butterflyConfigPath === undefined) { config.butterflyConfigPath = '_config.butterfly.yml'; } }
configInit(config)
function getButterflyCommentsConfig() { try { if (!fs.existsSync(config.butterflyConfigPath)) { console.log('') console.log(`\x1b[31m%s\x1b[0m`, `Butterfly 主题配置文件不存在,请检查${config.butterflyConfigPath}文件路径`); return null; }
const content = fs.readFileSync(config.butterflyConfigPath, 'utf8'); const lines = content.split('\n');
let inCommentsSection = false; let foundUse = false; let useValue = '';
for (const line of lines) { const trimmedLine = line.trim();
if (trimmedLine === 'comments:') { inCommentsSection = true; continue; }
if (foundUse) { break; }
if (inCommentsSection && trimmedLine.startsWith('use:')) { foundUse = true;
const match = trimmedLine.match(/^use:\s*(.+)$/); if (match && match[1]) { useValue = match[1].trim();
if ((useValue.startsWith("'") && useValue.endsWith("'")) || (useValue.startsWith('"') && useValue.endsWith('"'))) { useValue = useValue.substring(1, useValue.length - 1); } } }
if (inCommentsSection && trimmedLine && !trimmedLine.startsWith(' ') && !trimmedLine.startsWith('\t') && !trimmedLine.startsWith('#')) { if (trimmedLine !== 'comments:') { inCommentsSection = false; } } }
if (!foundUse) { console.log(`\x1b[31m%s\x1b[0m`, `在 ${config.butterflyConfigPath} 中未找到 comments.use 配置`); return null; }
return useValue;
} catch (error) { console.log(`\x1b[31m%s\x1b[0m`, `解析 Butterfly 主题配置文件时出错: ${error.message}`); return null; } }
function checkButterflyCommentsConfig() { const commentsUse = getButterflyCommentsConfig();
if (!commentsUse) { return false; }
const commentSystems = commentsUse.split(',').map(s => s.trim());
const gitalkIndex = commentSystems.indexOf('Gitalk');
if (gitalkIndex === -1) { console.log(`\x1b[31m%s\x1b[0m`, `Gitalk 不在当前启用的评论系统中。当前启用的评论系统: ${commentSystems.join(', ')}`); console.log(`\x1b[33m%s\x1b[0m`, `请修改 ${config.butterflyConfigPath} 文件中的 comments.use 配置,将 Gitalk 设为第一个评论系统`); return false; }
if (gitalkIndex !== 0) { console.log(`\x1b[33m%s\x1b[0m`, `Gitalk 不是第一个评论系统(当前是第 ${gitalkIndex + 1} 个)。当前启用的评论系统: ${commentSystems.join(', ')}`); console.log(`\x1b[33m%s\x1b[0m`, `请修改 ${config.butterflyConfigPath} 文件中的 comments.use 配置,将 Gitalk 设为第一个评论系统`); return false; } console.log('') console.log(`\x1b[32m%s\x1b[0m`, `Gitalk是所选评论系统,脚本将继续执行`); return true; }
const autoGitalkInit = { gitalkCache: null, getFiles (dir, files_) { files_ = files_ || []; const files = fs.readdirSync(dir); for (let i in files) { let name = dir + '/' + files[i]; if (fs.statSync(name).isDirectory()) { this.getFiles(name, files_); } else { if (name.endsWith('.md')) { files_.push(name); } } } return files_; }, async readItem(file) { const fileStream = fs.createReadStream(file);
const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity, });
let start = false; let frontMatterEnded = false; let post = {};
for await (const line of rl) { if (!frontMatterEnded) { if (start === true) { if (line.trim() === '---') { frontMatterEnded = true; continue; } const items = line.split(':') const key = items[0].trim(); if (key && items.length > 1) { let value = items.slice(1).join(':').trim(); if ((value.startsWith("'") && value.endsWith("'")) || (value.startsWith('"') && value.endsWith('"'))) { value = value.substring(1, value.length - 1); } post[key] = value; } } else { if (line.trim() === '---') { start = true } } } }
fileStream.close()
if (Object.keys(post).length === 0) { console.log(`\x1b[33m%s\x1b[0m`, `gitalk: warn read empty from: ${file}`); return null } if (post['comment'] === false || post['comment'] === 'false') { console.log(`\x1b[36m%s\x1b[0m`, `gitalk: ignore by comment = ${post['comment']} : ${file}`); return null }
if (!('title' in post)) { console.log(`\x1b[31m%s\x1b[0m`, `gitalk: ignore because the title miss: ${file}`); return null }
if (config.hexoPermalink.includes(':abbrlink') && !('abbrlink' in post)) { console.log('') console.log(`\x1b[31m%s\x1b[0m`, `gitalk忽略${file},因为缺少abbrlink,它需要永久链接`); return null }
if (!('date' in post)) { post['date'] = new Date().toISOString(); } if (!('categories' in post)) { post['categories'] = []; } if (!('tags' in post)) { post['tags'] = []; }
return post },
async readPosts(dir) { const posts = []; for (let file of this.getFiles(dir)) { const post = await this.readItem(file); if (post != null) { posts.push(post) } }
return posts },
getRemoteCache() { return new Promise((resolve, reject) => { const req = https.get(config.cacheRemote, function (res) { const chunks = [];
res.on('data', function (chunk) { chunks.push(chunk); });
res.on('end', function () { try { return resolve(JSON.parse(Buffer.concat(chunks).toString())); } catch (e) { return reject(e); } });
res.on('error', function (error) { return reject(error); }); });
req.end(); }) },
async checkIssueByGithubAPI(title) { if (!config.enableGithubApiCheck || !config.token || !config.username || !config.repo) { return false; }
return new Promise((resolve) => { const apiUrl = `https://api.github.com/repos/${config.username}/${config.repo}/issues?state=all`;
const options = { headers: { 'User-Agent': 'gitalk-init', 'Authorization': `token ${config.token}`, 'Accept': 'application/vnd.github.v3+json' }, timeout: config.githubApiTimeout };
const req = https.get(apiUrl, options, (res) => { let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => { try { if (res.statusCode === 200) { const issues = JSON.parse(data); const exists = issues.some(issue => issue.title === title); resolve(exists); } else { console.log(`\x1b[33m%s\x1b[0m`, `GitHub API 请求失败,状态码: ${res.statusCode}`); resolve(false); } } catch (error) { console.log(`\x1b[31m%s\x1b[0m`, `解析 GitHub API 响应失败: ${error.message}`); resolve(false); } }); });
req.on('error', (error) => { console.log(`\x1b[31m%s\x1b[0m`, `GitHub API 请求错误: ${error.message}`); resolve(false); });
req.on('timeout', () => { console.log(`\x1b[33m%s\x1b[0m`, `GitHub API 请求超时`); req.destroy(); resolve(false); });
req.end(); }); },
async getIsInitByCache(pathname, title){ let isInitialized = false;
if (this.gitalkCache === null) { this.gitalkCache = false; try { this.gitalkCache = JSON.parse(fs.readFileSync(config.cacheFile).toString('utf-8')); console.log('') console.log(`\x1b[32m%s\x1b[0m`, '读取缓存文件成功 ' + config.cacheFile) console.log('') } catch (e) { console.log('') if (e.code === 'ENOENT') { console.log(`\x1b[33m%s\x1b[0m`, '缓存文件不存在,正在创建空缓存文件: ' + config.cacheFile); try { const dir = path.dirname(config.cacheFile); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } fs.writeFileSync(config.cacheFile, '[]', 'utf8'); this.gitalkCache = []; console.log(`\x1b[32m%s\x1b[0m`, '成功创建空缓存文件: ' + config.cacheFile); console.log(''); } catch (createError) { console.log(`\x1b[31m%s\x1b[0m`, '创建缓存文件失败: ' + createError.message); console.log(`\x1b[31m%s\x1b[0m`, '缓存文件路径: ' + config.cacheFile); console.log(''); } } else { console.log(`\x1b[31m%s\x1b[0m`, '读取缓存文件失败: ' + e.message); console.log(`\x1b[31m%s\x1b[0m`, '缓存文件路径: ' + config.cacheFile); console.log('');
if (config.cacheRemote) { console.log('') console.log(`\x1b[90m%s\x1b[0m`, '正在从 ' + config.cacheRemote + ' 读取文件') console.log('') try { this.gitalkCache = await this.getRemoteCache() console.log('') console.log(`\x1b[32m%s\x1b[0m`, '读取缓存文件成功 ' + config.cacheRemote) console.log('') } catch (remoteError) { console.log('') console.log(`\x1b[31m%s\x1b[0m`, '读取远程缓存文件失败: ' + remoteError.message); console.log(`\x1b[31m%s\x1b[0m`, '远程缓存文件路径: ' + config.cacheRemote); console.log('') } } } } }
if (this.gitalkCache && Array.isArray(this.gitalkCache)) { isInitialized = this.gitalkCache.some(({pathname: itemPath}) => itemPath === pathname); }
if (!isInitialized && config.enableGithubApiCheck && title) { console.log(`\x1b[90m%s\x1b[0m`, `本地缓存中未找到,正在通过 GitHub API 检查文章 "${title}" 的 issue...`); isInitialized = await this.checkIssueByGithubAPI(title);
if (isInitialized && config.enableCache) { this.gitalkCache = this.gitalkCache || []; this.gitalkCache.push({ pathname }); await this.write(config.cacheFile, JSON.stringify(this.gitalkCache, null, 2)); console.log(`\x1b[32m%s\x1b[0m`, `通过 GitHub API 发现 issue 已存在,已更新缓存`); } }
return isInitialized; },
async write(fileName, content, flag = 'w+') { const dir = path.dirname(fileName); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); }
return new Promise((resolve) => { fs.open(fileName, flag, function (err, fd) { if (err) { resolve([err, false]); return; } fs.writeFile(fd, content, function (err) { if (err) { resolve([err, false]); return; } fs.close(fd, (err) => { if (err) { resolve([err, false]); } }); resolve([false, true]); }); }); }); },
async simulateMouseMovement() { try { const moveScript = ` Add-Type -AssemblyName System.Windows.Forms $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds for ($i = 0; $i -lt 5; $i++) { $x = Get-Random -Minimum 100 -Maximum ($screen.Width - 100) $y = Get-Random -Minimum 100 -Maximum ($screen.Height - 100) [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y) Start-Sleep -Milliseconds 300 } `; await execPromise(`powershell -Command "${moveScript}"`); } catch (e) { console.log(`\x1b[33m%s\x1b[0m`, `模拟鼠标移动失败: ${e.message}`); } },
async openBrowser(url) { try { await execPromise(`start "" "${url}"`); console.log(`\x1b[33m%s\x1b[0m`, `已打开浏览器访问`); } catch (error) { console.error(`打开浏览器时出错: ${error.message}`); } },
async closeBrowser() { try { await execPromise(`taskkill /F /IM chrome.exe 2>nul || taskkill /F /IM msedge.exe 2>nul || taskkill /F /IM firefox.exe 2>nul`); console.log(`\x1b[32m%s\x1b[0m`, `已关闭浏览器`); } catch (error) { console.log(`\x1b[33m%s\x1b[0m`, `关闭浏览器时出现非致命错误: ${error.message}`); } },
async openUrlsSequentially(urls, waitTime, maxPages = 0) { if (!urls || urls.length === 0) { console.log(`\x1b[33m%s\x1b[0m`, `没有需要打开的页面`); return; }
const urlsToOpen = maxPages > 0 ? urls.slice(0, maxPages) : urls;
try { console.log(`\x1b[32m%s\x1b[0m`, `准备逐个打开 ${urlsToOpen.length} 个页面...`); console.log(''); for (let i = 0; i < urlsToOpen.length; i++) { const urlObj = urlsToOpen[i]; const url = urlObj.url; const title = urlObj.title;
if (title) { console.log(`\x1b[32m%s\x1b[0m`, `[${title}]----准备打开文章内容`); } console.log(`\x1b[33m%s\x1b[0m`, `正在打开页面 ${i + 1}/${urlsToOpen.length}: ${url}`);
await this.openBrowser(url);
await new Promise(resolve => setTimeout(resolve, 1000));
if (config.enableMouseMovement) { console.log(`\x1b[33m%s\x1b[0m`, `正在模拟鼠标活动...`); await this.simulateMouseMovement();
await new Promise(resolve => setTimeout(resolve, config.mouseActiveTime)); }
const remainingTime = waitTime - 1000 - (config.enableMouseMovement ? config.mouseActiveTime : 0); if (remainingTime > 0) { await new Promise(resolve => setTimeout(resolve, remainingTime)); }
console.log(`\x1b[33m%s\x1b[0m`, `页面处理完成`); }
console.log('') console.log(`\x1b[32m%s\x1b[0m`, `所有页面处理完成,正在关闭浏览器...`); await this.closeBrowser();
} catch (error) { console.error(`处理URL时出错: ${error.message}`); } },
async openUrlsInBatches(urls, waitTime, batchSize) { if (!urls || urls.length === 0) { console.log(`\x1b[33m%s\x1b[0m`, `没有需要打开的页面`); return; }
const totalUrls = urls.length; let processedCount = 0; let batchCount = 1;
while (processedCount < totalUrls) { const remainingUrls = totalUrls - processedCount; const currentBatchSize = Math.min(batchSize, remainingUrls); const currentBatch = urls.slice(processedCount, processedCount + currentBatchSize);
console.log(''); console.log(`\x1b[32m%s\x1b[0m`, `##########################################第 ${batchCount} 批##########################################`); console.log(`\x1b[33m%s\x1b[0m`, `正在处理第 ${batchCount} 批,共 ${currentBatchSize} 个页面(剩余 ${remainingUrls - currentBatchSize} 个)`);
await this.openUrlsSequentially(currentBatch, waitTime, currentBatchSize);
processedCount += currentBatchSize; batchCount++;
if (processedCount < totalUrls) { console.log(''); console.log(`\x1b[36m%s\x1b[0m`, `第 ${batchCount - 1} 批处理完成,等待 5 秒后处理下一批...`); await new Promise(resolve => setTimeout(resolve, 5000)); } }
console.log(''); console.log(`\x1b[32m%s\x1b[0m`, `########################################所有批次处理完成########################################`); console.log(''); },
generatePermalink(post) { const { hexoPermalink } = config; const { abbrlink, title, date, categories, year, month, day, hour, minute, second } = post;
let permalinkStr = hexoPermalink;
const dateObj = date ? new Date(date) : new Date(); const dateObjYear = dateObj.getFullYear(); const dateObjMonth = String(dateObj.getMonth() + 1).padStart(2, '0'); const dateObjDay = String(dateObj.getDate()).padStart(2, '0'); const dateObjHour = String(dateObj.getHours()).padStart(2, '0'); const dateObjMinute = String(dateObj.getMinutes()).padStart(2, '0'); const dateObjSecond = String(dateObj.getSeconds()).padStart(2, '0');
const processedTitle = title.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\-]+/g, '').replace(/\-\-+/g, '-');
let processedCategories = ''; if (categories && Array.isArray(categories)) { processedCategories = categories.join('/'); } else if (categories && typeof categories === 'string') { processedCategories = categories; }
permalinkStr = permalinkStr .replace(':year', year || dateObjYear) .replace(':month', month || dateObjMonth) .replace(':day', day || dateObjDay) .replace(':hour', hour || dateObjHour) .replace(':minute', minute || dateObjMinute) .replace(':second', second || dateObjSecond) .replace(':title', processedTitle) .replace(':abbrlink', abbrlink || '') .replace(':category', processedCategories);
if (!permalinkStr.startsWith('/')) { permalinkStr = '/' + permalinkStr; }
return permalinkStr; },
async start(postDir) { const posts = await this.readPosts(postDir); const errorData = []; const initializedData = []; const successData = []; const urlsToOpen = [];
for (const item of posts) { const pathname = this.generatePermalink(item); const { title } = item;
const isInitialized = await this.getIsInitByCache(pathname, title);
if (isInitialized) { console.log(`\x1b[36m%s\x1b[0m`, `文章已处理----[${title}]`); initializedData.push({pathname}); continue; }
console.log(`\x1b[32m%s\x1b[0m`, `[${title}]----发现新文章 `);
if (config.openInBrowser) { const fullUrl = `${config.hexoUrl}${pathname}`; urlsToOpen.push({ url: fullUrl, title: title }); }
successData.push({ pathname }); console.log(`\x1b[32m%s\x1b[0m`, `[${title}]----文章已记录!`); console.log(`\x1b[32m%s\x1b[0m`, `文章链接: ${config.hexoUrl}${pathname}`); console.log(''); }
if (config.openInBrowser && urlsToOpen.length > 0) { console.log(''); console.log(`\x1b[33m%s\x1b[0m`,'##########################################访问网页##########################################')
if (config.maxPagesToOpen === 0) { console.log(`\x1b[33m%s\x1b[0m`, `准备一次性打开所有 ${urlsToOpen.length} 个页面`); console.log(''); await this.openUrlsSequentially(urlsToOpen, config.browserWaitTime, 0); } else { console.log(''); console.log(`\x1b[33m%s\x1b[0m`, `准备分批打开页面,每批 ${config.maxPagesToOpen} 个,共 ${urlsToOpen.length} 个`); await this.openUrlsInBatches(urlsToOpen, config.browserWaitTime, config.maxPagesToOpen); }
console.log(`\x1b[33m%s\x1b[0m`,'##########################################访问网页##########################################') console.log(''); }
console.log(''); console.log(`\x1b[35m%s\x1b[0m`, '##########################################运行结果##########################################');
if (errorData.length !== 0) { console.log(`\x1b[31m%s\x1b[0m`, `报错数据: ${errorData.length} 条。`); console.log(JSON.stringify(errorData, null, 2)) }
console.log(`\x1b[35m%s\x1b[0m`, `本次成功: ${successData.length} 条。`);
if (config.enableCache) { console.log(`\x1b[35m%s\x1b[0m`, `写入缓存: ${(initializedData.length + successData.length)} 条,已处理 ${initializedData.length} 条,本次成功: ${successData.length} 条。参考文件 ${config.cacheFile}。`); await this.write(config.cacheFile, JSON.stringify(initializedData.concat(successData), null, 2)); } else { console.log(`\x1b[35m%s\x1b[0m`, `已处理: ${initializedData.length} 条。`); }
}, }
if (checkButterflyCommentsConfig()) { autoGitalkInit.start(config.postsDir).then(() => { console.log('\x1b[35m%s\x1b[0m','文章处理完成') console.log('\x1b[35m%s\x1b[0m','##########################################运行结果##########################################'); }); } else { console.log('\x1b[31m%s\x1b[0m', '脚本执行已停止,请先修改配置项中的 Butterfly 主题配置文件路径'); console.log(''); console.log('\x1b[31m%s\x1b[0m', '##########################################脚本已停止##########################################'); }
|