diff --git a/app/Livewire/BlogMaker.php b/app/Livewire/BlogMaker.php index a20121d..53e058a 100644 --- a/app/Livewire/BlogMaker.php +++ b/app/Livewire/BlogMaker.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use App\Services\MarkdownParser; use Illuminate\Http\RedirectResponse; use Illuminate\View\View; use Livewire\Component; @@ -31,16 +32,21 @@ public function updatedContent(): void $this->parseTextToMarkdown($this->content); } - function parseTextToMarkdown($content): void + public function parseTextToMarkdown($content): void { - //parse markdown - $parsed_content = app('markdown')->parse($this->content); - $this->markdownContent = $parsed_content; + $parser = new MarkdownParser(); + $this->markdownContent = $parser->parse($content); } + public function uploadImage() { // Implement image upload logic here + Image::create([ + //TODO: Get the blog ID, + 'blog_id' => 1, + 'path' => $this->tempImage->store('images', 'public'), + ]); } public function saveBlog(): RedirectResponse diff --git a/app/Services/MarkdownParser.php b/app/Services/MarkdownParser.php new file mode 100644 index 0000000..6e34c08 --- /dev/null +++ b/app/Services/MarkdownParser.php @@ -0,0 +1,71 @@ +addExtension(new CommonMarkCoreExtension()); + $environment->addExtension(new AttributesExtension()); + $environment->addExtension(new TableExtension()); + + $this->converter = new CommonMarkConverter([ + 'html_input' => 'strip', + 'allow_unsafe_links' => false, + ], $environment); + } + + public function parse(string $markdown): string + { + $html = $this->converter->convert($markdown); + + // Apply Tailwind CSS styles + return $this->applyTailwindStyles($html); + } + + protected function applyTailwindStyles(string $html): string + { + $styles = [ + 'h1' => 'text-4xl font-bold mb-4', + 'h2' => 'text-3xl font-bold mb-3', + 'h3' => 'text-2xl font-bold mb-2', + 'p' => 'text-base mb-4', + 'ul' => 'list-disc pl-5 mb-4', + 'ol' => 'list-decimal pl-5 mb-4', + 'li' => 'mb-1', + 'a' => 'text-blue-500 underline', + 'img' => 'max-w-full h-auto', + 'strong' => 'font-bold', + 'table' => 'min-w-full divide-y divide-gray-200', + 'thead' => 'bg-gray-50', + 'th' => 'px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider', + 'tbody' => 'bg-white divide-y divide-gray-200', + 'td' => 'px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900', + ]; + + foreach ($styles as $tag => $class) { + $html = preg_replace( + "/(<{$tag}[^>]*>)/i", + "$1", + $html + ); + $html = preg_replace( + "/(<\/{$tag}>)/i", + "$1", + $html + ); + } + + return $html; + } +} diff --git a/resources/views/blogs/create.blade.php b/resources/views/blogs/create.blade.php index 66b9d97..e7bbaa9 100644 --- a/resources/views/blogs/create.blade.php +++ b/resources/views/blogs/create.blade.php @@ -25,6 +25,9 @@ +