|
3 | 3 | * Copyright (c) 2025 Posit, PBC |
4 | 4 | */ |
5 | 5 |
|
| 6 | +use hashlink::LinkedHashMap; |
6 | 7 | use quarto_markdown_pandoc::pandoc::location::{Location, Range, SourceInfo}; |
7 | | -use quarto_markdown_pandoc::pandoc::{MetaValue, RawBlock, rawblock_to_meta}; |
| 8 | +use quarto_markdown_pandoc::pandoc::{ |
| 9 | + Inline, MetaValue, RawBlock, parse_metadata_strings, rawblock_to_meta, |
| 10 | +}; |
8 | 11 | use std::fs; |
9 | 12 |
|
10 | 13 | #[test] |
@@ -54,3 +57,183 @@ fn test_metadata_parsing() { |
54 | 57 | Some(MetaValue::MetaList(_)) |
55 | 58 | )); |
56 | 59 | } |
| 60 | + |
| 61 | +#[test] |
| 62 | +fn test_yaml_tagged_strings() { |
| 63 | + // Test that YAML tags (!path, !glob, !str) prevent markdown parsing |
| 64 | + let content = fs::read_to_string("tests/yaml-tagged-strings.qmd").unwrap(); |
| 65 | + |
| 66 | + let block = RawBlock { |
| 67 | + format: "quarto_minus_metadata".to_string(), |
| 68 | + text: content, |
| 69 | + source_info: SourceInfo::with_range(Range { |
| 70 | + start: Location { |
| 71 | + offset: 0, |
| 72 | + row: 0, |
| 73 | + column: 0, |
| 74 | + }, |
| 75 | + end: Location { |
| 76 | + offset: 0, |
| 77 | + row: 0, |
| 78 | + column: 0, |
| 79 | + }, |
| 80 | + }), |
| 81 | + }; |
| 82 | + |
| 83 | + let mut meta = rawblock_to_meta(block); |
| 84 | + let mut outer_meta = LinkedHashMap::new(); |
| 85 | + |
| 86 | + // Parse metadata strings |
| 87 | + for (k, v) in meta.drain() { |
| 88 | + let parsed = parse_metadata_strings(v, &mut outer_meta); |
| 89 | + outer_meta.insert(k, parsed); |
| 90 | + } |
| 91 | + |
| 92 | + // Check plain_path - should be MetaInlines with Span wrapper |
| 93 | + let plain_path = outer_meta.get("plain_path").expect("plain_path not found"); |
| 94 | + if let MetaValue::MetaInlines(inlines) = plain_path { |
| 95 | + assert_eq!(inlines.len(), 1, "Expected exactly one inline"); |
| 96 | + if let Inline::Span(span) = &inlines[0] { |
| 97 | + assert!(span.attr.1.contains(&"yaml-tagged-string".to_string())); |
| 98 | + assert_eq!(span.attr.2.get("tag"), Some(&"path".to_string())); |
| 99 | + // Extract the string content |
| 100 | + if let Inline::Str(s) = &span.content[0] { |
| 101 | + assert_eq!(s.text, "images/neovim-*.png"); |
| 102 | + } else { |
| 103 | + panic!("Expected Str inline inside Span"); |
| 104 | + } |
| 105 | + } else { |
| 106 | + panic!("Expected Span inline, got: {:?}", inlines[0]); |
| 107 | + } |
| 108 | + } else { |
| 109 | + panic!("Expected MetaInlines for plain_path"); |
| 110 | + } |
| 111 | + |
| 112 | + // Check glob_pattern |
| 113 | + let glob_pattern = outer_meta |
| 114 | + .get("glob_pattern") |
| 115 | + .expect("glob_pattern not found"); |
| 116 | + if let MetaValue::MetaInlines(inlines) = glob_pattern { |
| 117 | + if let Inline::Span(span) = &inlines[0] { |
| 118 | + assert_eq!(span.attr.2.get("tag"), Some(&"glob".to_string())); |
| 119 | + if let Inline::Str(s) = &span.content[0] { |
| 120 | + assert_eq!(s.text, "posts/*/index.qmd"); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // Check literal_string |
| 126 | + let literal_string = outer_meta |
| 127 | + .get("literal_string") |
| 128 | + .expect("literal_string not found"); |
| 129 | + if let MetaValue::MetaInlines(inlines) = literal_string { |
| 130 | + if let Inline::Span(span) = &inlines[0] { |
| 131 | + assert_eq!(span.attr.2.get("tag"), Some(&"str".to_string())); |
| 132 | + if let Inline::Str(s) = &span.content[0] { |
| 133 | + assert_eq!(s.text, "_foo_.py"); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Check regular_markdown - should have parsed markdown (Emph element) |
| 139 | + let regular_markdown = outer_meta |
| 140 | + .get("regular_markdown") |
| 141 | + .expect("regular_markdown not found"); |
| 142 | + if let MetaValue::MetaInlines(inlines) = regular_markdown { |
| 143 | + // Should contain Emph for *emphasis* |
| 144 | + let has_emph = inlines |
| 145 | + .iter() |
| 146 | + .any(|inline| matches!(inline, Inline::Emph(_))); |
| 147 | + assert!( |
| 148 | + has_emph, |
| 149 | + "regular_markdown should have Emph element from *emphasis*" |
| 150 | + ); |
| 151 | + } else { |
| 152 | + panic!("Expected MetaInlines for regular_markdown"); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +#[test] |
| 157 | +fn test_yaml_markdown_parse_failure() { |
| 158 | + // Test that untagged strings that fail markdown parsing are gracefully handled |
| 159 | + let content = fs::read_to_string("tests/yaml-markdown-parse-failure.qmd").unwrap(); |
| 160 | + |
| 161 | + let block = RawBlock { |
| 162 | + format: "quarto_minus_metadata".to_string(), |
| 163 | + text: content, |
| 164 | + source_info: SourceInfo::with_range(Range { |
| 165 | + start: Location { |
| 166 | + offset: 0, |
| 167 | + row: 0, |
| 168 | + column: 0, |
| 169 | + }, |
| 170 | + end: Location { |
| 171 | + offset: 0, |
| 172 | + row: 0, |
| 173 | + column: 0, |
| 174 | + }, |
| 175 | + }), |
| 176 | + }; |
| 177 | + |
| 178 | + let mut meta = rawblock_to_meta(block); |
| 179 | + let mut outer_meta = LinkedHashMap::new(); |
| 180 | + |
| 181 | + // Parse metadata strings - this should not panic |
| 182 | + for (k, v) in meta.drain() { |
| 183 | + let parsed = parse_metadata_strings(v, &mut outer_meta); |
| 184 | + outer_meta.insert(k, parsed); |
| 185 | + } |
| 186 | + |
| 187 | + // Check untagged_path - should be wrapped in error span |
| 188 | + let untagged_path = outer_meta |
| 189 | + .get("untagged_path") |
| 190 | + .expect("untagged_path not found"); |
| 191 | + if let MetaValue::MetaInlines(inlines) = untagged_path { |
| 192 | + if let Inline::Span(span) = &inlines[0] { |
| 193 | + assert!( |
| 194 | + span.attr |
| 195 | + .1 |
| 196 | + .contains(&"yaml-markdown-syntax-error".to_string()) |
| 197 | + ); |
| 198 | + if let Inline::Str(s) = &span.content[0] { |
| 199 | + assert_eq!(s.text, "posts/*/index.qmd"); |
| 200 | + } |
| 201 | + } else { |
| 202 | + panic!("Expected Span inline for failed parse"); |
| 203 | + } |
| 204 | + } else { |
| 205 | + panic!("Expected MetaInlines for untagged_path"); |
| 206 | + } |
| 207 | + |
| 208 | + // Check another_glob - should also be wrapped in error span |
| 209 | + let another_glob = outer_meta |
| 210 | + .get("another_glob") |
| 211 | + .expect("another_glob not found"); |
| 212 | + if let MetaValue::MetaInlines(inlines) = another_glob { |
| 213 | + if let Inline::Span(span) = &inlines[0] { |
| 214 | + assert!( |
| 215 | + span.attr |
| 216 | + .1 |
| 217 | + .contains(&"yaml-markdown-syntax-error".to_string()) |
| 218 | + ); |
| 219 | + if let Inline::Str(s) = &span.content[0] { |
| 220 | + assert_eq!(s.text, "images/*.png"); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + // Check underscore_file - this one should successfully parse as markdown with Emph |
| 226 | + let underscore_file = outer_meta |
| 227 | + .get("underscore_file") |
| 228 | + .expect("underscore_file not found"); |
| 229 | + if let MetaValue::MetaInlines(inlines) = underscore_file { |
| 230 | + // _foo_ should become Emph element |
| 231 | + let has_emph = inlines |
| 232 | + .iter() |
| 233 | + .any(|inline| matches!(inline, Inline::Emph(_))); |
| 234 | + assert!( |
| 235 | + has_emph, |
| 236 | + "underscore_file should have Emph element from _foo_" |
| 237 | + ); |
| 238 | + } |
| 239 | +} |
0 commit comments