Skip to content

Commit 676e6ca

Browse files
committed
✨ feat(mq-lang, mq-markdown): Add table header selector support
This commit enhances table handling capabilities in mq by adding support for table header selectors: - Added `is_table_header()` helper function to builtin.mq for checking table header nodes - Extended `.table` selector in builtin.rs to match table header nodes when using `.table` without row/column parameters - Added `is_table_header()` method to the Node enum in mq-markdown for type checking - Included comprehensive test cases for table header selector matching scenarios - Added documentation for `.table` and `.[]` selectors in BUILTIN_SELECTOR_DOC This change allows users to select and manipulate table headers using the same `.table` selector syntax, improving consistency in table processing workflows.
1 parent edffbb9 commit 676e6ca

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

crates/mq-lang/builtin.mq

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ def is_h_level(md, level):
162162
error("Invalid heading level: " + to_string(level))
163163
end
164164

165+
# Checks if markdown is table header
166+
def is_table_header(md): to_md_name(md) == "table_header";
167+
165168
# Checks if markdown is emphasis
166169
def is_em(md): to_md_name(md) == "emphasis";
167170

crates/mq-lang/src/eval/builtin.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2643,6 +2643,14 @@ pub static BUILTIN_SELECTOR_DOC: LazyLock<FxHashMap<SmolStr, BuiltinSelectorDoc>
26432643
},
26442644
);
26452645

2646+
map.insert(
2647+
SmolStr::new(".table"),
2648+
BuiltinSelectorDoc {
2649+
description: "Selects a table cell node with the specified row and column.",
2650+
params: &["row", "column"],
2651+
},
2652+
);
2653+
26462654
map.insert(
26472655
SmolStr::new(".html"),
26482656
BuiltinSelectorDoc {
@@ -2683,6 +2691,14 @@ pub static BUILTIN_SELECTOR_DOC: LazyLock<FxHashMap<SmolStr, BuiltinSelectorDoc>
26832691
},
26842692
);
26852693

2694+
map.insert(
2695+
SmolStr::new(".[]"),
2696+
BuiltinSelectorDoc {
2697+
description: "Selects a list node with the specified index and checked state.",
2698+
params: &["indent", "checked"],
2699+
},
2700+
);
2701+
26862702
map.insert(
26872703
SmolStr::new(".mdx_js_esm"),
26882704
BuiltinSelectorDoc {
@@ -3712,7 +3728,7 @@ pub fn eval_selector(node: &mq_markdown::Node, selector: &Selector) -> bool {
37123728
(None, Some(column1), mq_markdown::Node::TableCell(mq_markdown::TableCell { column: column2, .. })) => {
37133729
*column1 == column2
37143730
}
3715-
(None, None, mq_markdown::Node::TableCell(_)) => true,
3731+
(None, None, mq_markdown::Node::TableCell(_)) | (None, None, mq_markdown::Node::TableHeader(_)) => true,
37163732
_ => false,
37173733
},
37183734
Selector::Html => node.is_html(),
@@ -4224,6 +4240,26 @@ mod tests {
42244240
Selector::Table(Some(1), None),
42254241
true
42264242
)]
4243+
#[case::table_header_with_no_row_col(
4244+
Node::TableHeader(mq_markdown::TableHeader { align: vec![], position: None }),
4245+
Selector::Table(None, None),
4246+
true
4247+
)]
4248+
#[case::table_header_with_only_row(
4249+
Node::TableHeader(mq_markdown::TableHeader { align: vec![], position: None }),
4250+
Selector::Table(Some(2), None),
4251+
false
4252+
)]
4253+
#[case::table_header_with_only_col(
4254+
Node::TableHeader(mq_markdown::TableHeader { align: vec![], position: None }),
4255+
Selector::Table(None, Some(3)),
4256+
false
4257+
)]
4258+
#[case::table_header_with_row_col(
4259+
Node::TableHeader(mq_markdown::TableHeader { align: vec![], position: None }),
4260+
Selector::Table(Some(1), Some(1)),
4261+
false
4262+
)]
42274263
#[case::list_with_matching_index_checked(
42284264
Node::List(mq_markdown::List { values: vec!["test".to_string().into()], ordered: false, index: 1, level: 1, checked: Some(true), position: None }),
42294265
Selector::List(Some(1), Some(true)),

crates/mq-markdown/src/node.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,10 @@ impl Node {
13821382
matches!(self, Self::TableCell(_))
13831383
}
13841384

1385+
pub fn is_table_header(&self) -> bool {
1386+
matches!(self, Self::TableHeader(_))
1387+
}
1388+
13851389
pub fn is_table_row(&self) -> bool {
13861390
matches!(self, Self::TableRow(_))
13871391
}

0 commit comments

Comments
 (0)