nucleusflow 0.0.1

A powerful Rust library for content processing, enabling static site generation, document conversion, and templating.
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
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
681
682
683
684
685
686
687
688
689
690
//! # Markdown Processing Module
//!
//! Provides a secure and flexible Markdown processing framework with configurable options,
//! metadata extraction, and content sanitization. This module focuses on safety,
//! performance, and extensibility.
//!
//! ## Key Features
//!
//! - **Safe Processing**: Secure content handling with robust sanitization
//! - **Metadata Extraction**: YAML frontmatter parsing with type-safe handling
//! - **Table of Contents**: Automatic generation of nested TOC structures
//! - **Configurable Options**: Support for tables, footnotes, and strikethrough
//! - **Content Validation**: Protection against XSS and other injection attacks
//!
//! ## Example Usage
//!
//! ```rust
//! use nucleusflow::processors::markdown::MarkdownProcessor;
//! use nucleusflow::core::traits::Processor;
//!
//! let processor = MarkdownProcessor::new()
//!     .with_tables(true)
//!     .with_footnotes(true)
//!     .with_strikethrough(true);
//!
//! let content = r#"---
//! title: My Post
//! tags: [rust, markdown]
//! ---
//! # Hello World
//!
//! This is a **markdown** document.
//! "#;
//!
//! let result = processor.process(content.to_string(), None).unwrap();
//! ```

use crate::core::{
    error::{ProcessingError, Result},
    traits::Processor,
};
use pulldown_cmark::{
    html, Event, HeadingLevel, Options as MarkdownOptions, Parser, Tag,
};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use serde_yml::from_str;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// Maximum allowed size for Markdown content in bytes (10MB)
const MAX_CONTENT_SIZE: usize = 10 * 1024 * 1024;

/// List of allowed HTML tags that won't be stripped during sanitization
const ALLOWED_HTML_TAGS: &[&str] = &[
    "p",
    "br",
    "h1",
    "h2",
    "h3",
    "h4",
    "h5",
    "h6",
    "strong",
    "em",
    "del",
    "ul",
    "ol",
    "li",
    "code",
    "pre",
    "blockquote",
    "hr",
    "table",
    "thead",
    "tbody",
    "tr",
    "th",
    "td",
    "img",
    "a",
    "nav",
];

/// Configuration options for markdown processing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessorConfig {
    /// Enable sanitization of HTML output
    #[serde(default = "default_true")]
    pub sanitize: bool,

    /// Enable generation of Table of Contents
    #[serde(default)]
    pub toc: bool,

    /// Maximum heading level for TOC (1-6)
    #[serde(default = "default_toc_level")]
    pub toc_max_level: u8,

    /// Enable automatic link references
    #[serde(default = "default_true")]
    pub auto_links: bool,

    /// Custom processor options
    #[serde(default)]
    pub options: HashMap<String, JsonValue>,
}

impl Default for ProcessorConfig {
    fn default() -> Self {
        Self {
            sanitize: true,
            toc: false,
            toc_max_level: 3,
            auto_links: true,
            options: HashMap::new(),
        }
    }
}

/// Metadata extracted from Markdown content.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContentMetadata {
    /// Content title
    pub title: Option<String>,
    /// Content description
    pub description: Option<String>,
    /// Publication date
    pub date: Option<String>,
    /// Content tags
    pub tags: Vec<String>,
    /// Custom metadata fields
    pub custom: HashMap<String, JsonValue>,
}

/// Represents a heading in the Table of Contents.
#[derive(Debug)]
struct TocEntry {
    /// Heading text
    text: String,
    /// Heading level (1-6)
    level: u8,
    /// Generated ID for the heading
    id: String,
}

/// Processor for Markdown content with advanced features and security.
#[derive(Debug, Clone)]
pub struct MarkdownProcessor {
    options: MarkdownOptions,
    config: ProcessorConfig,
    /// Cache of allowed HTML tags for faster sanitization
    allowed_tags: Arc<HashSet<String>>,
}

impl MarkdownProcessor {
    /// Creates a new MarkdownProcessor with default settings.
    pub fn new() -> Self {
        let allowed_tags = ALLOWED_HTML_TAGS
            .iter()
            .map(|&tag| tag.to_string())
            .collect();

        Self {
            options: MarkdownOptions::empty(),
            config: ProcessorConfig::default(),
            allowed_tags: Arc::new(allowed_tags),
        }
    }

