dinja-core 0.4.1

Safe MDX renderer with a Rust core and Python bindings
Documentation
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
//! Engine loading and initialization
//!
//! This module handles loading JavaScript libraries, setting up globals,
//! and initializing custom engines.

use crate::error::MdxError;
use anyhow::{Context, Result as AnyhowResult};
use deno_core::JsRuntime;
use std::fs;
use std::path::Path;

use super::constants::{script_tags, static_files};

/// Sets up global JavaScript objects needed by the libraries
pub(super) fn setup_globals(runtime: &mut JsRuntime) -> Result<(), MdxError> {
    const SETUP_GLOBALS: &str = r#"
        // Create global object if it doesn't exist
        if (typeof global === 'undefined') {
            globalThis.global = globalThis;
        }
        if (typeof window === 'undefined') {
            globalThis.window = globalThis;
        }
        if (typeof self === 'undefined') {
            globalThis.self = globalThis;
        }
        
        // Add minimal timer functions for engine
        if (typeof setTimeout === 'undefined') {
            globalThis.setTimeout = function(fn, delay) {
                // For SSR, execute immediately (we don't need real timers)
                if (delay === 0 || delay === undefined) {
                    fn();
                }
                return 0;
            };
        }
        if (typeof clearTimeout === 'undefined') {
            globalThis.clearTimeout = function() {};
        }
        if (typeof setInterval === 'undefined') {
            globalThis.setInterval = function(fn, delay) {
                return 0;
            };
        }
        if (typeof clearInterval === 'undefined') {
            globalThis.clearInterval = function() {};
        }
        if (typeof requestAnimationFrame === 'undefined') {
            globalThis.requestAnimationFrame = function(fn) {
                return 0;
            };
        }
        if (typeof cancelAnimationFrame === 'undefined') {
            globalThis.cancelAnimationFrame = function() {};
        }
        
        // Create a minimal document mock for server-side rendering
        // The actual DOM operations will be handled by engine-render-to-string
        if (typeof document === 'undefined') {
            globalThis.document = {
                createElementNS: function(ns, tag) { 
                    var el = { 
                        localName: tag,
                        nodeType: 1,
                        setAttribute: function() {},
                        removeAttribute: function() {},
                        appendChild: function() {},
                        insertBefore: function() {},
                        removeChild: function() {},
                        childNodes: [],
                        attributes: [],
                        style: {}
                    };
                    return el;
                },
                createTextNode: function(text) { 
                    return { 
                        nodeType: 3,
                        data: text || '',
                        parentNode: null
                    }; 
                },
                documentElement: {
                    appendChild: function() {},
                    insertBefore: function() {},
                    childNodes: [],
                    firstChild: null
                },
                createElement: function(tag) {
                    return document.createElementNS(null, tag);
                }
            };
        }

        // Helper to extract props with a given prefix (e.g. $attrs(props, "x-"))
        if (typeof globalThis.$attrs === 'undefined') {
            globalThis.$attrs = function(props, prefix) {
                if (!props || typeof props !== 'object') {
                    return {};
                }
                if (typeof prefix !== 'string' || !prefix.length) {
                    prefix = 'x-';
                }
                return Object.fromEntries(
                    Object.entries(props).filter(function(entry) {
                        return typeof entry[0] === 'string' && entry[0].startsWith(prefix);
                    })
                );
            };
        }

        // Object spread helper for ES5 compatibility (used by Oxc transformer)
        if (typeof globalThis._objectSpread === 'undefined') {
            globalThis._objectSpread = function(target) {
                for (var i = 1; i < arguments.length; i++) {
                    var source = arguments[i];
                    if (source != null) {
                        for (var key in source) {
                            if (Object.prototype.hasOwnProperty.call(source, key)) {
                                target[key] = source[key];
                            }
                        }
                    }
                }
                return target;
            };
        }
    "#;

    runtime
        .execute_script(script_tags::SETUP, SETUP_GLOBALS)
        .map_err(|e| MdxError::TsxTransform(format!("Failed to setup globals: {e:?}")))?;
    Ok(())
}

/// Wraps JavaScript code in a try-catch block for better error reporting
pub(super) fn wrap_js_code(code: &str, file_name: &str) -> String {
    format!(
        r#"
        try {{
            {code}
        }} catch (e) {{
            var errorMsg = 'JavaScript Error in {file_name}: ';
            if (e.message) errorMsg += e.message;
            else errorMsg += String(e);
            if (e.stack) errorMsg += '\nStack: ' + e.stack;
            throw new Error(errorMsg);
        }}
        "#,
        code = code,
        file_name = file_name
    )
}

