Implement ID-based routing and folder auto-generation from titles
Major features:
- Switch from slug-based to ID-based routing (/documents/123)
- Enable title editing with automatic slug/path regeneration
- Auto-generate folder structure from title slashes (e.g., Laravel/Livewire/Components)
- Persist sidebar folder open/close state using localStorage
- Remove slug unique constraint (ID routing makes it unnecessary)
- Implement recursive tree view with multi-level folder support
Architecture changes:
- DocumentService: Add generatePathAndSlug() for title-based path generation
- Routes: Change from {document:slug} to {document} for ID binding
- SidebarTree: Extract recursive rendering to partials/tree-item.blade.php
- Database: Remove unique constraint from documents.slug column
UI improvements:
- Display only last path component in sidebar (Components vs Laravel/Livewire/Components)
- Folder state persists across page navigation via localStorage
- Title field accepts slashes for folder organization
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
295
src/app/Models/Document.php
Normal file
295
src/app/Models/Document.php
Normal file
@@ -0,0 +1,295 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Helpers\SlugHelper;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
|
||||
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
|
||||
|
||||
class Document extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'path',
|
||||
'title',
|
||||
'slug',
|
||||
'content',
|
||||
'rendered_html',
|
||||
'frontmatter',
|
||||
'file_size',
|
||||
'file_hash',
|
||||
'file_modified_at',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'frontmatter' => 'array',
|
||||
'file_modified_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Frontmatterをパース(互換性のため残す)
|
||||
*
|
||||
* @param string $content
|
||||
* @return array{frontmatter: array, content: string}
|
||||
*/
|
||||
protected static function parseFrontmatter(string $content): array
|
||||
{
|
||||
$frontmatter = [];
|
||||
$bodyContent = $content;
|
||||
|
||||
// Frontmatterの検出(--- で囲まれた部分)
|
||||
if (preg_match('/^---\s*\n(.*?)\n---\s*\n(.*)/s', $content, $matches)) {
|
||||
$frontmatterText = $matches[1];
|
||||
$bodyContent = $matches[2];
|
||||
|
||||
// 簡易的なYAMLパース(key: value形式のみ)
|
||||
$lines = explode("\n", $frontmatterText);
|
||||
foreach ($lines as $line) {
|
||||
if (preg_match('/^([^:]+):\s*(.*)$/', $line, $lineMatches)) {
|
||||
$frontmatter[trim($lineMatches[1])] = trim($lineMatches[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'frontmatter' => $frontmatter,
|
||||
'content' => trim($bodyContent),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Markdownをレンダリング
|
||||
*
|
||||
* @param string $markdown
|
||||
* @return string
|
||||
*/
|
||||
public static function renderMarkdown(string $markdown): string
|
||||
{
|
||||
$converter = new CommonMarkConverter([
|
||||
'html_input' => 'strip',
|
||||
'allow_unsafe_links' => false,
|
||||
]);
|
||||
|
||||
$converter->getEnvironment()->addExtension(new GithubFlavoredMarkdownExtension());
|
||||
|
||||
return $converter->convert($markdown)->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* [[wiki-link]]を抽出してリンクテーブルに同期
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function syncLinks(): void
|
||||
{
|
||||
// 既存のリンクを削除
|
||||
$this->outgoingLinks()->delete();
|
||||
|
||||
// [[wiki-link]]を抽出
|
||||
preg_match_all('/\[\[([^\]]+)\]\]/', $this->content, $matches);
|
||||
|
||||
if (empty($matches[1])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$position = 0;
|
||||
foreach ($matches[1] as $linkTitle) {
|
||||
$linkTitle = trim($linkTitle);
|
||||
|
||||
// リンク先のドキュメントを検索
|
||||
$targetDocument = static::where('title', $linkTitle)
|
||||
->orWhere('slug', SlugHelper::generate($linkTitle))
|
||||
->first();
|
||||
|
||||
DocumentLink::create([
|
||||
'source_document_id' => $this->id,
|
||||
'target_document_id' => $targetDocument?->id,
|
||||
'target_title' => $linkTitle,
|
||||
'position' => $position++,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [[wiki-link]]をHTMLリンクに変換
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function processLinks(): string
|
||||
{
|
||||
return preg_replace_callback(
|
||||
'/\[\[([^\]]+)\]\]/',
|
||||
function ($matches) {
|
||||
$linkTitle = trim($matches[1]);
|
||||
$slug = SlugHelper::generate($linkTitle);
|
||||
|
||||
// リンク先のドキュメントを検索
|
||||
$targetDocument = static::where('title', $linkTitle)
|
||||
->orWhere('slug', $slug)
|
||||
->first();
|
||||
|
||||
if ($targetDocument) {
|
||||
return '<a href="/documents/' . $targetDocument->slug . '" class="wiki-link">' . e($linkTitle) . '</a>';
|
||||
} else {
|
||||
return '<a href="/documents/create?title=' . urlencode($linkTitle) . '" class="wiki-link wiki-link-new">' . e($linkTitle) . '</a>';
|
||||
}
|
||||
},
|
||||
$this->rendered_html
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 全文検索スコープ
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param string $searchTerm
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeSearch(Builder $query, string $searchTerm): Builder
|
||||
{
|
||||
return $query->whereRaw(
|
||||
'MATCH(title, content) AGAINST(? IN BOOLEAN MODE)',
|
||||
[$searchTerm]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* ディレクトリ内検索スコープ
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param string $directory
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeInDirectory(Builder $query, string $directory): Builder
|
||||
{
|
||||
$directory = rtrim($directory, '/') . '/';
|
||||
return $query->where('path', 'like', $directory . '%');
|
||||
}
|
||||
|
||||
/**
|
||||
* 作成者リレーション
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function creator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新者リレーション
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function updater(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'updated_by');
|
||||
}
|
||||
|
||||
/**
|
||||
* 発リンク(このドキュメントから他へのリンク)
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function outgoingLinks(): HasMany
|
||||
{
|
||||
return $this->hasMany(DocumentLink::class, 'source_document_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 被リンク(他のドキュメントからこのドキュメントへのリンク)
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function incomingLinks(): HasMany
|
||||
{
|
||||
return $this->hasMany(DocumentLink::class, 'target_document_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* このドキュメントを最近閲覧したユーザー
|
||||
*
|
||||
* @return HasManyThrough
|
||||
*/
|
||||
public function recentByUsers(): HasManyThrough
|
||||
{
|
||||
return $this->hasManyThrough(
|
||||
User::class,
|
||||
RecentDocument::class,
|
||||
'document_id',
|
||||
'id',
|
||||
'id',
|
||||
'user_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* ディレクトリパスを取得
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDirectoryAttribute(): string
|
||||
{
|
||||
return dirname($this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* ファイル名を取得
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFilenameAttribute(): string
|
||||
{
|
||||
return basename($this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 絶対パスを取得
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAbsolutePathAttribute(): string
|
||||
{
|
||||
return Storage::disk('markdown')->path($this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* タイトルセット時にslugも自動生成
|
||||
*
|
||||
* @param string $value
|
||||
* @return void
|
||||
*/
|
||||
public function setTitleAttribute(string $value): void
|
||||
{
|
||||
$this->attributes['title'] = $value;
|
||||
|
||||
if (empty($this->attributes['slug'])) {
|
||||
$this->attributes['slug'] = SlugHelper::generate($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
65
src/app/Models/DocumentLink.php
Normal file
65
src/app/Models/DocumentLink.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class DocumentLink extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'source_document_id',
|
||||
'target_document_id',
|
||||
'target_title',
|
||||
'position',
|
||||
];
|
||||
|
||||
/**
|
||||
* ソース(リンク元)ドキュメント
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function sourceDocument(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Document::class, 'source_document_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* ターゲット(リンク先)ドキュメント
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function targetDocument(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Document::class, 'target_document_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* リンク先が未作成かどうかを判定
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isBroken(): bool
|
||||
{
|
||||
return is_null($this->target_document_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* リンク先ドキュメントのURLを取得
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
if ($this->isBroken()) {
|
||||
return '/documents/create?title=' . urlencode($this->target_title);
|
||||
}
|
||||
|
||||
return '/documents/' . $this->targetDocument->slug;
|
||||
}
|
||||
}
|
||||
95
src/app/Models/RecentDocument.php
Normal file
95
src/app/Models/RecentDocument.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecentDocument extends Model
|
||||
{
|
||||
/**
|
||||
* テーブルにはtimestampsがないため無効化
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'document_id',
|
||||
'accessed_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'accessed_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーリレーション
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* ドキュメントリレーション
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function document(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Document::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* ドキュメントアクセスを記録
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $documentId
|
||||
* @return void
|
||||
*/
|
||||
public static function recordAccess(int $userId, int $documentId): void
|
||||
{
|
||||
static::updateOrCreate(
|
||||
[
|
||||
'user_id' => $userId,
|
||||
'document_id' => $documentId,
|
||||
],
|
||||
[
|
||||
'accessed_at' => now(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーの最近閲覧したドキュメントを取得
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $limit
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function getRecentForUser(int $userId, int $limit = 10)
|
||||
{
|
||||
return static::where('user_id', $userId)
|
||||
->with('document')
|
||||
->orderBy('accessed_at', 'desc')
|
||||
->limit($limit)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
48
src/app/Models/User.php
Normal file
48
src/app/Models/User.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user