1use rowan::{GreenNodeBuilder, Language as _, TextRange, TextSize};
2
3use super::syntax::{DocumentationLanguage, SyntaxKind, SyntaxNode};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct ParseDiagnostic {
8 pub kind: DiagnosticKind,
9 pub range: TextRange,
10}
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum DiagnosticKind {
15 UnclosedFence,
17}
18
19#[derive(Debug, Clone)]
21pub struct ParseOutput {
22 pub cst: SyntaxNode,
23 pub diagnostics: Vec<ParseDiagnostic>,
24}
25
26pub fn parse(text: &str) -> ParseOutput {
28 assert!(
29 text.len() <= u32::MAX as usize,
30 "documentation exceeds Rowan's 4 GiB limit"
31 );
32 Parser::new(text).parse()
33}
34
35pub fn reconstruct(root: &SyntaxNode) -> String {
37 root.text().to_string()
38}
39
40#[derive(Debug, Clone, Copy)]
41struct Line {
42 start: usize,
43 content_end: usize,
44 end: usize,
45}
46
47impl Line {
48 fn content(self, text: &str) -> &str {
49 &text[self.start..self.content_end]
50 }
51
52 fn is_blank(self, text: &str) -> bool {
53 self.content(text).trim_matches([' ', '\t']).is_empty()
54 }
55}
56
57#[derive(Debug, Clone, Copy)]
58struct FenceOpen {
59 marker_start: usize,
60 marker_end: usize,
61 marker: u8,
62 length: usize,
63}
64
65#[derive(Debug, Clone, Copy)]
66enum FencePrefix {
67 Spaces(usize),
68 IndentedContainer,
69}
70
71#[derive(Clone, Copy)]
72struct InlineLine {
73 line: Line,
74 content_start: usize,
75 quote_marker: Option<usize>,
76}
77
78impl InlineLine {
79 fn new(line: Line, content_start: usize) -> Self {
80 Self {
81 line,
82 content_start,
83 quote_marker: None,
84 }
85 }
86}
87
88struct InlinePiece {
89 logical: std::ops::Range<usize>,
90 source: std::ops::Range<usize>,
91 kind: InlinePieceKind,
92}
93
94enum InlinePieceKind {
95 Content,
96 SoftBreak,
97 HardBreak,
98 Prefix(SyntaxKind),
99}
100
101struct Parser<'a> {
102 text: &'a str,
103 lines: Vec<Line>,
104 builder: GreenNodeBuilder<'static>,
105 diagnostics: Vec<ParseDiagnostic>,
106}
107
108impl<'a> Parser<'a> {
109 fn new(text: &'a str) -> Self {
110 Self {
111 text,
112 lines: lines(text),
113 builder: GreenNodeBuilder::new(),
114 diagnostics: Vec::new(),
115 }
116 }
117
118 fn parse(mut self) -> ParseOutput {
119 self.start(SyntaxKind::ROOT);
120 let mut line = 0;
121 while line < self.lines.len() {
122 if self.lines[line].is_blank(self.text) {
123 let current = self.lines[line];
124 self.token(SyntaxKind::BLANK_LINE, current.start, current.end);
125 line += 1;
126 continue;
127 }
128 line = self.parse_block(line);
129 }
130 self.finish();
131 let green = self.builder.finish();
132 ParseOutput {
133 cst: SyntaxNode::new_root(green),
134 diagnostics: self.diagnostics,
135 }
136 }
137
138 fn parse_block(&mut self, line: usize) -> usize {
139 if let Some(expression_end) = block_interpolation(self.lines[line], self.text) {
140 return self.emit_block_interpolation(line, expression_end);
141 }
142 if let Some(open) = fence_open(self.lines[line], self.text) {
143 if let Some(close) = self.find_fence_close(line + 1, open, FencePrefix::Spaces(0)) {
144 return self.emit_fence(
145 line,
146 close,
147 open,
148 FencePrefix::Spaces(0),
149 self.lines[line].start,
150 );
151 }
152 self.diagnostics.push(ParseDiagnostic {
153 kind: DiagnosticKind::UnclosedFence,
154 range: range(open.marker_start, open.marker_end),
155 });
156 return self.emit_paragraph(line);
157 }
158 if atx_heading(self.lines[line], self.text).is_some() {
159 return self.emit_atx_heading(line);
160 }
161 if line + 1 < self.lines.len()
162 && contains_unescaped_pipe(self.lines[line].content(self.text))
163 && table_alignment(self.lines[line + 1].content(self.text))
164 {
165 return self.emit_table(line);
166 }
167 if block_quote_content(self.lines[line], self.text).is_some() {
168 return self.emit_block_quote(line);
169 }
170 if admonition_header(self.lines[line], self.text).is_some() {
171 return self.emit_admonition(line);
172 }
173 if footnote_header(self.lines[line], self.text).is_some() {
174 return self.emit_footnote(line);
175 }
176 if list_marker(self.lines[line], self.text).is_some() {
177 return self.emit_list(line);
178 }
179 if indentation(self.lines[line].content(self.text)) >= 4
180 || self.lines[line].content(self.text).starts_with('\t')
181 {
182 return self.emit_indented_code(line);
183 }
184 if thematic_break(self.lines[line].content(self.text)) {
185 return self.emit_simple_line(line, SyntaxKind::THEMATIC_BREAK);
186 }
187 if math_block(self.lines[line], self.text).is_some() {
188 return self.emit_math_block(line);
189 }
190 if line + 1 < self.lines.len()
195 && !self.lines[line].is_blank(self.text)
196 && setext_level(self.lines[line + 1], self.text).is_some()
197 {
198 return self.emit_setext_heading(line);
199 }
200 self.emit_paragraph(line)
201 }
202
203 fn find_fence_close(&self, from: usize, open: FenceOpen, prefix: FencePrefix) -> Option<usize> {
204 for line_index in from..self.lines.len() {
205 let line = self.lines[line_index];
206 if line.is_blank(self.text) && self.line_after_prefix(line, prefix).is_none() {
207 if matches!(prefix, FencePrefix::Spaces(_))
209 && self.lines.get(line_index + 1).is_some_and(|next| {
210 next.is_blank(self.text) && self.line_after_prefix(*next, prefix).is_none()
211 })
212 {
213 return None;
214 }
215 continue;
216 }
217 let line = self.line_after_prefix(line, prefix)?;
218 let content = line.content(self.text);
219 let end = content
220 .bytes()
221 .take_while(|&byte| byte == open.marker)
222 .count();
223 if end == open.length && content[end..].trim_matches([' ', '\t']).is_empty() {
224 return Some(line_index);
225 }
226 }
227 None
228 }
229
230 fn line_after_prefix(&self, line: Line, prefix: FencePrefix) -> Option<Line> {
231 let start = match prefix {
232 FencePrefix::Spaces(prefix) => {
233 if !line
234 .content(self.text)
235 .as_bytes()
236 .get(..prefix)?
237 .iter()
238 .all(|&byte| byte == b' ')
239 {
240 return None;
241 }
242 line.start + prefix
243 }
244 FencePrefix::IndentedContainer => indented_content_start(line, self.text)?,
245 };
246 Some(Line { start, ..line })
247 }
248
249 fn emit_fence(
250 &mut self,
251 open_line: usize,
252 close_line: usize,
253 open: FenceOpen,
254 prefix: FencePrefix,
255 opening_start: usize,
256 ) -> usize {
257 let opening = self.lines[open_line];
258 let closing = self.lines[close_line];
259 let info = self.text[open.marker_end..opening.content_end].trim();
260 let node_kind = if info == "math" {
261 SyntaxKind::MATH_BLOCK
262 } else {
263 SyntaxKind::FENCED_CODE_BLOCK
264 };
265 self.start(node_kind);
266 self.token(SyntaxKind::WHITESPACE, opening_start, open.marker_start);
267 self.token(SyntaxKind::FENCE, open.marker_start, open.marker_end);
268 self.token(
269 SyntaxKind::INFO_STRING,
270 open.marker_end,
271 opening.content_end,
272 );
273 self.token(SyntaxKind::NEWLINE, opening.content_end, opening.end);
274
275 let content_kind = if node_kind == SyntaxKind::MATH_BLOCK {
276 SyntaxKind::MATH_CONTENT
277 } else {
278 SyntaxKind::CODE_CONTENT
279 };
280 if matches!(prefix, FencePrefix::Spaces(0)) {
281 self.token(content_kind, opening.end, closing.start);
282 } else {
283 for line_index in open_line + 1..close_line {
284 let line = self.lines[line_index];
285 let content_start = match prefix {
286 FencePrefix::Spaces(prefix) => {
287 line.start + prefix.min(indentation(line.content(self.text)))
288 }
289 FencePrefix::IndentedContainer => {
290 indented_content_start(line, self.text).unwrap_or(line.content_end)
291 }
292 };
293 self.token(SyntaxKind::WHITESPACE, line.start, content_start);
294 self.token(content_kind, content_start, line.end);
295 }
296 }
297
298 let marker_start = self
299 .line_after_prefix(closing, prefix)
300 .expect("closing fence retains its container prefix")
301 .start;
302 let mut marker_end = marker_start;
303 while marker_end < closing.content_end && self.text.as_bytes()[marker_end] == open.marker {
304 marker_end += 1;
305 }
306 self.token(SyntaxKind::WHITESPACE, closing.start, marker_start);
307 self.token(SyntaxKind::FENCE, marker_start, marker_end);
308 self.token(SyntaxKind::MARKER, marker_end, closing.content_end);
309 self.token(SyntaxKind::NEWLINE, closing.content_end, closing.end);
310 self.finish();
311 close_line + 1
312 }
313
314 fn emit_atx_heading(&mut self, line_index: usize) -> usize {
315 let line = self.lines[line_index];
316 let (marker_start, marker_end, content_start, content_end) =
317 atx_heading(line, self.text).expect("checked heading");
318 self.start(SyntaxKind::ATX_HEADING);
319 self.token(SyntaxKind::WHITESPACE, line.start, marker_start);
320 self.token(SyntaxKind::HEADING_MARKER, marker_start, marker_end);
321 self.token(SyntaxKind::WHITESPACE, marker_end, content_start);
322 self.parse_inlines(content_start, content_end);
323 self.token(SyntaxKind::MARKER, content_end, line.content_end);
324 self.token(SyntaxKind::NEWLINE, line.content_end, line.end);
325 self.finish();
326 line_index + 1
327 }
328
329 fn emit_setext_heading(&mut self, line_index: usize) -> usize {
330 let content = self.lines[line_index];
331 let underline = self.lines[line_index + 1];
332 self.start(SyntaxKind::SETEXT_HEADING);
333 self.emit_trimmed_inlines(content);
334 self.token(SyntaxKind::NEWLINE, content.content_end, content.end);
335 let indent = indentation(underline.content(self.text));
336 self.token(
337 SyntaxKind::WHITESPACE,
338 underline.start,
339 underline.start + indent,
340 );
341 self.token(
342 SyntaxKind::HEADING_MARKER,
343 underline.start + indent,
344 underline.content_end,
345 );
346 self.token(SyntaxKind::NEWLINE, underline.content_end, underline.end);
347 self.finish();
348 line_index + 2
349 }
350
351 fn emit_table(&mut self, line_index: usize) -> usize {
352 self.start(SyntaxKind::TABLE);
353 let mut line = line_index;
354 while line < self.lines.len()
355 && !self.lines[line].is_blank(self.text)
356 && contains_unescaped_pipe(self.lines[line].content(self.text))
357 {
358 self.emit_table_row(self.lines[line]);
359 line += 1;
360 }
361 self.finish();
362 line
363 }
364
365 fn emit_table_row(&mut self, line: Line) {
366 self.start(SyntaxKind::TABLE_ROW);
367 let mut cursor = line.start;
368 let mut cell_start = line.start;
369 while cursor < line.content_end {
370 if self.text.as_bytes()[cursor] == b'|'
371 && (cursor == line.start || self.text.as_bytes()[cursor - 1] != b'\\')
372 {
373 if cell_start < cursor {
374 self.start(SyntaxKind::TABLE_CELL);
375 self.parse_inlines(cell_start, cursor);
376 self.finish();
377 }
378 self.token(SyntaxKind::MARKER, cursor, cursor + 1);
379 cursor += 1;
380 cell_start = cursor;
381 } else {
382 cursor += self.text[cursor..].chars().next().unwrap().len_utf8();
383 }
384 }
385 if cell_start < line.content_end {
386 self.start(SyntaxKind::TABLE_CELL);
387 self.parse_inlines(cell_start, line.content_end);
388 self.finish();
389 }
390 self.token(SyntaxKind::NEWLINE, line.content_end, line.end);
391 self.finish();
392 }
393
394 fn emit_block_quote(&mut self, line_index: usize) -> usize {
395 self.start(SyntaxKind::BLOCK_QUOTE);
396 let mut line_index = line_index;
397 while line_index < self.lines.len() {
398 let line = self.lines[line_index];
399 let Some((_marker_start, content_start)) = block_quote_content(line, self.text) else {
400 break;
401 };
402 if content_start == line.content_end {
403 self.token(SyntaxKind::BLANK_LINE, line.start, line.end);
404 line_index += 1;
405 continue;
406 }
407
408 if list_marker_range(self.text, content_start, line.content_end).is_some() {
409 self.start(SyntaxKind::LIST);
410 while line_index < self.lines.len() {
411 let item_line = self.lines[line_index];
412 let Some((quote_marker, quote_content)) =
413 block_quote_content(item_line, self.text)
414 else {
415 break;
416 };
417 let Some((list_start, list_end, item_content)) =
418 list_marker_range(self.text, quote_content, item_line.content_end)
419 else {
420 break;
421 };
422 self.start(SyntaxKind::LIST_ITEM);
423 self.emit_quote_prefix(item_line, quote_marker, quote_content);
424 self.token(SyntaxKind::WHITESPACE, quote_content, list_start);
425 self.token(SyntaxKind::LIST_MARKER, list_start, list_end);
426 self.token(SyntaxKind::WHITESPACE, list_end, item_content);
427 let mut inlines = vec![InlineLine::new(
428 Line {
429 start: item_content,
430 ..item_line
431 },
432 item_content,
433 )];
434 line_index += 1;
435 while line_index < self.lines.len() {
436 let continuation = self.lines[line_index];
437 let Some((quote_marker, quote_content_start)) =
438 block_quote_content(continuation, self.text)
439 else {
440 break;
441 };
442 let indent =
443 indentation(&self.text[quote_content_start..continuation.content_end]);
444 if quote_content_start + indent == continuation.content_end
445 || indent < item_content - quote_content
446 || list_marker_range(
447 self.text,
448 quote_content_start,
449 continuation.content_end,
450 )
451 .is_some()
452 {
453 break;
454 }
455 inlines.push(InlineLine {
456 line: continuation,
457 content_start: quote_content_start + indent,
458 quote_marker: Some(quote_marker),
459 });
460 line_index += 1;
461 }
462 self.flush_inline_lines(&mut inlines, SyntaxKind::NEWLINE);
463 self.finish();
464 }
465 self.finish();
466 continue;
467 }
468
469 self.start(SyntaxKind::PARAGRAPH);
470 let mut inlines = Vec::new();
471 while line_index < self.lines.len() {
472 let paragraph_line = self.lines[line_index];
473 let Some((quote_marker, quote_content)) =
474 block_quote_content(paragraph_line, self.text)
475 else {
476 break;
477 };
478 if quote_content == paragraph_line.content_end
479 || list_marker_range(self.text, quote_content, paragraph_line.content_end)
480 .is_some()
481 {
482 break;
483 }
484 inlines.push(InlineLine {
485 line: paragraph_line,
486 content_start: quote_content,
487 quote_marker: Some(quote_marker),
488 });
489 line_index += 1;
490 }
491 self.flush_inline_lines(&mut inlines, SyntaxKind::SOFT_BREAK);
492 self.finish();
493 }
494 self.finish();
495 line_index
496 }
497
498 fn emit_quote_prefix(&mut self, line: Line, marker_start: usize, content_start: usize) {
499 self.token(SyntaxKind::WHITESPACE, line.start, marker_start);
500 self.token(SyntaxKind::QUOTE_MARKER, marker_start, marker_start + 1);
501 self.token(SyntaxKind::WHITESPACE, marker_start + 1, content_start);
502 }
503
504 fn emit_admonition(&mut self, line_index: usize) -> usize {
505 let header = self.lines[line_index];
506 let (marker_start, category, title) =
507 admonition_header(header, self.text).expect("checked admonition");
508 self.start(SyntaxKind::ADMONITION);
509 self.token(SyntaxKind::WHITESPACE, header.start, marker_start);
510 self.token(SyntaxKind::MARKER, marker_start, marker_start + 3);
511 self.token(SyntaxKind::WHITESPACE, marker_start + 3, category.start);
512 self.token(
513 SyntaxKind::ADMONITION_CATEGORY,
514 category.start,
515 category.end,
516 );
517 if let Some(title) = title {
518 self.token(SyntaxKind::WHITESPACE, category.end, title.start);
519 self.token(SyntaxKind::ADMONITION_TITLE, title.start, title.end);
520 self.token(SyntaxKind::WHITESPACE, title.end, header.content_end);
521 } else {
522 self.token(SyntaxKind::WHITESPACE, category.end, header.content_end);
523 }
524 self.token(SyntaxKind::NEWLINE, header.content_end, header.end);
525 let next = self.emit_indented_container_blocks(line_index + 1);
526 self.finish();
527 next
528 }
529
530 fn emit_indented_container_blocks(&mut self, mut line_index: usize) -> usize {
531 while line_index < self.lines.len() {
532 let line = self.lines[line_index];
533 if line.is_blank(self.text) {
534 self.token(SyntaxKind::BLANK_LINE, line.start, line.end);
535 line_index += 1;
536 continue;
537 }
538 let Some(content_start) = indented_content_start(line, self.text) else {
539 break;
540 };
541 if let Some(open) = fence_open(
542 Line {
543 start: content_start,
544 ..line
545 },
546 self.text,
547 ) {
548 if let Some(close) =
549 self.find_fence_close(line_index + 1, open, FencePrefix::IndentedContainer)
550 {
551 line_index = self.emit_fence(
552 line_index,
553 close,
554 open,
555 FencePrefix::IndentedContainer,
556 line.start,
557 );
558 continue;
559 }
560 self.diagnostics.push(ParseDiagnostic {
561 kind: DiagnosticKind::UnclosedFence,
562 range: range(open.marker_start, open.marker_end),
563 });
564 }
565 if let Some((marker_start, marker_end, _)) =
566 list_marker_range(self.text, content_start, line.content_end)
567 {
568 let ordered = self.text.as_bytes()[marker_start..marker_end]
569 .first()
570 .is_some_and(u8::is_ascii_digit);
571 line_index = self.emit_list_level(line_index, marker_start - line.start, ordered);
572 continue;
573 }
574
575 self.start(SyntaxKind::PARAGRAPH);
576 let mut inlines = Vec::new();
577 while line_index < self.lines.len() {
578 let paragraph_line = self.lines[line_index];
579 let Some(content_start) = indented_content_start(paragraph_line, self.text) else {
580 break;
581 };
582 if list_marker_range(self.text, content_start, paragraph_line.content_end).is_some()
583 || (!inlines.is_empty()
584 && fence_open(
585 Line {
586 start: content_start,
587 ..paragraph_line
588 },
589 self.text,
590 )
591 .is_some())
592 {
593 break;
594 }
595 inlines.push(InlineLine::new(paragraph_line, content_start));
596 line_index += 1;
597 if line_index >= self.lines.len() || self.lines[line_index].is_blank(self.text) {
598 break;
599 }
600 }
601 self.flush_inline_lines(&mut inlines, SyntaxKind::SOFT_BREAK);
602 self.finish();
603 }
604 line_index
605 }
606
607 fn emit_footnote(&mut self, line_index: usize) -> usize {
608 let header = self.lines[line_index];
609 let (label_start, label_end, content_start) =
610 footnote_header(header, self.text).expect("checked footnote");
611 self.start(SyntaxKind::FOOTNOTE_DEFINITION);
612 self.token(SyntaxKind::WHITESPACE, header.start, label_start);
613 self.token(SyntaxKind::FOOTNOTE_LABEL, label_start, label_end);
614 self.token(SyntaxKind::WHITESPACE, label_end, content_start);
615 self.start(SyntaxKind::PARAGRAPH);
616 let mut inlines = vec![InlineLine::new(
617 Line {
618 start: content_start,
619 ..header
620 },
621 content_start,
622 )];
623
624 let mut next = line_index + 1;
625 while next < self.lines.len() {
626 let line = self.lines[next];
627 if line.is_blank(self.text) {
628 break;
629 }
630 let Some(content_start) = indented_content_start(line, self.text) else {
631 break;
632 };
633 if list_marker_range(self.text, content_start, line.content_end).is_some() {
634 break;
635 }
636 inlines.push(InlineLine::new(line, content_start));
637 next += 1;
638 }
639 self.flush_inline_lines(&mut inlines, SyntaxKind::SOFT_BREAK);
640 self.finish();
641 next = self.emit_indented_container_blocks(next);
642 self.finish();
643 next
644 }
645
646 fn emit_list(&mut self, line_index: usize) -> usize {
647 let (marker_start, marker_end, _) =
648 list_marker(self.lines[line_index], self.text).expect("checked list");
649 let base_indent = marker_start - self.lines[line_index].start;
650 let ordered = self.text.as_bytes()[marker_start..marker_end]
651 .first()
652 .is_some_and(u8::is_ascii_digit);
653 self.emit_list_level(line_index, base_indent, ordered)
654 }
655
656 fn emit_list_level(
657 &mut self,
658 mut line_index: usize,
659 base_indent: usize,
660 ordered: bool,
661 ) -> usize {
662 self.start(SyntaxKind::LIST);
663 while line_index < self.lines.len() {
664 let line = self.lines[line_index];
665 let Some((marker_start, marker_end, content_start)) = list_marker_any(line, self.text)
666 else {
667 break;
668 };
669 let indent = marker_start - line.start;
670 let item_ordered = self.text.as_bytes()[marker_start..marker_end]
671 .first()
672 .is_some_and(u8::is_ascii_digit);
673 if indent != base_indent || item_ordered != ordered {
674 break;
675 }
676 self.start(SyntaxKind::LIST_ITEM);
677 self.token(SyntaxKind::WHITESPACE, line.start, marker_start);
678 self.token(SyntaxKind::LIST_MARKER, marker_start, marker_end);
679 self.token(SyntaxKind::WHITESPACE, marker_end, content_start);
680 let item_indent = marker_end - line.start + usize::from(content_start > marker_end);
681 let mut inlines = Vec::new();
682 let first_fence = fence_open(
683 Line {
684 start: line.start + item_indent,
685 ..line
686 },
687 self.text,
688 )
689 .and_then(|open| {
690 if let Some(close) =
691 self.find_fence_close(line_index + 1, open, FencePrefix::Spaces(item_indent))
692 {
693 Some((open, close))
694 } else {
695 self.diagnostics.push(ParseDiagnostic {
696 kind: DiagnosticKind::UnclosedFence,
697 range: range(open.marker_start, open.marker_end),
698 });
699 None
700 }
701 });
702 if let Some((open, close)) = first_fence {
703 line_index = self.emit_fence(
704 line_index,
705 close,
706 open,
707 FencePrefix::Spaces(item_indent),
708 content_start,
709 );
710 } else {
711 inlines.push(InlineLine::new(
712 Line {
713 start: content_start,
714 ..line
715 },
716 content_start,
717 ));
718 line_index += 1;
719 }
720 let mut after_blank = false;
721
722 while line_index < self.lines.len() {
723 let continuation = self.lines[line_index];
724 if continuation.is_blank(self.text) {
725 let Some(next) = self.lines.get(line_index + 1).copied() else {
726 break;
727 };
728 if next.is_blank(self.text) {
729 break;
730 }
731 let continues_list = list_marker_any(next, self.text)
732 .is_some_and(|(start, _, _)| start - next.start >= base_indent);
733 let continues_item = indentation(next.content(self.text)) > base_indent;
734 if !continues_list && !continues_item {
735 break;
736 }
737 self.flush_inline_lines(&mut inlines, SyntaxKind::NEWLINE);
738 self.token(SyntaxKind::BLANK_LINE, continuation.start, continuation.end);
739 line_index += 1;
740 after_blank = true;
741 continue;
742 }
743 if let Some((nested_start, nested_end, _)) =
744 list_marker_any(continuation, self.text)
745 {
746 let nested_indent = nested_start - continuation.start;
747 let nested_ordered = self.text.as_bytes()[nested_start..nested_end]
748 .first()
749 .is_some_and(u8::is_ascii_digit);
750 if nested_indent > base_indent {
751 self.flush_inline_lines(&mut inlines, SyntaxKind::NEWLINE);
752 line_index =
753 self.emit_list_level(line_index, nested_indent, nested_ordered);
754 after_blank = false;
755 continue;
756 }
757 break;
758 }
759 let indent = indentation(continuation.content(self.text));
760 if indent <= base_indent {
761 break;
762 }
763 if after_blank
764 && indented_content_start_after(continuation, self.text, item_indent).is_some()
765 {
766 self.flush_inline_lines(&mut inlines, SyntaxKind::NEWLINE);
767 line_index = self.emit_indented_code_after(line_index, item_indent);
768 after_blank = false;
769 continue;
770 }
771 if let Some(open) = self
772 .line_after_prefix(continuation, FencePrefix::Spaces(item_indent))
773 .and_then(|line| fence_open(line, self.text))
774 {
775 if let Some(close) = self.find_fence_close(
776 line_index + 1,
777 open,
778 FencePrefix::Spaces(item_indent),
779 ) {
780 self.flush_inline_lines(&mut inlines, SyntaxKind::NEWLINE);
781 line_index = self.emit_fence(
782 line_index,
783 close,
784 open,
785 FencePrefix::Spaces(item_indent),
786 continuation.start,
787 );
788 after_blank = false;
789 continue;
790 }
791 self.diagnostics.push(ParseDiagnostic {
792 kind: DiagnosticKind::UnclosedFence,
793 range: range(open.marker_start, open.marker_end),
794 });
795 }
796 inlines.push(InlineLine::new(continuation, continuation.start + indent));
797 line_index += 1;
798 after_blank = false;
799 }
800 self.flush_inline_lines(&mut inlines, SyntaxKind::NEWLINE);
801 self.finish();
802 }
803 self.finish();
804 line_index
805 }
806
807 fn emit_indented_code(&mut self, line_index: usize) -> usize {
808 self.emit_indented_code_after(line_index, 0)
809 }
810
811 fn emit_indented_code_after(&mut self, line_index: usize, prefix: usize) -> usize {
812 self.start(SyntaxKind::INDENTED_CODE_BLOCK);
813 let mut line_index = line_index;
814 while line_index < self.lines.len() {
815 let line = self.lines[line_index];
816 if line.is_blank(self.text) {
817 self.token(SyntaxKind::CODE_CONTENT, line.start, line.end);
818 line_index += 1;
819 continue;
820 }
821 let Some(content_start) = indented_content_start_after(line, self.text, prefix) else {
822 break;
823 };
824 self.token(SyntaxKind::WHITESPACE, line.start, content_start);
825 self.token(SyntaxKind::CODE_CONTENT, content_start, line.content_end);
826 self.token(SyntaxKind::NEWLINE, line.content_end, line.end);
827 line_index += 1;
828 }
829 self.finish();
830 line_index
831 }
832
833 fn emit_math_block(&mut self, line_index: usize) -> usize {
834 let line = self.lines[line_index];
835 let (open, content_start, content_end, close) =
836 math_block(line, self.text).expect("checked math block");
837 self.start(SyntaxKind::MATH_BLOCK);
838 self.token(SyntaxKind::WHITESPACE, line.start, open);
839 self.token(SyntaxKind::MARKER, open, content_start);
840 self.token(SyntaxKind::MATH_CONTENT, content_start, content_end);
841 self.token(SyntaxKind::MARKER, content_end, close);
842 self.token(SyntaxKind::WHITESPACE, close, line.content_end);
843 self.token(SyntaxKind::NEWLINE, line.content_end, line.end);
844 self.finish();
845 line_index + 1
846 }
847
848 fn emit_block_interpolation(&mut self, line_index: usize, expression_end: usize) -> usize {
849 let line = self.lines[line_index];
850 self.start(SyntaxKind::INTERPOLATION);
851 self.token(SyntaxKind::MARKER, line.start, line.start + 1);
852 self.token(SyntaxKind::TEXT, line.start + 1, expression_end);
853 self.token(SyntaxKind::WHITESPACE, expression_end, line.content_end);
854 self.token(SyntaxKind::NEWLINE, line.content_end, line.end);
855 self.finish();
856 line_index + 1
857 }
858
859 fn emit_simple_line(&mut self, line_index: usize, kind: SyntaxKind) -> usize {
860 let line = self.lines[line_index];
861 self.start(kind);
862 self.token(SyntaxKind::MARKER, line.start, line.content_end);
863 self.token(SyntaxKind::NEWLINE, line.content_end, line.end);
864 self.finish();
865 line_index + 1
866 }
867
868 fn emit_paragraph(&mut self, line_index: usize) -> usize {
869 self.start(SyntaxKind::PARAGRAPH);
870 let mut current = line_index;
871 let mut inlines = Vec::new();
872 while current < self.lines.len() && !self.lines[current].is_blank(self.text) {
873 if current > line_index && self.is_breaking_block(current) {
874 break;
875 }
876 let line = self.lines[current];
877 inlines.push(InlineLine::new(line, line.start));
878 current += 1;
879 }
880 self.flush_inline_lines(&mut inlines, SyntaxKind::SOFT_BREAK);
881 self.finish();
882 current
883 }
884
885 fn is_breaking_block(&self, line: usize) -> bool {
886 let current = self.lines[line];
887 fence_open(current, self.text).is_some()
888 || atx_heading(current, self.text).is_some()
889 || block_quote_content(current, self.text).is_some()
890 || admonition_header(current, self.text).is_some()
891 || footnote_header(current, self.text).is_some()
892 || list_marker(current, self.text).is_some()
893 }
894
895 fn emit_trimmed_inlines(&mut self, line: Line) {
896 let raw = line.content(self.text);
897 let leading = raw.len() - raw.trim_start().len();
898 let trailing = raw.trim_end().len();
899 self.token(SyntaxKind::WHITESPACE, line.start, line.start + leading);
900 self.parse_inlines(line.start + leading, line.start + trailing);
901 self.token(
902 SyntaxKind::WHITESPACE,
903 line.start + trailing,
904 line.content_end,
905 );
906 }
907
908 fn parse_inlines(&mut self, start: usize, end: usize) {
909 InlineParser {
910 text: self.text,
911 source: self.text,
912 builder: &mut self.builder,
913 pieces: &[],
914 piece_index: 0,
915 literal_end: end,
916 }
917 .parse_inlines(start, end);
918 }
919
920 fn flush_inline_lines(&mut self, lines: &mut Vec<InlineLine>, final_break: SyntaxKind) {
921 let Some(last) = lines.last().copied() else {
922 return;
923 };
924 let last_hard_break = last.content_start < last.line.content_end
925 && self.text.as_bytes()[last.line.content_end - 1] == b'\\'
926 && last.line.content_end < last.line.end;
927 if lines.len() == 1 && !last_hard_break {
928 if let Some(marker) = last.quote_marker {
929 self.emit_quote_prefix(last.line, marker, last.content_start);
930 } else {
931 self.token(SyntaxKind::WHITESPACE, last.line.start, last.content_start);
932 }
933 self.parse_inlines(last.content_start, last.line.content_end);
934 self.token(final_break, last.line.content_end, last.line.end);
935 lines.clear();
936 return;
937 }
938 let mut text = String::new();
939 let mut pieces = Vec::new();
940 for (index, inline) in lines.iter().enumerate() {
941 let line = inline.line;
942 let mut prefix = |kind, start, end| {
943 if start < end {
944 pieces.push(InlinePiece {
945 logical: text.len()..text.len(),
946 source: start..end,
947 kind: InlinePieceKind::Prefix(kind),
948 });
949 }
950 };
951 if let Some(marker) = inline.quote_marker {
952 prefix(SyntaxKind::WHITESPACE, line.start, marker);
953 prefix(SyntaxKind::QUOTE_MARKER, marker, marker + 1);
954 prefix(SyntaxKind::WHITESPACE, marker + 1, inline.content_start);
955 } else {
956 prefix(SyntaxKind::WHITESPACE, line.start, inline.content_start);
957 }
958 if inline.content_start < line.content_end {
959 let start = text.len();
960 text.push_str(&self.text[inline.content_start..line.content_end]);
961 pieces.push(InlinePiece {
962 logical: start..text.len(),
963 source: inline.content_start..line.content_end,
964 kind: InlinePieceKind::Content,
965 });
966 }
967 let hard_break = inline.content_start < line.content_end
968 && self.text.as_bytes()[line.content_end - 1] == b'\\'
969 && line.content_end < line.end;
970 if index + 1 < lines.len() || hard_break {
971 let start = text.len();
972 text.push(if hard_break { '\n' } else { ' ' });
973 pieces.push(InlinePiece {
974 logical: start..text.len(),
975 source: line.content_end..line.end,
976 kind: if hard_break {
977 InlinePieceKind::HardBreak
978 } else {
979 InlinePieceKind::SoftBreak
980 },
981 });
982 }
983 }
984 let mut parser = InlineParser {
985 text: &text,
986 source: self.text,
987 builder: &mut self.builder,
988 pieces: &pieces,
989 piece_index: 0,
990 literal_end: text.find('\n').unwrap_or(text.len()),
991 };
992 parser.parse_inlines(0, text.len());
993 for piece in &pieces[parser.piece_index..] {
995 if let InlinePieceKind::Prefix(kind) = piece.kind {
996 self.token(kind, piece.source.start, piece.source.end);
997 }
998 }
999 if !last_hard_break {
1000 self.token(final_break, last.line.content_end, last.line.end);
1001 }
1002 lines.clear();
1003 }
1004
1005 fn start(&mut self, kind: SyntaxKind) {
1006 self.builder
1007 .start_node(DocumentationLanguage::kind_to_raw(kind));
1008 }
1009
1010 fn finish(&mut self) {
1011 self.builder.finish_node();
1012 }
1013
1014 fn token(&mut self, kind: SyntaxKind, start: usize, end: usize) {
1015 if start < end {
1016 self.builder.token(
1017 DocumentationLanguage::kind_to_raw(kind),
1018 &self.text[start..end],
1019 );
1020 }
1021 }
1022}
1023
1024struct InlineParser<'a, 'b> {
1027 text: &'a str,
1028 source: &'a str,
1029 builder: &'b mut GreenNodeBuilder<'static>,
1030 pieces: &'a [InlinePiece],
1031 piece_index: usize,
1032 literal_end: usize,
1033}
1034
1035impl InlineParser<'_, '_> {
1036 fn parse_inlines(&mut self, start: usize, end: usize) {
1037 let mut cursor = start;
1038 let mut text_start = start;
1039 while cursor < end {
1040 let byte = self.text.as_bytes()[cursor];
1041 let trigger = matches!(byte, b'\\' | b'!' | b'[' | b'<' | b'*' | b'_' | b'`' | b'$')
1042 || (byte == b'-' && cursor + 1 < end && self.text.as_bytes()[cursor + 1] == b'-');
1043 if !trigger {
1044 cursor += self.text[cursor..end].chars().next().unwrap().len_utf8();
1045 continue;
1046 }
1047
1048 self.token(SyntaxKind::TEXT, text_start, cursor);
1051 let opener = cursor;
1052 let parsed = match self.text.as_bytes()[cursor] {
1053 b'\\' => self.emit_escape(cursor, end),
1054 b'!' if cursor + 1 < end && self.text.as_bytes()[cursor + 1] == b'[' => {
1055 self.emit_image(cursor, end)
1056 }
1057 b'[' => self.emit_link_or_footnote(cursor, end),
1058 b'<' => self.emit_autolink(cursor, end),
1059 b'*' | b'_' => self.emit_emphasis(cursor, end),
1060 b'`' => self.emit_backticks(cursor, end),
1061 b'$' => self.emit_dollar(cursor, end),
1062 b'-' if cursor + 1 < end && self.text.as_bytes()[cursor + 1] == b'-' => {
1063 if cursor + 2 < end && self.text.as_bytes()[cursor + 2] == b'-' {
1064 Some((SyntaxKind::EM_DASH, cursor + 3))
1065 } else {
1066 Some((SyntaxKind::EN_DASH, cursor + 2))
1067 }
1068 }
1069 _ => None,
1070 };
1071 let Some((kind, next)) = parsed else {
1072 cursor += self.text[cursor..end].chars().next().unwrap().len_utf8();
1073 text_start = opener;
1074 continue;
1075 };
1076 if matches!(
1077 kind,
1078 SyntaxKind::ESCAPE | SyntaxKind::EN_DASH | SyntaxKind::EM_DASH
1079 ) {
1080 self.token(kind, cursor, next);
1081 }
1082 cursor = next;
1083 text_start = cursor;
1084 }
1085 self.token(SyntaxKind::TEXT, text_start, end);
1086 }
1087
1088 fn emit_escape(&mut self, start: usize, end: usize) -> Option<(SyntaxKind, usize)> {
1089 let next = start + 1;
1090 if next >= end {
1091 return None;
1092 }
1093 let ch = self.text[next..end].chars().next()?;
1094 if ch == '\n' {
1095 self.start(SyntaxKind::HARD_BREAK);
1096 self.token(SyntaxKind::ESCAPE, start, next);
1097 self.token(SyntaxKind::NEWLINE, next, next + 1);
1098 self.finish();
1099 return Some((SyntaxKind::HARD_BREAK, next + 1));
1100 }
1101 "\\`*_#+-.!{}[]()$"
1102 .contains(ch)
1103 .then_some((SyntaxKind::ESCAPE, next + ch.len_utf8()))
1104 }
1105
1106 fn emit_image(&mut self, start: usize, end: usize) -> Option<(SyntaxKind, usize)> {
1107 let end = self.literal_inline_end(start, end);
1108 let label_end = find_closing(self.text, start + 2, end, b'[', b']')?;
1109 let mut open = label_end + 1;
1110 while open < end && self.text.as_bytes()[open].is_ascii_whitespace() {
1111 open += 1;
1112 }
1113 if open >= end || self.text.as_bytes()[open] != b'(' {
1114 return None;
1115 }
1116 let destination_end = find_closing(self.text, open + 1, end, b'(', b')')?;
1117 self.start(SyntaxKind::IMAGE);
1118 self.token(SyntaxKind::MARKER, start, start + 2);
1119 self.literal_token(SyntaxKind::TEXT, start + 2, label_end);
1120 self.token(SyntaxKind::MARKER, label_end, open + 1);
1121 self.token(SyntaxKind::LINK_DESTINATION, open + 1, destination_end);
1122 self.token(SyntaxKind::MARKER, destination_end, destination_end + 1);
1123 self.finish();
1124 Some((SyntaxKind::IMAGE, destination_end + 1))
1125 }
1126
1127 fn emit_link_or_footnote(&mut self, start: usize, end: usize) -> Option<(SyntaxKind, usize)> {
1128 let end = self.literal_inline_end(start, end);
1129 if start + 2 < end && self.text.as_bytes()[start + 1] == b'^' {
1130 let close = self.text[start + 2..end].find(']')? + start + 2;
1131 if self.text[start + 2..close]
1132 .chars()
1133 .all(|ch| ch.is_alphanumeric() || ch == '_')
1134 {
1135 self.start(SyntaxKind::FOOTNOTE_REFERENCE);
1136 self.token(SyntaxKind::MARKER, start, start + 2);
1137 self.token(SyntaxKind::FOOTNOTE_LABEL, start + 2, close);
1138 self.token(SyntaxKind::MARKER, close, close + 1);
1139 self.finish();
1140 return Some((SyntaxKind::FOOTNOTE_REFERENCE, close + 1));
1141 }
1142 }
1143
1144 let label_end = find_closing(self.text, start + 1, end, b'[', b']')?;
1145 let open = label_end + 1;
1146 if open >= end || self.text.as_bytes()[open] != b'(' {
1147 return None;
1148 }
1149 let destination_end = find_closing(self.text, open + 1, end, b'(', b')')?;
1150 self.start(SyntaxKind::LINK);
1151 self.token(SyntaxKind::MARKER, start, start + 1);
1152 self.parse_inlines(start + 1, label_end);
1153 self.token(SyntaxKind::MARKER, label_end, open + 1);
1154 self.token(SyntaxKind::LINK_DESTINATION, open + 1, destination_end);
1155 self.token(SyntaxKind::MARKER, destination_end, destination_end + 1);
1156 self.finish();
1157 Some((SyntaxKind::LINK, destination_end + 1))
1158 }
1159
1160 fn emit_autolink(&mut self, start: usize, end: usize) -> Option<(SyntaxKind, usize)> {
1161 let end = self.literal_inline_end(start, end);
1162 let close = self.text[start + 1..end].find('>')? + start + 1;
1163 let destination = &self.text[start + 1..close];
1164 if !is_autolink(destination) {
1165 return None;
1166 }
1167 self.start(SyntaxKind::AUTOLINK);
1168 self.token(SyntaxKind::MARKER, start, start + 1);
1169 self.token(SyntaxKind::LINK_DESTINATION, start + 1, close);
1170 self.token(SyntaxKind::MARKER, close, close + 1);
1171 self.finish();
1172 Some((SyntaxKind::AUTOLINK, close + 1))
1173 }
1174
1175 fn emit_emphasis(&mut self, start: usize, end: usize) -> Option<(SyntaxKind, usize)> {
1176 let byte = self.text.as_bytes()[start];
1177 let length = if start + 1 < end && self.text.as_bytes()[start + 1] == byte {
1178 2
1179 } else {
1180 1
1181 };
1182 let delimiter = &self.text[start..start + length];
1183 let close = self.text[start + length..end].find(delimiter)? + start + length;
1184 if close == start + length {
1185 return None;
1186 }
1187 self.start(if length == 2 {
1188 SyntaxKind::STRONG
1189 } else {
1190 SyntaxKind::EMPHASIS
1191 });
1192 self.token(SyntaxKind::MARKER, start, start + length);
1193 self.parse_inlines(start + length, close);
1194 self.token(SyntaxKind::MARKER, close, close + length);
1195 self.finish();
1196 Some((SyntaxKind::EMPHASIS, close + length))
1197 }
1198
1199 fn emit_backticks(&mut self, start: usize, end: usize) -> Option<(SyntaxKind, usize)> {
1200 let end = self.literal_inline_end(start, end);
1201 let mut open_end = start;
1202 while open_end < end && self.text.as_bytes()[open_end] == b'`' {
1203 open_end += 1;
1204 }
1205 let delimiter = &self.text[start..open_end];
1206 let close = self.text[open_end..end].find(delimiter)? + open_end;
1207 let kind = if delimiter.len().is_multiple_of(2) {
1208 SyntaxKind::INLINE_MATH
1209 } else {
1210 SyntaxKind::INLINE_CODE
1211 };
1212 self.start(kind);
1213 self.token(SyntaxKind::MARKER, start, open_end);
1214 self.token(
1215 if kind == SyntaxKind::INLINE_MATH {
1216 SyntaxKind::MATH_CONTENT
1217 } else {
1218 SyntaxKind::CODE_CONTENT
1219 },
1220 open_end,
1221 close,
1222 );
1223 self.token(SyntaxKind::MARKER, close, close + delimiter.len());
1224 self.finish();
1225 Some((kind, close + delimiter.len()))
1226 }
1227
1228 fn emit_dollar(&mut self, start: usize, end: usize) -> Option<(SyntaxKind, usize)> {
1229 if let Some(relative) = self.text[start + 1..end].find('$') {
1230 let close = start + 1 + relative;
1231 self.start(SyntaxKind::INLINE_MATH);
1232 self.token(SyntaxKind::MARKER, start, start + 1);
1233 self.token(SyntaxKind::MATH_CONTENT, start + 1, close);
1234 self.token(SyntaxKind::MARKER, close, close + 1);
1235 self.finish();
1236 return Some((SyntaxKind::INLINE_MATH, close + 1));
1237 }
1238 let expression_end = interpolation_end(self.text, start, end)?;
1239 self.start(SyntaxKind::INTERPOLATION);
1240 self.token(SyntaxKind::MARKER, start, start + 1);
1241 self.literal_token(SyntaxKind::TEXT, start + 1, expression_end);
1242 self.finish();
1243 Some((SyntaxKind::INTERPOLATION, expression_end))
1244 }
1245
1246 fn literal_inline_end(&mut self, start: usize, end: usize) -> usize {
1247 if start > self.literal_end {
1250 self.literal_end = self.text[start..]
1251 .find('\n')
1252 .map_or(self.text.len(), |offset| start + offset);
1253 }
1254 end.min(self.literal_end)
1255 }
1256
1257 fn start(&mut self, kind: SyntaxKind) {
1258 while let Some(piece) = self.pieces.get(self.piece_index)
1261 && let InlinePieceKind::Prefix(kind) = piece.kind
1262 {
1263 self.builder.token(
1264 DocumentationLanguage::kind_to_raw(kind),
1265 &self.source[piece.source.clone()],
1266 );
1267 self.piece_index += 1;
1268 }
1269 self.builder
1270 .start_node(DocumentationLanguage::kind_to_raw(kind));
1271 }
1272
1273 fn finish(&mut self) {
1274 self.builder.finish_node();
1275 }
1276
1277 fn token(&mut self, kind: SyntaxKind, start: usize, end: usize) {
1278 let soft_break = if kind == SyntaxKind::TEXT {
1279 SyntaxKind::SOFT_BREAK
1280 } else {
1281 kind
1282 };
1283 self.mapped_token(kind, start, end, soft_break);
1284 }
1285
1286 fn literal_token(&mut self, kind: SyntaxKind, start: usize, end: usize) {
1287 self.mapped_token(kind, start, end, kind);
1288 }
1289
1290 fn mapped_token(&mut self, kind: SyntaxKind, start: usize, end: usize, soft_break: SyntaxKind) {
1291 if start == end {
1292 return;
1293 }
1294 if self.pieces.is_empty() {
1295 self.builder.token(
1296 DocumentationLanguage::kind_to_raw(kind),
1297 &self.source[start..end],
1298 );
1299 return;
1300 }
1301 let mut cursor = start;
1302 while cursor < end {
1303 let piece = &self.pieces[self.piece_index];
1304 let (source_start, source_end, token_kind, next) = match piece.kind {
1305 InlinePieceKind::Content => {
1306 let next = end.min(piece.logical.end);
1307 (
1308 piece.source.start + cursor - piece.logical.start,
1309 piece.source.start + next - piece.logical.start,
1310 kind,
1311 next,
1312 )
1313 }
1314 InlinePieceKind::SoftBreak => {
1315 (piece.source.start, piece.source.end, soft_break, cursor + 1)
1316 }
1317 InlinePieceKind::HardBreak => (
1318 piece.source.start,
1319 piece.source.end,
1320 SyntaxKind::NEWLINE,
1321 cursor + 1,
1322 ),
1323 InlinePieceKind::Prefix(prefix_kind) => {
1324 (piece.source.start, piece.source.end, prefix_kind, cursor)
1325 }
1326 };
1327 self.builder.token(
1328 DocumentationLanguage::kind_to_raw(token_kind),
1329 &self.source[source_start..source_end],
1330 );
1331 cursor = next;
1332 if cursor == piece.logical.end {
1333 self.piece_index += 1;
1334 }
1335 }
1336 }
1337}
1338
1339fn lines(text: &str) -> Vec<Line> {
1340 let mut out = Vec::new();
1341 let mut start = 0;
1342 while start < text.len() {
1343 let Some(relative) = text[start..].find('\n') else {
1344 out.push(Line {
1345 start,
1346 content_end: text.len(),
1347 end: text.len(),
1348 });
1349 break;
1350 };
1351 let newline = start + relative;
1352 let content_end = if newline > start && text.as_bytes()[newline - 1] == b'\r' {
1353 newline - 1
1354 } else {
1355 newline
1356 };
1357 out.push(Line {
1358 start,
1359 content_end,
1360 end: newline + 1,
1361 });
1362 start = newline + 1;
1363 }
1364 out
1365}
1366
1367fn indentation(text: &str) -> usize {
1368 text.as_bytes()
1369 .iter()
1370 .take_while(|&&byte| byte == b' ')
1371 .count()
1372}
1373
1374fn indented_content_start(line: Line, text: &str) -> Option<usize> {
1375 indented_content_start_after(line, text, 0)
1376}
1377
1378fn indented_content_start_after(line: Line, text: &str, prefix: usize) -> Option<usize> {
1379 let content = line.content(text);
1380 if content.len() < prefix
1381 || !content.as_bytes()[..prefix]
1382 .iter()
1383 .all(|&byte| byte == b' ')
1384 {
1385 return None;
1386 }
1387 let tail = &content[prefix..];
1388 if tail.starts_with('\t') {
1389 Some(line.start + prefix + 1)
1390 } else {
1391 (indentation(tail) >= 4).then_some(line.start + prefix + 4)
1392 }
1393}
1394
1395fn fence_open(line: Line, text: &str) -> Option<FenceOpen> {
1396 let content = line.content(text);
1397 let indent = indentation(content);
1398 if indent > 3 {
1399 return None;
1400 }
1401 let marker = *content.as_bytes().get(indent)?;
1402 if marker != b'`' && marker != b'~' {
1403 return None;
1404 }
1405 let mut end = indent;
1406 while end < content.len() && content.as_bytes()[end] == marker {
1407 end += 1;
1408 }
1409 if end - indent < 3 || content.as_bytes()[end..].contains(&marker) {
1410 return None;
1411 }
1412 Some(FenceOpen {
1413 marker_start: line.start + indent,
1414 marker_end: line.start + end,
1415 marker,
1416 length: end - indent,
1417 })
1418}
1419
1420fn atx_heading(line: Line, text: &str) -> Option<(usize, usize, usize, usize)> {
1421 let content = line.content(text);
1422 let indent = indentation(content);
1423 if indent > 3 {
1424 return None;
1425 }
1426 let bytes = content.as_bytes();
1427 let mut marker_end = indent;
1428 while marker_end < bytes.len() && bytes[marker_end] == b'#' {
1429 marker_end += 1;
1430 }
1431 let level = marker_end - indent;
1432 if !(1..=6).contains(&level)
1433 || (marker_end < bytes.len() && !bytes[marker_end].is_ascii_whitespace())
1434 {
1435 return None;
1436 }
1437 let mut content_start = marker_end;
1438 while content_start < bytes.len() && matches!(bytes[content_start], b' ' | b'\t') {
1439 content_start += 1;
1440 }
1441 let mut content_end = content.len();
1442 while content_end > content_start && matches!(bytes[content_end - 1], b' ' | b'\t') {
1443 content_end -= 1;
1444 }
1445 let trailing_end = content_end;
1446 while content_end > content_start && bytes[content_end - 1] == b'#' {
1447 content_end -= 1;
1448 }
1449 if content_end < trailing_end
1450 && content_end > content_start
1451 && bytes[content_end - 1].is_ascii_whitespace()
1452 {
1453 while content_end > content_start && bytes[content_end - 1].is_ascii_whitespace() {
1454 content_end -= 1;
1455 }
1456 } else {
1457 content_end = trailing_end;
1458 }
1459 Some((
1460 line.start + indent,
1461 line.start + marker_end,
1462 line.start + content_start,
1463 line.start + content_end,
1464 ))
1465}
1466
1467fn setext_level(line: Line, text: &str) -> Option<u8> {
1468 let trimmed = line.content(text).trim_matches([' ', '\t']);
1469 if trimmed.len() < 3 {
1470 return None;
1471 }
1472 let marker = trimmed.as_bytes()[0];
1473 if !matches!(marker, b'=' | b'-') || !trimmed.bytes().all(|byte| byte == marker) {
1474 return None;
1475 }
1476 Some(if marker == b'=' { 1 } else { 2 })
1477}
1478
1479fn block_quote_content(line: Line, text: &str) -> Option<(usize, usize)> {
1480 let content = line.content(text);
1481 let indent = indentation(content);
1482 if indent > 3 || content.as_bytes().get(indent) != Some(&b'>') {
1483 return None;
1484 }
1485 let marker = line.start + indent;
1486 let content = if text.as_bytes().get(marker + 1) == Some(&b' ') {
1487 marker + 2
1488 } else {
1489 marker + 1
1490 };
1491 Some((marker, content))
1492}
1493
1494fn admonition_header(
1495 line: Line,
1496 text: &str,
1497) -> Option<(
1498 usize,
1499 std::ops::Range<usize>,
1500 Option<std::ops::Range<usize>>,
1501)> {
1502 let content = line.content(text);
1503 let indent = indentation(content);
1504 if indent > 3 || !content[indent..].starts_with("!!! ") {
1505 return None;
1506 }
1507 let marker_start = line.start + indent;
1508 let rest_start = marker_start + 4;
1509 let rest = &text[rest_start..line.content_end];
1510 let category_len = rest
1511 .bytes()
1512 .take_while(|byte| byte.is_ascii_lowercase())
1513 .count();
1514 if category_len == 0 {
1515 return None;
1516 }
1517 let category = rest_start..rest_start + category_len;
1518 let tail = text[category.end..line.content_end].trim();
1519 let title = if tail.is_empty() {
1520 None
1521 } else if tail.starts_with('"') && tail.ends_with('"') && tail.len() >= 2 {
1522 let offset = text[category.end..line.content_end].find('"')?;
1525 Some(category.end + offset..category.end + offset + tail.len())
1526 } else {
1527 return None;
1528 };
1529 Some((marker_start, category, title))
1530}
1531
1532fn footnote_header(line: Line, text: &str) -> Option<(usize, usize, usize)> {
1533 let content = line.content(text);
1534 let indent = indentation(content);
1535 if indent > 3 || !content[indent..].starts_with("[^") {
1536 return None;
1537 }
1538 let label_start = line.start + indent;
1539 let close = text[label_start + 2..line.content_end].find("]:")? + label_start + 2;
1540 if !text[label_start + 2..close]
1541 .chars()
1542 .all(|ch| ch.is_alphanumeric() || ch == '_')
1543 {
1544 return None;
1545 }
1546 let label_end = close + 2;
1547 let mut content_start = label_end;
1548 while content_start < line.content_end && text.as_bytes()[content_start].is_ascii_whitespace() {
1549 content_start += 1;
1550 }
1551 Some((label_start, label_end, content_start))
1552}
1553
1554fn list_marker(line: Line, text: &str) -> Option<(usize, usize, usize)> {
1555 let marker = list_marker_any(line, text)?;
1556 (marker.0 - line.start <= 3).then_some(marker)
1557}
1558
1559fn list_marker_any(line: Line, text: &str) -> Option<(usize, usize, usize)> {
1560 list_marker_range(text, line.start, line.content_end)
1561}
1562
1563fn list_marker_range(text: &str, start: usize, end_offset: usize) -> Option<(usize, usize, usize)> {
1564 let content = &text[start..end_offset];
1565 let indent = indentation(content);
1566 let bytes = content.as_bytes();
1567 let mut end = indent;
1568 if matches!(bytes.get(end), Some(b'*' | b'+' | b'-')) {
1569 end += 1;
1570 } else {
1571 while end < bytes.len() && bytes[end].is_ascii_digit() {
1572 end += 1;
1573 }
1574 if end == indent || !matches!(bytes.get(end), Some(b'.' | b')')) {
1575 return None;
1576 }
1577 end += 1;
1578 }
1579 if end < bytes.len() && !bytes[end].is_ascii_whitespace() {
1580 return None;
1581 }
1582 let mut content_start = end;
1583 while content_start < bytes.len() && bytes[content_start].is_ascii_whitespace() {
1584 content_start += 1;
1585 }
1586 Some((start + indent, start + end, start + content_start))
1587}
1588
1589fn thematic_break(content: &str) -> bool {
1590 let mut marker = None;
1591 let mut count = 0;
1592 for byte in content.bytes() {
1593 if byte.is_ascii_whitespace() {
1594 continue;
1595 }
1596 if !matches!(byte, b'*' | b'-') {
1597 return false;
1598 }
1599 if marker.is_some_and(|current| current != byte) {
1600 return false;
1601 }
1602 marker = Some(byte);
1603 count += 1;
1604 }
1605 count >= 3
1606}
1607
1608fn math_block(line: Line, text: &str) -> Option<(usize, usize, usize, usize)> {
1609 let raw = line.content(text);
1610 let trimmed = raw.trim();
1611 if trimmed.len() < 2 || !trimmed.starts_with('$') || !trimmed.ends_with('$') {
1612 return None;
1613 }
1614 let open = line.start + raw.len() - raw.trim_start().len();
1615 let close = open + trimmed.len();
1616 Some((open, open + 1, close - 1, close))
1617}
1618
1619fn block_interpolation(line: Line, text: &str) -> Option<usize> {
1620 if text.as_bytes().get(line.start) != Some(&b'$') {
1621 return None;
1622 }
1623 let end = interpolation_end(text, line.start, line.content_end)?;
1624 text[end..line.content_end]
1625 .trim_matches([' ', '\t'])
1626 .is_empty()
1627 .then_some(end)
1628}
1629
1630fn contains_unescaped_pipe(text: &str) -> bool {
1631 text.as_bytes()
1632 .iter()
1633 .enumerate()
1634 .any(|(index, &byte)| byte == b'|' && (index == 0 || text.as_bytes()[index - 1] != b'\\'))
1635}
1636
1637fn table_alignment(text: &str) -> bool {
1638 let trimmed = text.trim();
1639 let trimmed = trimmed.strip_prefix('|').unwrap_or(trimmed);
1640 let trimmed = trimmed.strip_suffix('|').unwrap_or(trimmed);
1641 let mut cells = 0;
1642 for cell in trimmed.split('|') {
1643 let cell = cell.trim();
1644 if cell.len() < 3 || !cell.bytes().all(|byte| matches!(byte, b'-' | b':')) {
1645 return false;
1646 }
1647 cells += 1;
1648 }
1649 cells > 0
1650}
1651
1652fn find_closing(text: &str, start: usize, end: usize, open: u8, close: u8) -> Option<usize> {
1653 let mut depth = 0usize;
1654 let mut cursor = start;
1655 while cursor < end {
1656 let byte = text.as_bytes()[cursor];
1657 if byte == b'\\' {
1658 cursor += 1;
1659 if cursor < end {
1660 cursor += text[cursor..end].chars().next()?.len_utf8();
1661 }
1662 continue;
1663 }
1664 if byte == open {
1665 depth += 1;
1666 } else if byte == close {
1667 if depth == 0 {
1668 return Some(cursor);
1669 }
1670 depth -= 1;
1671 }
1672 cursor += text[cursor..end].chars().next()?.len_utf8();
1673 }
1674 None
1675}
1676
1677fn interpolation_end(text: &str, start: usize, end: usize) -> Option<usize> {
1678 let first = start + 1;
1679 if first >= end || text.as_bytes()[first].is_ascii_whitespace() {
1680 return None;
1681 }
1682 if text.as_bytes()[first] == b'(' {
1683 return find_closing(text, first + 1, end, b'(', b')').map(|close| close + 1);
1684 }
1685 let mut cursor = first;
1686 let mut saw_name = false;
1687 while cursor < end {
1688 let ch = text[cursor..end].chars().next()?;
1689 if ch.is_alphanumeric() || ch == '_' || ch == '!' || ch == '?' || !ch.is_ascii() {
1690 saw_name = true;
1691 cursor += ch.len_utf8();
1692 } else if ch == '.' {
1693 let after_dot = cursor + 1;
1694 let Some(next) = text[after_dot..end].chars().next() else {
1695 break;
1696 };
1697 if next.is_alphanumeric() || next == '_' || !next.is_ascii() {
1698 cursor = after_dot;
1699 } else {
1700 break;
1701 }
1702 } else {
1703 break;
1704 }
1705 }
1706 if saw_name && cursor < end && text.as_bytes()[cursor] == b'(' {
1707 cursor = find_closing(text, cursor + 1, end, b'(', b')')? + 1;
1708 }
1709 saw_name.then_some(cursor)
1710}
1711
1712fn is_autolink(text: &str) -> bool {
1713 text.contains("://")
1714 || text.starts_with("mailto:")
1715 || (text.contains('@') && !text.chars().any(char::is_whitespace))
1716}
1717
1718fn range(start: usize, end: usize) -> TextRange {
1719 TextRange::new(TextSize::from(start as u32), TextSize::from(end as u32))
1720}