---
import Base from '../../layouts/Base.astro';
import fs from 'node:fs';
import path from 'node:path';

export function getStaticPaths() {
  const dataDir = path.join(process.cwd(), 'src/data/articles');
  if (!fs.existsSync(dataDir)) return [];
  const files = fs.readdirSync(dataDir).filter(f => f.endsWith('.html'));
  return files.map(file => ({
    params: { slug: file.replace('.html', '') },
    props: { filePath: path.join(dataDir, file) }
  }));
}

const { filePath } = Astro.props;
const html = fs.readFileSync(filePath, 'utf-8');
const titleMatch = html.match(/<title>([^<]+)<\/title>/i);
const title = titleMatch ? titleMatch[1] : 'Articles';
let content = html;
const bodyStart = content.indexOf('<body');
if (bodyStart > 0) { content = content.substring(content.indexOf('>', bodyStart) + 1); }
const bodyEnd = content.indexOf('</body>');
if (bodyEnd > 0) { content = content.substring(0, bodyEnd); }
const cw = content.indexOf('class="d-content-wrap"');
if (cw > 0) {
  content = content.substring(content.lastIndexOf('<div', cw));
  const ft = content.indexOf('<div class="d-footer">');
  if (ft > 0) content = content.substring(0, ft);
}
---
<Base title={title}>
  <Fragment set:html={content} />
</Base>
