auditmysite 1.7.3

Resource-efficient WCAG 2.2 AA Accessibility Checker in Rust
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Lightweight Knowledge Graph Builder
//!
//! Builds a knowledge graph from structured data (Schema.org) and internal
//! link structure. Detects entity relationships, suggests internal linking
//! opportunities, and exports as JSON-LD.

use serde::{Deserialize, Serialize};

use super::{
    build_dimension, AiSignal, AiSignalKind, AiSignalValues, DimensionKind, DimensionScore,
};

/// Knowledge graph analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeGraphAnalysis {
    /// Dimension score (0–100) with individual signals
    pub dimension: DimensionScore,
    /// Discovered entities
    pub entities: Vec<GraphEntity>,
    /// Detected relationships between entities
    pub relationships: Vec<EntityRelationship>,
    /// Internal linking suggestions based on entity co-occurrence
    pub link_suggestions: Vec<LinkSuggestion>,
}

/// An entity extracted from structured data or page structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphEntity {
    /// Entity name
    pub name: String,
    /// Entity type (Schema.org type or inferred)
    pub entity_type: String,
    /// Source of extraction
    pub source: EntitySource,
    /// Additional properties extracted
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub properties: Vec<(String, String)>,
}

/// How an entity was discovered
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EntitySource {
    /// From JSON-LD / Schema.org markup
    SchemaOrg,
    /// From heading structure
    Heading,
    /// From meta tags
    Meta,
}

impl std::fmt::Display for EntitySource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EntitySource::SchemaOrg => write!(f, "Schema.org"),
            EntitySource::Heading => write!(f, "Heading"),
            EntitySource::Meta => write!(f, "Meta"),
        }
    }
}

/// A relationship between two entities
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityRelationship {
    /// Subject entity name
    pub subject: String,
    /// Relationship type
    pub predicate: String,
    /// Object entity name
    pub object: String,
    /// Source of the relationship
    pub source: EntitySource,
}

/// Which kind of link suggestion reason applies (for localized re-derivation).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum KgSuggestionKind {
    /// Too few internal links between entities (uses `internal_links`)
    FewInternalLinks,
    /// A topic only recognized as a heading (uses the entity name)
    TopicOnlyHeading,
}

/// Suggestion for internal linking based on entity analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinkSuggestion {
    /// Which reason this is (for localized re-derivation)
    pub kind: KgSuggestionKind,
    /// Internal link count referenced by `reason` (FewInternalLinks)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub internal_links: Option<u32>,
    /// Entity that should be linked (canonical English for synthetic labels)
    pub entity: String,
    /// Reason for the suggestion (canonical English)
    pub reason: String,
}

/// Input data for knowledge graph building
pub(crate) struct KnowledgeGraphInput {
    /// JSON-LD schemas found on the page
    pub schemas: Vec<SchemaEntity>,
    /// Page title from meta
    pub page_title: Option<String>,
    /// Site name from meta/schema
    pub site_name: Option<String>,
    /// Internal link targets found on the page
    pub internal_links: u32,
    /// Whether breadcrumb schema exists
    pub has_breadcrumb: bool,
}

/// A schema entity extracted from JSON-LD
pub(crate) struct SchemaEntity {
    pub schema_type: String,
    pub name: Option<String>,
    pub properties: Vec<(String, String)>,
}

