Skip to content

Commit 1ccdd87

Browse files
committed
Enable GitLab multiline alerts
1 parent 428ad9a commit 1ccdd87

File tree

7 files changed

+552
-72
lines changed

7 files changed

+552
-72
lines changed

script/cibuild

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/descript
5252
|| failed=1
5353
python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/alerts.md "$PROGRAM_ARG -e alerts" \
5454
|| failed=1
55+
python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/multiline_alerts.md "$PROGRAM_ARG -e alerts -e multiline-block-quotes" \
56+
|| failed=1
5557

5658
python3 spec_tests.py --no-normalize --spec regression.txt "$PROGRAM_ARG" \
5759
|| failed=1

src/parser/alert.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ pub struct NodeAlert {
99

1010
/// Originated from a multiline blockquote.
1111
pub multiline: bool,
12+
13+
/// The length of the fence (multiline only).
14+
pub fence_length: usize,
15+
16+
/// The indentation level of the fence marker (multiline only)
17+
pub fence_offset: usize,
1218
}
1319

1420
/// The type of alert.

src/parser/mod.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,17 @@ where
15251525
return (false, container, should_continue);
15261526
}
15271527
}
1528-
NodeValue::Alert(..) => {
1529-
if !self.parse_block_quote_prefix(line) {
1528+
NodeValue::Alert(ref alert) => {
1529+
if alert.multiline {
1530+
if !self.parse_multiline_block_quote_prefix(
1531+
line,
1532+
container,
1533+
ast,
1534+
&mut should_continue,
1535+
) {
1536+
return (false, container, should_continue);
1537+
}
1538+
} else if !self.parse_block_quote_prefix(line) {
15301539
return (false, container, should_continue);
15311540
}
15321541
}
@@ -2033,20 +2042,32 @@ where
20332042

20342043
let alert_startpos = self.first_nonspace;
20352044
let mut title_startpos = self.first_nonspace;
2045+
let mut fence_length = 0;
20362046

20372047
while line[title_startpos] != b']' {
2048+
if line[title_startpos] == b'>' {
2049+
fence_length += 1
2050+
}
20382051
title_startpos += 1;
20392052
}
20402053
title_startpos += 1;
20412054

2055+
if fence_length == 2
2056+
|| (fence_length >= 3 && !self.options.extension.multiline_block_quotes)
2057+
{
2058+
return false;
2059+
}
2060+
20422061
// anything remaining on this line is considered an alert title
20432062
let mut tmp = entity::unescape_html(&line[title_startpos..]);
20442063
strings::trim(&mut tmp);
20452064
strings::unescape(&mut tmp);
20462065

20472066
let na = NodeAlert {
20482067
alert_type,
2049-
multiline: false,
2068+
multiline: fence_length >= 3,
2069+
fence_length,
2070+
fence_offset: self.first_nonspace - self.offset,
20502071
title: if tmp.is_empty() {
20512072
None
20522073
} else {
@@ -2077,8 +2098,8 @@ where
20772098
self.find_first_nonspace(line);
20782099
let indented = self.indent >= CODE_INDENT;
20792100

2080-
if self.handle_multiline_blockquote(container, line, indented, &mut matched)
2081-
|| self.handle_alert(container, line, indented)
2101+
if self.handle_alert(container, line, indented)
2102+
|| self.handle_multiline_blockquote(container, line, indented, &mut matched)
20822103
|| self.handle_blockquote(container, line, indented)
20832104
|| self.handle_atx_heading(container, line, indented, &mut matched)
20842105
|| self.handle_code_fence(container, line, indented, &mut matched)
@@ -2397,6 +2418,7 @@ where
23972418
NodeValue::MultilineBlockQuote(ref node_value) => {
23982419
(node_value.fence_length, node_value.fence_offset)
23992420
}
2421+
NodeValue::Alert(ref node_value) => (node_value.fence_length, node_value.fence_offset),
24002422
_ => unreachable!(),
24012423
};
24022424

src/scanners.re

+6-5
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,13 @@ pub fn alert_start(s: &[u8]) -> Option<AlertType> {
126126
let mut cursor = 0;
127127
let mut marker = 0;
128128
let len = s.len();
129+
129130
/*!re2c
130-
'> [!note]' { return Some(AlertType::Note); }
131-
'> [!tip]' { return Some(AlertType::Tip); }
132-
'> [!important]' { return Some(AlertType::Important); }
133-
'> [!warning]' { return Some(AlertType::Warning); }
134-
'> [!caution]' { return Some(AlertType::Caution); }
131+
[>]{1,} ' [!note]' { return Some(AlertType::Note); }
132+
[>]{1,} ' [!tip]' { return Some(AlertType::Tip); }
133+
[>]{1,} ' [!important]' { return Some(AlertType::Important); }
134+
[>]{1,} ' [!warning]' { return Some(AlertType::Warning); }
135+
[>]{1,} ' [!caution]' { return Some(AlertType::Caution); }
135136
* { return None; }
136137
*/
137138
}

0 commit comments

Comments
 (0)