/// Loads a JavaScript file and executes it in the runtime
pub(super) fn load_js_file(
    runtime: &mut JsRuntime,
    static_path: &Path,
    file_name: &str,
    script_tag: &'static str,
) -> AnyhowResult<()> {
    let file_path = static_path.join(file_name);
    let code = fs::read_to_string(&file_path)
        .with_context(|| format!("Failed to read {}", file_path.display()))?;

    let wrapped_code = wrap_js_code(&code, file_name);

    runtime
        .execute_script(script_tag, wrapped_code)
        .map_err(|e| {
            let error_details = format!("{e:?}");
            MdxError::TsxTransform(format!(
                "Failed to load {}: {}. \
                This might be due to missing browser APIs or incompatible JavaScript code.",
                file_name, error_details
            ))
        })?;

    Ok(())
}

/// Verifies that a JavaScript variable is available in the global scope
pub(super) fn verify_global_var(
    runtime: &mut JsRuntime,
    var_name: &str,
    check_script_tag: &'static str,
    verify_script: &'static str,
) -> Result<(), MdxError> {
    runtime
        .execute_script(check_script_tag, verify_script)
        .map_err(|e| MdxError::TsxTransform(format!("Failed to verify {}: {e:?}", var_name)))?;
    Ok(())
}

/// Loads engine library and verifies it's available
pub(super) fn load_engine_library(
    runtime: &mut JsRuntime,
    static_path: &Path,
) -> Result<(), MdxError> {
    load_js_file(
        runtime,
        static_path,
        static_files::ENGINE_MIN_JS,
        script_tags::ENGINE,
    )
    .map_err(|e| MdxError::TsxTransform(format!("Failed to load engine: {e:?}")))?;

    verify_global_var(
        runtime,
        "engine",
        script_tags::CHECK_ENGINE,
        r#"
        if (typeof engine === 'undefined') {
            throw new Error('engine.min.js loaded but engine variable is not available in global scope');
        }
        "#,
    )?;

    // Wrap h function to convert component string names back to function references
    const WRAP_H_FUNCTION: &str = r#"
        // Wrap engine.h to handle component rendering
        if (typeof engine !== 'undefined' && engine.h) {
            const originalH = engine.h;
            engine.h = function(tag, props, ...children) {
                // For HTML rendering: If tag is a function reference to a registered component,
                // call it directly instead of using Preact's component lifecycle
                if (typeof tag === 'function') {
                    // Check if this function is a registered component
                    let isRegisteredComponent = false;
                    let componentName = null;
                    if (globalThis.__registered_component_names) {
                        for (let key in globalThis) {
                            try {
                                if (globalThis[key] === tag &&
                                    globalThis.__registered_component_names.includes(key)) {
                                    isRegisteredComponent = true;
                                    componentName = key;
                                    break;
                                }
                            } catch (e) {
                                continue;
                            }
                        }
                    }

                    if (isRegisteredComponent) {
                        // Call component directly for SSR
                        const componentProps = props || {};
                        if (children && children.length > 0) {
                            componentProps.children = children.length === 1 ? children[0] : children;
                        }
                        return tag(componentProps);
                    }
                }

                // For Schema rendering: If tag is a string component name, look it up
                if (typeof tag === 'string') {
                    if (globalThis.__registered_component_names &&
                        globalThis.__registered_component_names.includes(tag)) {
                        const component = globalThis[tag];
                        if (typeof component === 'function') {
                            const componentProps = props || {};
                            if (children && children.length > 0) {
                                componentProps.children = children.length === 1 ? children[0] : children;
                            }
                            return component(componentProps);
                        }
                    }
                }

                // Handle Fragment/null/undefined
                if (typeof engine !== 'undefined' && (tag === engine.Fragment || tag === null || tag === undefined)) {
                    tag = 'Fragment';
                }

                return originalH(tag, props || {}, ...children);
            };
        }
        
        // Also wrap global h function if it exists
        if (typeof h !== 'undefined' && typeof h === 'function') {
            const originalGlobalH = h;
            globalThis.h = function(tag, props, ...children) {
                // For HTML rendering: If tag is a function reference to a registered component,
                // call it directly instead of using Preact's component lifecycle
                if (typeof tag === 'function') {
                    let isRegisteredComponent = false;
                    if (globalThis.__registered_component_names) {
                        for (let key in globalThis) {
                            try {
                                if (globalThis[key] === tag &&
                                    globalThis.__registered_component_names.includes(key)) {
                                    isRegisteredComponent = true;
                                    break;
                                }
                            } catch (e) {
                                continue;
                            }
                        }
                    }

                    if (isRegisteredComponent) {
                        const componentProps = props || {};
                        if (children && children.length > 0) {
                            componentProps.children = children.length === 1 ? children[0] : children;
                        }
                        return tag(componentProps);
                    }
                }

                // For Schema rendering: If tag is a string component name, look it up
                if (typeof tag === 'string') {
                    if (globalThis.__registered_component_names &&
                        globalThis.__registered_component_names.includes(tag)) {
                        const component = globalThis[tag];
                        if (typeof component === 'function') {
                            const componentProps = props || {};
                            if (children && children.length > 0) {
                                componentProps.children = children.length === 1 ? children[0] : children;
                            }
                            return component(componentProps);
                        }
                    }
                }

                // Handle Fragment/null/undefined
                if (typeof engine !== 'undefined' && (tag === engine.Fragment || tag === null || tag === undefined)) {
                    tag = 'Fragment';
                }

                return originalGlobalH(tag, props || {}, ...children);
            };
        }
    "#;

    runtime
        .execute_script(script_tags::WRAP_H_FUNCTION, WRAP_H_FUNCTION)
        .map_err(|e| MdxError::TsxTransform(format!("Failed to wrap h function: {e:?}")))?;

    Ok(())
}