pub(crate) fn analyze_knowledge_graph(input: &KnowledgeGraphInput) -> KnowledgeGraphAnalysis {
    let en = true; // bake canonical English; PDF re-derives via stored kinds
    let mut entities = Vec::new();
    let mut relationships = Vec::new();
    let mut link_suggestions = Vec::new();

    // Extract entities from Schema.org
    for schema in &input.schemas {
        if let Some(name) = &schema.name {
            entities.push(GraphEntity {
                name: name.clone(),
                entity_type: schema.schema_type.clone(),
                source: EntitySource::SchemaOrg,
                properties: schema.properties.clone(),
            });
        }
    }

    // Extract entities from page structure
    if let Some(title) = &input.page_title {
        if !title.is_empty() && !entities.iter().any(|e| e.name == *title) {
            entities.push(GraphEntity {
                name: title.clone(),
                entity_type: "WebPage".into(),
                source: EntitySource::Meta,
                properties: vec![],
            });
        }
    }

    if let Some(site) = &input.site_name {
        if !site.is_empty() && !entities.iter().any(|e| e.name == *site) {
            entities.push(GraphEntity {
                name: site.clone(),
                entity_type: "WebSite".into(),
                source: EntitySource::Meta,
                properties: vec![],
            });
        }
    }

    // Build relationships from Schema.org data
    build_schema_relationships(&entities, &input.schemas, &mut relationships);

    // Build relationships from page/site structure
    if let (Some(title), Some(site)) = (&input.page_title, &input.site_name) {
        if !title.is_empty() && !site.is_empty() {
            relationships.push(EntityRelationship {
                subject: title.clone(),
                predicate: "isPartOf".into(),
                object: site.clone(),
                source: EntitySource::Meta,
            });
        }
    }

    // Breadcrumb relationships
    if input.has_breadcrumb {
        if let Some(title) = &input.page_title {
            relationships.push(EntityRelationship {
                subject: title.clone(),
                predicate: "breadcrumbPath".into(),
                object: "BreadcrumbList".into(),
                source: EntitySource::SchemaOrg,
            });
        }
    }

    // Generate link suggestions for entities without rich schema
    if entities.len() > 1 && input.internal_links < 5 {
        link_suggestions.push(LinkSuggestion {
            kind: KgSuggestionKind::FewInternalLinks,
            internal_links: Some(input.internal_links),
            entity: ai_kg_suggestion_entity(KgSuggestionKind::FewInternalLinks, "", en),
            reason: ai_kg_link_suggestion_reason(
                KgSuggestionKind::FewInternalLinks,
                "",
                input.internal_links,
                en,
            ),
        });
    }

    for entity in &entities {
        if entity.source == EntitySource::Heading && entity.entity_type == "Topic" {
            link_suggestions.push(LinkSuggestion {
                kind: KgSuggestionKind::TopicOnlyHeading,
                internal_links: None,
                entity: entity.name.clone(),
                reason: ai_kg_link_suggestion_reason(
                    KgSuggestionKind::TopicOnlyHeading,
                    &entity.name,
                    0,
                    en,
                ),
            });
        }
    }

    // Score signals
    let mut signals = Vec::new();

    // 1. Entity count
    let good_entity_count = entities.len() >= 3;
    signals.push(AiSignal::new(
        AiSignalKind::EntitiesDetected,
        good_entity_count,
        0.20,
        AiSignalValues {
            entity_count: Some(entities.len() as u32),
            ..Default::default()
        },
    ));

    // 2. Schema.org entity ratio
    let schema_entities = entities
        .iter()
        .filter(|e| e.source == EntitySource::SchemaOrg)
        .count();
    let good_schema_ratio = schema_entities >= 2;
    signals.push(AiSignal::new(
        AiSignalKind::SchemaOrgEntities,
        good_schema_ratio,
        0.20,
        AiSignalValues {
            schema_entity_count: Some(schema_entities as u32),
            ..Default::default()
        },
    ));

    // 3. Relationship count
    let good_relationships = relationships.len() >= 2;
    signals.push(AiSignal::new(
        AiSignalKind::Relationships,
        good_relationships,
        0.20,
        AiSignalValues {
            relationship_count: Some(relationships.len() as u32),
            ..Default::default()
        },
    ));

    // 4. Entity types diversity
    let unique_types: std::collections::HashSet<&str> =
        entities.iter().map(|e| e.entity_type.as_str()).collect();
    let diverse = unique_types.len() >= 3;
    signals.push(AiSignal::new(
        AiSignalKind::TypeDiversity,
        diverse,
        0.10,
        AiSignalValues {
            unique_type_count: Some(unique_types.len() as u32),
            ..Default::default()
        },
    ));

    // 5. Breadcrumb hierarchy
    signals.push(AiSignal::new(
        AiSignalKind::BreadcrumbHierarchy,
        input.has_breadcrumb,
        0.10,
        AiSignalValues::default(),
    ));

    // 6. Internal linking density (for graph connectivity)
    let good_linking = input.internal_links >= 5;
    signals.push(AiSignal::new(
        AiSignalKind::LinkingDensity,
        good_linking,
        0.10,
        AiSignalValues {
            internal_links: Some(input.internal_links),
            ..Default::default()
        },
    ));

    // 7. Properties completeness
    let entities_with_props = entities.iter().filter(|e| !e.properties.is_empty()).count();
    let good_props = entities_with_props >= 1 && schema_entities >= 1;
    signals.push(AiSignal::new(
        AiSignalKind::PropertyCompleteness,
        good_props,
        0.10,
        AiSignalValues {
            entities_with_props: Some(entities_with_props as u32),
            entity_count: Some(entities.len() as u32),
            ..Default::default()
        },
    ));

    KnowledgeGraphAnalysis {
        dimension: build_dimension(DimensionKind::KnowledgeGraph, &signals),
        entities,
        relationships,
        link_suggestions,
    }
}

