--- import fs from 'node:fs' import path from 'node:path' import { Prism } from '@astrojs/prism' interface Props { /** * The CSS class(es) to be added to the `pre` HTML element when rendering code blocks in Markdown. * Note that this prop is not used when the component is invoked directly. */ class?: string /** * The code to highlight. * If an array is passed, elements will be joined with a new line. */ code?: string | string[] /** * The CSS class(es) to be added to the `div` wrapper HTML element. */ containerClass?: string /** * The language to use for highlighting. * @see https://prismjs.com/#supported-languages */ lang?: string /** * If the `filePath` prop is defined, this prop can be used to specify a regex containing a match group to extract * only a part of the file. */ fileMatch?: string /** * A path to the file containing the code to highlight relative to the root of the repository. * This takes precedence over the `code` prop. */ filePath?: string } const { class: className, code, containerClass, fileMatch, filePath, lang } = Astro.props let codeToDisplay = filePath ? fs.readFileSync(path.join(process.cwd(), filePath), 'utf8') : Array.isArray(code) ? code.join('\n') : code if (filePath && fileMatch && codeToDisplay) { const match = codeToDisplay.match(new RegExp(fileMatch)) if (!match || !match[0]) { throw new Error(`The file at ${filePath} does not contains a match for the regex '${fileMatch}'.`) } codeToDisplay = match[0] } ---
{ Astro.slots.has('pre') ? ( ) : (
) }
{ codeToDisplay && lang ? ( ) : ( /* prettier-ignore */
) }