Files
lucide/docs/.vitepress/markdown/sandpack.ts
Copilot 75b55160aa chore(dev): upgrade ESLint to latest compatible stack (v10) (#4378)
* chore: upgrade eslint to v9 latest with compatible lint deps

Agent-Logs-Url: https://github.com/lucide-icons/lucide/sessions/f7c1f1b5-d213-4afa-91a8-b799a16397ec

* Fix eslint config

* Fix a lot of eslint errors

* Fix eslint

* Fix eslint errors

* Fix eslint errors

* Fix types

* Fix eslint warning

* Format code

* Add eslint 10

* Remove unused vars

* Update pnpm

* Update deps

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Eric Fennis <eric.fennis@gmail.com>
2026-08-18 13:09:22 +02:00

130 lines
3.7 KiB
TypeScript

import type MarkdownIt from 'markdown-it';
import type { RenderRule } from 'markdown-it/lib/renderer.mjs';
import type Token from 'markdown-it/lib/token.mjs';
import container from 'markdown-it-container';
import sandpackTheme from '../theme/sandpackTheme.json';
type SnackParams = {
defaultFiles?: Record<
string,
{
code: string;
active?: boolean;
hidden?: boolean;
}
>;
};
type ContainerArgs = [typeof container, string, { render: RenderRule }];
export default function sandpackPlugin(md: MarkdownIt, pluginOptions: SnackParams = {}) {
if (md == null) {
throw new Error('MarkdownIt instance is required for sandpackPlugin');
}
const escapeHtml = md?.utils?.escapeHtml;
const renderSandbox = (tokenList: Token[], index: number) => {
const renderFunc = (tokens: Token[], idx: number) => {
if (tokens[idx].nesting === 1) {
const attrs = Object.fromEntries(tokens[idx].attrs || []);
const files: Record<
string,
{
code: string;
active?: boolean;
hidden?: boolean;
}
> = {};
for (
let i = idx + 1;
!(tokens[i].nesting === -1 && tokens[i].type === 'container_sandpack_close');
++i
) {
if (tokens[i].type === 'fence' && tokens[i].tag === 'code') {
const info = tokens[i].info ?? '';
const [, fileName, params = ''] = info.split(' ');
const active = params.includes('[active]');
const hidden = params.includes('[hidden]');
const code = tokens[i].content;
if (fileName && code) {
files[fileName] = {
code,
...(active && { active: true }),
...(hidden && { hidden: true }),
};
}
}
}
const { dependencies, showTabs, externalResources, editorWidthPercentage, ...options } =
attrs;
const dependencyList = dependencies?.split(',')?.map((dep: string) => dep.trim()) ?? [];
const dependencyObject = dependencyList.reduce(
(acc: Record<string, string>, name: string) => {
acc[name] = 'latest';
return acc;
},
{},
);
const externalResourcesList = externalResources
?.split(',')
?.map((res: string) => res.trim())
?.filter((res: string) => res.length > 0);
const filesWithDefaultStyles = {
...pluginOptions.defaultFiles,
...files,
};
return `\
<Sandpack\
template="${escapeHtml(attrs.template || 'vanilla')}"\
:theme="${escapeHtml(JSON.stringify(sandpackTheme))}"\
:customSetup="${escapeHtml(
dependencyList
? JSON.stringify({
dependencies: dependencyList.length ? dependencyObject : {},
})
: undefined,
)}"
:files="${escapeHtml(JSON.stringify(filesWithDefaultStyles))}"\
:options="${escapeHtml(
JSON.stringify({
...(showTabs ? { showTabs: JSON.parse(showTabs) } : {}),
externalResources: externalResourcesList,
editorWidthPercentage: editorWidthPercentage
? Number(editorWidthPercentage)
: undefined,
...options,
}),
)}"\
>`;
}
return `</Sandpack>`;
};
return renderFunc(tokenList, index);
};
function createCodeGroup(): ContainerArgs {
return [
container,
'sandpack',
{
render(tokens, idx) {
return renderSandbox(tokens, idx);
},
},
];
}
md.use(...createCodeGroup());
}