    /// Enables table support in Markdown processing.
    pub fn with_tables(mut self, enable: bool) -> Self {
        if enable {
            self.options.insert(MarkdownOptions::ENABLE_TABLES);
        } else {
            self.options.remove(MarkdownOptions::ENABLE_TABLES);
        }
        self
    }

    /// Enables strikethrough support in Markdown processing.
    pub fn with_strikethrough(mut self, enable: bool) -> Self {
        if enable {
            self.options.insert(MarkdownOptions::ENABLE_STRIKETHROUGH);
        } else {
            self.options.remove(MarkdownOptions::ENABLE_STRIKETHROUGH);
        }
        self
    }

    /// Enables footnote support in Markdown processing.
    pub fn with_footnotes(mut self, enable: bool) -> Self {
        if enable {
            self.options.insert(MarkdownOptions::ENABLE_FOOTNOTES);
        } else {
            self.options.remove(MarkdownOptions::ENABLE_FOOTNOTES);
        }
        self
    }

    /// Applies configuration options to the processor.
    pub fn with_config(mut self, config: ProcessorConfig) -> Self {
        self.config = config;
        self
    }

    /// Extracts and validates metadata from Markdown content.
    fn extract_metadata(
        &self,
        content: &str,
    ) -> Result<ContentMetadata> {
        let mut metadata = ContentMetadata::default();
        let mut lines = content.lines();

        // Handle YAML frontmatter
        if content.starts_with("---\n") {
            let mut frontmatter = String::with_capacity(1024);
            let _ = lines.next(); // Skip first "---"

            for line in lines.by_ref() {
                if line == "---" {
                    break;
                }
                frontmatter.push_str(line);
                frontmatter.push('\n');
            }

            if let Ok(yaml) =
                from_str::<HashMap<String, JsonValue>>(&frontmatter)
            {
                Self::process_metadata(&mut metadata, yaml)?;
            }
        }

        // Extract title from first H1 if not found in frontmatter
        if metadata.title.is_none() {
            for line in content.lines() {
                if let Some(title) = line.strip_prefix("# ") {
                    metadata.title = Some(title.trim().to_string());
                    break;
                }
            }
        }

        Ok(metadata)
    }

