-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertmarkdown.php
More file actions
71 lines (60 loc) · 2.59 KB
/
convertmarkdown.php
File metadata and controls
71 lines (60 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$sqlpath = $_SERVER['DOCUMENT_ROOT'];
$sqlpath .= "/sql-connect.php";
include_once($sqlpath);
$parsepath = $_SERVER['DOCUMENT_ROOT'];
$parsepath .= "/plugins/Parsedown.php";
include_once($parsepath);
$parsedown = new Parsedown();
// Fetch the data from your database
$sql = "SELECT * FROM notes";
$sqldata = mysqli_query($dbcon, $sql) or die('Error getting data');
// Loop through each record
while ($row = mysqli_fetch_assoc($sqldata)) {
$noteID = $row['id']; // Assuming there's an 'id' column in your 'notes' table
$markdownContent = $row['body']; // Now using 'body' column instead of 'content'
// First, remove any existing processed comments (<!--processed...-->)
$markdownContent = preg_replace('/<!--processed-.*?-->/s', '', $markdownContent);
// This step will convert any other Markdown into HTML without affecting our custom data-mention spans
$htmlContent = $parsedown->text($markdownContent);
// Use regular expression to replace anchor tags that start with '@' with a span tag
$newHtmlContent = preg_replace_callback(
'/<a[^>]*>(@[^<]+)<\/a>/',
function($matches) {
// Check if this mention has already been processed (this avoids re-processing)
if (strpos($matches[1], '@') === 0) {
// Replace anchor tag with a span tag and add data-mention attribute and mention class
return '<span class="mention" data-mention="' . htmlspecialchars($matches[1]) . '">' . htmlspecialchars($matches[1]) . '</span>';
}
return $matches[0]; // Return unchanged if already processed
},
$htmlContent
);
// Replace Markdown checkboxes with HTML checkboxes
$htmlContent = preg_replace_callback(
'/\[\s?([x ]?)\]/', // Match [ ] or [x]
function($matches) {
// Check if the box is checked or unchecked
$checked = ($matches[1] === 'x') ? 'checked' : '';
return '<input type="checkbox" ' . $checked . ' disabled>';
},
$htmlContent
);
// Update the content in the database with the new HTML (without the onclick)
$updateSql = "UPDATE notes SET body = ? WHERE id = ?";
$stmt = $dbcon->prepare($updateSql);
if (!$stmt) {
die('Statement preparation failed: ' . $dbcon->error);
}
$stmt->bind_param("si", $newHtmlContent, $noteID); // 's' for string, 'i' for integer
$stmt->execute();
if ($stmt->affected_rows > 0) {
echo "Note ID $noteID updated successfully.<br>";
} else {
echo "Error updating Note ID $noteID.<br>";
}
}
echo "All selected notes processed and updated with new mention method.";
?>