Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash in GFMAlerts when falling back to blockquote #50

Merged
merged 1 commit into from
Dec 9, 2024

Conversation

mcdurdin
Copy link
Member

@mcdurdin mcdurdin commented Dec 9, 2024

The code paths when a normal blockquote was encountered were not quite right. Also tidied up a few other bits and pieces for robustness.

Fixes: #49
Fixes: HELP-KEYMAN-COM-DH8

@mcdurdin mcdurdin added this to the A18S17 milestone Dec 9, 2024
@mcdurdin mcdurdin added the fix label Dec 9, 2024
Copy link
Member Author

@mcdurdin mcdurdin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
$this->BlockTypes['>'][] = 'Alert';
}
private $icon = array(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the icons into a private property so that they weren't being created again each call to blockQuote()


public function __construct()
{
$this->BlockTypes['>'][] = 'Alert';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was overwriting the default blockQuote definition, unnecessarily, requiring a blockAlert function. Instead, we just override blockQuote() and add handling when an alert pattern is found.

Comment on lines 29 to +31
if(!preg_match('/^\>\s*\[\!(IMPORTANT|TIP|NOTE|WARNING|CAUTION)\]/i', $line['text'], $matches)) {
return parent::blockQuote($line);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the string is not an alert pattern, then just fall back to the default blockQuote handler

Comment on lines +45 to +46
// We set isAlert to allow subsequent lines to be part of the alert
'isAlert' => true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what was needed to allow us to track alert styling for blockQuoteContinue calls

if(!preg_match('/^\>\s(.*)/', $line['text'], $matches)) {
return null;
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't clear to me if we should be return null or just return, because both seem to be used in Parsedown. However, blockQuoteContinue parent handler just does a return for these kinds of cases, so copied that

return $block;
}

protected function blockQuoteComplete($block) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was not needed, because it did nothing

The code paths when a normal blockquote was encountered were not quite
right. Also tidied up a few other bits and pieces for robustness.

Fixes: #49
Fixes: HELP-KEYMAN-COM-DH8
@mcdurdin mcdurdin force-pushed the fix/49-alerts-crashing-with-blockquote branch from 3d97cf1 to 9a20ed4 Compare December 9, 2024 08:31
@Meng-Heng
Copy link
Collaborator

I'm doing a test on localhost at the moment. I will be back with more info.

@Meng-Heng
Copy link
Collaborator

LGTM 😄

A minor thing I found, @mcdurdin. I'm using two > together and it is only showing the last >. Would this cause any error, or would be okay to leave it? I have not found how to fetch that yet, just this for now.

if(preg_match('/^\>\s(.*)/', $line['text'], $matches)) {
            if(!isset($block['isAlert'])) {
                return;
            }
        }

Test:

> [!IMPORTANT]
> This is important.
> Another important line.

> [!NOTE] 
> Keyman Facts.

> [!TIP] 
> Keyboard layouts are defined with a clear and easy to understand keyboard grammar.

> [!CAUTION] 
> Keyboard layouts are distributed through an open catalog to all major desktop and mobile platforms.
Continue your Markdown documentation.
> [!WARNING] 
> This is the last GFMAlerts.

> [!TIP] 
> [!IMPORTANT]
Paragraph

# Heading

* This is the last GFMAlerts

> This one won't show                    // if two `>` are together like this, the top one won't show? 
> Paragraph

Result:
image

}

// Handles any interruption
if(isset($block['interrupted'])) {
return $block;
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good cleanup.
I didn't see where 'interrupted' ever gets set

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a clone of the Parsedown code -- interrupted is set by Parsedown

@mcdurdin
Copy link
Member Author

mcdurdin commented Dec 9, 2024

A minor thing I found, @mcdurdin. I'm using two > together and it is only showing the last >. Would this cause any error, or would be okay to leave it? I have not found how to fetch that yet, just this for now.

Here's what I am seeing (top is cropped off):

image

@mcdurdin
Copy link
Member Author

mcdurdin commented Dec 9, 2024

and this:

# Heading

* This is the last GFMAlerts

> This one won't show
>
> Paragraph

gives this:

image

Copy link
Contributor

@darcywong00 darcywong00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@mcdurdin mcdurdin merged commit f6374e7 into main Dec 9, 2024
1 check passed
@mcdurdin mcdurdin deleted the fix/49-alerts-crashing-with-blockquote branch December 9, 2024 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

bug: ErrorException: Warning: Invalid argument supplied for foreach() - in GFMAlerts handler
3 participants