PHP code example of ptnghia / laravel-tiptap-editor
1. Go to this page and download the library: Download ptnghia/laravel-tiptap-editor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ptnghia / laravel-tiptap-editor example snippets
public function store(Request $request)
{
$request->validate(['content' => '),
]);
}
use Suspended\TiptapEditor\Facades\TiptapEditor;
$html = TiptapEditor::render($post->content_json);
// or pass an array directly
$html = TiptapEditor::render($post->content_array);
use Suspended\TiptapEditor\Traits\HasTiptapContent;
class Post extends Model
{
use HasTiptapContent;
// Expects a 'content_json' column (override with $tiptapColumn)
}
$post->renderContent(); // JSON → safe HTML string
$post->getExcerpt(200); // Plain-text excerpt, 200 chars
$post->getPlainText(); // All text, no markup
$post->getHeadings(); // [['level' => 1, 'text' => '...']]
$post->hasContent(); // bool
$post->getTiptapContent(); // Raw content as array
$post->setTiptapContent($json); // Set from array or JSON string
use Suspended\TiptapEditor\Services\AiContentService;
$ai = app(AiContentService::class);
$response = $ai->generate('Write a blog post about Laravel packages');
echo $response->content; // HTML string
echo $response->tokensUsed; // int
$refined = $ai->refine($existingHtml, 'Make it more concise');
$summary = $ai->summarize($longContent, maxLength: 300);
$translated = $ai->translate($content, 'vi');
use Suspended\TiptapEditor\Facades\TiptapEditor;
// Sanitize before storing
$cleanJson = TiptapEditor::sanitize($request->input('content'));
$post->content_json = $cleanJson;
use Suspended\TiptapEditor\Support\NodeRegistry;
use Suspended\TiptapEditor\Services\HtmlRenderer;
// In a ServiceProvider boot()
$registry = app(NodeRegistry::class);
$registry->registerSchema('myBlock', [
'allowed_attributes' => ['class', 'data-type'],
'allowed_children' => ['paragraph'],
]);
$registry->register('myBlock', function (array $node, HtmlRenderer $renderer): string {
$content = $renderer->renderChildren($node);
return "<div class=\"my-block\">{$content}</div>";
});
use Suspended\TiptapEditor\Contracts\AiProviderInterface;
use Suspended\TiptapEditor\Services\Ai\AiResponse;
class MyProvider implements AiProviderInterface
{
public function generate(string $prompt, array $options = []): AiResponse
{
$content = '...'; // call your API
return new AiResponse(
content: $content,
tiptapJson: null,
tokensUsed: 0,
provider: 'my-provider',
model: 'my-model',
);
}
public function supports(string $capability): bool
{
return in_array($capability, ['generate', 'refine']);
}
public function getName(): string { return 'my-provider'; }
}
app(AiContentService::class)->registerProvider('my-provider', new MyProvider());