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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
//! Heading (atx) is a construct that occurs in the [flow] content type.
//!
//! They’re formed with the following BNF:
//!
//! ```bnf
//! heading_atx ::= 1*6'#' [ 1*space_or_tab text [ 1*space_or_tab 1*'#' ] ] *space_or_tab
//!
//! text ::= code - eol
//! space_or_tab ::= ' ' | '\t'
//! ```
//!
//! Headings in markdown relate to the `<h1>` through `<h6>` elements in HTML.
//! See [*§ 4.3.6 The `h1`, `h2`, `h3`, `h4`, `h5`, and `h6` elements* in the
//! HTML spec][html] for more info.
//!
//! `CommonMark` introduced the requirement on whitespace existing after the
//! opening sequence and before text.
//! In older markdown versions, this was not required, and headings would form
//! without it.
//!
//! In markdown, it is also possible to create headings with a
//! [heading (setext)][heading_setext] construct.
//! The benefit of setext headings is that their text can include line endings,
//! and by extensions also hard breaks (e.g., with
//! [hard break (escape)][hard_break_escape]).
//! However, their limit is that they cannot form `<h3>` through `<h6>`
//! headings.
//! Due to this limitation, it is recommended to use atx headings.
//!
//! > 🏛 **Background**: the word *setext* originates from a small markup
//! > language by Ian Feldman from 1991.
//! > See [*§ Setext* on Wikipedia][wiki-setext] for more info.
//! > The word *atx* originates from a tiny markup language by Aaron Swartz
//! > from 2002.
//! > See [*§ atx, the true structured text format* on `aaronsw.com`][atx] for
//! > more info.
//!
//! ## Tokens
//!
//! * [`HeadingAtx`][TokenType::HeadingAtx]
//! * [`HeadingAtxSequence`][TokenType::HeadingAtxSequence]
//! * [`HeadingAtxText`][TokenType::HeadingAtxText]
//! * [`HeadingAtxSpaceOrTab`][TokenType::HeadingAtxSpaceOrTab]
//!
//! ## References
//!
//! * [`heading-atx.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark-core-commonmark/dev/lib/heading-atx.js)
//! * [*§ 4.2 ATX headings* in `CommonMark`](https://spec.commonmark.org/0.30/#atx-headings)
//!
//! [flow]: crate::content::flow
//! [heading_setext]: crate::construct::heading_setext
//! [hard_break_escape]: crate::construct::hard_break_escape
//! [html]: https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements
//! [wiki-setext]: https://en.wikipedia.org/wiki/Setext
//! [atx]: http://www.aaronsw.com/2002/atx/
use super::partial_space_or_tab::{
space_or_tab, space_or_tab_with_options, Options as SpaceOrTabOptions,
};
use crate::constant::HEADING_ATX_OPENING_FENCE_SIZE_MAX;
use crate::tokenizer::{Code, State, StateFnResult, TokenType, Tokenizer};
/// Start of a heading (atx).
///
/// ```markdown
/// |## alpha
/// ```
pub fn start(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
tokenizer.enter(TokenType::HeadingAtx);
tokenizer.attempt_opt(space_or_tab(), before)(tokenizer, code)
}
/// Start of a heading (atx), after whitespace.
///
/// ```markdown
/// |## alpha
/// ```
pub fn before(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
if Code::Char('#') == code {
tokenizer.enter(TokenType::HeadingAtxSequence);
sequence_open(tokenizer, code, 0)
} else {
(State::Nok, None)
}
}
/// In the opening sequence.
///
/// ```markdown
/// #|# alpha
/// ```
fn sequence_open(tokenizer: &mut Tokenizer, code: Code, rank: usize) -> StateFnResult {
match code {
Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') if rank > 0 => {
tokenizer.exit(TokenType::HeadingAtxSequence);
at_break(tokenizer, code)
}
Code::Char('#') if rank < HEADING_ATX_OPENING_FENCE_SIZE_MAX => {
tokenizer.consume(code);
(
State::Fn(Box::new(move |tokenizer, code| {
sequence_open(tokenizer, code, rank + 1)
})),
None,
)
}
_ if rank > 0 => {
tokenizer.exit(TokenType::HeadingAtxSequence);
tokenizer.go(
space_or_tab_with_options(SpaceOrTabOptions {
kind: TokenType::HeadingAtxSpaceOrTab,
min: 1,
max: usize::MAX,
}),
at_break,
)(tokenizer, code)
}
_ => (State::Nok, None),
}
}
/// After something but before something else.
///
/// ```markdown
/// ## |alpha
/// ## alpha| bravo
/// ## alpha |bravo
/// ## alpha bravo|##
/// ## alpha bravo ##|
/// ```
fn at_break(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
match code {
Code::None | Code::CarriageReturnLineFeed | Code::Char('\n' | '\r') => {
tokenizer.exit(TokenType::HeadingAtx);
(State::Ok, Some(vec![code]))
}
Code::VirtualSpace | Code::Char('\t' | ' ') => tokenizer.go(
space_or_tab_with_options(SpaceOrTabOptions {
kind: TokenType::HeadingAtxSpaceOrTab,
min: 1,
max: usize::MAX,
}),
at_break,
)(tokenizer, code),
Code::Char('#') => {
tokenizer.enter(TokenType::HeadingAtxSequence);
further_sequence(tokenizer, code)
}
Code::Char(_) => {
tokenizer.enter(TokenType::HeadingAtxText);
tokenizer.enter(TokenType::ChunkText);
data(tokenizer, code)
}
}
}
/// In a further sequence (after whitespace).
/// Could be normal “visible” hashes in the heading or a final sequence.
///
/// ```markdown
/// ## alpha #|#
/// ```
fn further_sequence(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
if let Code::Char('#') = code {
tokenizer.consume(code);
(State::Fn(Box::new(further_sequence)), None)
} else {
tokenizer.exit(TokenType::HeadingAtxSequence);
at_break(tokenizer, code)
}
}
/// In text.
///
/// ```markdown
/// ## al|pha
/// ```
fn data(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
match code {
// Note: `#` for closing sequence must be preceded by whitespace, otherwise it’s just text.
Code::None | Code::CarriageReturnLineFeed | Code::Char('\t' | '\n' | '\r' | ' ') => {
tokenizer.exit(TokenType::ChunkText);
tokenizer.exit(TokenType::HeadingAtxText);
at_break(tokenizer, code)
}
_ => {
tokenizer.consume(code);
(State::Fn(Box::new(data)), None)
}
}
}
|