Skip to content

Commit e9d3c0b

Browse files
committed
parse and process nested metadata
1 parent 634812c commit e9d3c0b

File tree

6 files changed

+7145
-6642
lines changed

6 files changed

+7145
-6642
lines changed

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,14 @@ fn process_fenced_div_block<T: Write>(
986986
PandocNativeIntermediate::IntermediateSection(blocks) => {
987987
content.extend(blocks);
988988
}
989+
PandocNativeIntermediate::IntermediateMetadataString(text, range) => {
990+
// for now we assume it's metadata and emit it as a rawblock
991+
content.push(Block::RawBlock(RawBlock {
992+
format: "quarto_minus_metadata".to_string(),
993+
text,
994+
source_info: SourceInfo::with_range(range),
995+
}));
996+
}
989997
_ => {
990998
writeln!(
991999
buf,
@@ -1030,6 +1038,14 @@ fn process_block_quote<T: Write>(
10301038
PandocNativeIntermediate::IntermediateSection(section) => {
10311039
content.extend(section);
10321040
}
1041+
PandocNativeIntermediate::IntermediateMetadataString(text, range) => {
1042+
// for now we assume it's metadata and emit it as a rawblock
1043+
content.push(Block::RawBlock(RawBlock {
1044+
format: "quarto_minus_metadata".to_string(),
1045+
text,
1046+
source_info: SourceInfo::with_range(range),
1047+
}));
1048+
}
10331049
_ => {
10341050
writeln!(
10351051
buf,
@@ -1394,7 +1410,7 @@ fn process_list_item(
13941410
let mut list_attr: Option<ListAttributes> = None;
13951411
let children = children
13961412
.into_iter()
1397-
.filter(|(node, child)| {
1413+
.filter_map(|(node, child)| {
13981414
if node == "list_marker_dot" || node == "list_marker_parenthesis" {
13991415
// this is an ordered list, so we need to set the flag
14001416
let PandocNativeIntermediate::IntermediateOrderedListMarker(marker_number, _) =
@@ -1403,23 +1419,28 @@ fn process_list_item(
14031419
panic!("Expected OrderedListMarker in list_item, got {:?}", child);
14041420
};
14051421
list_attr = Some((
1406-
*marker_number,
1422+
marker_number,
14071423
ListNumberStyle::Decimal,
14081424
match node.as_str() {
14091425
"list_marker_parenthesis" => ListNumberDelim::OneParen,
14101426
"list_marker_dot" => ListNumberDelim::Period,
14111427
_ => panic!("Unexpected list marker node: {}", node),
14121428
},
14131429
));
1414-
return false; // skip the marker node
1430+
return None; // skip the marker node
1431+
}
1432+
match child {
1433+
PandocNativeIntermediate::IntermediateBlock(block) => Some(block),
1434+
PandocNativeIntermediate::IntermediateMetadataString(text, range) => {
1435+
// for now we assume it's metadata and emit it as a rawblock
1436+
Some(Block::RawBlock(RawBlock {
1437+
format: "quarto_minus_metadata".to_string(),
1438+
text,
1439+
source_info: SourceInfo::with_range(range),
1440+
}))
1441+
}
1442+
_ => None,
14151443
}
1416-
matches!(child, PandocNativeIntermediate::IntermediateBlock(_))
1417-
})
1418-
.map(|(_, child)| {
1419-
let PandocNativeIntermediate::IntermediateBlock(block) = child else {
1420-
panic!("Expected Block in paragraph, got {:?}", child);
1421-
};
1422-
block
14231444
})
14241445
.collect();
14251446
PandocNativeIntermediate::IntermediateListItem(children, node_location(node), list_attr)

crates/tree-sitter-qmd/tree-sitter-markdown/grammar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = grammar({
4545
$.fenced_div_block,
4646
$._blank_line,
4747
$.pipe_table,
48+
prec(-1, $.minus_metadata),
4849
),
4950
section: $ => choice($._section1, $._section2, $._section3, $._section4, $._section5, $._section6),
5051
_section1: $ => prec.right(seq(

crates/tree-sitter-qmd/tree-sitter-markdown/src/grammar.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,14 @@
21772177
{
21782178
"type": "SYMBOL",
21792179
"name": "pipe_table"
2180+
},
2181+
{
2182+
"type": "PREC",
2183+
"value": -1,
2184+
"content": {
2185+
"type": "SYMBOL",
2186+
"name": "minus_metadata"
2187+
}
21802188
}
21812189
]
21822190
},

crates/tree-sitter-qmd/tree-sitter-markdown/src/node-types.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
"type": "list",
113113
"named": true
114114
},
115+
{
116+
"type": "minus_metadata",
117+
"named": true
118+
},
115119
{
116120
"type": "paragraph",
117121
"named": true
@@ -276,6 +280,10 @@
276280
"type": "list",
277281
"named": true
278282
},
283+
{
284+
"type": "minus_metadata",
285+
"named": true
286+
},
279287
{
280288
"type": "paragraph",
281289
"named": true
@@ -464,6 +472,10 @@
464472
"type": "list_marker_star",
465473
"named": true
466474
},
475+
{
476+
"type": "minus_metadata",
477+
"named": true
478+
},
467479
{
468480
"type": "paragraph",
469481
"named": true
@@ -688,6 +700,10 @@
688700
"type": "list",
689701
"named": true
690702
},
703+
{
704+
"type": "minus_metadata",
705+
"named": true
706+
},
691707
{
692708
"type": "paragraph",
693709
"named": true

0 commit comments

Comments
 (0)