Various fixes for the blog and editor. As well as finally adding in the carousel!
🚀 Deploy website on push / 🎉 Deploy (push) Successful in 24s

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
2024-11-04 22:17:42 +00:00
parent 7d6eeb2310
commit b28e7b2da5
13 changed files with 2287 additions and 944 deletions
+35 -4
View File
@@ -673,9 +673,17 @@ EOD;
*/
public function changeHTMLSrc(string $body, string $to, string $from): string
{
// $body = preg_replace_callback('/>([^<]+)</', function($matches) {
// // Convert special characters to HTML entities
// return '>' . htmlentities(trim($matches[1]), ENT_QUOTES | ENT_HTML5, 'UTF-8') . '<';
// }, $body);
$htmlDoc = new DOMDocument();
$body = mb_convert_encoding($body, "HTML-ENTITIES", "UTF-8");
$htmlDoc->loadHTML($body, LIBXML_NOERROR);
// Load the raw HTML content into DOMDocument
@$htmlDoc->loadHTML($body, LIBXML_NOERROR);
// Get the body and process images
$doc = $htmlDoc->getElementsByTagName('body')->item(0);
$imgs = $doc->getElementsByTagName('img');
@@ -688,9 +696,11 @@ EOD;
$srcList[] = $src;
$fileName = basename($src);
// Update the src attribute to the new location
$img->setAttribute("src", substr($to, 2) . $fileName);
}
// Rename files and clean up old ones
$files = scandir($from);
foreach ($files as $file)
{
@@ -706,15 +716,36 @@ EOD;
}
}
// Process the HTML content for output
$newBody = '';
foreach ($doc->childNodes as $node)
{
$newHTML = $htmlDoc->saveHTML($node);
$newBody .= mb_convert_encoding($newHTML, "UTF-8", mb_detect_encoding($newHTML));
// Only convert text nodes to HTML entities
if ($node->nodeType === XML_TEXT_NODE)
{
$newBody .= $this->convertToHtmlEntities($node->nodeValue); // Convert text nodes
}
else
{
$newBody .= $htmlDoc->saveHTML($node); // Keep HTML tags intact
}
}
return $newBody;
}
/**
* Convert all characters in a string to HTML entities while leaving HTML tags intact.
* @param string $text - The text to convert
* @return string - The converted text with HTML entities
*/
private function convertToHtmlEntities(string $text): string
{
// Convert characters to HTML entities using mb_encode_numericentity
return htmlentities($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
/**
* Get all posts with the given category
* @param string $category - Category of the post