    /// Processes and validates metadata from YAML frontmatter.
    fn process_metadata(
        metadata: &mut ContentMetadata,
        yaml: HashMap<String, JsonValue>,
    ) -> Result<()> {
        for (key, value) in yaml {
            match key.as_str() {
                "title" => {
                    metadata.title = value
                        .as_str()
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty());
                }
                "description" => {
                    metadata.description = value
                        .as_str()
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty());
                }
                "date" => {
                    metadata.date = value
                        .as_str()
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty());
                }
                "tags" => {
                    if let Some(tags) = value.as_array() {
                        metadata.tags = tags
                            .iter()
                            .filter_map(|v| {
                                v.as_str()
                                    .map(|s| s.trim().to_string())
                                    .filter(|s| !s.is_empty())
                            })
                            .collect();
                    }
                }
                _ => {
                    let _ = metadata.custom.insert(key, value);
                }
            }
        }

        Ok(())
    }

    /// Generates an accessible Table of Contents.
    fn generate_toc(&self, content: &str) -> Result<String> {
        let mut toc = String::from(
        "<nav class=\"toc\" aria-label=\"Table of Contents\">\n<ul>\n",
    );
        let mut entries = Vec::new();
        let parser = Parser::new_ext(content, self.options);
        let mut current_text = String::new();
        let mut current_level = None;

        for event in parser {
            match event {
                Event::Start(Tag::Heading { level, .. }) => {
                    current_text.clear();
                    current_level = Some(level);
                }
                Event::Text(text) => {
                    current_text.push_str(&text);
                }
                Event::End(_) => {
                    if let Some(level) = current_level.take() {
                        let level_num = match level {
                            HeadingLevel::H1 => 1,
                            HeadingLevel::H2 => 2,
                            HeadingLevel::H3 => 3,
                            HeadingLevel::H4 => 4,
                            HeadingLevel::H5 => 5,
                            HeadingLevel::H6 => 6,
                        };

                        if level_num <= self.config.toc_max_level {
                            let id =
                                self.generate_heading_id(&current_text);
                            entries.push(TocEntry {
                                text: current_text.clone(),
                                level: level_num,
                                id,
                            });
                        }
                    }
                }
                _ => {}
            }
        }

        self.build_toc_html(&mut toc, &entries)?;
        toc.push_str("</ul>\n</nav>");
        Ok(toc)
    }

    /// Generates a unique ID for a heading.
    fn generate_heading_id(&self, text: &str) -> String {
        text.to_lowercase()
            .chars()
            .filter_map(|c| match c {
                'a'..='z' | '0'..='9' => Some(c),
                ' ' | '-' | '_' => Some('-'),
                _ => None,
            })
            .collect()
    }

    /// Builds the HTML structure for the Table of Contents.
    fn build_toc_html(
        &self,
        toc: &mut String,
        entries: &[TocEntry],
    ) -> Result<()> {
        let mut current_level = 1;

        for entry in entries {
            while entry.level > current_level {
                toc.push_str("<ul>\n");
                current_level += 1;
            }
            while entry.level < current_level {
                toc.push_str("</ul>\n");
                current_level -= 1;
            }

            toc.push_str(&format!(
                "<li><a href=\"#{}\" aria-label=\"{}\">{}</a></li>\n",
                entry.id, entry.text, entry.text
            ));
        }

        while current_level > 1 {
            toc.push_str("</ul>\n");
            current_level -= 1;
        }

        Ok(())
    }

    /// Sanitizes HTML content to prevent XSS and other injection attacks.
    fn sanitize_html(&self, html: &str) -> Result<String> {
        let mut output = String::with_capacity(html.len());
        let mut in_tag = false;
        let mut current_tag = String::new();

        for c in html.chars() {
            match c {
                '<' => {
                    in_tag = true;
                    current_tag.clear();
                }
                '>' if in_tag => {
                    in_tag = false;
                    let tag_name = current_tag
                        .split_whitespace()
                        .next()
                        .unwrap_or("")
                        .trim_start_matches('/')
                        .to_lowercase();

                    if self.allowed_tags.contains(&tag_name) {
                        output.push('<');
                        output.push_str(&current_tag);
                        output.push('>');
                    }
                }
                _ if in_tag => {
                    current_tag.push(c);
                }
                _ => {
                    if !in_tag {
                        output.push(c);
                    }
                }
            }
        }

        Ok(output)
    }

    /// Validates that the content is safe to process.
    fn validate(&self, content: &str) -> Result<()> {
        // Check content size
        if content.len() > MAX_CONTENT_SIZE {
            return Err(ProcessingError::ContentProcessing {
                details: format!(
                    "Content exceeds maximum size of {} bytes",
                    MAX_CONTENT_SIZE
                ),
                source: None,
            });
        }

        // Check for empty content
        if content.trim().is_empty() {
            return Err(ProcessingError::ContentProcessing {
                details: "Content cannot be empty".to_string(),
                source: None,
            });
        }

        // Check for suspicious patterns
        let suspicious_patterns = [
            "javascript:",
            "data:",
            "vbscript:",
            "onclick",
            "onerror",
            "onload",
            "eval(",
        ];

        for pattern in &suspicious_patterns {
            if content.to_lowercase().contains(pattern) {
                return Err(ProcessingError::ContentProcessing {
                    details: format!(
                        "Suspicious content pattern detected: {}",
                        pattern
                    ),
                    source: None,
                });
            }
        }

        Ok(())
    }
}

impl Default for MarkdownProcessor {
    fn default() -> Self {
        Self::new()
    }
}

impl Processor for MarkdownProcessor {
    type Input = String;
    type Output = String;
    type Context = JsonValue;

    fn process(
        &self,
        content: String,
        context: Option<&Self::Context>,
    ) -> Result<Self::Output> {
        // Validate content
        self.validate(&content)?;

        // Extract metadata
        let metadata = self.extract_metadata(&content)?;

        // Parse configuration from context
        let config: ProcessorConfig = context
            .and_then(|ctx| serde_json::from_value(ctx.clone()).ok())
            .unwrap_or_default();

        // Parse Markdown to HTML
        let parser = Parser::new_ext(&content, self.options);
        let mut html_output = String::with_capacity(content.len() * 2);
        html::push_html(&mut html_output, parser);

        // Generate and prepend TOC if enabled
        if config.toc {
            let toc = self.generate_toc(&content)?;
            println!("Generated ToC: {}", toc); // Debugging line
            html_output = format!("{}\n{}", toc, html_output);
        }

        // Sanitize if enabled
        let processed = if config.sanitize {
            self.sanitize_html(&html_output)?
        } else {
            html_output
        };

        // Add metadata as JSON-LD if present
        if !metadata.custom.is_empty() {
            let json_ld = serde_json::to_string(&metadata.custom)
                .map_err(|e| ProcessingError::ContentProcessing {
                    details: "Failed to serialize metadata".to_string(),
                    source: Some(Box::new(e)),
                })?;
            Ok(format!(
                "{}\n<script type=\"application/ld+json\">{}</script>",
                processed, json_ld
            ))
        } else {
            Ok(processed)
        }
    }
}

