1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Markdown input rules (D13). A rule fires on a trigger typed into a text
//! leaf and rewrites what was typed; each firing is its own undo step, so
//! one undo restores the literal text. The set is configurable with
//! [`BlockEditorState::with_input_rules`](super::BlockEditorState::with_input_rules).
/// One input rule. Construct the built-in rules with the associated
/// functions, or take them all with [`InputRule::markdown_defaults`].
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct InputRule(pub(crate) RuleKind);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(crate) enum RuleKind {
Heading,
BulletList,
OrderedList,
TaskItem,
Blockquote,
CodeFence,
HorizontalRule,
InlineMarks,
Link,
Table,
}
impl InputRule {
/// `# ` … `###### ` at the start of a paragraph: a heading.
pub const fn heading() -> Self {
Self(RuleKind::Heading)
}
/// `- `, `* `, `+ ` at the start of a paragraph: a bullet list.
pub const fn bullet_list() -> Self {
Self(RuleKind::BulletList)
}
/// `1. ` (any number) at the start of a paragraph: an ordered list.
pub const fn ordered_list() -> Self {
Self(RuleKind::OrderedList)
}
/// `[ ] ` / `[x] ` at the start of a paragraph or list item (so also
/// `- [ ] `): a task item, unchecked / checked.
pub const fn task_item() -> Self {
Self(RuleKind::TaskItem)
}
/// `> ` at the start of a paragraph: a blockquote.
pub const fn blockquote() -> Self {
Self(RuleKind::Blockquote)
}
/// ```` ``` ```` / ```` ```lang ```` then Enter or space: a code block.
pub const fn code_fence() -> Self {
Self(RuleKind::CodeFence)
}
/// `---` then Enter in an otherwise empty paragraph: a horizontal rule.
pub const fn horizontal_rule() -> Self {
Self(RuleKind::HorizontalRule)
}
/// `` `x` ``, `**x**`, `*x*`, `~~x~~` on the closing delimiter.
pub const fn inline_marks() -> Self {
Self(RuleKind::InlineMarks)
}
/// `[text](url)` on the closing `)`: `text` with a link mark.
pub const fn link() -> Self {
Self(RuleKind::Link)
}
/// `| a | b |` then Enter at the end of a paragraph: a table whose
/// header row holds those cells, plus one empty body row.
pub const fn table() -> Self {
Self(RuleKind::Table)
}
/// Every built-in rule.
pub fn markdown_defaults() -> Vec<Self> {
vec![
Self::heading(),
Self::bullet_list(),
Self::ordered_list(),
Self::task_item(),
Self::blockquote(),
Self::code_fence(),
Self::horizontal_rule(),
Self::inline_marks(),
Self::link(),
Self::table(),
]
}
}