Can you fix this php script so that there aren't two line breaks in the comments?


<?php
require_once 'YAMLParser.php'; // Include the YAML parser

// Set the directory for storing tour dates
$directory = './tour-dates/';

// Create the directory if it doesn't exist
if (!is_dir($directory)) {
    mkdir($directory, 0755, true);
}

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Add a new tour date
    if (isset($_POST['action']) && $_POST['action'] === 'add') {
        if (!empty($_POST['date']) && !empty($_POST['location'])) {
            $date = trim($_POST['date']);
            $location = trim($_POST['location']);
            $comments = !empty($_POST['comments']) ? trim($_POST['comments']) : '';

            // Prepare YAML content
            $yamlContent = YAMLParser::encode([
                'date' => $date,
                'location' => $location,
                'comments' => $comments
            ]);

            // Save as .md file with a unique name
            $filename = $directory . uniqid('tour_', true) . '.md';
            file_put_contents($filename, $yamlContent);
        }
    }

    // Edit an existing tour date
    if (isset($_POST['action']) && $_POST['action'] === 'edit' && !empty($_POST['file'])) {
        $filename = $directory . basename($_POST['file']); // Sanitize file input
        if (file_exists($filename)) {
            $date = trim($_POST['date']);
            $location = trim($_POST['location']);
            $comments = !empty($_POST['comments']) ? trim($_POST['comments']) : '';

            // Prepare YAML content
            $yamlContent = YAMLParser::encode([
                'date' => $date,
                'location' => $location,
                'comments' => $comments
            ]);

            // Overwrite the file
            file_put_contents($filename, $yamlContent);
        }
    }

    // Delete a tour date
    if (isset($_POST['action']) && $_POST['action'] === 'delete' && !empty($_POST['file'])) {
        $filename = $directory . basename($_POST['file']); // Sanitize file input
        if (file_exists($filename)) {
            unlink($filename); // Delete the file
        }
    }

    // Redirect to prevent form resubmission
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

// Load and parse tour dates from Markdown files
$tourDates = [];
foreach (glob($directory . '*.md') as $filepath) {
    $content = file_get_contents($filepath);
    $parsedData = YAMLParser::decode($content);

    $tourDates[] = [
        'file' => basename($filepath),
        'date' => $parsedData['date'] ?? '',
        'location' => $parsedData['location'] ?? '',
        'comments' => $parsedData['comments'] ?? ''
    ];
}

// Sort tour dates by newest first
usort($tourDates, function ($a, $b) {
    return strcmp($b['file'], $a['file']);
});

// Function to auto-link URLs in comments
function autoLink($text)
{
    // Convert special characters to HTML entities
    $escapedText = htmlspecialchars($text);

    // Avoid processing existing <br> or <br /> tags
    $escapedText = str_replace(['<br>', '<br />'], "\n", $escapedText);

    // Convert URLs into clickable links
    $linkedText = preg_replace(
        '/(https?:\/\/[^\s]+)/',
        '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>',
        $escapedText
    );

    // Convert newlines to a single <br /> tag
    return nl2br($linkedText, false); // 'false' prevents XHTML-style <br />
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Tour Dates</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Roboto', sans-serif;
            background-color: #171724;
            color: #e0e0e0;
            margin: 0;
            padding: 0;
            text-align: center;
        }

        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
        }

        h1, h2 {
            color: #f39c12;
        }

        form {
            background: #1e1e2f;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        input[type="text"], textarea {
            width: 90%;
            padding: 10px;
            margin: 10px 0;
            border: none;
            background: #333;
            color: #fff;
            border-radius: 5px;
            font-size: 16px;
        }

        input[type="text"]:focus, textarea:focus {
            outline: none;
            background: #444;
        }

        textarea {
            height: 100px;
            resize: none;
        }

        .btn {
            background-color: #f39c12;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-transform: uppercase;
            font-weight: bold;
        }

        .btn:hover {
            background-color: #d87a0b;
        }

        .tour-list {
            list-style: none;
            padding: 0;
        }

        .tour-item {
            background-color: #1e1e2f;
            margin-bottom: 20px;
            padding: 20px;
            border-radius: 8px;
            text-align: left;
        }

        .tour-item .date {
            font-size: 18px;
            font-weight: bold;
            color: #f39c12;
        }

        .tour-item .location {
            font-size: 16px;
            color: #4CAF50;
        }

        .tour-item .comments {
            font-size: 18px;
            color: #ddd;
            white-space: pre-wrap;
            margin-top: 10px;
    line-height: 1.6; /* Adjust this value to change spacing between lines */
        }

        .tour-item .actions button {
            margin-top: 10px;
            display: inline-block;
            margin-right: 10px;
        }

        .edit-form {
            display: none;
        }

        .editing .display {
            display: none;
        }

        .editing .edit-form {
            display: block;
        }
    </style>
    <script>
        function toggleEditForm(itemId) {
            const item = document.getElementById(itemId);
            item.classList.toggle('editing');
        }

        function confirmDelete(file) {
            if (confirm("Are you sure you want to delete this entry?")) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.innerHTML = `
                    <input type="hidden" name="action" value="delete">
                    <input type="hidden" name="file" value="${file}">
                `;
                document.body.appendChild(form);
                form.submit();
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <h1>Manage Tour Dates</h1>
        <form method="POST">
            <input type="hidden" name="action" value="add">
            <input type="text" name="date" placeholder="e.g., 20 Nov 2023" required>
            <input type="text" name="location" placeholder="e.g., New York, NY - Webster Hall" required>
            <textarea name="comments" placeholder="Additional details (optional)"></textarea>
            <button class="btn" type="submit">Add Tour Date</button>
        </form>

        <h2>Current Tour Dates</h2>
        <ul class="tour-list">
            <?php foreach ($tourDates as $tour): ?>
                <li class="tour-item" id="item-<?php echo htmlspecialchars($tour['file']); ?>">
                    <div class="display">
                        <div class="date"><?php echo htmlspecialchars($tour['date']); ?></div>
                        <div class="location"><?php echo htmlspecialchars($tour['location']); ?></div>
                        <div class="comments"><?php echo autoLink($tour['comments']); ?></div>
                        <div class="actions">
                            <button class="btn" onclick="toggleEditForm('item-<?php echo htmlspecialchars($tour['file']); ?>')">Edit</button>
                            <button class="btn" onclick="confirmDelete('<?php echo htmlspecialchars($tour['file']); ?>')">Delete</button>
                        </div>
                    </div>
                    <form class="edit-form" method="POST">
                        <input type="hidden" name="action" value="edit">
                        <input type="hidden" name="file" value="<?php echo htmlspecialchars($tour['file']); ?>">
                        <input type="text" name="date" value="<?php echo htmlspecialchars($tour['date']); ?>" required>
                        <input type="text" name="location" value="<?php echo htmlspecialchars($tour['location']); ?>" required>
                        <textarea name="comments"><?php echo htmlspecialchars($tour['comments']); ?></textarea>
                        <button class="btn" type="submit">Save</button>
                        <button class="btn" type="button" onclick="toggleEditForm('item-<?php echo htmlspecialchars($tour['file']); ?>')">Cancel</button>
                    </form>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
</body>
</html>