File size: 1,718 Bytes
0f84d64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const DOCS_PATH = path.resolve(__dirname, '..', 'app-docs.md');

let cachedSections = [];
let cachedMtimeMs = 0;

function slugify(text) {
  return String(text || '')
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '');
}

async function loadSections() {
  const stats = await fs.stat(DOCS_PATH);
  if (cachedSections.length && cachedMtimeMs === stats.mtimeMs) return cachedSections;

  const raw = await fs.readFile(DOCS_PATH, 'utf8');
  const parts = raw.split(/^##\s+/m);
  const first = parts.shift() || '';
  const sections = [];

  const overview = first.replace(/^#.*$/m, '').trim();
  if (overview) {
    sections.push({
      id: 'overview',
      title: 'Overview',
      content: overview,
    });
  }

  parts.forEach((part) => {
    const lines = part.split('\n');
    const title = (lines.shift() || '').trim();
    const content = lines.join('\n').trim();
    if (!title || !content) return;
    sections.push({
      id: slugify(title),
      title,
      content,
    });
  });

  cachedSections = sections;
  cachedMtimeMs = stats.mtimeMs;
  return sections;
}

export async function listAppDocSections() {
  const sections = await loadSections();
  return sections.map(({ id, title }) => ({ id, title }));
}

export async function readAppDocSection(sectionId) {
  const target = String(sectionId || '').trim().toLowerCase();
  if (!target) return null;
  const sections = await loadSections();
  return sections.find((section) =>
    section.id === target || section.title.toLowerCase() === target
  ) || null;
}