// ─── Link suggestion text (single source of truth) ───────────────────────────

/// Localized entity label for a link suggestion. Real topic names pass through;
/// the synthetic FewInternalLinks "Page" label localizes.
pub(crate) fn ai_kg_suggestion_entity(kind: KgSuggestionKind, name: &str, en: bool) -> String {
    match kind {
        KgSuggestionKind::FewInternalLinks => {
            if en {
                "Page".into()
            } else {
                "Seite".into()
            }
        }
        KgSuggestionKind::TopicOnlyHeading => name.to_string(),
    }
}

/// Localized reason text for a link suggestion (single source of truth).
pub fn ai_kg_link_suggestion_reason(
    kind: KgSuggestionKind,
    entity_name: &str,
    internal_links: u32,
    en: bool,
) -> String {
    match kind {
        KgSuggestionKind::FewInternalLinks => {
            if en {
                format!(
                    "Only {} internal links — entities should be linked to each other",
                    internal_links
                )
            } else {
                format!(
                    "Nur {} interne Links — Entitäten sollten untereinander verlinkt werden",
                    internal_links
                )
            }
        }
        KgSuggestionKind::TopicOnlyHeading => {
            if en {
                format!(
                    "Topic '{}' only recognized as a heading — Schema.org markup or internal linking recommended",
                    entity_name
                )
            } else {
                format!(
                    "Thema '{}' nur als Überschrift erkannt — Schema.org-Markup oder interne Verlinkung empfohlen",
                    entity_name
                )
            }
        }
    }
}

// ─── Signal detail text (single source of truth) ─────────────────────────────

pub(crate) fn detail_entities_detected(present: bool, v: &AiSignalValues, en: bool) -> String {
    let count = v.entity_count.unwrap_or(0);
    if en {
        format!(
            "{} entities extracted — {}",
            count,
            if present {
                "rich knowledge model"
            } else {
                "few machine-readable entities"
            }
        )
    } else {
        format!(
            "{} Entitäten extrahiert — {}",
            count,
            if present {
                "reichhaltiges Wissensmodell"
            } else {
                "wenig maschinenlesbare Entitäten"
            }
        )
    }
}

pub(crate) fn detail_schema_entities(present: bool, v: &AiSignalValues, en: bool) -> String {
    let count = v.schema_entity_count.unwrap_or(0);
    if en {
        format!(
            "{} entities from Schema.org — {}",
            count,
            if present {
                "good machine readability"
            } else {
                "few structured entities"
            }
        )
    } else {
        format!(
            "{} Entitäten aus Schema.org — {}",
            count,
            if present {
                "gute Maschinenlesbarkeit"
            } else {
                "wenig strukturierte Entitäten"
            }
        )
    }
}

