Wiki Phase 3: internal links, backlinks, and tags
Connectivity phase of the wiki upgrade (see WIKI_UPGRADE.md). Schema (additive new tables): wiki_tags, wiki_page_tags, wiki_links. Internal links & backlinks: - new wiki.links.js parses a saved body for /wiki/<slug> (and data-wiki-slug) targets; wiki_links is rebuilt on every save - article shows a "Linked from" section (published backlinks) and renders links to non-existent pages as red links (server returns missing_links) - editor gains an internal-link picker listing existing pages Tags: - pages accept a tags[] array; tags upsert on save, page tag-set is replaced, and orphaned tags are auto-pruned (on save and delete) - public/admin list filter by ?tag=; /wiki/tags lists tags with published counts - article shows tag chips; the index has a flat tag-filtered view; editor has a comma-separated tags field Verified end-to-end: A->B backlink appears, red link detected, link index rebuilds on edit, tag filtering + chips + pruning all work. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -309,10 +309,17 @@ phase if preferred). Do not merge a phase that hasn't been verified.
|
||||
`<script>` stripped); a toolbar edit (insert divider) saved and persisted. TipTap
|
||||
is code-split into its own chunk (lazy-loaded), keeping it off the public bundle.
|
||||
|
||||
### Phase 3 — Connectivity
|
||||
### Phase 3 — Connectivity ✅
|
||||
- Internal `[[slug]]` links + red-link detection; `wiki_links` rebuild on save;
|
||||
backlinks on the article; tags + tag/category filtering.
|
||||
- **Exit check**: link page A→B, confirm B shows A under "Linked from"; tag filter works.
|
||||
- **Verified** (2026-06-27): internal links authored via an in-editor page picker
|
||||
(links to `/wiki/<slug>`); A→B made B list A under "Linked from"; a link to a
|
||||
non-existent page renders as a red link; removing the link on save cleared the
|
||||
backlink (link index rebuilt). Tags upsert on save, filter via `?tag=` (chips +
|
||||
flat index view), list with published counts, and orphan tags are auto-pruned.
|
||||
- Implementation note: links are plain anchors to `/wiki/<slug>` (the WYSIWYG fits
|
||||
this better than `[[ ]]` syntax); the sanitizer also allows `data-wiki-slug`.
|
||||
|
||||
### Phase 4 — Discovery & trust
|
||||
- FULLTEXT search (public search box + admin filter); revision history list /
|
||||
|
||||
@@ -49,8 +49,15 @@ export const api = {
|
||||
status: () => req('/public/status'),
|
||||
posts: (category) => req(`/public/posts/${category}`),
|
||||
post: (category, idOrSlug) => req(`/public/posts/${category}/${idOrSlug}`),
|
||||
wiki: (category) => req(`/public/wiki${category ? `?category=${encodeURIComponent(category)}` : ''}`),
|
||||
wiki: (opts = {}) => {
|
||||
const qs = new URLSearchParams()
|
||||
if (opts.category) qs.set('category', opts.category)
|
||||
if (opts.tag) qs.set('tag', opts.tag)
|
||||
const s = qs.toString()
|
||||
return req(`/public/wiki${s ? `?${s}` : ''}`)
|
||||
},
|
||||
wikiCategories: () => req('/public/wiki/categories'),
|
||||
wikiTags: () => req('/public/wiki/tags'),
|
||||
wikiPage: (slug) => req(`/public/wiki/${slug}`),
|
||||
contact: (payload) => req('/public/contact', { method: 'POST', body: payload }),
|
||||
|
||||
@@ -83,6 +90,7 @@ export const api = {
|
||||
publishWiki: (slug, published) =>
|
||||
req(`/admin/wiki/${slug}/publish`, { method: 'PATCH', body: { published } }),
|
||||
deleteWiki: (slug) => req(`/admin/wiki/${slug}`, { method: 'DELETE' }),
|
||||
listWikiTags: () => req('/admin/wiki/tags'),
|
||||
listWikiCategories: () => req('/admin/wiki/categories'),
|
||||
createWikiCategory: (data) => req('/admin/wiki/categories', { method: 'POST', body: data }),
|
||||
updateWikiCategory: (id, data) =>
|
||||
|
||||
@@ -21,9 +21,15 @@ function Btn({ onClick, active, disabled, title, children }) {
|
||||
)
|
||||
}
|
||||
|
||||
export default function RichTextEditor({ value, onChange }) {
|
||||
function escapeHtml(s) {
|
||||
return String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c])
|
||||
}
|
||||
|
||||
export default function RichTextEditor({ value, onChange, pages = [] }) {
|
||||
const fileRef = useRef(null)
|
||||
const [uploading, setUploading] = useState(false)
|
||||
const [linkMenu, setLinkMenu] = useState(false)
|
||||
const [linkFilter, setLinkFilter] = useState('')
|
||||
|
||||
const editor = useEditor({
|
||||
extensions: [
|
||||
@@ -55,6 +61,17 @@ export default function RichTextEditor({ value, onChange }) {
|
||||
editor.chain().focus().extendMarkRange('link').setLink({ href: url }).run()
|
||||
}
|
||||
|
||||
function insertInternalLink(page) {
|
||||
const { from, to } = editor.state.selection
|
||||
if (from === to) {
|
||||
editor.chain().focus().insertContent(`<a href="/wiki/${page.slug}">${escapeHtml(page.title)}</a> `).run()
|
||||
} else {
|
||||
editor.chain().focus().extendMarkRange('link').setLink({ href: `/wiki/${page.slug}` }).run()
|
||||
}
|
||||
setLinkMenu(false)
|
||||
setLinkFilter('')
|
||||
}
|
||||
|
||||
async function onPickImage(e) {
|
||||
const file = e.target.files?.[0]
|
||||
e.target.value = '' // allow re-selecting the same file
|
||||
@@ -109,6 +126,9 @@ export default function RichTextEditor({ value, onChange }) {
|
||||
<Btn title="Link" active={editor.isActive('link')} onClick={setLink}>
|
||||
🔗
|
||||
</Btn>
|
||||
<Btn title="Link to another wiki page" disabled={pages.length === 0} onClick={() => setLinkMenu((v) => !v)}>
|
||||
📄
|
||||
</Btn>
|
||||
<Btn title="Insert image" disabled={uploading} onClick={() => fileRef.current?.click()}>
|
||||
{uploading ? '…' : '🖼'}
|
||||
</Btn>
|
||||
@@ -121,6 +141,32 @@ export default function RichTextEditor({ value, onChange }) {
|
||||
</Btn>
|
||||
</div>
|
||||
|
||||
{linkMenu && (
|
||||
<div className="rte-linkmenu">
|
||||
<input
|
||||
autoFocus
|
||||
className="input"
|
||||
placeholder="Filter pages…"
|
||||
value={linkFilter}
|
||||
onChange={(e) => setLinkFilter(e.target.value)}
|
||||
/>
|
||||
<div className="rte-linkmenu-list">
|
||||
{pages
|
||||
.filter((p) => {
|
||||
const q = linkFilter.trim().toLowerCase()
|
||||
return !q || p.title.toLowerCase().includes(q) || p.slug.includes(q)
|
||||
})
|
||||
.slice(0, 30)
|
||||
.map((p) => (
|
||||
<button key={p.slug} type="button" className="rte-linkmenu-item" onClick={() => insertInternalLink(p)}>
|
||||
<span>{p.title}</span>
|
||||
<span className="rte-linkmenu-slug">/{p.slug}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<EditorContent editor={editor} className="rte-content prose" />
|
||||
<input ref={fileRef} type="file" accept="image/*" onChange={onPickImage} hidden />
|
||||
</div>
|
||||
|
||||
@@ -15,19 +15,25 @@ export default function WikiEditor({ slug, onClose, onSaved }) {
|
||||
excerpt: '',
|
||||
category_id: '',
|
||||
published: true,
|
||||
tags: '',
|
||||
})
|
||||
const [categories, setCategories] = useState([])
|
||||
const [pages, setPages] = useState([])
|
||||
const [loading, setLoading] = useState(isEdit)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
|
||||
// Categories for the dropdown (both new and edit).
|
||||
// Categories (dropdown) + pages (internal-link picker), for both new and edit.
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
api.admin
|
||||
.listWikiCategories()
|
||||
.then((cats) => active && setCategories(cats))
|
||||
.catch(() => {})
|
||||
api.admin
|
||||
.listWiki()
|
||||
.then((list) => active && setPages(list.map((p) => ({ slug: p.slug, title: p.title }))))
|
||||
.catch(() => {})
|
||||
return () => {
|
||||
active = false
|
||||
}
|
||||
@@ -48,6 +54,7 @@ export default function WikiEditor({ slug, onClose, onSaved }) {
|
||||
excerpt: p.excerpt || '',
|
||||
category_id: p.category_id != null ? String(p.category_id) : '',
|
||||
published: Boolean(p.published),
|
||||
tags: (p.tags || []).map((t) => t.label).join(', '),
|
||||
}),
|
||||
)
|
||||
.catch(() => active && setError('Could not load this page.'))
|
||||
@@ -66,6 +73,10 @@ export default function WikiEditor({ slug, onClose, onSaved }) {
|
||||
excerpt: form.excerpt.trim(),
|
||||
category_id: form.category_id ? Number(form.category_id) : null,
|
||||
published: form.published,
|
||||
tags: form.tags
|
||||
.split(',')
|
||||
.map((t) => t.trim())
|
||||
.filter(Boolean),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,10 +185,24 @@ export default function WikiEditor({ slug, onClose, onSaved }) {
|
||||
placeholder="One-line summary shown on the wiki home."
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<span className="field-label">Tags (comma-separated)</span>
|
||||
<input
|
||||
type="text"
|
||||
value={form.tags}
|
||||
onChange={set('tags')}
|
||||
className="input"
|
||||
placeholder="beginner, pvp, towns"
|
||||
/>
|
||||
</label>
|
||||
<div>
|
||||
<span className="field-label">Body (use Heading 2 for table-of-contents sections)</span>
|
||||
<Suspense fallback={<span className="spin" />}>
|
||||
<RichTextEditor value={form.body} onChange={(html) => setForm((f) => ({ ...f, body: html }))} />
|
||||
<RichTextEditor
|
||||
value={form.body}
|
||||
onChange={(html) => setForm((f) => ({ ...f, body: html }))}
|
||||
pages={pages.filter((p) => p.slug !== form.slug)}
|
||||
/>
|
||||
</Suspense>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -38,17 +38,22 @@ function PageCard({ page }) {
|
||||
export default function Wiki() {
|
||||
const [searchParams] = useSearchParams()
|
||||
const activeCategory = searchParams.get('category')
|
||||
const { loading, error, data } = useAsync(() =>
|
||||
Promise.all([api.wikiCategories(), api.wiki()]).then(([categories, pages]) => ({
|
||||
categories,
|
||||
pages,
|
||||
})),
|
||||
const activeTag = searchParams.get('tag')
|
||||
// Tag view fetches a tag-filtered page list; otherwise all pages (grouped here).
|
||||
const { loading, error, data } = useAsync(
|
||||
() =>
|
||||
Promise.all([api.wikiCategories(), api.wiki(activeTag ? { tag: activeTag } : {})]).then(
|
||||
([categories, pages]) => ({ categories, pages }),
|
||||
),
|
||||
[activeTag],
|
||||
)
|
||||
|
||||
const allSections = data ? groupByCategory(data.categories, data.pages) : []
|
||||
const sections = activeCategory
|
||||
? allSections.filter((s) => s.slug === activeCategory)
|
||||
: allSections
|
||||
const hasPages = data && data.pages.length > 0
|
||||
const filtered = Boolean(activeCategory || activeTag)
|
||||
|
||||
return (
|
||||
<PublicLayout section="wiki">
|
||||
@@ -61,39 +66,54 @@ export default function Wiki() {
|
||||
/>
|
||||
{loading && <Loading />}
|
||||
{error && <ErrorState message="Could not load the wiki right now." />}
|
||||
{!loading && !error && !hasPages && <EmptyState>No wiki pages yet.</EmptyState>}
|
||||
{!loading && !error && !hasPages && !filtered && <EmptyState>No wiki pages yet.</EmptyState>}
|
||||
|
||||
{!loading && !error && activeCategory && (
|
||||
{!loading && !error && filtered && (
|
||||
<p className="sans" style={{ margin: '0 0 8px', fontSize: '0.85rem' }}>
|
||||
<Link to="/wiki" style={{ color: 'var(--accent)', textDecoration: 'none' }}>
|
||||
← All sections
|
||||
</Link>
|
||||
{activeTag && <span className="muted"> · Tagged #{activeTag}</span>}
|
||||
</p>
|
||||
)}
|
||||
{!loading && !error && hasPages && activeCategory && sections.length === 0 && (
|
||||
<EmptyState>No pages in this section yet.</EmptyState>
|
||||
)}
|
||||
|
||||
{sections.map((section) => (
|
||||
<section key={section.id} style={{ marginTop: 36 }}>
|
||||
<h2
|
||||
className="display"
|
||||
style={{ margin: '0 0 4px', fontSize: '1.5rem', color: 'var(--accent)' }}
|
||||
>
|
||||
{section.title}
|
||||
</h2>
|
||||
{section.description && (
|
||||
<p className="muted" style={{ margin: '0 0 16px', fontSize: '0.95rem' }}>
|
||||
{section.description}
|
||||
</p>
|
||||
)}
|
||||
<div className="grid-4" style={{ marginTop: section.description ? 0 : 12 }}>
|
||||
{section.pages.map((p) => (
|
||||
{/* Tag view: a flat list of matching pages (tags cross categories). */}
|
||||
{!loading && !error && activeTag &&
|
||||
(data.pages.length === 0 ? (
|
||||
<EmptyState>No pages with this tag.</EmptyState>
|
||||
) : (
|
||||
<div className="grid-4" style={{ marginTop: 12 }}>
|
||||
{data.pages.map((p) => (
|
||||
<PageCard key={p.slug} page={p} />
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
))}
|
||||
))}
|
||||
|
||||
{/* Category / full view: grouped sections. */}
|
||||
{!loading && !error && !activeTag && hasPages && activeCategory && sections.length === 0 && (
|
||||
<EmptyState>No pages in this section yet.</EmptyState>
|
||||
)}
|
||||
{!activeTag &&
|
||||
sections.map((section) => (
|
||||
<section key={section.id} style={{ marginTop: 36 }}>
|
||||
<h2
|
||||
className="display"
|
||||
style={{ margin: '0 0 4px', fontSize: '1.5rem', color: 'var(--accent)' }}
|
||||
>
|
||||
{section.title}
|
||||
</h2>
|
||||
{section.description && (
|
||||
<p className="muted" style={{ margin: '0 0 16px', fontSize: '0.95rem' }}>
|
||||
{section.description}
|
||||
</p>
|
||||
)}
|
||||
<div className="grid-4" style={{ marginTop: section.description ? 0 : 12 }}>
|
||||
{section.pages.map((p) => (
|
||||
<PageCard key={p.slug} page={p} />
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
))}
|
||||
</div>
|
||||
</PublicLayout>
|
||||
)
|
||||
|
||||
@@ -15,8 +15,9 @@ function slugify(text) {
|
||||
}
|
||||
|
||||
// Parse the stored body HTML: sanitize (defense in depth — the server also
|
||||
// sanitizes on save), then assign ids to <h2> headings and collect a TOC.
|
||||
function buildArticle(body) {
|
||||
// sanitizes on save), assign ids to <h2> headings and collect a TOC, and mark
|
||||
// internal links to pages that don't exist as "red links".
|
||||
function buildArticle(body, missing) {
|
||||
if (!body) return { html: '', toc: [] }
|
||||
if (typeof window === 'undefined' || !window.DOMParser) return { html: '', toc: [] }
|
||||
const safe = DOMPurify.sanitize(body)
|
||||
@@ -27,13 +28,22 @@ function buildArticle(body) {
|
||||
h.id = id
|
||||
toc.push({ id, label: h.textContent })
|
||||
})
|
||||
doc.querySelectorAll('a[href^="/wiki/"]').forEach((a) => {
|
||||
const target = a.getAttribute('href').replace(/^\/wiki\//, '').replace(/[#?].*$/, '')
|
||||
a.removeAttribute('target') // internal links stay in-app
|
||||
if (missing.has(target)) {
|
||||
a.classList.add('wiki-red-link')
|
||||
a.setAttribute('title', 'This page does not exist yet')
|
||||
}
|
||||
})
|
||||
return { html: doc.body.innerHTML, toc }
|
||||
}
|
||||
|
||||
export default function WikiArticle() {
|
||||
const { slug } = useParams()
|
||||
const { loading, error, data: page } = useAsync(() => api.wikiPage(slug), [slug])
|
||||
const { html, toc } = useMemo(() => buildArticle(page?.body), [page])
|
||||
const missing = useMemo(() => new Set(page?.missing_links || []), [page])
|
||||
const { html, toc } = useMemo(() => buildArticle(page?.body, missing), [page, missing])
|
||||
|
||||
return (
|
||||
<PublicLayout section="wiki">
|
||||
@@ -100,6 +110,15 @@ export default function WikiArticle() {
|
||||
<p className="sans" style={{ margin: '18px 0 0', color: 'var(--dim)', fontSize: '0.78rem', letterSpacing: '0.04em' }}>
|
||||
Last updated {longDate(page.updated_at) || '—'}
|
||||
</p>
|
||||
{page.tags && page.tags.length > 0 && (
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginTop: 14 }}>
|
||||
{page.tags.map((t) => (
|
||||
<Link key={t.slug} to={`/wiki?tag=${t.slug}`} className="wiki-tag">
|
||||
#{t.label}
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div style={{ height: 1, background: 'var(--line)', margin: '30px 0' }} />
|
||||
{html ? (
|
||||
<div className="prose" dangerouslySetInnerHTML={{ __html: html }} />
|
||||
@@ -107,6 +126,28 @@ export default function WikiArticle() {
|
||||
<p className="muted">This page has no content yet.</p>
|
||||
)}
|
||||
|
||||
{page.backlinks && page.backlinks.length > 0 && (
|
||||
<section
|
||||
style={{ marginTop: 40, borderTop: '1px solid var(--line)', paddingTop: 22 }}
|
||||
>
|
||||
<p
|
||||
className="sans"
|
||||
style={{ margin: '0 0 12px', color: 'var(--accent)', fontSize: '0.66rem', fontWeight: 700, letterSpacing: '0.14em', textTransform: 'uppercase' }}
|
||||
>
|
||||
Linked from
|
||||
</p>
|
||||
<ul style={{ margin: 0, paddingLeft: 18, fontFamily: 'var(--sans)', fontSize: '0.92rem' }}>
|
||||
{page.backlinks.map((b) => (
|
||||
<li key={b.slug} style={{ marginBottom: 6 }}>
|
||||
<Link to={`/wiki/${b.slug}`} style={{ color: 'var(--accent)', textDecoration: 'none' }}>
|
||||
{b.title}
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
)}
|
||||
|
||||
<nav style={{ display: 'flex', justifyContent: 'flex-start', marginTop: 40 }}>
|
||||
<Link to="/wiki" className="pill">
|
||||
← All wiki pages
|
||||
|
||||
@@ -314,10 +314,10 @@ button[disabled] {
|
||||
|
||||
/* ===== Rich text editor (TipTap) ===== */
|
||||
.rte {
|
||||
position: relative;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
background: var(--bg);
|
||||
overflow: hidden;
|
||||
}
|
||||
.rte:focus-within {
|
||||
border-color: var(--accent);
|
||||
@@ -380,6 +380,72 @@ button[disabled] {
|
||||
color: var(--dim);
|
||||
pointer-events: none;
|
||||
}
|
||||
/* Internal-link picker popover */
|
||||
.rte-linkmenu {
|
||||
position: absolute;
|
||||
z-index: 20;
|
||||
top: 50px;
|
||||
left: 10px;
|
||||
width: min(360px, calc(100% - 20px));
|
||||
padding: 10px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
background: var(--panel-a);
|
||||
box-shadow: var(--shadow-card);
|
||||
}
|
||||
.rte-linkmenu-list {
|
||||
margin-top: 8px;
|
||||
max-height: 220px;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
}
|
||||
.rte-linkmenu-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
gap: 12px;
|
||||
padding: 7px 9px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: transparent;
|
||||
color: var(--text);
|
||||
font-family: var(--sans);
|
||||
font-size: 0.88rem;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
}
|
||||
.rte-linkmenu-item:hover {
|
||||
background: var(--blue);
|
||||
color: var(--ink);
|
||||
}
|
||||
.rte-linkmenu-slug {
|
||||
color: var(--dim);
|
||||
font-family: ui-monospace, Menlo, monospace;
|
||||
font-size: 0.76rem;
|
||||
}
|
||||
|
||||
/* ===== Wiki connectivity (tags + red links) ===== */
|
||||
.wiki-tag {
|
||||
display: inline-block;
|
||||
padding: 3px 10px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 999px;
|
||||
background: rgba(127, 153, 189, 0.1);
|
||||
color: var(--accent);
|
||||
font-family: var(--sans);
|
||||
font-size: 0.78rem;
|
||||
text-decoration: none;
|
||||
}
|
||||
.wiki-tag:hover {
|
||||
border-color: var(--accent);
|
||||
background: var(--blue);
|
||||
}
|
||||
.prose a.wiki-red-link {
|
||||
color: #d98b84;
|
||||
border-bottom: 1px dotted #d98b84;
|
||||
}
|
||||
|
||||
/* ===== Admin tables ===== */
|
||||
.adm-table {
|
||||
|
||||
@@ -58,6 +58,30 @@ CREATE TABLE IF NOT EXISTS wiki_pages (
|
||||
FULLTEXT INDEX idx_wiki_search (title, body)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- Wiki tags (many-to-many with pages).
|
||||
CREATE TABLE IF NOT EXISTS wiki_tags (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
slug VARCHAR(120) NOT NULL UNIQUE,
|
||||
label VARCHAR(120) NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS wiki_page_tags (
|
||||
page_id INT NOT NULL,
|
||||
tag_id INT NOT NULL,
|
||||
PRIMARY KEY (page_id, tag_id),
|
||||
CONSTRAINT fk_wpt_page FOREIGN KEY (page_id) REFERENCES wiki_pages(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_wpt_tag FOREIGN KEY (tag_id) REFERENCES wiki_tags(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- Internal-link index, rebuilt on each save. target_slug may point at a page
|
||||
-- that does not exist yet (a "red link").
|
||||
CREATE TABLE IF NOT EXISTS wiki_links (
|
||||
source_page_id INT NOT NULL,
|
||||
target_slug VARCHAR(120) NOT NULL,
|
||||
CONSTRAINT fk_wiki_links_src FOREIGN KEY (source_page_id) REFERENCES wiki_pages(id) ON DELETE CASCADE,
|
||||
INDEX idx_wiki_links_target (target_slug)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
`key` VARCHAR(64) PRIMARY KEY,
|
||||
value TEXT NULL,
|
||||
|
||||
@@ -14,22 +14,15 @@ const SUMMARY_COLS =
|
||||
const FROM = 'FROM wiki_pages p LEFT JOIN wiki_categories c ON c.id = p.category_id'
|
||||
const ORDER = 'ORDER BY p.sort_order ASC, p.title ASC'
|
||||
|
||||
// ── Page reads ─────────────────────────────────────────────────────────
|
||||
// Published summaries (public). Optional category filter by id.
|
||||
async function listPublishedSummaries(categoryId = null) {
|
||||
if (categoryId != null) {
|
||||
return query(
|
||||
`SELECT ${SUMMARY_COLS} ${FROM} WHERE p.published = 1 AND p.category_id = ? ${ORDER}`,
|
||||
[categoryId],
|
||||
)
|
||||
}
|
||||
return query(`SELECT ${SUMMARY_COLS} ${FROM} WHERE p.published = 1 ${ORDER}`)
|
||||
}
|
||||
|
||||
// All summaries (admin), with optional category / status filters.
|
||||
async function listAllSummaries({ categoryId = null, published = null } = {}) {
|
||||
// Shared summary query builder with optional category / tag / status filters.
|
||||
function buildSummaryQuery({ categoryId = null, tagId = null, published = null }) {
|
||||
const joins = []
|
||||
const where = []
|
||||
const params = []
|
||||
if (tagId != null) {
|
||||
joins.push('JOIN wiki_page_tags pt ON pt.page_id = p.id AND pt.tag_id = ?')
|
||||
params.push(tagId)
|
||||
}
|
||||
if (categoryId != null) {
|
||||
where.push('p.category_id = ?')
|
||||
params.push(categoryId)
|
||||
@@ -39,7 +32,22 @@ async function listAllSummaries({ categoryId = null, published = null } = {}) {
|
||||
params.push(published ? 1 : 0)
|
||||
}
|
||||
const clause = where.length ? `WHERE ${where.join(' AND ')}` : ''
|
||||
return query(`SELECT ${SUMMARY_COLS} ${FROM} ${clause} ${ORDER}`, params)
|
||||
return {
|
||||
sql: `SELECT ${SUMMARY_COLS} ${FROM} ${joins.join(' ')} ${clause} ${ORDER}`,
|
||||
params,
|
||||
}
|
||||
}
|
||||
|
||||
// Published summaries (public). Optional category / tag filters.
|
||||
async function listPublishedSummaries({ categoryId = null, tagId = null } = {}) {
|
||||
const { sql, params } = buildSummaryQuery({ categoryId, tagId, published: true })
|
||||
return query(sql, params)
|
||||
}
|
||||
|
||||
// All summaries (admin), with optional category / tag / status filters.
|
||||
async function listAllSummaries({ categoryId = null, tagId = null, published = null } = {}) {
|
||||
const { sql, params } = buildSummaryQuery({ categoryId, tagId, published })
|
||||
return query(sql, params)
|
||||
}
|
||||
|
||||
async function findBySlug(slug) {
|
||||
@@ -151,6 +159,80 @@ async function deleteCategory(id) {
|
||||
return query('DELETE FROM wiki_categories WHERE id = ?', [id])
|
||||
}
|
||||
|
||||
// ── Tags ───────────────────────────────────────────────────────────────
|
||||
async function listTags() {
|
||||
return query(
|
||||
`SELECT t.id, t.slug, t.label,
|
||||
(SELECT COUNT(*) FROM wiki_page_tags pt
|
||||
JOIN wiki_pages p ON p.id = pt.page_id
|
||||
WHERE pt.tag_id = t.id AND p.published = 1) AS published_count
|
||||
FROM wiki_tags t ORDER BY t.label ASC`,
|
||||
)
|
||||
}
|
||||
|
||||
async function findTagBySlug(slug) {
|
||||
const rows = await query('SELECT id, slug, label FROM wiki_tags WHERE slug = ? LIMIT 1', [slug])
|
||||
return rows[0] || null
|
||||
}
|
||||
|
||||
async function getTagsForPage(pageId) {
|
||||
return query(
|
||||
'SELECT t.slug, t.label FROM wiki_tags t ' +
|
||||
'JOIN wiki_page_tags pt ON pt.tag_id = t.id WHERE pt.page_id = ? ORDER BY t.label ASC',
|
||||
[pageId],
|
||||
)
|
||||
}
|
||||
|
||||
async function upsertTag(slug, label) {
|
||||
await query('INSERT INTO wiki_tags (slug, label) VALUES (?, ?) ON DUPLICATE KEY UPDATE label = VALUES(label)', [
|
||||
slug,
|
||||
label,
|
||||
])
|
||||
const rows = await query('SELECT id FROM wiki_tags WHERE slug = ? LIMIT 1', [slug])
|
||||
return rows[0].id
|
||||
}
|
||||
|
||||
async function setPageTags(pageId, tagIds) {
|
||||
await query('DELETE FROM wiki_page_tags WHERE page_id = ?', [pageId])
|
||||
for (const tagId of tagIds) {
|
||||
await query('INSERT IGNORE INTO wiki_page_tags (page_id, tag_id) VALUES (?, ?)', [pageId, tagId])
|
||||
}
|
||||
}
|
||||
|
||||
// Drop tags no longer attached to any page (keeps the tag list tidy).
|
||||
async function deleteOrphanTags() {
|
||||
return query('DELETE FROM wiki_tags WHERE id NOT IN (SELECT tag_id FROM wiki_page_tags)')
|
||||
}
|
||||
|
||||
// ── Internal links / backlinks ─────────────────────────────────────────
|
||||
async function clearLinks(pageId) {
|
||||
return query('DELETE FROM wiki_links WHERE source_page_id = ?', [pageId])
|
||||
}
|
||||
|
||||
async function insertLink(pageId, targetSlug) {
|
||||
return query('INSERT INTO wiki_links (source_page_id, target_slug) VALUES (?, ?)', [pageId, targetSlug])
|
||||
}
|
||||
|
||||
// Pages that link TO targetSlug (excludes the page linking to itself).
|
||||
async function getBacklinks(targetSlug, { publishedOnly = true } = {}) {
|
||||
const pub = publishedOnly ? 'AND p.published = 1' : ''
|
||||
return query(
|
||||
`SELECT DISTINCT p.slug, p.title FROM wiki_links l
|
||||
JOIN wiki_pages p ON p.id = l.source_page_id
|
||||
WHERE l.target_slug = ? AND p.slug <> ? ${pub}
|
||||
ORDER BY p.title ASC`,
|
||||
[targetSlug, targetSlug],
|
||||
)
|
||||
}
|
||||
|
||||
// Of the given slugs, which actually exist (for red-link detection).
|
||||
async function getExistingSlugs(slugs) {
|
||||
if (!slugs || slugs.length === 0) return new Set()
|
||||
const placeholders = slugs.map(() => '?').join(',')
|
||||
const rows = await query(`SELECT slug FROM wiki_pages WHERE slug IN (${placeholders})`, slugs)
|
||||
return new Set(rows.map((r) => r.slug))
|
||||
}
|
||||
|
||||
// ── Seeding (idempotent) ───────────────────────────────────────────────
|
||||
async function seedDefault(slug, title, body) {
|
||||
await query('INSERT IGNORE INTO wiki_pages (slug, title, body) VALUES (?, ?, ?)', [
|
||||
@@ -191,6 +273,16 @@ module.exports = {
|
||||
insertCategory,
|
||||
updateCategory,
|
||||
deleteCategory,
|
||||
listTags,
|
||||
findTagBySlug,
|
||||
getTagsForPage,
|
||||
upsertTag,
|
||||
setPageTags,
|
||||
deleteOrphanTags,
|
||||
clearLinks,
|
||||
insertLink,
|
||||
getBacklinks,
|
||||
getExistingSlugs,
|
||||
seedDefault,
|
||||
seedDefaultCategory,
|
||||
assignCategoryBySlug,
|
||||
|
||||
15
server/src/model/wiki/wiki.links.js
Normal file
15
server/src/model/wiki/wiki.links.js
Normal file
@@ -0,0 +1,15 @@
|
||||
// Extract internal wiki-link targets from a saved (already sanitized) body.
|
||||
// Internal links are anchors to /wiki/<slug> or elements carrying a
|
||||
// data-wiki-slug attribute. Returns a de-duplicated array of slugs.
|
||||
function extractTargets(html) {
|
||||
if (!html) return []
|
||||
const targets = new Set()
|
||||
const hrefRe = /href="\/wiki\/([a-z0-9-]+)"/g
|
||||
const dataRe = /data-wiki-slug="([a-z0-9-]+)"/g
|
||||
let m
|
||||
while ((m = hrefRe.exec(html))) targets.add(m[1])
|
||||
while ((m = dataRe.exec(html))) targets.add(m[1])
|
||||
return [...targets]
|
||||
}
|
||||
|
||||
module.exports = { extractTargets }
|
||||
@@ -1,45 +1,97 @@
|
||||
const wikiDb = require('./wiki.db')
|
||||
const { cleanBody } = require('../../utils/sanitizeHtml')
|
||||
const { extractTargets } = require('./wiki.links')
|
||||
|
||||
function slugifyTag(label) {
|
||||
return String(label)
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/(^-|-$)/g, '')
|
||||
}
|
||||
|
||||
// Upsert each label into wiki_tags and set the page's tag set exactly.
|
||||
async function syncTags(pageId, tags) {
|
||||
const ids = []
|
||||
const seen = new Set()
|
||||
for (const raw of tags) {
|
||||
const label = String(raw).trim()
|
||||
if (!label) continue
|
||||
const slug = slugifyTag(label)
|
||||
if (!slug || seen.has(slug)) continue
|
||||
seen.add(slug)
|
||||
ids.push(await wikiDb.upsertTag(slug, label))
|
||||
}
|
||||
await wikiDb.setPageTags(pageId, ids)
|
||||
await wikiDb.deleteOrphanTags()
|
||||
}
|
||||
|
||||
// Rebuild the page's outgoing internal-link rows from its (sanitized) body.
|
||||
async function rebuildLinks(pageId, html) {
|
||||
await wikiDb.clearLinks(pageId)
|
||||
for (const target of extractTargets(html)) {
|
||||
await wikiDb.insertLink(pageId, target)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Pages ──────────────────────────────────────────────────────────────
|
||||
async function listPublished(categoryId = null) {
|
||||
return wikiDb.listPublishedSummaries(categoryId)
|
||||
async function listPublished(filters = {}) {
|
||||
return wikiDb.listPublishedSummaries(filters)
|
||||
}
|
||||
|
||||
async function listAll(filters = {}) {
|
||||
return wikiDb.listAllSummaries(filters)
|
||||
}
|
||||
|
||||
// Admin detail: page + its tags.
|
||||
async function getBySlug(slug) {
|
||||
return wikiDb.findBySlug(slug)
|
||||
const page = await wikiDb.findBySlug(slug)
|
||||
if (!page) return null
|
||||
page.tags = await wikiDb.getTagsForPage(page.id)
|
||||
return page
|
||||
}
|
||||
|
||||
// Public detail: page + tags + backlinks + missing (red) link targets.
|
||||
async function getPublishedBySlug(slug) {
|
||||
return wikiDb.findPublishedBySlug(slug)
|
||||
const page = await wikiDb.findPublishedBySlug(slug)
|
||||
if (!page) return null
|
||||
page.tags = await wikiDb.getTagsForPage(page.id)
|
||||
page.backlinks = await wikiDb.getBacklinks(slug, { publishedOnly: true })
|
||||
const targets = extractTargets(page.body)
|
||||
const existing = await wikiDb.getExistingSlugs(targets)
|
||||
page.missing_links = targets.filter((t) => !existing.has(t))
|
||||
return page
|
||||
}
|
||||
|
||||
async function create({ slug, title, body, excerpt, categoryId, published, updatedBy }) {
|
||||
async function create({ slug, title, body, excerpt, categoryId, published, updatedBy, tags }) {
|
||||
const clean = cleanBody(body)
|
||||
await wikiDb.insert({
|
||||
slug,
|
||||
title,
|
||||
body: cleanBody(body),
|
||||
body: clean,
|
||||
excerpt: excerpt || null,
|
||||
categoryId: categoryId ?? null,
|
||||
published: published !== false, // default published unless explicitly false
|
||||
published: published !== false,
|
||||
updatedBy,
|
||||
})
|
||||
return wikiDb.findBySlug(slug)
|
||||
const page = await wikiDb.findBySlug(slug)
|
||||
if (Array.isArray(tags)) await syncTags(page.id, tags)
|
||||
await rebuildLinks(page.id, clean)
|
||||
return getBySlug(slug)
|
||||
}
|
||||
|
||||
// Partial update — only keys present in `input` are written. Body is sanitized;
|
||||
// published_at is stamped the first time a page goes live.
|
||||
// Partial update — only keys present in `input` are written.
|
||||
async function update(slug, input) {
|
||||
const current = await wikiDb.findBySlug(slug)
|
||||
if (!current) return null
|
||||
|
||||
const fields = { updated_by: input.updatedBy ?? null }
|
||||
let cleanForLinks = null
|
||||
if ('title' in input) fields.title = input.title
|
||||
if ('body' in input) fields.body = cleanBody(input.body)
|
||||
if ('body' in input) {
|
||||
fields.body = cleanBody(input.body)
|
||||
cleanForLinks = fields.body
|
||||
}
|
||||
if ('excerpt' in input) fields.excerpt = input.excerpt || null
|
||||
if ('categoryId' in input) fields.category_id = input.categoryId ?? null
|
||||
if ('published' in input) {
|
||||
@@ -48,7 +100,9 @@ async function update(slug, input) {
|
||||
}
|
||||
|
||||
await wikiDb.updateBySlug(slug, fields)
|
||||
return wikiDb.findBySlug(slug)
|
||||
if (Array.isArray(input.tags)) await syncTags(current.id, input.tags)
|
||||
if (cleanForLinks != null) await rebuildLinks(current.id, cleanForLinks)
|
||||
return getBySlug(slug)
|
||||
}
|
||||
|
||||
async function setPublished(slug, published) {
|
||||
@@ -57,11 +111,22 @@ async function setPublished(slug, published) {
|
||||
const fields = { published: published ? 1 : 0 }
|
||||
if (published && !current.published_at) fields.published_at = new Date()
|
||||
await wikiDb.updateBySlug(slug, fields)
|
||||
return wikiDb.findBySlug(slug)
|
||||
return getBySlug(slug)
|
||||
}
|
||||
|
||||
async function remove(slug) {
|
||||
return wikiDb.deleteBySlug(slug)
|
||||
const res = await wikiDb.deleteBySlug(slug)
|
||||
await wikiDb.deleteOrphanTags() // page_tags cascade on delete; drop now-empty tags
|
||||
return res
|
||||
}
|
||||
|
||||
// ── Tags ───────────────────────────────────────────────────────────────
|
||||
async function listTags() {
|
||||
return wikiDb.listTags()
|
||||
}
|
||||
|
||||
async function getTagBySlug(slug) {
|
||||
return wikiDb.findTagBySlug(slug)
|
||||
}
|
||||
|
||||
// ── Categories ─────────────────────────────────────────────────────────
|
||||
@@ -97,7 +162,6 @@ async function removeCategory(id) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
list: listPublished, // back-compat alias (old callers expected published list)
|
||||
listPublished,
|
||||
listAll,
|
||||
getBySlug,
|
||||
@@ -106,6 +170,8 @@ module.exports = {
|
||||
update,
|
||||
setPublished,
|
||||
remove,
|
||||
listTags,
|
||||
getTagBySlug,
|
||||
listCategories,
|
||||
getCategoryBySlug,
|
||||
getCategoryById,
|
||||
|
||||
@@ -177,6 +177,10 @@ async function listWiki(req, res) {
|
||||
const category = await wiki.getCategoryBySlug(req.query.category)
|
||||
filters.categoryId = category ? category.id : -1 // unknown → match nothing
|
||||
}
|
||||
if (req.query.tag) {
|
||||
const tag = await wiki.getTagBySlug(req.query.tag)
|
||||
filters.tagId = tag ? tag.id : -1 // unknown → match nothing
|
||||
}
|
||||
if (req.query.status === 'draft') filters.published = false
|
||||
if (req.query.status === 'published') filters.published = true
|
||||
return res.json(await wiki.listAll(filters))
|
||||
@@ -221,6 +225,7 @@ async function createWiki(req, res) {
|
||||
excerpt: req.body.excerpt || null,
|
||||
categoryId: cat.value,
|
||||
published: req.body.published !== false,
|
||||
tags: Array.isArray(req.body.tags) ? req.body.tags : undefined,
|
||||
updatedBy: req.user.id,
|
||||
})
|
||||
await activity.log({ req, action: 'wiki.create', detail: { slug: page.slug } })
|
||||
@@ -241,6 +246,7 @@ async function updateWiki(req, res) {
|
||||
if ('body' in req.body) input.body = req.body.body || null
|
||||
if ('excerpt' in req.body) input.excerpt = req.body.excerpt || null
|
||||
if ('published' in req.body) input.published = Boolean(req.body.published)
|
||||
if ('tags' in req.body) input.tags = Array.isArray(req.body.tags) ? req.body.tags : []
|
||||
if ('category_id' in req.body) {
|
||||
const cat = await resolveCategoryId(req.body)
|
||||
if (!cat.ok) return res.status(400).json({ message: 'Unknown category' })
|
||||
@@ -282,6 +288,15 @@ async function deleteWiki(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Wiki tags ──────────────────────────────────────────────────────────
|
||||
async function listWikiTags(req, res) {
|
||||
try {
|
||||
return res.json(await wiki.listTags())
|
||||
} catch (err) {
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
}
|
||||
|
||||
// ── Wiki categories ────────────────────────────────────────────────────
|
||||
async function listWikiCategories(req, res) {
|
||||
try {
|
||||
@@ -473,6 +488,7 @@ module.exports = {
|
||||
updateWiki,
|
||||
publishWiki,
|
||||
deleteWiki,
|
||||
listWikiTags,
|
||||
listWikiCategories,
|
||||
createWikiCategory,
|
||||
updateWikiCategory,
|
||||
|
||||
@@ -90,6 +90,9 @@ adminRouter.put(
|
||||
)
|
||||
adminRouter.delete('/wiki/categories/:id', param('id').isInt(), validate, ctrl.deleteWikiCategory)
|
||||
|
||||
// ── Wiki tags ──────────────────────────────────────────────────────────
|
||||
adminRouter.get('/wiki/tags', ctrl.listWikiTags)
|
||||
|
||||
// ── Wiki pages ─────────────────────────────────────────────────────────
|
||||
adminRouter.get('/wiki', ctrl.listWiki)
|
||||
adminRouter.post(
|
||||
@@ -99,6 +102,7 @@ adminRouter.post(
|
||||
body('excerpt').optional({ values: 'falsy' }).isString().isLength({ max: 400 }),
|
||||
body('category_id').optional({ values: 'null' }).isInt(),
|
||||
body('published').optional().isBoolean(),
|
||||
body('tags').optional().isArray(),
|
||||
validate,
|
||||
ctrl.createWiki,
|
||||
)
|
||||
@@ -109,6 +113,7 @@ adminRouter.put(
|
||||
body('excerpt').optional({ values: 'falsy' }).isString().isLength({ max: 400 }),
|
||||
body('category_id').optional({ values: 'null' }).isInt(),
|
||||
body('published').optional().isBoolean(),
|
||||
body('tags').optional().isArray(),
|
||||
validate,
|
||||
ctrl.updateWiki,
|
||||
)
|
||||
|
||||
@@ -58,15 +58,28 @@ async function getWikiCategories(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getWikiTags(req, res) {
|
||||
try {
|
||||
return res.json(await wiki.listTags())
|
||||
} catch (err) {
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
}
|
||||
|
||||
async function getWikiList(req, res) {
|
||||
try {
|
||||
let categoryId = null
|
||||
const filters = {}
|
||||
if (req.query.category) {
|
||||
const category = await wiki.getCategoryBySlug(req.query.category)
|
||||
if (!category) return res.json([]) // unknown category → no pages
|
||||
categoryId = category.id
|
||||
filters.categoryId = category.id
|
||||
}
|
||||
return res.json(await wiki.listPublished(categoryId))
|
||||
if (req.query.tag) {
|
||||
const tag = await wiki.getTagBySlug(req.query.tag)
|
||||
if (!tag) return res.json([]) // unknown tag → no pages
|
||||
filters.tagId = tag.id
|
||||
}
|
||||
return res.json(await wiki.listPublished(filters))
|
||||
} catch (err) {
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
@@ -100,6 +113,7 @@ module.exports = {
|
||||
getPosts,
|
||||
getPost,
|
||||
getWikiCategories,
|
||||
getWikiTags,
|
||||
getWikiList,
|
||||
getWikiPage,
|
||||
contact,
|
||||
|
||||
@@ -25,8 +25,9 @@ publicRouter.post(
|
||||
publicRouter.get('/posts/:category', siteMode, ctrl.getPosts)
|
||||
publicRouter.get('/posts/:category/:idOrSlug', siteMode, ctrl.getPost)
|
||||
publicRouter.get('/wiki', siteMode, ctrl.getWikiList)
|
||||
// Static path must precede the :slug route so it isn't captured as a slug.
|
||||
// Static paths must precede the :slug route so they aren't captured as a slug.
|
||||
publicRouter.get('/wiki/categories', siteMode, ctrl.getWikiCategories)
|
||||
publicRouter.get('/wiki/tags', siteMode, ctrl.getWikiTags)
|
||||
publicRouter.get('/wiki/:slug', siteMode, ctrl.getWikiPage)
|
||||
|
||||
module.exports = publicRouter
|
||||
|
||||
Reference in New Issue
Block a user