// Helper functions for default values
fn default_true() -> bool {
    true
}

fn default_toc_level() -> u8 {
    3
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_markdown_processor_basic() {
        let processor = MarkdownProcessor::new();
        let input = "# Test\n\nThis is a **test**.";
        let result = processor.process(input.to_owned(), None).unwrap();
        assert!(result.contains("<h1>"));
        assert!(result.contains("<strong>"));
    }

    #[test]
    fn test_markdown_processor_with_options() {
        let processor = MarkdownProcessor::new()
            .with_tables(true)
            .with_strikethrough(true);

        let input =
            "# Test\n\n| A | B |\n|---|---|\n| 1 | 2 |\n\n~~strike~~";
        let result = processor.process(input.to_owned(), None).unwrap();
        assert!(result.contains("<table>"));
        assert!(result.contains("<del>"));
    }

    #[test]
    fn test_metadata_extraction() {
        let processor = MarkdownProcessor::new();
        let input = r#"---
title: Test Post
description: A test post
date: 2024-01-01
tags:
  - test
  - example
custom_field: value
---

# Content"#;

        let metadata = processor.extract_metadata(input).unwrap();
        assert_eq!(metadata.title, Some("Test Post".to_string()));
        assert_eq!(
            metadata.description,
            Some("A test post".to_string())
        );
        assert_eq!(metadata.date, Some("2024-01-01".to_string()));
        assert_eq!(metadata.tags, vec!["test", "example"]);
        assert!(metadata.custom.contains_key("custom_field"));
    }

    #[test]
    fn test_toc_generation() {
        let processor = MarkdownProcessor::new();
        let input = "# H1\n\n## H2\n\n### H3";
        let context = json!({
            "toc": true
        });

        let result = processor
            .process(input.to_owned(), Some(&context))
            .unwrap();
        println!("Result: {}", result); // Debugging line

        assert!(result.contains(r#"<nav class="toc""#));
        assert!(result.contains("<ul>"));
    }

    #[test]
    fn test_sanitization() {
        let processor = MarkdownProcessor::new();
        let input = "# Test\n\n<script>alert('xss')</script>";
        let context = json!({
            "sanitize": true
        });

        let result = processor
            .process(input.to_owned(), Some(&context))
            .unwrap();
        assert!(!result.contains("<script>"));
    }

    #[test]
    fn test_validation() {
        let processor = MarkdownProcessor::new();

        // Test empty content
        assert!(processor.validate("").is_err());

        // Test content size
        let large_content = "a".repeat(MAX_CONTENT_SIZE + 1);
        assert!(processor.validate(&large_content).is_err());

        // Test suspicious patterns
        assert!(processor.validate("javascript:alert(1)").is_err());
        assert!(processor.validate("onclick='alert(1)'").is_err());

        // Test valid content
        assert!(processor.validate("# Valid content").is_ok());
    }

    #[test]
    fn test_heading_id_generation() {
        let processor = MarkdownProcessor::new();
        let id = processor.generate_heading_id("Hello World! 123");
        assert_eq!(id, "hello-world-123");
    }

    #[test]
    fn test_custom_metadata() {
        let processor = MarkdownProcessor::new();
        let input = r#"---
title: Test
custom:
  key1: value1
  key2: 42
---
# Content"#;

        let metadata = processor.extract_metadata(input).unwrap();
        assert!(metadata.custom.contains_key("custom"));
    }

    #[test]
    fn test_sanitization_with_allowed_tags() {
        let processor = MarkdownProcessor::new();
        let input = r#"
<p>Valid paragraph</p>
<script>alert('bad')</script>
<img src="valid.jpg" alt="valid">
<iframe src="bad.html"></iframe>
"#;
        let result = processor.sanitize_html(input).unwrap();

        assert!(result.contains("<p>"));
        assert!(result.contains("<img"));
        assert!(!result.contains("<script>"));
        assert!(!result.contains("<iframe>"));
    }
}