-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Alerts: how to make it recognizes the next line of Markdown Alerts? #884
Comments
I'd love something like this too |
It would be someting like this. protected function blockAlert($Line): ?array
{
// List alert types from the config (e.g., 'NOTE', 'WARNING')
$alertTypes = ['NOTE', 'WARNING'];
// Build the regex pattern dynamically based on the alert types
$alertTypesPattern = implode('|', array_map('strtoupper', $alertTypes));
// Create the full regex pattern for matching alert block syntax
$pattern = '/^> \[!(' . $alertTypesPattern . ')\]/';
// Check if the line matches the alert pattern
if (preg_match($pattern, $Line['text'], $matches)) {
$type = strtolower($matches[1]); // Extract the alert type and convert to lowercase
$title = ucfirst($type); // Capitalize the first letter for the alert title
// Get class name for alerts from the configuration
$class = 'markdown-alert';
// Build the alert block with appropriate HTML attributes and content
$Block = [
'element' => [
'name' => 'div',
'attributes' => [
'class' => "{$class} {$class}-{$type}", // Add alert type as a class (e.g., 'alert alert-note')
],
'handler' => 'elements', // Use 'elements' because we'll be adding more content elements later
'text' => [
[
'name' => 'p',
'attributes' => [
'class' => "{$class}-title", // Assign title-specific class for the alert
],
'text' => $title, // Set the alert title (e.g., "Note")
],
],
],
];
return $Block; // Return the parsed alert block
}
return null; // Return null if the line does not match the alert pattern
}
protected function blockAlertContinue($Line, array $Block)
{
// List alert types from the config (e.g., 'NOTE', 'WARNING')
$alertTypes = ['NOTE', 'WARNING'];
// Build the regex pattern dynamically based on the alert types
$alertTypesPattern = implode('|', array_map('strtoupper', $alertTypes));
// Create the full regex pattern for identifying new alert blocks
$pattern = '/^> \[!(' . $alertTypesPattern . ')\]/';
// If the line matches a new alert block, terminate the current one
if (preg_match($pattern, $Line['text'])) {
return null; // Return null to terminate the current alert block
}
// Check if the line continues the current alert block with '>' followed by content
if ($Line['text'][0] === '>' && preg_match('/^> ?(.*)/', $Line['text'], $matches)) {
// If the block was interrupted, add an empty paragraph for spacing
if (isset($Block['interrupted'])) {
$Block['element']['text'][] = ['text' => ''];
unset($Block['interrupted']); // Reset the interrupted status
}
// Append the new line content to the current block
$Block['element']['text'][] = [
'name' => 'p',
'text' => $matches[1], // Add the text following the '>'
];
return $Block; // Return the updated block
}
// If the line does not start with '>' and the block is not interrupted, append it
if (!isset($Block['interrupted'])) {
$Block['element']['text'][] = [
'name' => 'p',
'text' => $Line['text'], // Add the text directly to the alert block
];
return $Block; // Return the updated block
}
return null; // Return null if the continuation conditions are not met
}
protected function blockAlertComplete($Block)
{
return $Block; // Finalize and return the alert block
} this is from my own code, I have tried to remove any unnecessary code for the example, but let me know if you need more help |
Hi @BenjaminHoegh, I've seen your work on the ParsedownEntended extension when trying to launch my package. Although, I found a workaround without using the BlockContinue or Complete, your code looks promising. I will need to test it sometime. Thanks for taking the time! I'll let you know. |
Workaround? Interesting how did you achieve that? |
What I'm trying to convert:
Start:
Continues:
I'm trying to get whatever is in the new line to correctly go to the 'text' above. But when I run, this is the error:
Any help will be appreciated. Thank you!
The text was updated successfully, but these errors were encountered: