Skip to content

Commit

Permalink
fix a crash when parsing alert block syntax (#593)
Browse files Browse the repository at this point in the history
* fix a crash when parsing alert block syntax

* update version.dart

* Remove the reference to 'parser.lines' in canParse().
  • Loading branch information
devoncarew authored Mar 7, 2024
1 parent dd47c5d commit 1ca5166
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 7.2.2-wip
## 7.2.2

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

## 7.2.1
Expand Down
11 changes: 5 additions & 6 deletions lib/src/block_syntaxes/alert_block_syntax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class AlertBlockSyntax extends BlockSyntax {

@override
bool canParse(BlockParser parser) {
return pattern.hasMatch(parser.current.content) &&
parser.lines.any((line) => _contentLineRegExp.hasMatch(line.content));
return alertPattern.hasMatch(parser.current.content);
}

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

while (!parser.isDone) {
final strippedContent =
parser.current.content.replaceFirst(RegExp(r'^\s*>?\s*'), '');
final match = strippedContent.isEmpty
final lineContent = parser.current.content.trimLeft();
final strippedContent = lineContent.replaceFirst(RegExp(r'^>?\s*'), '');
final match = strippedContent.isEmpty && !lineContent.startsWith('>')
? null
: _contentLineRegExp.firstMatch(strippedContent);
if (match != null) {
Expand All @@ -51,7 +50,7 @@ class AlertBlockSyntax extends BlockSyntax {
continue;
}

final lastLine = childLines.last;
final lastLine = childLines.isEmpty ? Line('') : childLines.last;

// A paragraph continuation is OK. This is content that cannot be parsed
// as any other syntax except Paragraph, and it doesn't match the bar in
Expand Down
6 changes: 3 additions & 3 deletions lib/src/patterns.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ final htmlCharactersPattern = RegExp(
final linkReferenceDefinitionPattern = RegExp(r'^[ ]{0,3}\[');

/// Alert type patterns.
/// A alert block is similar to a blockquote,
/// starts with `> [!TYPE]`, and only 5 types are supported
/// with case-insensitive.
///
/// A alert block is similar to a blockquote, starts with `> [!TYPE]`, and only
/// 5 types are supported (case-insensitive).
final alertPattern = RegExp(
r'^\s{0,3}>\s{0,3}\\?\[!(note|tip|important|caution|warning)\\?\]\s*$',
caseSensitive: false,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: markdown
version: 7.2.2-wip

version: 7.2.2
description: >-
A portable Markdown library written in Dart that can parse Markdown into HTML.
repository: https://github.com/dart-lang/markdown

topics:
- markdown

Expand Down
31 changes: 31 additions & 0 deletions test/extensions/alert_extension.unit
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,34 @@ Additional markdown text.
with two lines.</p>
</div>
<p>Additional markdown text.</p>
>>> supports continuation lines
> [!note]
> A sample note
with two lines.

Additional markdown text.
<<<
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>A sample note
with two lines.</p>
</div>
<p>Additional markdown text.</p>
>>> crash repro #584.1
> [!Warning]
>
> Some extensions won't work on dynamic types.
<<<
<div class="markdown-alert markdown-alert-warning">
<p class="markdown-alert-title">Warning</p>
<p>Some extensions won't work on dynamic types.</p>
</div>
>>> crash repro #584.2
> [!NOTE]
>
> if you receive the following error:
<<<
<div class="markdown-alert markdown-alert-note">
<p class="markdown-alert-title">Note</p>
<p>if you receive the following error:</p>
</div>

0 comments on commit 1ca5166

Please sign in to comment.