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:
2026-06-27 11:25:15 -05:00
parent 4a7dbf0085
commit 7c081ae749
15 changed files with 516 additions and 70 deletions

View File

@@ -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,