PHP code example of gusvasconcelos / markdown-converter

1. Go to this page and download the library: Download gusvasconcelos/markdown-converter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gusvasconcelos / markdown-converter example snippets


use GusVasconcelos\MarkdownConverter\MarkdownConverter;

// Initialize the converter
$converter = new MarkdownConverter();

// Create markdown content
$converter
    ->heading('API Request Log')
    ->paragraph('Request ID: 1234567890')
    ->horizontalRule()
    ->codeBlock('{"name":"John","age":30,"email":"[email protected]"}', "json")
    ->link('https://api.example.com', 'API Documentation')
    ->write(__DIR__, "example"); // Writes to example.md file

$document = (new MarkdownConverter())
    ->heading('System Log', 2)
    ->paragraph('Timestamp: ' . date('Y-m-d H:i:s'))
    ->horizontalRule()
    ->codeBlock($errorDetails, 'php')
    ->bold('Status: ')
    ->code('ERROR')
    ->paragraph('')
    ->blockquote('This is an important system message')
    ->link('https://support.example.com', 'Get Support')
    ->write(__DIR__, "system-log");

$converter->heading('Main Title'); // # Main Title
$converter->heading('Subtitle', 2); // ## Subtitle

$converter->paragraph('This is an example paragraph.'); 
// This is an example paragraph.

$converter->bold('Bold text'); // **Bold text**

$converter->italic('Italic text'); // *Italic text*

$converter->blockquote('This is an important quote'); 
// > This is an important quote

$converter->code('$variable = "value"'); // `$variable = "value"`

$converter->codeBlock('{"name":"John","age":30}', 'json');
// 

$converter->image('https://example.com/image.jpg', 'Alternative text', 'Title');
// ![Alternative text](https://example.com/image.jpg "Title")

$converter->link('https://example.com', 'Example Site', 'Visit our site');
// [Example Site](https://example.com "Visit our site")

$converter->orderedList(['Item 1', 'Item 2', 'Item 3']);
// 1. Item 1
// 2. Item 2
// 3. Item 3

$converter->unorderedList(['Item 1', 'Item 2', 'Item 3']);
// - Item 1
// - Item 2
// - Item 3

$converter->horizontalRule(); // ---

$converter->emoji('😀'); // 😀

$converter->write(__DIR__, "document"); // Creates document.md

$content = (string) $converter; // Gets all content

$converter->heading('Title')->paragraph('Text');
$element = $converter->get(0); // Gets the first element (heading)

$converter->heading('Title')->paragraph('Text 1')->paragraph('Text 2');
$converter->removeAt(1); // Removes "Text 1"

use GusVasconcelos\MarkdownConverter\Syntax\BoldSyntax;

$converter->heading('Title')->paragraph('Text');
$converter->replace(1, new BoldSyntax('Bold Text')); // Replaces the paragraph

$converter->heading('Title')->paragraph('Text');
$total = $converter->count(); // Returns 2

$converter->heading('Title')->paragraph('Text');
$converter->clear(); // Removes everything

$elements = $converter->all(); // Gets MarkdownSyntaxCollection

$document = new MarkdownConverter();

// Build document
$document
    ->heading('Sales Report')
    ->paragraph('First quarter data')
    ->codeBlock('// Example code', 'php')
    ->paragraph('Detailed analysis');

// Check number of elements
echo "Total elements: " . $document->count(); // 4

// Replace code with a list
use GusVasconcelos\MarkdownConverter\Syntax\UnorderedListSyntax;
$list = new UnorderedListSyntax(['Sales: 100k', 'Profit: 25k', 'Customers: 500']);
$document->replace(2, $list);

// Remove last paragraph
$document->removeAt(3);

// Get final result
$finalContent = (string) $document;

$report = new MarkdownConverter();

// Data from database
$sales = ['January' => 15000, 'February' => 18000, 'March' => 22000];
$topProducts = ['Product A', 'Product B', 'Product C'];

$report
    ->heading('Monthly Sales Report', 1)
    ->paragraph('Period: ' . date('Y-m-d'))
    ->emoji('📈')
    ->horizontalRule()
    
    ->heading('Executive Summary', 2)
    ->paragraph('Total quarterly sales: $' . number_format(array_sum($sales), 2))
    ->blockquote('Quarterly target achieved successfully!')
    
    ->heading('Sales by Month', 2)
    ->orderedList(array_map(function($month, $value) {
        return "$month: $" . number_format($value, 2);
    }, array_keys($sales), $sales))
    
    ->heading('Top Products', 2)
    ->unorderedList($topProducts)
    
    ->horizontalRule()
    ->italic('Report automatically generated on ' . date('m/d/Y H:i'))
    
    ->write(__DIR__, 'sales-report');

$logger = new MarkdownConverter();

$logger
    ->heading('System Error Log', 1)
    ->paragraph('Timestamp: ' . date('Y-m-d H:i:s'))
    ->horizontalRule()
    
    ->heading('Critical Error', 2)
    ->bold('Level: ')
    ->code('CRITICAL')
    ->paragraph('Database connection failure.')
    ->codeBlock('PDOException: Connection refused', 'text')
    
    ->heading('Stack Trace', 3)
    ->codeBlock($stackTrace ?? 'Stack trace not available', 'php')
    
    ->heading('Recommended Action', 2)
    ->blockquote('Check connection settings and MySQL server status')
    
    ->write('/var/log/app/', 'error-' . date('Y-m-d'));

$emailTemplate = new MarkdownConverter();

$emailTemplate
    ->heading('Welcome to our platform!', 1)
    ->emoji('🎉')
    ->paragraph('Hello John, we\'re happy to have you with us!')
    
    ->heading('Next Steps', 2)
    ->orderedList([
        'Complete your profile',
        'Explore our features',
        'Contact us if you need help'
    ])
    
    ->horizontalRule()
    ->paragraph('Need help?')
    ->link('https://support.example.com', 'Help Center')
    
    ->paragraph('')
    ->italic('Example Team');

// Convert to HTML via Markdown parser
$htmlContent = $markdownParser->parse((string) $emailTemplate);