hexo-renderer-mdx 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/index.js +71 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,8 +8,8 @@ A [Hexo](https://hexo.io/) renderer plugin for [MDX](https://mdxjs.com/) - Markd
|
|
|
8
8
|
- ⚛️ React component integration
|
|
9
9
|
- 📝 Markdown compatibility
|
|
10
10
|
- 🎨 Custom component support
|
|
11
|
-
-
|
|
12
|
-
-
|
|
11
|
+
- 📁 ES6 import statements for external packages
|
|
12
|
+
- ⚡ Fast compilation with @mdx-js/mdx
|
|
13
13
|
- 🔄 Automatic hydration bundle rebuilds on `hexo generate` and when components change during `hexo server`
|
|
14
14
|
|
|
15
15
|
## Installation
|
package/index.js
CHANGED
|
@@ -441,8 +441,10 @@ if (
|
|
|
441
441
|
hexo.env && hexo.env.cmd === 'server'
|
|
442
442
|
) {
|
|
443
443
|
hexo.extend.filter.register('after_init', function() {
|
|
444
|
-
// Set up file watcher for
|
|
444
|
+
// Set up file watcher for component paths from the JSON mapping
|
|
445
445
|
const sourceDir = path.join(hexo.source_dir, 'components');
|
|
446
|
+
const projectRoot = hexo && hexo.base_dir ? hexo.base_dir : process.cwd();
|
|
447
|
+
const componentPathJsonPath = path.join(projectRoot, 'hexo-renderer-mdx.component-path.json');
|
|
446
448
|
|
|
447
449
|
// Only initialize the persistent watcher during `hexo server` runs.
|
|
448
450
|
// For other commands (clean/generate), skip watcher to allow the process to exit.
|
|
@@ -450,34 +452,72 @@ hexo.extend.filter.register('after_init', function() {
|
|
|
450
452
|
return;
|
|
451
453
|
}
|
|
452
454
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
+
// Function to read component paths from JSON and extract keys
|
|
456
|
+
function getComponentPathsFromJson() {
|
|
457
|
+
try {
|
|
458
|
+
if (fs.existsSync(componentPathJsonPath)) {
|
|
459
|
+
const mapping = JSON.parse(fs.readFileSync(componentPathJsonPath, 'utf8')) || {};
|
|
460
|
+
return Object.keys(mapping).filter(p => fs.existsSync(p));
|
|
461
|
+
}
|
|
462
|
+
} catch (e) {
|
|
463
|
+
// ignore parse/read errors
|
|
464
|
+
}
|
|
465
|
+
return [];
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// Function to recreate the watcher with current component paths
|
|
469
|
+
function recreateWatcher() {
|
|
455
470
|
if (mdxComponentWatcher) {
|
|
456
471
|
mdxComponentWatcher.close();
|
|
457
472
|
}
|
|
473
|
+
|
|
474
|
+
const componentPaths = getComponentPathsFromJson();
|
|
458
475
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
persistent: true
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
console.log(`INFO Watching components directory: ${sourceDir}`);
|
|
465
|
-
|
|
466
|
-
// Add event listeners for debugging
|
|
467
|
-
mdxComponentWatcher.on('ready', () => {
|
|
468
|
-
console.log('INFO Watcher ready, monitoring for changes...');
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
mdxComponentWatcher.on('error', (error) => {
|
|
472
|
-
console.error('INFO Watcher error:', error);
|
|
473
|
-
});
|
|
474
|
-
|
|
475
|
-
mdxComponentWatcher.on('all', (event, path) => {
|
|
476
|
-
if (event === 'change' || event === 'add' || event === 'unlink') {
|
|
477
|
-
console.log(`INFO Watcher event: ${event} - ${path}`);
|
|
478
|
-
}
|
|
479
|
-
});
|
|
476
|
+
// Watch both the component files and the JSON mapping file itself
|
|
477
|
+
const pathsToWatch = [...componentPaths, componentPathJsonPath];
|
|
480
478
|
|
|
479
|
+
if (pathsToWatch.length === 0) {
|
|
480
|
+
console.log(`INFO No component paths to watch yet`);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
try {
|
|
485
|
+
mdxComponentWatcher = chokidar.watch(pathsToWatch, {
|
|
486
|
+
ignored: /node_modules|\.git/,
|
|
487
|
+
persistent: true
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
console.log(`INFO Watching ${componentPaths.length} component path(s)`);
|
|
491
|
+
|
|
492
|
+
// Add event listeners for debugging
|
|
493
|
+
mdxComponentWatcher.on('ready', () => {
|
|
494
|
+
console.log('INFO Watcher ready, monitoring for changes...');
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
mdxComponentWatcher.on('error', (error) => {
|
|
498
|
+
console.error('INFO Watcher error:', error);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
mdxComponentWatcher.on('all', (event, watchedPath) => {
|
|
502
|
+
if (event === 'change' || event === 'add' || event === 'unlink') {
|
|
503
|
+
console.log(`INFO Watcher event: ${event} - ${watchedPath}`);
|
|
504
|
+
|
|
505
|
+
// If the JSON mapping file was changed, update the watcher
|
|
506
|
+
if (watchedPath === componentPathJsonPath && (event === 'change' || event === 'add')) {
|
|
507
|
+
console.log(`INFO Component mapping updated, refreshing watched paths...`);
|
|
508
|
+
process.nextTick(() => {
|
|
509
|
+
recreateWatcher();
|
|
510
|
+
});
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
} catch (err) {
|
|
516
|
+
console.warn('Failed to create component watcher:', err.message);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
try {
|
|
481
521
|
const handleComponentChange = (changedPath) => {
|
|
482
522
|
console.log(`\nINFO ⚡ Component file changed: ${changedPath}`);
|
|
483
523
|
console.log(`INFO Clearing caches and triggering regeneration...`);
|
|
@@ -487,7 +527,7 @@ hexo.extend.filter.register('after_init', function() {
|
|
|
487
527
|
|
|
488
528
|
// Clear the require cache for all components and Babel
|
|
489
529
|
Object.keys(require.cache).forEach(key => {
|
|
490
|
-
if (key.includes(
|
|
530
|
+
if (key.includes('source/components') || key.includes('.hexo-mdx-entry') || key.includes('source\\components')) {
|
|
491
531
|
delete require.cache[key];
|
|
492
532
|
}
|
|
493
533
|
});
|
|
@@ -563,8 +603,7 @@ hexo.extend.filter.register('after_init', function() {
|
|
|
563
603
|
const hashes = Array.from(new Set(affectedMdxFiles.map(f => crypto.createHash('md5').update(f).digest('hex').slice(0, 8))));
|
|
564
604
|
hashes.forEach(h => bundleEntryByHash(h));
|
|
565
605
|
// Resume watcher
|
|
566
|
-
|
|
567
|
-
mdxComponentWatcher.on('change', handleComponentChange);
|
|
606
|
+
recreateWatcher();
|
|
568
607
|
return;
|
|
569
608
|
}
|
|
570
609
|
// Fallback to full clean+generate below
|
|
@@ -582,13 +621,11 @@ hexo.extend.filter.register('after_init', function() {
|
|
|
582
621
|
}
|
|
583
622
|
console.log('INFO ✓ Refresh your browser to see changes');
|
|
584
623
|
// Resume watcher
|
|
585
|
-
|
|
586
|
-
mdxComponentWatcher.on('change', handleComponentChange);
|
|
624
|
+
recreateWatcher();
|
|
587
625
|
}).catch(err => {
|
|
588
626
|
console.warn('Regeneration error:', err.message);
|
|
589
627
|
// Resume watcher even on error
|
|
590
|
-
|
|
591
|
-
mdxComponentWatcher.on('change', handleComponentChange);
|
|
628
|
+
recreateWatcher();
|
|
592
629
|
});
|
|
593
630
|
});
|
|
594
631
|
};
|
|
@@ -599,6 +636,9 @@ hexo.extend.filter.register('after_init', function() {
|
|
|
599
636
|
} catch (err) {
|
|
600
637
|
console.warn('Component file watcher setup warning:', err.message);
|
|
601
638
|
}
|
|
639
|
+
|
|
640
|
+
// Initialize the watcher for the first time
|
|
641
|
+
recreateWatcher();
|
|
602
642
|
});
|
|
603
643
|
}
|
|
604
644
|
|