---
import fs from 'node:fs'
import { getConfig } from '@libs/config'
import Code from '@shortcodes/Code.astro'
// Prints everything between `// js-docs-start "name"` and `// js-docs-end "name"`
// comments in the docs.
interface Props {
/**
* Reference name used to find the content to display within the content of the `file` prop.
*/
name: string
/**
* File path that contains the content to display relative to the root of the repository.
*/
file: string
}
const { name, file } = Astro.props
if (!name || !file) {
throw new Error(
`Missing required parameter(s) for the '' component, expected both 'name' and 'file' but got 'name: ${name}' and 'file: ${file}'.`
)
}
let content: string
try {
const fileContent = fs.readFileSync(file, 'utf8')
const matches = fileContent.match(new RegExp(`\/\/ js-docs-start ${name}\n((?:.|\n)*)\/\/ js-docs-end ${name}`, 'm'))
if (!matches || !matches[1]) {
throw new Error(
`Failed to find the content named '${name}', make sure that '// js-docs-start ${name}' and '// js-docs-end ${name}' are defined.`
)
}
content = matches[1]
// Fix the identation by removing extra spaces at the beginning of each line
const lines = content.split('\n')
const spaceCounts = lines.filter((line) => line.trim().length > 0).map((line) => line.match(/^ */)[0].length)
const minSpaces = spaceCounts.length ? Math.min(...spaceCounts) : 0
content = lines.map((line) => line.slice(minSpaces)).join('\n')
} catch (error) {
throw new Error(`Failed to find the content to render in the '' component at '${file}'.`, {
cause: error
})
}
---