pub(crate) fn detail_relationships(present: bool, v: &AiSignalValues, en: bool) -> String {
    let count = v.relationship_count.unwrap_or(0);
    if en {
        format!(
            "{} relationships between entities — {}",
            count,
            if present {
                "knowledge network recognizable"
            } else {
                "isolated entities, no network"
            }
        )
    } else {
        format!(
            "{} Beziehungen zwischen Entitäten — {}",
            count,
            if present {
                "Wissensnetz erkennbar"
            } else {
                "isolierte Entitäten, kein Netz"
            }
        )
    }
}

pub(crate) fn detail_type_diversity(present: bool, v: &AiSignalValues, en: bool) -> String {
    let count = v.unique_type_count.unwrap_or(0);
    if en {
        format!(
            "{} different entity types — {}",
            count,
            if present {
                "diverse knowledge model"
            } else {
                "little type diversity"
            }
        )
    } else {
        format!(
            "{} verschiedene Entitätstypen — {}",
            count,
            if present {
                "vielfältiges Wissensmodell"
            } else {
                "wenig Typen-Diversität"
            }
        )
    }
}

pub(crate) fn detail_breadcrumb(present: bool, en: bool) -> String {
    if present {
        if en {
            "Breadcrumb present — thematic classification in the graph possible".into()
        } else {
            "Breadcrumb vorhanden — thematische Einordnung im Graph möglich".into()
        }
    } else if en {
        "No breadcrumb — thematic classification missing".into()
    } else {
        "Kein Breadcrumb — thematische Einordnung fehlt".into()
    }
}

pub(crate) fn detail_linking_density(present: bool, v: &AiSignalValues, en: bool) -> String {
    let count = v.internal_links.unwrap_or(0);
    if en {
        format!(
            "{} internal links — {}",
            count,
            if present {
                "good connectivity in the graph"
            } else {
                "weak connectivity"
            }
        )
    } else {
        format!(
            "{} interne Links — {}",
            count,
            if present {
                "gute Vernetzung im Graph"
            } else {
                "schwache Vernetzung"
            }
        )
    }
}

pub(crate) fn detail_property_completeness(present: bool, v: &AiSignalValues, en: bool) -> String {
    let with_props = v.entities_with_props.unwrap_or(0);
    let total = v.entity_count.unwrap_or(0);
    if en {
        format!(
            "{}/{} entities with properties — {}",
            with_props,
            total,
            if present {
                "entities are described"
            } else {
                "entities without details"
            }
        )
    } else {
        format!(
            "{}/{} Entitäten mit Eigenschaften — {}",
            with_props,
            total,
            if present {
                "Entitäten sind beschrieben"
            } else {
                "Entitäten ohne Details"
            }
        )
    }
}

