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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user