Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /** * GFMR Utils - Unified Foundation Module * * Unified utility, configuration, debug, and error handling foundation * Provides common functionality as the base for all GFMR modules * * Consolidated 6 files into 1: * - gfmr-utils.js * - gfmr-config-accessor.js * - gfmr-debug-config.js * - gfmr-debug-helper.js * - markdown-config.js * - error-handler.js * * @package WpGfmRenderer * @since 2.0.0 */ (function(global) { 'use strict'; /** * WP GFM Utils - Unified Utility Class */ class WPGFMUtils { constructor() { // Configuration management this.config = new Map(); this.defaultConfig = new Map(); // Debug functionality this.debugMode = false; this.debugLevel = 'info'; // 'debug', 'info', 'warn', 'error' this.debugOutput = []; // Error handling this.errorHandlers = []; this.errorHistory = []; this.maxErrorHistory = 100; // Performance measurement this.metrics = new Map(); this.timers = new Map(); // Initialization this.initializeConfig(); this.initializeDebugMode(); this.setupGlobalErrorHandling(); } // ============================================= // Configuration Management System // ============================================= /** * Initialize configuration */ initializeConfig() { // Get WordPress configuration const wpConfig = global.wpGfmConfig || {}; const markdownConfig = global.wpGfmMarkdownConfig || {}; // Default configuration this.setDefaultConfig({ // Debug settings 'debug.enabled': false, 'debug.level': 'info', 'debug.console': true, 'debug.storage': false, // Performance settings 'performance.tracking': true, 'performance.maxMetrics': 1000, // Error handling settings 'error.reporting': true, 'error.maxHistory': 100, 'error.autoRecovery': true, // Internationalization settings 'i18n.locale': 'en', 'i18n.fallback': 'en', // Highlight settings 'highlight.theme': 'github-light', 'highlight.languages': ['javascript', 'python', 'php', 'html', 'css'], // Mermaid settings 'mermaid.theme': 'default', 'mermaid.maxWidth': 1200, // UI settings 'ui.copyButton': true, 'ui.accessibility': true, // Merged settings (retrieved from WordPress) ...wpConfig, ...markdownConfig }); this.debug('Configuration initialized', { wpConfig: Object.keys(wpConfig).length, markdownConfig: Object.keys(markdownConfig).length }); } /** * Set default configuration */ setDefaultConfig(defaults) { Object.entries(defaults).forEach(([key, value]) => { this.defaultConfig.set(key, value); if (!this.config.has(key)) { this.config.set(key, value); } }); } /** * Get configuration value */ getConfig(key, defaultValue = null) { if (this.config.has(key)) { return this.config.get(key); } if (this.defaultConfig.has(key)) { return this.defaultConfig.get(key); } // Get nested configuration using dot notation if (key.includes('.')) { const value = this.getNestedConfig(key); if (value !== undefined) { return value; } } return defaultValue; } /** * Get nested configuration (dot notation support) */ getNestedConfig(key) { const keys = key.split('.'); let current = global.wpGfmConfig || {}; for (const k of keys) { if (current && typeof current === 'object' && k in current) { current = current[k]; } else { return undefined; } } return current; } /** * Set configuration value */ setConfig(key, value) { this.config.set(key, value); this.debug(`Config set: ${key} = ${value}`); } // ============================================= // Debug System // ============================================= /** * Initialize debug mode */ initializeDebugMode() { // Check URL parameter const urlDebug = global.location && global.location.search.includes('debug=true'); // Check WordPress configuration const wpDebug = this.getConfig('debug.enabled', false); // Check global variable const globalDebug = global.wpGfmDebug === true; // Determine debug mode this.debugMode = urlDebug || wpDebug || globalDebug; this.debugLevel = this.getConfig('debug.level', 'info'); if (this.debugMode) { this.debug('Debug mode activated', { source: urlDebug ? 'URL' : wpDebug ? 'Config' : 'Global', level: this.debugLevel }); } } /** * Debug output */ debug(message, data = null, level = 'info') { if (!this.debugMode) return; const logEntry = { timestamp: new Date().toISOString(), level: level, message: message, data: data, stack: this.getConfig('debug.includeStack', false) ? new Error().stack : null }; // Console output if (this.getConfig('debug.console', true)) { const prefix = '[WP GFM Utils]'; switch (level) { case 'error': console.error(prefix, message, data); break; case 'warn': console.warn(prefix, message, data); break; case 'info': console.log(prefix, message, data); break; case 'debug': console.debug(prefix, message, data); break; } } // Storage save if (this.getConfig('debug.storage', false)) { this.debugOutput.push(logEntry); // Maximum storage limit const maxEntries = this.getConfig('debug.maxEntries', 1000); if (this.debugOutput.length > maxEntries) { this.debugOutput = this.debugOutput.slice(-maxEntries); } } } /** * Get debug information */ getDebugInfo() { return { enabled: this.debugMode, level: this.debugLevel, entries: this.debugOutput.length, config: Object.fromEntries(this.config), errors: this.errorHistory.length, metrics: Object.fromEntries(this.metrics) }; } // ============================================= // Error Handling System // ============================================= /** * Setup global error handling */ setupGlobalErrorHandling() { if (!this.getConfig('error.reporting', true)) return; // Capture JavaScript errors const originalErrorHandler = global.onerror; global.onerror = (message, source, lineno, colno, error) => { this.handleError({ type: 'javascript', message: message, source: source, line: lineno, column: colno, error: error }); if (originalErrorHandler) { return originalErrorHandler.call(global, message, source, lineno, colno, error); } return false; }; // Capture Promise rejections global.addEventListener('unhandledrejection', (event) => { this.handleError({ type: 'promise', message: 'Unhandled Promise Rejection', error: event.reason }); }); } /** * Register error handler */ addErrorHandler(handler, priority = 10) { this.errorHandlers.push({ handler, priority }); this.errorHandlers.sort((a, b) => a.priority - b.priority); } /** * Handle error */ handleError(error, context = null) { const errorEntry = { timestamp: new Date().toISOString(), error: error, context: context, id: this.generateErrorId() }; // Add to error history this.errorHistory.push(errorEntry); if (this.errorHistory.length > this.maxErrorHistory) { this.errorHistory = this.errorHistory.slice(-this.maxErrorHistory); } // Debug output this.debug('Error occurred', errorEntry, 'error'); // Execute registered handlers this.errorHandlers.forEach(({ handler }) => { try { handler(errorEntry); } catch (handlerError) { console.error('[WP GFM Utils] Error in error handler:', handlerError); } }); // Attempt auto recovery if (this.getConfig('error.autoRecovery', true)) { this.attemptAutoRecovery(errorEntry); } return errorEntry.id; } /** * Generate error ID */ generateErrorId() { return 'wpgfm_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9); } /** * Attempt auto recovery */ attemptAutoRecovery(errorEntry) { // Basic recovery strategy const error = errorEntry.error; if (error && error.message) { // Recovery for Shiki-related errors if (error.message.includes('shiki') || error.message.includes('highlight')) { this.debug('Attempting Shiki recovery'); // Execute emergency fix script if available if (global.wpGfmEmergencyHighlightFix) { setTimeout(() => global.wpGfmEmergencyHighlightFix(), 100); } } // Recovery for Mermaid-related errors if (error.message.includes('mermaid')) { this.debug('Attempting Mermaid recovery'); // Re-initialize Mermaid, etc. } } } // ============================================= // Performance Measurement // ============================================= /** * Start timer */ startTimer(name) { this.timers.set(name, performance.now()); } /** * End timer and get measured value */ endTimer(name) { const startTime = this.timers.get(name); if (!startTime) { this.debug(`Timer ${name} not found`, null, 'warn'); return null; } const elapsed = performance.now() - startTime; this.timers.delete(name); // Record in metrics this.recordMetric(name, elapsed); return elapsed; } /** * Record metric */ recordMetric(name, value, unit = 'ms') { if (!this.getConfig('performance.tracking', true)) return; const metric = { value: value, unit: unit, timestamp: Date.now() }; // Add to existing metrics if (!this.metrics.has(name)) { this.metrics.set(name, []); } const metrics = this.metrics.get(name); metrics.push(metric); // Maximum storage limit const maxMetrics = this.getConfig('performance.maxMetrics', 1000); if (metrics.length > maxMetrics) { metrics.splice(0, metrics.length - maxMetrics); } } /** * Get metric statistics */ getMetricStats(name) { const metrics = this.metrics.get(name); if (!metrics || metrics.length === 0) { return null; } const values = metrics.map(m => m.value); const sum = values.reduce((a, b) => a + b, 0); return { count: values.length, sum: sum, average: sum / values.length, min: Math.min(...values), max: Math.max(...values), latest: values[values.length - 1] }; } // ============================================= // General Utilities // ============================================= /** * Deep clone object */ deepClone(obj) { if (obj === null || typeof obj !== 'object') return obj; if (obj instanceof Date) return new Date(obj); if (obj instanceof Array) return obj.map(item => this.deepClone(item)); if (typeof obj === 'object') { const cloned = {}; Object.keys(obj).forEach(key => { cloned[key] = this.deepClone(obj[key]); }); return cloned; } } /** * Safe merge objects */ mergeObjects(...objects) { const result = {}; objects.forEach(obj => { if (obj && typeof obj === 'object') { Object.keys(obj).forEach(key => { result[key] = obj[key]; }); } }); return result; } /** * Sanitize string */ sanitizeString(str) { if (typeof str !== 'string') return ''; return str .replace(/[<>]/g, '') // Remove HTML tags .replace(/javascript:/gi, '') // Remove JavaScript URLs .trim(); } /** * Delayed function execution (debounce) */ debounce(func, wait, immediate = false) { let timeout; return function executedFunction(...args) { const later = () => { timeout = null; if (!immediate) func.apply(this, args); }; const callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(this, args); }; } /** * Limit function execution frequency (throttle) */ throttle(func, limit) { let inThrottle; return function(...args) { if (!inThrottle) { func.apply(this, args); inThrottle = true; setTimeout(() => inThrottle = false, limit); } }; } /** * Asset URL builder helper function * Build local asset URLs compliant with WordPress.org distribution * * @param {string} path - Relative path (e.g., 'assets/libs/shiki/shiki.min.js') * @returns {string} - Complete URL */ buildAssetUrl(path) { const baseUrl = global.wpGfmConfig?.pluginUrl || '/wp-content/plugins/markdown-renderer-for-github/'; // Normalize trailing slash from base URL const normalizedBase = baseUrl.replace(/\/+$/, ''); // Normalize leading slash from path const normalizedPath = path.replace(/^\/+/, ''); return `${normalizedBase}/${normalizedPath}`; } // ============================================= // External API (Global Exposure) // ============================================= /** * Module initialization complete */ initialize() { // Final initialization process this.debug('WP GFM Utils initialized', this.getDebugInfo()); // Initialization complete event if (global.document) { global.document.dispatchEvent(new CustomEvent('wpGfmUtilsReady', { detail: { utils: this } })); } return this; } /** * Health check */ healthCheck() { return { status: 'ok', version: '2.0.0', initialized: true, debugMode: this.debugMode, configLoaded: this.config.size > 0, errorsCount: this.errorHistory.length, metricsCount: this.metrics.size }; } /** * Get statistics */ getStats() { return { initialized: this.initialized, debugMode: this.debugMode, configEntries: this.config.size, errorCount: this.errorHandlers.length, metricsCount: this.metrics.size, timestamp: Date.now() }; } } // Create and expose global instance const wpGfmUtils = new WPGFMUtils(); // Expose as global variable global.wpGfmUtils = wpGfmUtils; global.WPGFMUtils = WPGFMUtils; // Also expose class (for testing) // Legacy API for backward compatibility global.wpGfmIsDebugMode = () => wpGfmUtils.debugMode; global.wpGfmGetConfig = (key, defaultValue) => wpGfmUtils.getConfig(key, defaultValue); global.wpGfmDebugLog = (message, data) => wpGfmUtils.debug(message, data); // Unified Error Handler API // Provides consistent error/warning logging across all GFMR modules global.wpGfmErrorHandler = { /** * Handle an error with context and details * * @param {Error|string} error - The error object or message * @param {string} context - Module or function context (e.g., 'Shiki', 'Mermaid') * @param {Object} details - Additional details for debugging */ handle: function(error, context, details) { const errorMessage = error instanceof Error ? error.message : String(error); const errorStack = error instanceof Error ? error.stack : null; console.error('[GFMR Error]', { context: context || 'Unknown', message: errorMessage, details: details || null, stack: errorStack }); // Also record in wpGfmUtils error history if available if (wpGfmUtils && typeof wpGfmUtils.handleError === 'function') { wpGfmUtils.handleError({ type: 'gfmr', message: errorMessage, source: context, error: error }, details); } }, /** * Log a warning message with context * * @param {string} message - The warning message * @param {string} context - Module or function context * @param {Object} details - Additional details for debugging */ warn: function(message, context, details) { console.warn('[GFMR Warn][' + (context || 'Unknown') + ']', message, details || ''); // Also record in debug log if available if (wpGfmUtils && typeof wpGfmUtils.debug === 'function') { wpGfmUtils.debug(message, details, 'warn'); } }, /** * Log an info message with context (for non-critical issues) * * @param {string} message - The info message * @param {string} context - Module or function context */ info: function(message, context) { if (wpGfmUtils && wpGfmUtils.debugMode) { console.info('[GFMR Info][' + (context || 'Unknown') + ']', message); } } }; // Execute initialization if (global.document && global.document.readyState === 'loading') { global.document.addEventListener('DOMContentLoaded', () => wpGfmUtils.initialize()); } else { // If DOM is already loaded setTimeout(() => wpGfmUtils.initialize(), 0); } // CommonJS / AMD support if (typeof module !== 'undefined' && module.exports) { module.exports = WPGFMUtils; } })(typeof window !== 'undefined' ? window : global); |