Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 0 additions & 201 deletions crates/qmd-syntax-helper/src/main_old.rs

This file was deleted.

13 changes: 11 additions & 2 deletions crates/quarto-markdown-pandoc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,17 @@ fn main() {
}

if !input.ends_with("\n") {
eprintln!("(Warning) Adding missing newline to end of input.");
//
if args.json_errors {
// Output as JSON to stderr
let warning_json = serde_json::json!({
"title": "Warning",
"message": "Adding missing newline to end of input"
});
eprintln!("{}", warning_json);
} else {
// Output as plain text to stderr
eprintln!("(Warning) Adding missing newline to end of input.");
}
input.push('\n'); // ensure the input ends with a newline
}

Expand Down
27 changes: 22 additions & 5 deletions crates/quarto-markdown-pandoc/src/readers/qmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::pandoc::{self, Block, Meta};
use crate::pandoc::{MetaValue, rawblock_to_meta};
use crate::readers::qmd_error_messages::{produce_error_message, produce_error_message_json};
use crate::traversals;
use crate::utils::error_collector::{JsonErrorCollector, TextErrorCollector};
use crate::utils::error_collector::{ErrorCollector, JsonErrorCollector, TextErrorCollector};
use std::io::Write;
use tree_sitter::LogType;
use tree_sitter_qmd::MarkdownParser;
Expand Down Expand Up @@ -140,26 +140,43 @@ where
let context = ASTContext::with_filename(filename.to_string());

// Create appropriate error collector based on whether JSON errors are requested
// and collect warnings after conversion
let mut result = if error_formatter.is_some() {
// JSON error format requested
let mut error_collector = JsonErrorCollector::new();
pandoc::treesitter_to_pandoc(
let pandoc_result = pandoc::treesitter_to_pandoc(
&mut output_stream,
&tree,
&input_bytes,
&context,
&mut error_collector,
)?
)?;

// Output warnings to stderr as JSON
let warnings = error_collector.messages();
for warning in warnings {
eprintln!("{}", warning);
}

pandoc_result
} else {
// Text error format (default)
let mut error_collector = TextErrorCollector::new();
pandoc::treesitter_to_pandoc(
let pandoc_result = pandoc::treesitter_to_pandoc(
&mut output_stream,
&tree,
&input_bytes,
&context,
&mut error_collector,
)?
)?;

// Output warnings to stderr as formatted text
let warnings = error_collector.messages();
for warning in warnings {
eprintln!("{}", warning);
}

pandoc_result
};
let mut meta_from_parses = Meta::default();

Expand Down
21 changes: 21 additions & 0 deletions crates/quarto-markdown-pandoc/tests/test_json_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,24 @@ fn test_label_range_note_type() {
"Should find at least one label-range note in the error"
);
}

#[test]
fn test_missing_newline_warning_json_format() {
// This test verifies that the missing newline warning is formatted as JSON
// when --json-errors is used. Currently this test will fail because the
// warning is always output as plain text.

// Create input without trailing newline
let input = "# Hello World";

// We can't easily test the binary's stderr output from here, but we can
// document the expected behavior: when --json-errors is used, the warning
// should be output as:
// {"title":"Warning","message":"Adding missing newline to end of input"}
//
// Currently it outputs:
// (Warning) Adding missing newline to end of input.

// This test just documents the issue. The actual fix will be in main.rs
// where the warning is emitted.
}
68 changes: 68 additions & 0 deletions crates/quarto-markdown-pandoc/tests/test_warnings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use quarto_markdown_pandoc::readers;
use quarto_markdown_pandoc::utils;

#[test]
fn test_caption_without_table_warning() {
// Create input with a caption after a div (not a table)
// This should parse successfully but emit a warning
let input = r#"::: {.my-div}
Some content
:::

: This caption has no table
"#;

// Parse the document
let result = readers::qmd::read(
input.as_bytes(),
false,
"test.md",
&mut std::io::sink(),
None::<
fn(&[u8], &utils::tree_sitter_log_observer::TreeSitterLogObserver, &str) -> Vec<String>,
>,
);

// Parsing should succeed (warnings are not errors)
assert!(result.is_ok(), "Document should parse successfully despite warning");

// TODO: Once the fix is implemented, we need to verify that the warning
// "Caption found without a preceding table" was actually output.
// For now, this test just verifies that parsing succeeds.
// After the fix, we'll need to capture stderr or modify the API
// to return warnings alongside the successful parse result.
}

#[test]
fn test_caption_with_table_no_warning() {
// Create input with a proper table caption
// This should parse successfully with no warnings
let input = r#"| A | B |
|---|---|
| 1 | 2 |

: Table caption
"#;

// Parse the document
let result = readers::qmd::read(
input.as_bytes(),
false,
"test.md",
&mut std::io::sink(),
None::<
fn(&[u8], &utils::tree_sitter_log_observer::TreeSitterLogObserver, &str) -> Vec<String>,
>,
);

// Parsing should succeed and no warnings should be emitted
assert!(result.is_ok(), "Document with valid table caption should parse successfully");

let (pandoc, _context) = result.unwrap();

// Verify we have a table in the output
assert!(
pandoc.blocks.iter().any(|b| matches!(b, quarto_markdown_pandoc::pandoc::Block::Table(_))),
"Should have a table in the output"
);
}