/// Loads engine render-to-string library and verifies it's available
pub(super) fn load_engine_render_library(
    runtime: &mut JsRuntime,
    static_path: &Path,
) -> Result<(), MdxError> {
    load_js_file(
        runtime,
        static_path,
        static_files::ENGINE_TO_STRING_MIN_JS,
        script_tags::ENGINE_TO_STRING,
    )
    .map_err(|e| MdxError::TsxTransform(format!("Failed to load engine_to_string: {e:?}")))?;

    verify_global_var(
        runtime,
        "engine_to_string",
        script_tags::CHECK_ENGINE_TO_STRING,
        r#"
        if (typeof engine_to_string === 'undefined') {
            throw new Error('engine_to_string.min.js loaded but engine_to_string variable is not available in global scope');
        }
        "#,
    )?;
    Ok(())
}

/// Loads core.js engine library and verifies it's available
pub(super) fn load_core_engine_library(
    runtime: &mut JsRuntime,
    static_path: &Path,
) -> Result<(), MdxError> {
    load_js_file(
        runtime,
        static_path,
        static_files::CORE_JS,
        script_tags::CORE_ENGINE,
    )
    .map_err(|e| MdxError::TsxTransform(format!("Failed to load core.js: {e:?}")))?;

    verify_global_var(
        runtime,
        "engine",
        script_tags::CHECK_CORE_ENGINE,
        r#"
        if (typeof engine === 'undefined' || !engine || typeof engine.render !== 'function') {
            throw new Error('core.js loaded but engine variable is not available or engine.render is not a function');
        }
        "#,
    )?;
    Ok(())
}

/// Loads static JavaScript files from the static directory into the engine context
///
/// # Arguments
/// * `runtime` - Mutable reference to the JsRuntime
/// * `static_dir` - Path to the directory containing static JavaScript files
/// * `engine_code` - Optional custom engine code to inject
pub(super) fn load_static_files_internal(
    runtime: &mut JsRuntime,
    static_dir: impl AsRef<Path>,
) -> AnyhowResult<()> {
    let static_path = static_dir.as_ref();

    // Set up global objects that might be needed by the JS libraries
    setup_globals(runtime)?;

    // Load engine libraries (preact, used for HTML/JavaScript rendering)
    load_engine_library(runtime, static_path).map_err(anyhow::Error::from)?;
    load_engine_render_library(runtime, static_path).map_err(anyhow::Error::from)?;

    // Save preact engine reference before loading core.js (which overwrites engine)
    const SAVE_PREACT_ENGINE: &str = r#"
        if (typeof engine !== 'undefined') {
            globalThis.__preactEngine = engine;
            globalThis.__preactEngine_to_string = engine_to_string;
        }
    "#;
    runtime
        .execute_script(script_tags::SETUP, SAVE_PREACT_ENGINE)
        .map_err(|e| {
            anyhow::Error::from(MdxError::TsxTransform(format!(
                "Failed to save preact engine reference: {e:?}"
            )))
        })?;

    // Load core.js engine (used for schema rendering, overwrites engine variable)
    load_core_engine_library(runtime, static_path).map_err(anyhow::Error::from)?;

    // Save core engine and restore preact engine
    const SETUP_DUAL_ENGINES: &str = r#"
        // Save core.js engine as coreEngine for schema rendering
        if (typeof engine !== 'undefined' && engine.render) {
            globalThis.coreEngine = engine;
        }
        // Restore preact engine for HTML/JavaScript rendering
        if (typeof globalThis.__preactEngine !== 'undefined') {
            globalThis.engine = globalThis.__preactEngine;
            if (typeof globalThis.__preactEngine_to_string !== 'undefined') {
                globalThis.engine_to_string = globalThis.__preactEngine_to_string;
            }
        }
    "#;
    runtime
        .execute_script(script_tags::SETUP, SETUP_DUAL_ENGINES)
        .map_err(|e| {
            anyhow::Error::from(MdxError::TsxTransform(format!(
                "Failed to setup dual engines: {e:?}"
            )))
        })?;

    Ok(())
}