mrmd-js 2.0.1 → 2.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Maxime Rivest
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -6387,6 +6387,143 @@ function createCssExecutor() {
6387
6387
  return new CssExecutor();
6388
6388
  }
6389
6389
 
6390
+ /**
6391
+ * Mermaid Executor
6392
+ *
6393
+ * Executes Mermaid diagram cells by rendering them to SVG.
6394
+ * Loads mermaid from CDN on first use and returns HTML displayData.
6395
+ *
6396
+ * @module execute/mermaid
6397
+ */
6398
+
6399
+
6400
+ /**
6401
+ * @typedef {import('../session/context/interface.js').ExecutionContext} ExecutionContext
6402
+ * @typedef {import('../types/execution.js').ExecuteOptions} ExecuteOptions
6403
+ * @typedef {import('../types/execution.js').ExecutionResult} ExecutionResult
6404
+ * @typedef {import('../types/execution.js').DisplayData} DisplayData
6405
+ */
6406
+
6407
+ /** CDN URL for mermaid */
6408
+ const MERMAID_CDN = 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js';
6409
+
6410
+ /** Counter for unique diagram IDs */
6411
+ let diagramCounter = 0;
6412
+
6413
+ /**
6414
+ * Load mermaid from CDN if not already loaded
6415
+ * @returns {Promise<void>}
6416
+ */
6417
+ async function ensureMermaidLoaded() {
6418
+ // Check if already loaded
6419
+ if (typeof window !== 'undefined' && window.mermaid) {
6420
+ return;
6421
+ }
6422
+
6423
+ // Load from CDN
6424
+ return new Promise((resolve, reject) => {
6425
+ const script = document.createElement('script');
6426
+ script.src = MERMAID_CDN;
6427
+ script.onload = () => {
6428
+ // Initialize mermaid with safe defaults
6429
+ window.mermaid.initialize({
6430
+ startOnLoad: false,
6431
+ theme: 'default',
6432
+ securityLevel: 'loose', // Allow clicks/links in diagrams
6433
+ fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
6434
+ });
6435
+ resolve();
6436
+ };
6437
+ script.onerror = () => reject(new Error('Failed to load mermaid from CDN'));
6438
+ document.head.appendChild(script);
6439
+ });
6440
+ }
6441
+
6442
+ /**
6443
+ * Mermaid executor - renders Mermaid diagrams to SVG
6444
+ */
6445
+ class MermaidExecutor extends BaseExecutor {
6446
+ /** @type {readonly string[]} */
6447
+ languages = ['mermaid'];
6448
+
6449
+ /**
6450
+ * Execute Mermaid diagram cell
6451
+ * @param {string} code - Mermaid diagram definition
6452
+ * @param {ExecutionContext} context - Execution context
6453
+ * @param {ExecuteOptions} [options] - Execution options
6454
+ * @returns {Promise<ExecutionResult>}
6455
+ */
6456
+ async execute(code, context, options = {}) {
6457
+ const startTime = performance.now();
6458
+
6459
+ try {
6460
+ // Ensure mermaid is loaded
6461
+ await ensureMermaidLoaded();
6462
+
6463
+ // Generate unique ID for this diagram
6464
+ const diagramId = `mermaid-diagram-${++diagramCounter}`;
6465
+
6466
+ // Render the diagram
6467
+ const { svg } = await window.mermaid.render(diagramId, code.trim());
6468
+
6469
+ const duration = performance.now() - startTime;
6470
+
6471
+ // Build display data with the rendered SVG
6472
+ /** @type {DisplayData[]} */
6473
+ const displayData = [
6474
+ {
6475
+ data: {
6476
+ 'text/html': svg,
6477
+ 'text/plain': `[Mermaid diagram rendered]`,
6478
+ },
6479
+ metadata: {
6480
+ mermaid: true,
6481
+ diagramId,
6482
+ },
6483
+ },
6484
+ ];
6485
+
6486
+ return {
6487
+ success: true,
6488
+ stdout: '',
6489
+ stderr: '',
6490
+ result: undefined,
6491
+ displayData,
6492
+ assets: [],
6493
+ executionCount: 0,
6494
+ duration,
6495
+ };
6496
+ } catch (error) {
6497
+ const duration = performance.now() - startTime;
6498
+ const errorMessage = error instanceof Error ? error.message : String(error);
6499
+
6500
+ // Return error as stderr with helpful message
6501
+ return {
6502
+ success: false,
6503
+ stdout: '',
6504
+ stderr: `Mermaid rendering error: ${errorMessage}`,
6505
+ result: undefined,
6506
+ error: {
6507
+ type: 'MermaidError',
6508
+ message: errorMessage,
6509
+ },
6510
+ displayData: [],
6511
+ assets: [],
6512
+ executionCount: 0,
6513
+ duration,
6514
+ };
6515
+ }
6516
+ }
6517
+ }
6518
+
6519
+ /**
6520
+ * Create a Mermaid executor
6521
+ * @returns {MermaidExecutor}
6522
+ */
6523
+ function createMermaidExecutor() {
6524
+ return new MermaidExecutor();
6525
+ }
6526
+
6390
6527
  /**
6391
6528
  * Execute Module
6392
6529
  *
@@ -6405,6 +6542,7 @@ function createDefaultExecutorRegistry() {
6405
6542
  registry.register(new JavaScriptExecutor());
6406
6543
  registry.register(new HtmlExecutor());
6407
6544
  registry.register(new CssExecutor());
6545
+ registry.register(new MermaidExecutor());
6408
6546
  return registry;
6409
6547
  }
6410
6548
 
@@ -7731,6 +7869,7 @@ exports.HtmlRenderer = HtmlRenderer;
7731
7869
  exports.IframeContext = IframeContext;
7732
7870
  exports.JavaScriptExecutor = JavaScriptExecutor;
7733
7871
  exports.MainContext = MainContext;
7872
+ exports.MermaidExecutor = MermaidExecutor;
7734
7873
  exports.MrpRuntime = MrpRuntime;
7735
7874
  exports.RUNTIME_NAME = RUNTIME_NAME;
7736
7875
  exports.RUNTIME_VERSION = RUNTIME_VERSION;
@@ -7750,6 +7889,7 @@ exports.createHtmlRenderer = createHtmlRenderer;
7750
7889
  exports.createIframeContext = createIframeContext;
7751
7890
  exports.createJavaScriptExecutor = createJavaScriptExecutor;
7752
7891
  exports.createMainContext = createMainContext;
7892
+ exports.createMermaidExecutor = createMermaidExecutor;
7753
7893
  exports.createRuntime = createRuntime;
7754
7894
  exports.createSession = createSession;
7755
7895
  exports.createSessionManager = createSessionManager;