|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | + |
| 4 | +export const SITE_URL = 'https://bref.sh' |
| 5 | + |
| 6 | +const contentDir = path.join(process.cwd(), 'content') |
| 7 | + |
| 8 | +// Top-level content folders whose pages are served as Markdown (for AI agents). |
| 9 | +// The marketing pages (home, /cloud, /support...) are React components, not |
| 10 | +// prose, so they are not exposed here. |
| 11 | +const MARKDOWN_ROOTS = ['docs', 'news'] |
| 12 | + |
| 13 | +/** |
| 14 | + * Resolve a site route (e.g. "docs/laravel/getting-started", "docs", "news/04-august-2026") |
| 15 | + * to its MDX source file, or null if none exists. |
| 16 | + */ |
| 17 | +export function findSourceFile(route) { |
| 18 | + // Normalize first so that `..` segments cannot escape the allowed roots |
| 19 | + const clean = path.posix.normalize('/' + route).replace(/^\/+|\/+$/g, '') |
| 20 | + const root = clean.split('/')[0] |
| 21 | + if (!MARKDOWN_ROOTS.includes(root)) return null |
| 22 | + |
| 23 | + const candidates = [ |
| 24 | + path.join(contentDir, `${clean}.mdx`), |
| 25 | + path.join(contentDir, `${clean}.md`), |
| 26 | + path.join(contentDir, clean, 'index.mdx'), |
| 27 | + ] |
| 28 | + // Reject any path that escapes content/ (e.g. via `..` segments). |
| 29 | + const resolvedContentDir = path.resolve(contentDir) + path.sep |
| 30 | + return ( |
| 31 | + candidates.find( |
| 32 | + candidate => path.resolve(candidate).startsWith(resolvedContentDir) && fs.existsSync(candidate) |
| 33 | + ) ?? null |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Turn the URL of a page (relative to the site root, without leading slash) |
| 39 | + * into the canonical HTML URL of that page. |
| 40 | + */ |
| 41 | +export function routeToUrl(route) { |
| 42 | + const clean = route.replace(/^\/+|\/+$/g, '') |
| 43 | + return clean ? `${SITE_URL}/${clean}` : SITE_URL |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Rewrite a link target found in a MDX file into an absolute URL. |
| 48 | + * `fileRoute` is the route of the file that contains the link (e.g. "docs/laravel/index"). |
| 49 | + */ |
| 50 | +function absoluteUrl(target, fileRoute) { |
| 51 | + if (/^(https?:|mailto:|#|data:)/.test(target)) return target |
| 52 | + |
| 53 | + const [pathPart, hash] = target.split('#') |
| 54 | + // Relative links are resolved from the directory of the file |
| 55 | + let resolved = pathPart.startsWith('/') |
| 56 | + ? pathPart |
| 57 | + : path.posix.join(path.posix.dirname('/' + fileRoute), pathPart) |
| 58 | + // `./getting-started.mdx` -> `/docs/laravel/getting-started` |
| 59 | + resolved = resolved.replace(/\.mdx?$/, '').replace(/\/index$/, '') |
| 60 | + return `${SITE_URL}${resolved}${hash ? '#' + hash : ''}` |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Convert the raw MDX source of a page into Markdown suitable for AI agents: |
| 65 | + * strips the frontmatter, imports and JSX comments, and makes all links absolute. |
| 66 | + * The occasional JSX component (tabs, cards...) is left as is. |
| 67 | + * |
| 68 | + * `fileRoute` is the route of the source file relative to content/, without |
| 69 | + * extension (e.g. "docs/laravel/index"). |
| 70 | + */ |
| 71 | +export function toMarkdown(source, fileRoute) { |
| 72 | + let content = source |
| 73 | + // Strip YAML frontmatter |
| 74 | + content = content.replace(/^---\n[\s\S]*?\n---\n/, '') |
| 75 | + // Strip JSX comments (invisible when rendered, but not valid Markdown) |
| 76 | + content = content.replace(/\{\/\*[\s\S]*?\*\/\}\n?/g, '') |
| 77 | + |
| 78 | + // The rest only applies outside of code blocks and inline code, so that code |
| 79 | + // samples (e.g. JS `import` statements or HTML links) are preserved. |
| 80 | + content = content |
| 81 | + .split(/(```[\s\S]*?```|`[^`\n]*`)/) |
| 82 | + .map((part, index) => (index % 2 === 1 ? part : transformProse(part, fileRoute))) |
| 83 | + .join('') |
| 84 | + |
| 85 | + // Clean up excessive blank lines at the start |
| 86 | + content = content.replace(/^\s*\n+/, '') |
| 87 | + return content |
| 88 | +} |
| 89 | + |
| 90 | +function transformProse(content, fileRoute) { |
| 91 | + // Strip import statements |
| 92 | + content = content.replace(/^import\s+.*?(?:from\s+['"].*?['"])?;?\s*$/gm, '') |
| 93 | + // Absolute URLs in Markdown links: [text](./page.mdx#anchor) -> [text](https://bref.sh/docs/page#anchor) |
| 94 | + content = content.replace(/\]\(([^)\s]+)((?:\s[^)]*)?)\)/g, (match, target, title) => { |
| 95 | + return `](${absoluteUrl(target, fileRoute)}${title})` |
| 96 | + }) |
| 97 | + // Absolute URLs in JSX/HTML links: href="./page.mdx" -> href="https://bref.sh/docs/page" |
| 98 | + content = content.replace(/href="([^"]+)"/g, (match, target) => `href="${absoluteUrl(target, fileRoute)}"`) |
| 99 | + return content |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Read a page and return its Markdown, or null if the page has no source file. |
| 104 | + */ |
| 105 | +export function readMarkdown(route) { |
| 106 | + const filePath = findSourceFile(route) |
| 107 | + if (!filePath) return null |
| 108 | + const fileRoute = path.relative(contentDir, filePath).replace(/\.mdx?$/, '') |
| 109 | + return toMarkdown(fs.readFileSync(filePath, 'utf8'), fileRoute) |
| 110 | +} |
0 commit comments