From ff031400076098c09599061737e0309da369ccfd Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 29 Oct 2023 22:11:24 -0400 Subject: [PATCH] Order the footnotes based on when they start processing rather than when they finish. This has the benefit of making the output order make sense when footnote definitions reference footnote references but I mostly did it to match the behavior of upstream org-mode. --- src/intermediate/registry.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/intermediate/registry.rs b/src/intermediate/registry.rs index a835283..9631268 100644 --- a/src/intermediate/registry.rs +++ b/src/intermediate/registry.rs @@ -82,13 +82,21 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>( } Ok(existing_id) } else { - let contents = convert_reference_contents(registry.clone(), definition).await?; - let pos = { + let existing_id = { let mut registry = registry.lock().unwrap(); - registry.footnote_ids.push((label, contents)); + registry.footnote_ids.push((label, Vec::new())); registry.footnote_ids.len() - 1 }; - Ok(pos) + let contents = convert_reference_contents(registry.clone(), definition).await?; + { + let mut registry = registry.lock().unwrap(); + let entry = registry + .footnote_ids + .get_mut(existing_id) + .expect("If-statement proves this to be Some."); + entry.1 = contents; + } + Ok(existing_id) } } @@ -163,9 +171,20 @@ pub(crate) async fn promote_footnote_definition<'orig, 'parse>( definition }; if let Some(elements) = definition { + let existing_id = { + let mut registry = registry.lock().unwrap(); + registry.footnote_ids.push((Some(label), Vec::new())); + registry.footnote_ids.len() - 1 + }; let contents = convert_definition_contents(registry.clone(), elements).await?; - let mut registry = registry.lock().unwrap(); - registry.footnote_ids.push((Some(label), contents)); + { + let mut registry = registry.lock().unwrap(); + let entry = registry + .footnote_ids + .get_mut(existing_id) + .expect("If-statement proves this to be Some."); + entry.1 = contents; + } } Ok(()) }