Skip to content

Commit 6ead60a

Browse files
authored
fix a crash when parsing alert block syntax (dart-archive/markdown#593)
* fix a crash when parsing alert block syntax * update version.dart * Remove the reference to 'parser.lines' in canParse().
1 parent d6a07ba commit 6ead60a

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

pkgs/markdown/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## 7.2.2-wip
1+
## 7.2.2
22

3+
* Fix a crash parsing alert block syntax (#584).
4+
* Have alert block syntax support multiple paragraphs (#577).
35
* Require Dart `^3.2.0`.
46

57
## 7.2.1

pkgs/markdown/lib/src/block_syntaxes/alert_block_syntax.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class AlertBlockSyntax extends BlockSyntax {
2121

2222
@override
2323
bool canParse(BlockParser parser) {
24-
return pattern.hasMatch(parser.current.content) &&
25-
parser.lines.any((line) => _contentLineRegExp.hasMatch(line.content));
24+
return alertPattern.hasMatch(parser.current.content);
2625
}
2726

2827
/// Whether this alert ends with a lazy continuation line.
@@ -39,9 +38,9 @@ class AlertBlockSyntax extends BlockSyntax {
3938
_lazyContinuation = false;
4039

4140
while (!parser.isDone) {
42-
final strippedContent =
43-
parser.current.content.replaceFirst(RegExp(r'^\s*>?\s*'), '');
44-
final match = strippedContent.isEmpty
41+
final lineContent = parser.current.content.trimLeft();
42+
final strippedContent = lineContent.replaceFirst(RegExp(r'^>?\s*'), '');
43+
final match = strippedContent.isEmpty && !lineContent.startsWith('>')
4544
? null
4645
: _contentLineRegExp.firstMatch(strippedContent);
4746
if (match != null) {
@@ -51,7 +50,7 @@ class AlertBlockSyntax extends BlockSyntax {
5150
continue;
5251
}
5352

54-
final lastLine = childLines.last;
53+
final lastLine = childLines.isEmpty ? Line('') : childLines.last;
5554

5655
// A paragraph continuation is OK. This is content that cannot be parsed
5756
// as any other syntax except Paragraph, and it doesn't match the bar in

pkgs/markdown/lib/src/patterns.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ final htmlCharactersPattern = RegExp(
153153
final linkReferenceDefinitionPattern = RegExp(r'^[ ]{0,3}\[');
154154

155155
/// Alert type patterns.
156-
/// A alert block is similar to a blockquote,
157-
/// starts with `> [!TYPE]`, and only 5 types are supported
158-
/// with case-insensitive.
156+
///
157+
/// A alert block is similar to a blockquote, starts with `> [!TYPE]`, and only
158+
/// 5 types are supported (case-insensitive).
159159
final alertPattern = RegExp(
160160
r'^\s{0,3}>\s{0,3}\\?\[!(note|tip|important|caution|warning)\\?\]\s*$',
161161
caseSensitive: false,

pkgs/markdown/lib/src/version.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/markdown/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: markdown
2-
version: 7.2.2-wip
3-
2+
version: 7.2.2
43
description: >-
54
A portable Markdown library written in Dart that can parse Markdown into HTML.
65
repository: https://github.com/dart-lang/markdown
6+
77
topics:
88
- markdown
99

pkgs/markdown/test/extensions/alert_extension.unit

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,34 @@ Additional markdown text.
129129
with two lines.</p>
130130
</div>
131131
<p>Additional markdown text.</p>
132+
>>> supports continuation lines
133+
> [!note]
134+
> A sample note
135+
with two lines.
136+
137+
Additional markdown text.
138+
<<<
139+
<div class="markdown-alert markdown-alert-note">
140+
<p class="markdown-alert-title">Note</p>
141+
<p>A sample note
142+
with two lines.</p>
143+
</div>
144+
<p>Additional markdown text.</p>
145+
>>> crash repro #584.1
146+
> [!Warning]
147+
>
148+
> Some extensions won't work on dynamic types.
149+
<<<
150+
<div class="markdown-alert markdown-alert-warning">
151+
<p class="markdown-alert-title">Warning</p>
152+
<p>Some extensions won't work on dynamic types.</p>
153+
</div>
154+
>>> crash repro #584.2
155+
> [!NOTE]
156+
>
157+
> if you receive the following error:
158+
<<<
159+
<div class="markdown-alert markdown-alert-note">
160+
<p class="markdown-alert-title">Note</p>
161+
<p>if you receive the following error:</p>
162+
</div>

0 commit comments

Comments
 (0)