mirror of
https://github.com/sern-handler/website
synced 2026-06-23 00:02:30 +00:00
feat: migrate to starlight
This commit is contained in:
21
node_modules/astro-expressive-code/LICENSE
generated
vendored
Normal file
21
node_modules/astro-expressive-code/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Tibor Schiemann
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
15
node_modules/astro-expressive-code/README.md
generated
vendored
Normal file
15
node_modules/astro-expressive-code/README.md
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
# astro-expressive-code [](https://www.npmjs.com/package/astro-expressive-code) [](https://npmjs.org/package/astro-expressive-code)
|
||||
|
||||
This package is an [Astro](https://astro.build/) integration to automatically render code blocks in any markdown / MDX content on your Astro site using [Expressive Code](https://expressive-code.com/).
|
||||
|
||||
## Documentation
|
||||
|
||||
[Read the Expressive Code docs](https://expressive-code.com/) to learn more about the features provided by Expressive Code and this integration.
|
||||
|
||||
## When should I use this?
|
||||
|
||||
When you're writing content for your Astro site in markdown / MDX and want to improve the design and functionality of all contained code blocks using Expressive Code.
|
||||
|
||||
## Installation
|
||||
|
||||
Read the [installation instructions](https://expressive-code.com/installation/) to learn how to install Expressive Code on your Astro site.
|
||||
70
node_modules/astro-expressive-code/components/Code.astro
generated
vendored
Normal file
70
node_modules/astro-expressive-code/components/Code.astro
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
---
|
||||
import { addClassName, toHtml } from 'rehype-expressive-code/hast'
|
||||
import { getPageData } from './page-data'
|
||||
import { getRenderer } from './renderer'
|
||||
import type { CodeProps as Props, MarkerValueType } from './types'
|
||||
|
||||
function formatMessage(...messageParts: string[]) {
|
||||
return messageParts.map((part) => part.replace(/\s+/g, ' ')).join('\n\n')
|
||||
}
|
||||
|
||||
async function renderToHtml() {
|
||||
const defaultSlotContent = await Astro.slots.render('default')
|
||||
if (defaultSlotContent?.trim().length) {
|
||||
throw new Error(
|
||||
formatMessage(
|
||||
`Unsupported child content was found inside the component.
|
||||
The code to render must be passed to the \`code\` prop as a string.`,
|
||||
`Please remove the following child content:\n${defaultSlotContent}`
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
let { code, lang = '', meta = '', locale, class: className, ...props } = Astro.props
|
||||
|
||||
if (!code || !code.trim().length) {
|
||||
throw new Error('Missing code to render. The `code` prop must be set to a non-empty string.')
|
||||
}
|
||||
|
||||
const pageData = getPageData(Astro.request)
|
||||
// Note: It's important to store the incremented index in a local variable immediately,
|
||||
// as the `pageData` object is shared between all components on the current page
|
||||
// and can be changed by other Code components during the `await` calls below
|
||||
const groupIndex = ++pageData.blockGroupIndex
|
||||
|
||||
const renderer = await getRenderer()
|
||||
|
||||
const { renderedGroupAst } = await renderer.ec.render({
|
||||
code,
|
||||
language: lang,
|
||||
meta,
|
||||
locale,
|
||||
parentDocument: {
|
||||
positionInDocument: {
|
||||
groupIndex,
|
||||
},
|
||||
},
|
||||
props,
|
||||
})
|
||||
|
||||
if (renderedGroupAst?.type === 'element') {
|
||||
if (className) {
|
||||
const classNames = className.split(' ')
|
||||
classNames.forEach((className) => addClassName(renderedGroupAst, className))
|
||||
}
|
||||
}
|
||||
|
||||
return toHtml(renderedGroupAst)
|
||||
}
|
||||
|
||||
let html = ''
|
||||
try {
|
||||
html = await renderToHtml()
|
||||
} catch (err) {
|
||||
const prefix = `Failed to render a \`<Code>\` component on page ${Astro.request.url}:`
|
||||
const error = err instanceof Error ? err : new Error(String(err))
|
||||
throw new Error(`${prefix}\n\n${error.message}`, { cause: error })
|
||||
}
|
||||
---
|
||||
|
||||
<Fragment set:html={html} />
|
||||
17
node_modules/astro-expressive-code/components/index.ts
generated
vendored
Normal file
17
node_modules/astro-expressive-code/components/index.ts
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { Component, CodeProps } from './types'
|
||||
import { default as CodeComponent } from './Code.astro'
|
||||
|
||||
/**
|
||||
* Renders a code block to HTML using [Expressive Code](https://expressive-code.com/).
|
||||
*
|
||||
* The code to be rendered must be passed to the `code` prop.
|
||||
* Passing code as children is not supported to avoid escaping issues.
|
||||
*
|
||||
* To get syntax highlighted output, also set the `lang` prop to a valid
|
||||
* [language identifier](https://expressive-code.com/key-features/syntax-highlighting/#supported-languages)
|
||||
* (e.g. `js`, `ts`, `astro`, `html`, `bash`, or many others).
|
||||
*
|
||||
* You can also set the `meta` prop to a string. It supports the same syntax that you would use
|
||||
* in fenced code blocks in markdown/MDX files after the language identifier.
|
||||
*/
|
||||
export const Code = CodeComponent as Component<CodeProps>
|
||||
18
node_modules/astro-expressive-code/components/page-data.ts
generated
vendored
Normal file
18
node_modules/astro-expressive-code/components/page-data.ts
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
export type PageData = {
|
||||
url: string
|
||||
blockGroupIndex: number
|
||||
}
|
||||
|
||||
const pageDataMap = new Map<Request, PageData>()
|
||||
|
||||
export function getPageData(request: Request): PageData {
|
||||
let data = pageDataMap.get(request)
|
||||
if (!data) {
|
||||
data = {
|
||||
url: request.url,
|
||||
blockGroupIndex: -1,
|
||||
}
|
||||
pageDataMap.set(request, data)
|
||||
}
|
||||
return data
|
||||
}
|
||||
37
node_modules/astro-expressive-code/components/renderer.ts
generated
vendored
Normal file
37
node_modules/astro-expressive-code/components/renderer.ts
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
let cachedRenderer: ReturnType<typeof createRenderer> | undefined = undefined
|
||||
|
||||
export async function getRenderer() {
|
||||
if (!cachedRenderer) {
|
||||
cachedRenderer = createRenderer()
|
||||
}
|
||||
return await cachedRenderer
|
||||
}
|
||||
|
||||
async function createRenderer() {
|
||||
const { astroConfig, ecConfigFileOptions, ecIntegrationOptions = {} } = await import('virtual:astro-expressive-code/config')
|
||||
const { createAstroRenderer } = await import('virtual:astro-expressive-code/api')
|
||||
|
||||
const strIntegrationOptions = JSON.stringify(ecIntegrationOptions)
|
||||
if (strIntegrationOptions.includes('"[Function]"') || strIntegrationOptions.includes("'[Circular]'")) {
|
||||
throw new Error(
|
||||
`Your Astro config file contains Expressive Code options that are not serializable to JSON.
|
||||
To use the \`<Code>\` component, please create a separate config file called \`ec.config.mjs\`
|
||||
in your project root, move your Expressive Code options object into the config file,
|
||||
and export it as the default export.`.replace(/\s+/g, ' ')
|
||||
)
|
||||
}
|
||||
|
||||
let mergedEcConfig = { ...ecConfigFileOptions, ...ecIntegrationOptions }
|
||||
try {
|
||||
const { default: preprocessEcConfig } = await import('virtual:astro-expressive-code/preprocess-config')
|
||||
mergedEcConfig = (await preprocessEcConfig({ ecConfig: mergedEcConfig, astroConfig })) || mergedEcConfig
|
||||
} catch (error) {
|
||||
const msg = error instanceof Error ? error.message : (error as string)
|
||||
throw new Error(`Failed to preprocess Expressive Code config for the Code component: ${msg}`, { cause: error })
|
||||
}
|
||||
|
||||
return await createAstroRenderer({
|
||||
astroConfig,
|
||||
ecConfig: mergedEcConfig,
|
||||
})
|
||||
}
|
||||
44
node_modules/astro-expressive-code/components/types.ts
generated
vendored
Normal file
44
node_modules/astro-expressive-code/components/types.ts
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
import { ExpressiveCodeBlockProps } from 'rehype-expressive-code'
|
||||
|
||||
// This type helper is required to support component props in MDX files
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type Component<T> = (_props: T) => any
|
||||
|
||||
export type MarkerValueType = string | number | RegExp | (string | number | RegExp)[]
|
||||
|
||||
export type PartialAllowUndefined<T> = {
|
||||
[Key in keyof T]?: T[Key] | undefined
|
||||
}
|
||||
export interface CodeProps extends PartialAllowUndefined<ExpressiveCodeBlockProps> {
|
||||
/**
|
||||
* The plaintext contents of the code block.
|
||||
*/
|
||||
code: string
|
||||
/**
|
||||
* The code block's language.
|
||||
*
|
||||
* Please use a valid [language identifier](https://expressive-code.com/key-features/syntax-highlighting/#supported-languages)
|
||||
* to ensure proper syntax highlighting.
|
||||
*/
|
||||
lang?: string | undefined
|
||||
/**
|
||||
* An optional meta string. In markdown or MDX documents, this is the part of the
|
||||
* code block's opening fence that comes after the language name.
|
||||
*/
|
||||
meta?: string | undefined
|
||||
/**
|
||||
* The code block's locale (e.g. `en-US` or `de-DE`). This is used by plugins to display
|
||||
* localized strings depending on the language of the containing page.
|
||||
*
|
||||
* If no locale is defined here, most Expressive Code integrations will attempt to auto-detect
|
||||
* the block locale using the configured
|
||||
* [`getBlockLocale`](https://expressive-code.com/reference/configuration/#getblocklocale)
|
||||
* function, and finally fall back to the configured
|
||||
* [`defaultLocale`](https://expressive-code.com/reference/configuration/#defaultlocale).
|
||||
*/
|
||||
locale?: string | undefined
|
||||
/**
|
||||
* The CSS class name(s) to apply to the code block's container element.
|
||||
*/
|
||||
class?: string | undefined
|
||||
}
|
||||
1
node_modules/astro-expressive-code/dist/hast.d.ts
generated
vendored
Normal file
1
node_modules/astro-expressive-code/dist/hast.d.ts
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export * from 'rehype-expressive-code/hast';
|
||||
3
node_modules/astro-expressive-code/dist/hast.js
generated
vendored
Normal file
3
node_modules/astro-expressive-code/dist/hast.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
// src/hast.ts
|
||||
export * from "rehype-expressive-code/hast";
|
||||
//# sourceMappingURL=hast.js.map
|
||||
1
node_modules/astro-expressive-code/dist/hast.js.map
generated
vendored
Normal file
1
node_modules/astro-expressive-code/dist/hast.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../src/hast.ts"],"sourcesContent":["export * from 'rehype-expressive-code/hast'\n"],"mappings":";AAAA,cAAc;","names":[]}
|
||||
128
node_modules/astro-expressive-code/dist/index.d.ts
generated
vendored
Normal file
128
node_modules/astro-expressive-code/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
import { AstroIntegration } from 'astro';
|
||||
import { RehypeExpressiveCodeRenderer, RehypeExpressiveCodeOptions } from 'rehype-expressive-code';
|
||||
export * from 'rehype-expressive-code';
|
||||
|
||||
type ConfigSetupHookArgs = Parameters<NonNullable<AstroIntegration['hooks']['astro:config:setup']>>[0];
|
||||
type AstroConfig = ConfigSetupHookArgs['config'];
|
||||
/**
|
||||
* Contains the parts of the Astro config that are used by this integration.
|
||||
*/
|
||||
type PartialAstroConfig = Pick<AstroConfig, 'base' | 'root' | 'srcDir'> & {
|
||||
build?: Partial<Pick<AstroConfig['build'], 'assets' | 'assetsPrefix'>> | undefined;
|
||||
markdown?: Partial<{
|
||||
shikiConfig: Partial<Pick<AstroConfig['markdown']['shikiConfig'], 'langs'>>;
|
||||
}> | undefined;
|
||||
};
|
||||
|
||||
type CreateAstroRendererArgs = {
|
||||
ecConfig: AstroExpressiveCodeOptions;
|
||||
astroConfig: PartialAstroConfig;
|
||||
logger?: ConfigSetupHookArgs['logger'] | undefined;
|
||||
};
|
||||
type AstroExpressiveCodeRenderer = RehypeExpressiveCodeRenderer & {
|
||||
hashedStyles: [string, string][];
|
||||
hashedScripts: [string, string][];
|
||||
};
|
||||
declare function createAstroRenderer({ ecConfig, astroConfig, logger }: CreateAstroRendererArgs): Promise<AstroExpressiveCodeRenderer>;
|
||||
|
||||
type AstroExpressiveCodeOptions = RehypeExpressiveCodeOptions & {
|
||||
/**
|
||||
* Determines if the styles required to display code blocks should be emitted into a separate
|
||||
* CSS file rather than being inlined into the rendered HTML of the first code block per page.
|
||||
*
|
||||
* This is recommended for sites containing multiple pages with code blocks, as it will reduce
|
||||
* the overall footprint of the site when navigating between pages.
|
||||
*
|
||||
* The generated URL is located inside Astro's assets directory and includes a content hash
|
||||
* so it can be cached indefinitely by browsers. If you are using the default values for the
|
||||
* Astro config options `base`, `build.assets`, `build.assetsPrefix`, the resulting URL
|
||||
* will be `/_astro/ec.{hash}.css`.
|
||||
*
|
||||
* **Important**: To actually benefit from caching, please ensure that your hosting provider
|
||||
* serves the contents of the assets directory as immutable files with a long cache lifetime,
|
||||
* e.g. `Cache-Control: public,max-age=31536000,immutable`.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
emitExternalStylesheet?: boolean | undefined;
|
||||
/**
|
||||
* This advanced option allows you to influence the rendering process by creating
|
||||
* your own `AstroExpressiveCodeRenderer` or processing the base styles and JS modules
|
||||
* added to every page.
|
||||
*
|
||||
* The return value will be cached and used for all code blocks on the site.
|
||||
*/
|
||||
customCreateAstroRenderer?: ((args: CreateAstroRendererArgs) => Promise<AstroExpressiveCodeRenderer> | AstroExpressiveCodeRenderer) | undefined;
|
||||
/**
|
||||
* This advanced option allows you to preprocess the Expressive Code configuration
|
||||
* before it is used by the Astro integration or its exported `<Code>` component.
|
||||
*
|
||||
* For example, Starlight uses this option to provide different default settings
|
||||
* and additional theme options.
|
||||
*/
|
||||
customConfigPreprocessors?: CustomConfigPreprocessors | undefined;
|
||||
};
|
||||
type CustomConfigPreprocessors = {
|
||||
/**
|
||||
* To perform preprocessing on the Expressive Code configuration before it is used
|
||||
* by the Astro integration, set this property to a function. It will be called with
|
||||
* an object argument that contains the following properties:
|
||||
* - `ecConfig`: an Expressive Code config object merged from the optional EC config file
|
||||
* `ec.config.mjs` and any options passed directly to the integration
|
||||
* - `astroConfig`: an object containing commonly used settings from the Astro configuration
|
||||
*
|
||||
* The return value must be a valid Expressive Code configuration object.
|
||||
*/
|
||||
preprocessAstroIntegrationConfig: ConfigPreprocessorFn;
|
||||
/**
|
||||
* If you set `preprocessAstroIntegrationConfig` to a function, you must also set this property
|
||||
* to the JS source code of a Vite virtual module that exports the same function as its
|
||||
* default export.
|
||||
*
|
||||
* This is necessary to allow the `<Code>` component to access the same preprocessed config
|
||||
* as the Astro integration. The Astro integration cannot share the function directly with
|
||||
* the `<Code>` component because it runs in a separate Vite instance.
|
||||
*/
|
||||
preprocessComponentConfig: string;
|
||||
};
|
||||
type ConfigPreprocessorFn = (args: {
|
||||
ecConfig: unknown;
|
||||
astroConfig: PartialAstroConfig;
|
||||
}) => Promise<AstroExpressiveCodeOptions> | AstroExpressiveCodeOptions;
|
||||
|
||||
/**
|
||||
* Astro integration that adds Expressive Code support to code blocks in Markdown & MDX documents.
|
||||
*/
|
||||
declare function astroExpressiveCode(integrationOptions?: AstroExpressiveCodeOptions): {
|
||||
name: string;
|
||||
hooks: {
|
||||
'astro:config:setup': (args: unknown) => Promise<void>;
|
||||
};
|
||||
};
|
||||
/**
|
||||
* A utility function that helps you define an Expressive Code configuration object. It is meant
|
||||
* to be used inside the optional config file `ec.config.mjs` located in the root directory
|
||||
* of your Astro project, and its return value to be exported as the default export.
|
||||
*
|
||||
* Expressive Code will automatically detect this file and use the exported configuration object
|
||||
* to override its own default settings.
|
||||
*
|
||||
* Using this function is recommended, but not required. It just passes through the given object,
|
||||
* but it also provides type information for your editor's auto-completion and type checking.
|
||||
*
|
||||
* @example
|
||||
* ```js
|
||||
* // ec.config.mjs
|
||||
* import { defineEcConfig } from 'astro-expressive-code'
|
||||
*
|
||||
* export default defineEcConfig({
|
||||
* themes: ['dracula', 'github-light'],
|
||||
* styleOverrides: {
|
||||
* borderRadius: '0.5rem',
|
||||
* },
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
declare function defineEcConfig(config: AstroExpressiveCodeOptions): AstroExpressiveCodeOptions;
|
||||
|
||||
export { AstroExpressiveCodeOptions, AstroExpressiveCodeRenderer, ConfigPreprocessorFn, CreateAstroRendererArgs, CustomConfigPreprocessors, PartialAstroConfig, astroExpressiveCode, createAstroRenderer, astroExpressiveCode as default, defineEcConfig };
|
||||
319
node_modules/astro-expressive-code/dist/index.js
generated
vendored
Normal file
319
node_modules/astro-expressive-code/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,319 @@
|
||||
// src/index.ts
|
||||
import rehypeExpressiveCode from "rehype-expressive-code";
|
||||
|
||||
// src/ec-config.ts
|
||||
function getEcConfigFileUrl(projectRootUrl) {
|
||||
return new URL("./ec.config.mjs", projectRootUrl);
|
||||
}
|
||||
async function loadEcConfigFile(projectRootUrl) {
|
||||
const pathsToTry = [
|
||||
// This path works in most scenarios, but not when the integration is processed by Vite
|
||||
// due to a Vite bug affecting import URLs using the "file:" protocol
|
||||
new URL(`./ec.config.mjs?t=${Date.now()}`, projectRootUrl).href
|
||||
];
|
||||
if (import.meta.env?.BASE_URL?.length) {
|
||||
pathsToTry.push(`/ec.config.mjs?t=${Date.now()}`);
|
||||
}
|
||||
function coerceError(error) {
|
||||
if (typeof error === "object" && error !== null && "message" in error) {
|
||||
return error;
|
||||
}
|
||||
return { message: error };
|
||||
}
|
||||
for (const path of pathsToTry) {
|
||||
try {
|
||||
const module = await import(
|
||||
/* @vite-ignore */
|
||||
path
|
||||
);
|
||||
if (!module.default) {
|
||||
throw new Error(`Missing or invalid default export. Please export your Expressive Code config object as the default export.`);
|
||||
}
|
||||
return module.default;
|
||||
} catch (error) {
|
||||
const { message, code } = coerceError(error);
|
||||
if (code === "ERR_MODULE_NOT_FOUND" || code === "ERR_LOAD_URL") {
|
||||
if (message.replace(/(imported )?from .*$/, "").includes("ec.config.mjs"))
|
||||
continue;
|
||||
}
|
||||
throw new Error(
|
||||
`Your project includes an Expressive Code config file ("ec.config.mjs")
|
||||
that could not be loaded due to ${code ? `the error ${code}` : "the following error"}: ${message}`.replace(/\s+/g, " "),
|
||||
error instanceof Error ? { cause: error } : void 0
|
||||
);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// src/renderer.ts
|
||||
import { createRenderer, getStableObjectHash } from "rehype-expressive-code";
|
||||
|
||||
// src/astro-config.ts
|
||||
function serializePartialAstroConfig(config) {
|
||||
const partialConfig = {
|
||||
base: config.base,
|
||||
root: config.root,
|
||||
srcDir: config.srcDir
|
||||
};
|
||||
if (config.build) {
|
||||
partialConfig.build = {};
|
||||
if (config.build.assets)
|
||||
partialConfig.build.assets = config.build.assets;
|
||||
if (config.build.assetsPrefix)
|
||||
partialConfig.build.assetsPrefix = config.build.assetsPrefix;
|
||||
}
|
||||
if (config.markdown?.shikiConfig?.langs) {
|
||||
partialConfig.markdown = { shikiConfig: { langs: config.markdown.shikiConfig.langs } };
|
||||
}
|
||||
return JSON.stringify(partialConfig);
|
||||
}
|
||||
function getAssetsPrefix(fileExtension, assetsPrefix) {
|
||||
if (!assetsPrefix)
|
||||
return "";
|
||||
if (typeof assetsPrefix === "string")
|
||||
return assetsPrefix;
|
||||
const dotLessFileExtension = fileExtension.slice(1);
|
||||
if (assetsPrefix[dotLessFileExtension]) {
|
||||
return assetsPrefix[dotLessFileExtension];
|
||||
}
|
||||
return assetsPrefix.fallback;
|
||||
}
|
||||
function getAssetsBaseHref(fileExtension, assetsPrefix, base) {
|
||||
return (getAssetsPrefix(fileExtension, assetsPrefix) || base || "").trim().replace(/\/+$/g, "");
|
||||
}
|
||||
|
||||
// src/renderer.ts
|
||||
async function createAstroRenderer({ ecConfig, astroConfig, logger }) {
|
||||
const { emitExternalStylesheet = true, customCreateRenderer, plugins = [], shiki = true, ...rest } = ecConfig ?? {};
|
||||
const assetsDir = astroConfig.build?.assets || "_astro";
|
||||
const hashedStyles = [];
|
||||
const hashedScripts = [];
|
||||
plugins.push({
|
||||
name: "astro-expressive-code",
|
||||
hooks: {
|
||||
postprocessRenderedBlockGroup: ({ renderData, renderedGroupContents }) => {
|
||||
const isFirstGroupInDocument = renderedGroupContents[0]?.codeBlock.parentDocument?.positionInDocument?.groupIndex === 0;
|
||||
if (!isFirstGroupInDocument)
|
||||
return;
|
||||
const extraElements = [];
|
||||
hashedStyles.forEach(([hashedRoute]) => {
|
||||
extraElements.push({
|
||||
type: "element",
|
||||
tagName: "link",
|
||||
properties: { rel: "stylesheet", href: `${getAssetsBaseHref(".css", astroConfig.build?.assetsPrefix, astroConfig.base)}${hashedRoute}` },
|
||||
children: []
|
||||
});
|
||||
});
|
||||
hashedScripts.forEach(([hashedRoute]) => {
|
||||
extraElements.push({
|
||||
type: "element",
|
||||
tagName: "script",
|
||||
properties: { type: "module", src: `${getAssetsBaseHref(".js", astroConfig.build?.assetsPrefix, astroConfig.base)}${hashedRoute}` },
|
||||
children: []
|
||||
});
|
||||
});
|
||||
if (!extraElements.length)
|
||||
return;
|
||||
renderData.groupAst.children.unshift(...extraElements);
|
||||
}
|
||||
}
|
||||
});
|
||||
const mergedShikiConfig = shiki === true ? {} : shiki;
|
||||
if (mergedShikiConfig && !mergedShikiConfig.langs && astroConfig.markdown?.shikiConfig?.langs) {
|
||||
mergedShikiConfig.langs = astroConfig.markdown.shikiConfig.langs;
|
||||
}
|
||||
const renderer = await (customCreateRenderer ?? createRenderer)({
|
||||
plugins,
|
||||
logger,
|
||||
shiki: mergedShikiConfig,
|
||||
...rest
|
||||
});
|
||||
renderer.hashedStyles = hashedStyles;
|
||||
renderer.hashedScripts = hashedScripts;
|
||||
if (emitExternalStylesheet) {
|
||||
const combinedStyles = `${renderer.baseStyles}${renderer.themeStyles}`;
|
||||
hashedStyles.push(getHashedRouteWithContent(combinedStyles, `/${assetsDir}/ec.{hash}.css`));
|
||||
renderer.baseStyles = "";
|
||||
renderer.themeStyles = "";
|
||||
}
|
||||
const uniqueJsModules = [...new Set(renderer.jsModules)];
|
||||
const mergedJsCode = uniqueJsModules.join("\n");
|
||||
renderer.jsModules = [];
|
||||
hashedScripts.push(getHashedRouteWithContent(mergedJsCode, `/${assetsDir}/ec.{hash}.js`));
|
||||
return renderer;
|
||||
}
|
||||
function getHashedRouteWithContent(content, routeTemplate) {
|
||||
const contentHash = getStableObjectHash(content, { hashLength: 5 });
|
||||
return [routeTemplate.replace("{hash}", contentHash), content];
|
||||
}
|
||||
|
||||
// src/vite-plugin.ts
|
||||
import { stableStringify } from "rehype-expressive-code";
|
||||
function vitePluginAstroExpressiveCode({
|
||||
styles,
|
||||
scripts,
|
||||
ecIntegrationOptions,
|
||||
astroConfig,
|
||||
command
|
||||
}) {
|
||||
const modules = {};
|
||||
const configModuleContents = [];
|
||||
configModuleContents.push(`export const astroConfig = ${serializePartialAstroConfig(astroConfig)}`);
|
||||
const { customConfigPreprocessors, ...otherEcIntegrationOptions } = ecIntegrationOptions;
|
||||
configModuleContents.push(`export const ecIntegrationOptions = ${stableStringify(otherEcIntegrationOptions)}`);
|
||||
const strEcConfigFileUrlHref = JSON.stringify(getEcConfigFileUrl(astroConfig.root).href);
|
||||
configModuleContents.push(
|
||||
`let ecConfigFileOptions = {}`,
|
||||
`try {`,
|
||||
` ecConfigFileOptions = (await import('virtual:astro-expressive-code/ec-config')).default`,
|
||||
`} catch (e) {`,
|
||||
` console.error('*** Failed to load Expressive Code config file ${strEcConfigFileUrlHref}. You can ignore this message if you just renamed/removed the file.\\n\\n(Full error message: "' + (e?.message || e) + '")\\n')`,
|
||||
`}`,
|
||||
`export { ecConfigFileOptions }`
|
||||
);
|
||||
modules["virtual:astro-expressive-code/config"] = configModuleContents.join("\n");
|
||||
modules["virtual:astro-expressive-code/ec-config"] = "export default {}";
|
||||
modules["virtual:astro-expressive-code/preprocess-config"] = customConfigPreprocessors?.preprocessComponentConfig || `export default ({ ecConfig }) => ecConfig`;
|
||||
const noQuery = (source) => source.split("?")[0];
|
||||
const getVirtualModuleContents = (source) => {
|
||||
if (command === "dev") {
|
||||
for (const file of [...styles, ...scripts]) {
|
||||
const [fileName, contents] = file;
|
||||
if (noQuery(fileName) === noQuery(source))
|
||||
return contents;
|
||||
}
|
||||
}
|
||||
return source in modules ? modules[source] : void 0;
|
||||
};
|
||||
return {
|
||||
name: "vite-plugin-astro-expressive-code",
|
||||
async resolveId(source, importer) {
|
||||
if (source === "virtual:astro-expressive-code/api") {
|
||||
const resolved = await this.resolve("astro-expressive-code", importer);
|
||||
if (resolved)
|
||||
return resolved;
|
||||
return await this.resolve("astro-expressive-code");
|
||||
}
|
||||
if (source === "virtual:astro-expressive-code/ec-config") {
|
||||
const resolved = await this.resolve("./ec.config.mjs");
|
||||
if (resolved)
|
||||
return resolved;
|
||||
}
|
||||
if (getVirtualModuleContents(source))
|
||||
return `\0${source}`;
|
||||
},
|
||||
load: (id) => id?.[0] === "\0" ? getVirtualModuleContents(id.slice(1)) : void 0,
|
||||
// If any file imported by the EC config file changes, restart the server
|
||||
async handleHotUpdate({ modules: modules2, server }) {
|
||||
if (!modules2 || !server)
|
||||
return;
|
||||
const isImportedByEcConfig = (module, depth = 0) => {
|
||||
if (!module || !module.importers || depth >= 6)
|
||||
return false;
|
||||
for (const importingModule of module.importers) {
|
||||
if (noQuery(module.url).endsWith("/ec.config.mjs")) {
|
||||
return true;
|
||||
}
|
||||
if (isImportedByEcConfig(importingModule, depth + 1))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
if (modules2.some((module) => isImportedByEcConfig(module))) {
|
||||
await server.restart();
|
||||
}
|
||||
},
|
||||
buildEnd() {
|
||||
if (command === "build") {
|
||||
for (const file of [...styles, ...scripts]) {
|
||||
const [fileName, source] = file;
|
||||
this.emitFile({
|
||||
type: "asset",
|
||||
// Remove leading slash and any query params
|
||||
fileName: noQuery(fileName.slice(1)),
|
||||
source
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// src/index.ts
|
||||
export * from "rehype-expressive-code";
|
||||
function astroExpressiveCode(integrationOptions = {}) {
|
||||
const integration = {
|
||||
name: "astro-expressive-code",
|
||||
hooks: {
|
||||
"astro:config:setup": async (args) => {
|
||||
const { command, config: astroConfig, updateConfig, logger, addWatchFile } = args;
|
||||
const ownPosition = astroConfig.integrations.findIndex((integration2) => integration2.name === "astro-expressive-code");
|
||||
const mdxPosition = astroConfig.integrations.findIndex((integration2) => integration2.name === "@astrojs/mdx");
|
||||
if (ownPosition > -1 && mdxPosition > -1 && mdxPosition < ownPosition) {
|
||||
throw new Error(
|
||||
`Incorrect integration order: To allow code blocks on MDX pages to use
|
||||
astro-expressive-code, please move astroExpressiveCode() before mdx()
|
||||
in the "integrations" array of your Astro config file.`.replace(/\s+/g, " ")
|
||||
);
|
||||
}
|
||||
addWatchFile(getEcConfigFileUrl(astroConfig.root));
|
||||
const ecConfigFileOptions = await loadEcConfigFile(astroConfig.root);
|
||||
const mergedOptions = { ...ecConfigFileOptions, ...integrationOptions };
|
||||
const forwardedIntegrationOptions = { ...integrationOptions };
|
||||
delete forwardedIntegrationOptions.customConfigPreprocessors;
|
||||
if (Object.keys(ecConfigFileOptions).length > 0 && Object.keys(forwardedIntegrationOptions).length > 0) {
|
||||
logger.warn(
|
||||
`Your project includes an Expressive Code config file ("ec.config.mjs"),
|
||||
but your Astro config file also contains Expressive Code options.
|
||||
To avoid unexpected results from merging multiple config sources,
|
||||
move all Expressive Code options into its config file.
|
||||
Found options: ${Object.keys(forwardedIntegrationOptions).join(", ")}`.replace(/\s+/g, " ")
|
||||
);
|
||||
}
|
||||
const processedEcConfig = await mergedOptions.customConfigPreprocessors?.preprocessAstroIntegrationConfig({ ecConfig: mergedOptions, astroConfig }) || mergedOptions;
|
||||
const { customCreateAstroRenderer } = processedEcConfig;
|
||||
delete processedEcConfig.customCreateAstroRenderer;
|
||||
delete processedEcConfig.customConfigPreprocessors;
|
||||
const { hashedStyles, hashedScripts, ...renderer } = await (customCreateAstroRenderer ?? createAstroRenderer)({ astroConfig, ecConfig: processedEcConfig, logger });
|
||||
const rehypeExpressiveCodeOptions = {
|
||||
// Even though we have created a custom renderer, some options are used
|
||||
// by the rehype integration itself (e.g. `tabWidth`, `getBlockLocale`),
|
||||
// so we pass all of them through just to be safe
|
||||
...processedEcConfig,
|
||||
// Pass our custom renderer to the rehype integration
|
||||
customCreateRenderer: () => renderer
|
||||
};
|
||||
updateConfig({
|
||||
vite: {
|
||||
plugins: [
|
||||
vitePluginAstroExpressiveCode({
|
||||
styles: hashedStyles,
|
||||
scripts: hashedScripts,
|
||||
ecIntegrationOptions: integrationOptions,
|
||||
astroConfig,
|
||||
command
|
||||
})
|
||||
]
|
||||
},
|
||||
markdown: {
|
||||
syntaxHighlight: false,
|
||||
rehypePlugins: [[rehypeExpressiveCode, rehypeExpressiveCodeOptions]]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
return integration;
|
||||
}
|
||||
function defineEcConfig(config) {
|
||||
return config;
|
||||
}
|
||||
var src_default = astroExpressiveCode;
|
||||
export {
|
||||
astroExpressiveCode,
|
||||
createAstroRenderer,
|
||||
src_default as default,
|
||||
defineEcConfig
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
node_modules/astro-expressive-code/dist/index.js.map
generated
vendored
Normal file
1
node_modules/astro-expressive-code/dist/index.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
60
node_modules/astro-expressive-code/package.json
generated
vendored
Normal file
60
node_modules/astro-expressive-code/package.json
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"name": "astro-expressive-code",
|
||||
"version": "0.35.3",
|
||||
"description": "Astro integration for Expressive Code, a text marking & annotation engine for presenting source code on the web.",
|
||||
"keywords": [
|
||||
"astro-integration"
|
||||
],
|
||||
"author": "Tibor Schiemann",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/expressive-code/expressive-code.git",
|
||||
"directory": "packages/astro-expressive-code"
|
||||
},
|
||||
"type": "module",
|
||||
"module": "./dist/index.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
},
|
||||
"./hast": {
|
||||
"types": "./dist/hast.d.ts",
|
||||
"default": "./dist/hast.js"
|
||||
},
|
||||
"./components": "./components/index.ts"
|
||||
},
|
||||
"types": "./dist/index.d.ts",
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"hast": [
|
||||
"dist/hast.d.ts"
|
||||
]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"components",
|
||||
"virtual.d.ts"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"astro": "^4.0.0-beta || ^3.3.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"rehype-expressive-code": "^0.35.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"astro": "^4.5.2",
|
||||
"execa": "^7.1.1",
|
||||
"@internal/test-utils": "^0.2.28"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsup ./src/index.ts ./src/hast.ts --format esm --dts --sourcemap --clean",
|
||||
"coverage": "vitest run --coverage",
|
||||
"test": "vitest run --reporter verbose",
|
||||
"test-short": "vitest run --reporter basic",
|
||||
"test-watch": "vitest --reporter verbose",
|
||||
"watch": "pnpm build --watch src"
|
||||
}
|
||||
}
|
||||
16
node_modules/astro-expressive-code/virtual.d.ts
generated
vendored
Normal file
16
node_modules/astro-expressive-code/virtual.d.ts
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
declare module 'virtual:astro-expressive-code/config' {
|
||||
import type { AstroExpressiveCodeOptions, PartialAstroConfig } from 'astro-expressive-code'
|
||||
export const astroConfig: PartialAstroConfig
|
||||
export const ecConfigFileOptions: AstroExpressiveCodeOptions
|
||||
export const ecIntegrationOptions: AstroExpressiveCodeOptions
|
||||
}
|
||||
|
||||
declare module 'virtual:astro-expressive-code/preprocess-config' {
|
||||
import type { ConfigPreprocessorFn } from 'astro-expressive-code'
|
||||
const preprocessEcConfig: ConfigPreprocessorFn
|
||||
export default preprocessEcConfig
|
||||
}
|
||||
|
||||
declare module 'virtual:astro-expressive-code/api' {
|
||||
export const createAstroRenderer: typeof import('astro-expressive-code').createAstroRenderer
|
||||
}
|
||||
Reference in New Issue
Block a user