Add (ai gen)

This commit is contained in:
2023-10-01 00:35:27 +08:00
parent 645d0f1d02
commit f02081e30c
79 changed files with 4816 additions and 1041 deletions

55
app/Models/AiWriteup.php Normal file
View File

@@ -0,0 +1,55 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class AiWriteup
*
* @property int $id
* @property string $source
* @property string $source_url
* @property int $category_id
* @property string $title
* @property string $editor_format
* @property string|null $excerpt
* @property string|null $featured_image
* @property array|null $body
* @property float $cost
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Category $category
*/
class AiWriteup extends Model
{
protected $table = 'ai_writeups';
protected $casts = [
'category_id' => 'int',
'body' => 'json',
'cost' => 'float',
];
protected $fillable = [
'source',
'source_url',
'category_id',
'title',
'editor_format',
'excerpt',
'featured_image',
'body',
'cost',
];
public function category()
{
return $this->belongsTo(Category::class);
}
}

View File

@@ -31,7 +31,7 @@
*/
class Category extends Model
{
use SoftDeletes, Cachable;
use Cachable, SoftDeletes;
protected $table = 'categories';

View File

@@ -25,7 +25,7 @@
*/
class CountryLocale extends Model
{
use SoftDeletes, Cachable;
use Cachable, SoftDeletes;
protected $table = 'country_locales';

View File

@@ -8,10 +8,12 @@
use AlAminFirdows\LaravelEditorJs\Facades\LaravelEditorJs;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;
use Symfony\Component\DomCrawler\Crawler;
/**
* Class Post
@@ -85,7 +87,114 @@ public function post_category()
public function getHtmlBodyAttribute()
{
if (! is_empty($this->body)) {
return LaravelEditorJs::render(json_encode($this->body));
if ($this->editor == 'editorjs') {
return LaravelEditorJs::render(json_encode($this->body));
} elseif ($this->editor == 'markdown') {
//dd($this->body);
$html = Markdown::convert($this->body)->getContent();
//dd($html);
$html = str_replace('\\n', "\n", $html);
$crawler = new Crawler($html);
$firstHeaderProcessed = false;
$crawler->filter('h1, h2, h3, h4, h5, h6')->each(function (Crawler $node) use (&$firstHeaderProcessed) {
$element = $node->getNode(0);
// Create a new h3 element
$newElement = $element->ownerDocument->createElement('h3');
// Copy attributes from the old header element to the new h3 element
foreach ($element->attributes as $attribute) {
$newElement->setAttribute($attribute->name, $attribute->value);
}
// Add the 'fw-bold' class to the new h3 element
$existingClasses = $element->getAttribute('class');
$newClass = 'fw-bold';
$updatedClasses = ($existingClasses ? $existingClasses.' ' : '').$newClass;
$newElement->setAttribute('class', $updatedClasses);
// Move child nodes from the old header element to the new h3 element
while ($element->firstChild) {
$newElement->appendChild($element->firstChild);
}
// Replace the old header element with the new h3 element in the DOM
$element->parentNode->replaceChild($newElement, $element);
// Only for the first header
if (! $firstHeaderProcessed) {
$firstHeaderProcessed = true;
$nextSibling = $newElement->nextSibling;
while ($nextSibling) {
if ($nextSibling->nodeType === XML_ELEMENT_NODE) {
if ($nextSibling->nodeName === 'p' && ($nextSibling->getElementsByTagName('img')->length > 0 || $nextSibling->getElementsByTagName('figure')->length > 0)) {
// Remove <p> if it contains an <img> or <figure> directly
$nextSibling->parentNode->removeChild($nextSibling);
break;
} elseif ($nextSibling->nodeName === 'img' || $nextSibling->nodeName === 'figure') {
// Direct <img> or <figure> without wrapping <p>
$newElement->parentNode->removeChild($nextSibling);
break;
} else {
break;
}
} else {
$nextSibling = $nextSibling->nextSibling;
}
}
}
});
// Modify the DOM by wrapping the <img> tags inside a <figure> and adding the desired structure
$crawler->filter('img')->each(function (Crawler $node) {
$imgElement = $node->getNode(0);
// Update the class of the <img>
$existingClasses = $imgElement->getAttribute('class');
$newClasses = 'img-fluid rounded-2 shadow-sm mb-2';
$updatedClasses = ($existingClasses ? $existingClasses.' ' : '').$newClasses;
$imgElement->setAttribute('class', $updatedClasses);
// Create a new <figure> element
$figureElement = new \DOMElement('figure');
$imgElement->parentNode->insertBefore($figureElement, $imgElement);
// Move the <img> inside the <figure>
$figureElement->appendChild($imgElement);
$figureElement->setAttribute('class', 'image');
// Create a new <footer> element inside <figure>
$footerElement = new \DOMElement('footer', $imgElement->getAttribute('alt'));
$figureElement->appendChild($footerElement);
$footerElement->setAttribute('class', 'image-caption');
});
// Add Bootstrap 5 styling to tables
$crawler->filter('table')->each(function (Crawler $node) {
$tableElement = $node->getNode(0);
$existingClasses = $tableElement->getAttribute('class');
$newClass = 'table';
$updatedClasses = ($existingClasses ? $existingClasses.' ' : '').$newClass;
$tableElement->setAttribute('class', $updatedClasses);
});
// Convert the modified DOM back to HTML
$updatedHtml = '';
foreach ($crawler as $domElement) {
$updatedHtml .= $domElement->ownerDocument->saveHTML($domElement);
}
return $updatedHtml;
}
}
return '';

View File

@@ -0,0 +1,53 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class ShopeeSellerScrape
*
* @property int $id
* @property int|null $category_id
* @property string $seller
* @property string $country_iso
* @property int $epoch
* @property string $filename
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Category|null $category
* @property Collection|ShopeeSellerScrapedImage[] $shopee_seller_scraped_images
*/
class ShopeeSellerScrape extends Model
{
protected $table = 'shopee_seller_scrapes';
protected $casts = [
'category_id' => 'int',
'epoch' => 'int',
];
protected $fillable = [
'category_id',
'seller',
'country_iso',
'epoch',
'filename',
];
public function category()
{
return $this->belongsTo(Category::class);
}
public function shopee_seller_scraped_images()
{
return $this->hasMany(ShopeeSellerScrapedImage::class);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class ShopeeSellerScrapedImage
*
* @property int $id
* @property int $shopee_seller_scrape_id
* @property string $original_name
* @property string|null $image
* @property bool $featured
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property ShopeeSellerScrape $shopee_seller_scrape
*/
class ShopeeSellerScrapedImage extends Model
{
protected $table = 'shopee_seller_scraped_images';
protected $casts = [
'shopee_seller_scrape_id' => 'int',
'featured' => 'bool',
];
protected $fillable = [
'shopee_seller_scrape_id',
'original_name',
'image',
'featured',
];
public function shopee_seller_scrape()
{
return $this->belongsTo(ShopeeSellerScrape::class);
}
}