Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix: extract headings from raw content for fragment validation
Sushmithamallesh029b904f9427
diff --git a/docs/bunfig.toml b/docs/bunfig.tomlnew file mode 100644index 000000000..59df4b2f3--- /dev/null+++ b/docs/bunfig.toml@@ -0,0 +1 @@+preload = ["./scripts/preload.ts"]diff --git a/docs/scripts/preload.ts b/docs/scripts/preload.tsnew file mode 100644index 000000000..ed87d79fa--- /dev/null+++ b/docs/scripts/preload.ts@@ -0,0 +1,3 @@+import { createMdxPlugin } from 'fumadocs-mdx/bun';++Bun.plugin(createMdxPlugin());diff --git a/docs/scripts/validate-links.ts b/docs/scripts/validate-links.tsindex 466b5ecba..95587e733 100644--- a/docs/scripts/validate-links.ts+++ b/docs/scripts/validate-links.ts@@ -6,7 +6,6 @@ import {scanURLs,validateFiles,} from 'next-validate-link';-import type { InferPageType } from 'fumadocs-core/source';import {source,referenceSource,@@ -20,27 +19,89 @@ type AnySource =| typeof cookbooksSource| typeof toolkitsSource;+type PageOf = ReturnType<AnySource['getPages']>[number];++/**+ * Slugify a heading the same way rehype-slug does.+ */+function slugify(text: string): string {+ return text+ .toLowerCase()+ .replace(/[^\w\s-]/g, '')+ .replace(/[\s]+/g, '-')+ .replace(/-+/g, '-')+ .replace(/^-|-$/g, '');+}++/**+ * Extract heading anchors from raw MDX/markdown content.+ * Falls back to this when data.toc is unavailable (outside Next.js runtime).+ */+function extractHeadingsFromContent(content: string): string[] {+ const headings: string[] = [];+ for (const line of content.split('\n')) {+ const match = line.match(/^#{1,6}\s+(.+)$/);+ if (match) {+ headings.push(slugify(match[1]));+ }+ }+ return headings;+}++/**+ * Get headings for a page, trying data.toc first then falling back to raw content parsing.+ */+async function getHeadingsForPage(page: PageOf): Promise<string[]> {+ if (page.data.toc?.length) {+ return page.data.toc.map((item: { url: string }) => item.url.slice(1));+ }+ if ('getText' in page.data) {+ try {+ const content = await (page.data as { getText: (mode: string) => Promise<string> }).getText('raw');+ return extractHeadingsFromContent(content);+ } catch {+ // fall through+ }+ }+ if (page.absolutePath) {+ try {+ const content = await readFile(page.absolutePath, 'utf-8');+ return extractHeadingsFromContent(content);+ } catch {+ // fall through+ }+ }+ return [];+}++/**+ * Build populate entries for a source, resolving headings asynchronously.+ */+async function buildPopulateEntries(src: AnySource) {+ return Promise.all(+ src.getPages().map(async (page) => ({+ value: { slug: page.slugs },+ hashes: await getHeadingsForPage(page),+ })),+ );+}+async function checkLinks() {+ const [docsEntries, refEntries, cookbookEntries, toolkitEntries] = await Promise.all([+ buildPopulateEntries(source),+ buildPopulateEntries(referenceSource),+ buildPopulateEntries(cookbooksSource),+ buildPopulateEntries(toolkitsSource),+ ]);+const scanned = await scanURLs({preset: 'next',populate: {- // Dynamic routes (keys must include (home) route group to match app directory structure)- '(home)/docs/[[...slug]]': source.getPages().map((page) => ({- value: { slug: page.slugs },- hashes: getHeadings(page),- })),- '(home)/reference/[[...slug]]': referenceSource.getPages().map((page) => ({- value: { slug: page.slugs },- hashes: getHeadings(page),- })),- '(home)/cookbooks/[[...slug]]': cookbooksSource.getPages().map((page) => ({- value: { slug: page.slugs },- hashes: getHeadings(page),- })),- '(home)/toolkits/[[...slug]]': toolkitsSource.getPages().map((page) => ({- value: { slug: page.slugs },- hashes: getHeadings(page),- })),+ // Keys must include (home) route group to match app directory structure+ '(home)/docs/[[...slug]]': docsEntries,+ '(home)/reference/[[...slug]]': refEntries,+ '(home)/cookbooks/[[...slug]]': cookbookEntries,+ '(home)/toolkits/[[...slug]]': toolkitEntries,},});@@ -70,11 +131,6 @@ async function checkLinks() {}}-function getHeadings({ data }: InferPageType<AnySource>): string[] {- if (!data.toc) return [];- return data.toc.map((item) => item.url.slice(1));-}-async function getFiles(): Promise<FileObject[]> {const sources = [source, referenceSource, cookbooksSource, toolkitsSource];const allFiles: FileObject[] = [];