fn build_schema_relationships(
    entities: &[GraphEntity],
    schemas: &[SchemaEntity],
    relationships: &mut Vec<EntityRelationship>,
) {
    // Find author/publisher relationships
    for schema in schemas {
        let subject = match &schema.name {
            Some(n) => n.clone(),
            None => continue,
        };

        for (key, value) in &schema.properties {
            match key.as_str() {
                "author" | "creator" => {
                    relationships.push(EntityRelationship {
                        subject: subject.clone(),
                        predicate: key.clone(),
                        object: value.clone(),
                        source: EntitySource::SchemaOrg,
                    });
                }
                "publisher" | "provider" => {
                    relationships.push(EntityRelationship {
                        subject: subject.clone(),
                        predicate: key.clone(),
                        object: value.clone(),
                        source: EntitySource::SchemaOrg,
                    });
                }
                "isPartOf" | "mainEntityOfPage" | "about" => {
                    relationships.push(EntityRelationship {
                        subject: subject.clone(),
                        predicate: key.clone(),
                        object: value.clone(),
                        source: EntitySource::SchemaOrg,
                    });
                }
                _ => {}
            }
        }
    }

    // Connect entities of type Organization to the site
    let org_entities: Vec<&GraphEntity> = entities
        .iter()
        .filter(|e| e.entity_type == "Organization" || e.entity_type == "LocalBusiness")
        .collect();

    let article_entities: Vec<&GraphEntity> = entities
        .iter()
        .filter(|e| {
            e.entity_type == "Article"
                || e.entity_type == "BlogPosting"
                || e.entity_type == "NewsArticle"
        })
        .collect();

    // Infer publisher relationship if not explicit
    for article in &article_entities {
        let has_publisher = relationships
            .iter()
            .any(|r| r.subject == article.name && r.predicate == "publisher");
        if !has_publisher {
            if let Some(org) = org_entities.first() {
                relationships.push(EntityRelationship {
                    subject: article.name.clone(),
                    predicate: "publisher (inferred)".into(),
                    object: org.name.clone(),
                    source: EntitySource::SchemaOrg,
                });
            }
        }
    }
}

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

    fn rich_input() -> KnowledgeGraphInput {
        KnowledgeGraphInput {
            schemas: vec![
                SchemaEntity {
                    schema_type: "Article".into(),
                    name: Some("Understanding Rust".into()),
                    properties: vec![
                        ("author".into(), "Jane Doe".into()),
                        ("publisher".into(), "TechBlog".into()),
                    ],
                },
                SchemaEntity {
                    schema_type: "Organization".into(),
                    name: Some("TechBlog".into()),
                    properties: vec![("url".into(), "https://techblog.example".into())],
                },
                SchemaEntity {
                    schema_type: "Person".into(),
                    name: Some("Jane Doe".into()),
                    properties: vec![],
                },
            ],
            page_title: Some("Understanding Rust".into()),
            site_name: Some("TechBlog".into()),
            internal_links: 10,
            has_breadcrumb: true,
        }
    }

    fn minimal_input() -> KnowledgeGraphInput {
        KnowledgeGraphInput {
            schemas: vec![],
            page_title: None,
            site_name: None,
            internal_links: 0,
            has_breadcrumb: false,
        }
    }

    #[test]
    fn rich_input_produces_high_score() {
        let result = analyze_knowledge_graph(&rich_input());
        // Should score well with 3 schemas + breadcrumb + many links
        assert!(result.dimension.score >= 60);
        assert!(!result.entities.is_empty());
        assert!(!result.relationships.is_empty());
    }

    #[test]
    fn minimal_input_produces_low_score() {
        let result = analyze_knowledge_graph(&minimal_input());
        assert_eq!(result.dimension.score, 0);
        assert!(result.entities.is_empty());
        assert!(result.relationships.is_empty());
        assert!(result.link_suggestions.is_empty());
    }

    #[test]
    fn page_and_site_titles_become_entities() {
        let input = KnowledgeGraphInput {
            schemas: vec![],
            page_title: Some("My Page".into()),
            site_name: Some("My Site".into()),
            internal_links: 0,
            has_breadcrumb: false,
        };
        let result = analyze_knowledge_graph(&input);
        let names: Vec<&str> = result.entities.iter().map(|e| e.name.as_str()).collect();
        assert!(names.contains(&"My Page"));
        assert!(names.contains(&"My Site"));
        // A page+site relationship should be inferred
        assert!(result
            .relationships
            .iter()
            .any(|r| r.predicate == "isPartOf"));
    }

    #[test]
    fn low_internal_links_triggers_suggestion() {
        // 2 entities + < 5 links → link suggestion
        let input = KnowledgeGraphInput {
            schemas: vec![SchemaEntity {
                schema_type: "Article".into(),
                name: Some("Article One".into()),
                properties: vec![],
            }],
            page_title: Some("Article One".into()),
            site_name: Some("Blog".into()),
            internal_links: 2,
            has_breadcrumb: false,
        };
        let result = analyze_knowledge_graph(&input);
        assert!(!result.link_suggestions.is_empty());
    }

    #[test]
    fn breadcrumb_adds_relationship() {
        let input = KnowledgeGraphInput {
            schemas: vec![],
            page_title: Some("Leaf Page".into()),
            site_name: None,
            internal_links: 5,
            has_breadcrumb: true,
        };
        let result = analyze_knowledge_graph(&input);
        assert!(result
            .relationships
            .iter()
            .any(|r| r.predicate == "breadcrumbPath"));
    }
}