import Markdoc from '@markdoc/markdoc';
import type { Sidenote } from '@lib/types/content';

export function extractSidenotes(raw: string): Sidenote[] {
  const ast = Markdoc.parse(raw);
  const sidenotes: Sidenote[] = [];

  for (const node of ast.walk()) {
    if (node.type === 'tag' && node.tag === 'Sidenote') {
      const attrs = node.attributes;
      sidenotes.push({
        id: attrs.id,
        marker: attrs.marker ?? '⋄',
        title: attrs.title ?? '',
        type: attrs.type ?? 'default',
        content: attrs.content,
      });
    }
  }
  return sidenotes;
}
