UNPKG

react-native-shiki-engine

Version:

Shiki syntax highlighting for React Native. Zero bridge overhead with native Oniguruma regex engine.

60 lines (59 loc) 2.1 kB
"use strict"; import { TurboModuleRegistry } from 'react-native'; import ShikiEngine from "../NativeShikiEngine.js"; import { convertToOnigMatch } from "./utils.js"; export function createNativeEngine(options = {}) { const { maxCacheSize = 1000 } = options; if (!isNativeEngineAvailable()) { throw new Error('Native engine not available'); } return { createScanner(patterns) { if (!Array.isArray(patterns) || patterns.some(p => typeof p !== 'string' && !(p instanceof RegExp))) { throw new TypeError('Patterns must be an array of strings or RegExp objects'); } const stringPatterns = patterns.map(p => typeof p === 'string' ? p : p.source); const scannerId = ShikiEngine.createScanner(stringPatterns, maxCacheSize); if (typeof scannerId !== 'number') { throw new TypeError('Failed to create native scanner'); } return { findNextMatchSync(string, startPosition) { if (startPosition < 0) throw new RangeError('Start position must be >= 0'); const stringContent = typeof string === 'string' ? string : string.content; if (typeof stringContent !== 'string') throw new TypeError('Invalid input string'); try { const result = ShikiEngine.findNextMatchSync(scannerId, stringContent, startPosition); return convertToOnigMatch(result); } catch (err) { if (__DEV__) console.error('Error in findNextMatchSync:', err); throw err; } }, dispose() { try { ShikiEngine.destroyScanner(scannerId); } catch (err) { if (__DEV__) console.error('Error disposing scanner:', err); } } }; }, createString(s) { if (typeof s !== 'string') throw new TypeError('Input must be a string'); return { content: s }; } }; } export function isNativeEngineAvailable() { try { return TurboModuleRegistry.getEnforcing('ShikiEngine') != null; } catch { return false; } } //# sourceMappingURL=index.js.map