Skip to content

Commit 0ea1576

Browse files
committed
increment citationNodeNum counter
1 parent cca58ff commit 0ea1576

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

crates/quarto-markdown-pandoc/src/pandoc/treesitter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::pandoc::treesitter_utils::pipe_table::{
3232
process_pipe_table, process_pipe_table_cell, process_pipe_table_delimiter_cell,
3333
process_pipe_table_delimiter_row, process_pipe_table_header_or_row,
3434
};
35-
use crate::pandoc::treesitter_utils::postprocess::{desugar, merge_strs};
35+
use crate::pandoc::treesitter_utils::postprocess::{merge_strs, postprocess};
3636
use crate::pandoc::treesitter_utils::quoted_span::process_quoted_span;
3737
use crate::pandoc::treesitter_utils::raw_attribute::process_raw_attribute;
3838
use crate::pandoc::treesitter_utils::raw_specifier::process_raw_specifier;
@@ -714,7 +714,7 @@ pub fn treesitter_to_pandoc<T: Write>(
714714
let (_, PandocNativeIntermediate::IntermediatePandoc(pandoc)) = result else {
715715
panic!("Expected Pandoc, got {:?}", result)
716716
};
717-
let result = desugar(pandoc)?;
717+
let result = postprocess(pandoc)?;
718718
let result = merge_strs(result);
719719
Ok(result)
720720
}

crates/quarto-markdown-pandoc/src/pandoc/treesitter_utils/postprocess.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,28 @@ pub fn coalesce_abbreviations(inlines: Vec<Inline>) -> (Vec<Inline>, bool) {
194194
(result, did_coalesce)
195195
}
196196

197-
/// Apply desugaring transformations to the Pandoc AST
198-
pub fn desugar(doc: Pandoc) -> Result<Pandoc, Vec<String>> {
197+
/// Apply post-processing transformations to the Pandoc AST
198+
pub fn postprocess(doc: Pandoc) -> Result<Pandoc, Vec<String>> {
199199
let mut errors = Vec::new();
200200
let raw_reader_format_specifier: Lazy<Regex> =
201201
Lazy::new(|| Regex::new(r"<(?P<reader>.+)").unwrap());
202202
let result = {
203203
// Track seen header IDs to avoid duplicates
204204
let mut seen_ids: HashMap<String, usize> = HashMap::new();
205+
// Track citation count for numbering
206+
let mut citation_counter: usize = 0;
205207

206208
let mut filter = Filter::new()
209+
.with_cite(|mut cite| {
210+
// Increment citation counter for each Cite element
211+
citation_counter += 1;
212+
// Update all citations in this Cite element with the current counter
213+
for citation in &mut cite.citations {
214+
citation.note_num = citation_counter;
215+
}
216+
// Return Unchanged to allow recursion into cite content while avoiding re-filtering
217+
Unchanged(cite)
218+
})
207219
.with_superscript(|mut superscript| {
208220
let (content, changed) = trim_inlines(superscript.content);
209221
if !changed {
@@ -487,7 +499,7 @@ pub fn desugar(doc: Pandoc) -> Result<Pandoc, Vec<String>> {
487499
.with_attr(|attr| {
488500
// TODO in order to do good error messages here, attr will need source mapping
489501
errors.push(format!(
490-
"Found attr in desugar: {:?} - this should have been removed",
502+
"Found attr in postprocess: {:?} - this should have been removed",
491503
attr
492504
));
493505
FilterResult(vec![], false)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@cite1 @cite2
2+
3+
[@cite3; @cite4] @cite5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ Para [Cite [Citation { citationId = "cite1", citationPrefix = [], citationSuffix = [], citationMode = AuthorInText, citationNoteNum = 1, citationHash = 0 }] [Str "@cite1"], Space, Cite [Citation { citationId = "cite2", citationPrefix = [], citationSuffix = [], citationMode = AuthorInText, citationNoteNum = 2, citationHash = 0 }] [Str "@cite2"]], Para [Cite [Citation { citationId = "cite3", citationPrefix = [], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 3, citationHash = 0 }, Citation { citationId = "cite4", citationPrefix = [Space], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 3, citationHash = 0 }] [], Space, Cite [Citation { citationId = "cite5", citationPrefix = [], citationSuffix = [], citationMode = AuthorInText, citationNoteNum = 4, citationHash = 0 }] [Str "@cite5"]] ]

0 commit comments

Comments
 (0)