1. Go to this page and download the library: Download rafaelogic/writr 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/ */
rafaelogic / writr example snippets
// In your Controller
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'ture
$contentData = json_decode($validated['content'], true);
if (!isset($contentData['blocks']) || empty($contentData['blocks'])) {
return back()->withErrors(['content' => 'Content cannot be empty.']);
}
Document::create($validated);
return redirect()->route('documents.index')
->with('success', 'Document saved successfully!');
}
use Rafaelogic\Writr\Services\WritrService;
class DocumentController extends Controller
{
public function __construct(
private WritrService $writr
) {}
public function show(Document $document)
{
$content = json_decode($document->content, true);
// Convert to HTML for display
$html = $this->writr->toHtml($content);
// Convert to Markdown
$markdown = $this->writr->toMarkdown($content);
// Extract plain text (for search, etc.)
$plainText = $this->writr->toText($content);
// Get word count and reading time
$stats = $this->writr->getContentStats($content);
// Returns: ['words' => 245, 'characters' => 1428, 'reading_time' => 1.2]
// Generate table of contents
$toc = $this->writr->generateToc($content);
return view('documents.show', [
'document' => $document,
'html' => $html,
'stats' => $stats,
'toc' => $toc,
]);
}
}
// Custom validation rule
$request->validate([
'content' => [
' $data = json_decode($value, true);
// Validate structure
if (!isset($data['blocks']) || !is_array($data['blocks'])) {
$fail('Invalid content structure.');
}
// Validate content is not empty
if (empty($data['blocks'])) {
$fail('Content cannot be empty.');
}
// Validate each block
foreach ($data['blocks'] as $block) {
if (!isset($block['type']) || !isset($block['data'])) {
$fail('Invalid block structure.');
}
}
}
]
]);
// Make content searchable
class Document extends Model
{
protected $casts = [
'content' => 'array',
];
// Accessor for search indexing
public function getSearchableContentAttribute()
{
return app(WritrService::class)->toText($this->content);
}
// Scope for searching
public function scopeSearch($query, $term)
{
return $query->whereRaw(
"JSON_UNQUOTE(JSON_EXTRACT(content, '$.blocks[*].data.text')) LIKE ?",
["%{$term}%"]
);
}
}