@@ -420,27 +420,26 @@ impl<'s> Parser<'s> {
420
420
}
421
421
422
422
fn parse_impl ( & mut self ) -> anyhow:: Result < ( ) > {
423
- const SECTION_CONFIG_SNAPSHOT : & str = "<!-- snapshot-diagnostics --> " ;
423
+ const SECTION_CONFIG_SNAPSHOT : & str = "snapshot-diagnostics" ;
424
424
const CODE_BLOCK_END : & [ u8 ] = b"```" ;
425
+ const HTML_COMMENT_END : & [ u8 ] = b"-->" ;
425
426
426
427
while let Some ( first) = self . cursor . bump ( ) {
427
428
match first {
428
- '<' => {
429
- self . explicit_path = None ;
430
- self . preceding_blank_lines = 0 ;
431
- // If we want to support more comment directives, then we should
432
- // probably just parse the directive generically first. But it's
433
- // not clear if we'll want to add more, since comments are hidden
434
- // from GitHub Markdown rendering.
435
- if self
436
- . cursor
437
- . as_str ( )
438
- . starts_with ( & SECTION_CONFIG_SNAPSHOT [ 1 ..] )
429
+ '<' if self . cursor . eat_char3 ( '!' , '-' , '-' ) => {
430
+ if let Some ( position) =
431
+ memchr:: memmem:: find ( self . cursor . as_bytes ( ) , HTML_COMMENT_END )
439
432
{
440
- self . cursor . skip_bytes ( SECTION_CONFIG_SNAPSHOT . len ( ) - 1 ) ;
441
- self . process_snapshot_diagnostics ( ) ?;
433
+ let html_comment = self . cursor . as_str ( ) [ ..position] . trim ( ) ;
434
+ if html_comment == SECTION_CONFIG_SNAPSHOT {
435
+ self . process_snapshot_diagnostics ( ) ?;
436
+ }
437
+ self . cursor . skip_bytes ( position + HTML_COMMENT_END . len ( ) ) ;
438
+ } else {
439
+ bail ! ( "Unterminated HTML comment." ) ;
442
440
}
443
441
}
442
+
444
443
'#' => {
445
444
self . explicit_path = None ;
446
445
self . preceding_blank_lines = 0 ;
@@ -1729,6 +1728,31 @@ mod tests {
1729
1728
) ;
1730
1729
}
1731
1730
1731
+ #[ test]
1732
+ fn snapshot_diagnostic_directive_detection_ignores_whitespace ( ) {
1733
+ // A bit weird, but the fact that the parser rejects this indicates that
1734
+ // we have correctly recognised both of these HTML comments as a directive to have
1735
+ // snapshotting enabled for the file, which is what we want (even though they both
1736
+ // contain different amounts of whitespace to the "standard" snapshot directive).
1737
+ let source = dedent (
1738
+ "
1739
+ # Some header
1740
+
1741
+ <!-- snapshot-diagnostics -->
1742
+ <!--snapshot-diagnostics-->
1743
+
1744
+ ```py
1745
+ x = 1
1746
+ ```
1747
+ " ,
1748
+ ) ;
1749
+ let err = super :: parse ( "file.md" , & source) . expect_err ( "Should fail to parse" ) ;
1750
+ assert_eq ! (
1751
+ err. to_string( ) ,
1752
+ "Section config to enable snapshotting diagnostics should appear at most once." ,
1753
+ ) ;
1754
+ }
1755
+
1732
1756
#[ test]
1733
1757
fn section_directive_must_appear_before_config ( ) {
1734
1758
let source = dedent (
0 commit comments