Markdown parser fork with extended syntax for personal use.
1//! Hard break (escape) occurs in the [text][] content type.
2//!
3//! ## Grammar
4//!
5//! Hard break (escape) forms with the following BNF
6//! (<small>see [construct][crate::construct] for character groups</small>):
7//!
8//! ```bnf
9//! ; Restriction: followed by a line ending (that is part of the content
10//! ; instead of ending it).
11//! hard_break_escape ::= '\\'
12//! ```
13//!
14//! It is also possible to create a hard break with a
15//! [hard break (trailing)][hard_break_trailing].
16//!
17//! Punctuation characters can be escaped with a similar
18//! construct: a [character escape][character_escape] is a backslash followed
19//! by an ASCII punctuation character.
20//! Arbitrary characters can be escaped with
21//! [character references][character_reference].
22//!
23//! ## HTML
24//!
25//! Hard breaks in markdown relate to the HTML element `<br>`.
26//! See [*§ 4.5.27 The `br` element* in the HTML spec][html] for more info.
27//!
28//! ## Recommendation
29//!
30//! Always use hard break (escape), never hard break (trailing).
31//!
32//! ## Tokens
33//!
34//! * [`HardBreakEscape`][Name::HardBreakEscape]
35//!
36//! ## References
37//!
38//! * [`hard-break-escape.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark-core-commonmark/dev/lib/hard-break-escape.js)
39//! * [*§ 6.7 Hard line breaks* in `CommonMark`](https://spec.commonmark.org/0.31/#hard-line-breaks)
40//!
41//! [text]: crate::construct::text
42//! [character_escape]: crate::construct::character_escape
43//! [character_reference]: crate::construct::character_reference
44//! [hard_break_trailing]: crate::construct::partial_whitespace
45//! [html]: https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-br-element
46
47use crate::event::Name;
48use crate::state::{Name as StateName, State};
49use crate::tokenizer::Tokenizer;
50
51/// Start of hard break (escape).
52///
53/// ```markdown
54/// > | a\
55/// ^
56/// | b
57/// ```
58pub fn start(tokenizer: &mut Tokenizer) -> State {
59 if tokenizer.parse_state.options.constructs.hard_break_escape
60 && tokenizer.current == Some(b'\\')
61 {
62 tokenizer.enter(Name::HardBreakEscape);
63 tokenizer.consume();
64 State::Next(StateName::HardBreakEscapeAfter)
65 } else {
66 State::Nok
67 }
68}
69
70/// After `\`, at eol.
71///
72/// ```markdown
73/// > | a\
74/// ^
75/// | b
76/// ```
77pub fn after(tokenizer: &mut Tokenizer) -> State {
78 match tokenizer.current {
79 Some(b'\n') => {
80 tokenizer.exit(Name::HardBreakEscape);
81 State::Ok
82 }
83 _ => State::Nok,
84 }
85}