Markdown parser fork with extended syntax for personal use.
1//! Label start (link) occurs in the [text][] content type.
2//!
3//! ## Grammar
4//!
5//! Label start (link) forms with the following BNF
6//! (<small>see [construct][crate::construct] for character groups</small>):
7//!
8//! ```bnf
9//! label_start_link ::= '['
10//! ```
11//!
12//! ## HTML
13//!
14//! Label start (link) does not, on its own, relate to anything in HTML.
15//! When matched with a [label end][label_end], they together relate to the
16//! `<a>` element in HTML.
17//! See [*§ 4.5.1 The `a` element*][html_a] in the HTML spec for more info.
18//! Without an end, the character (`[`) is output.
19//!
20//! ## Tokens
21//!
22//! * [`LabelLink`][Name::LabelLink]
23//! * [`LabelMarker`][Name::LabelMarker]
24//!
25//! ## References
26//!
27//! * [`label-start-link.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark-core-commonmark/dev/lib/label-start-link.js)
28//! * [*§ 6.3 Links* in `CommonMark`](https://spec.commonmark.org/0.31/#links)
29//!
30//! [text]: crate::construct::text
31//! [label_end]: crate::construct::label_end
32//! [html_a]: https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-a-element
33
34use crate::event::Name;
35use crate::resolve::Name as ResolveName;
36use crate::state::State;
37use crate::tokenizer::{LabelKind, LabelStart, Tokenizer};
38
39/// Start of label (link) start.
40///
41/// ```markdown
42/// > | a [b] c
43/// ^
44/// ```
45pub fn start(tokenizer: &mut Tokenizer) -> State {
46 if tokenizer.parse_state.options.constructs.label_start_link && tokenizer.current == Some(b'[')
47 {
48 let start = tokenizer.events.len();
49 tokenizer.enter(Name::LabelLink);
50 tokenizer.enter(Name::LabelMarker);
51 tokenizer.consume();
52 tokenizer.exit(Name::LabelMarker);
53 tokenizer.exit(Name::LabelLink);
54 tokenizer.tokenize_state.label_starts.push(LabelStart {
55 kind: LabelKind::Link,
56 start: (start, tokenizer.events.len() - 1),
57 inactive: false,
58 });
59 tokenizer.register_resolver_before(ResolveName::Label);
60 State::Ok
61 } else {
62 State::Nok
63 }
64}