Files
lucide/docs/.vitepress/lib/codeExamples/createLabCodeExamples.ts
T

162 lines
3.2 KiB
TypeScript
Raw Normal View History

import { bundledLanguages, type ThemeRegistration } from 'shiki';
import { createHighlighter } from 'shiki';
2023-06-04 16:59:38 +02:00
type CodeExampleType = {
2024-02-01 22:38:21 +09:00
title: string;
language: string;
code: string;
}[];
2023-06-04 16:59:38 +02:00
const getIconCodes = (): CodeExampleType => {
return [
{
language: 'js',
title: 'Vanilla',
code: `\
import { createIcons, icons } from 'lucide';
2024-06-28 10:25:27 +02:00
import { $CamelCase } from '@lucide/lab';
createIcons({
icons: {
2024-06-28 10:25:27 +02:00
$CamelCase
}
});
document.body.append('<i data-lucide="$Name"></i>');\
`,
2023-06-04 16:59:38 +02:00
},
{
language: 'tsx',
2023-06-04 16:59:38 +02:00
title: 'React',
code: `import { Icon } from 'lucide-react';
2024-06-28 10:25:27 +02:00
import { $CamelCase } from '@lucide/lab';
2023-06-04 16:59:38 +02:00
const App = () => {
return (
2024-06-28 10:25:27 +02:00
<Icon iconNode={$CamelCase} />
2023-06-04 16:59:38 +02:00
);
};
export default App;
`,
},
{
language: 'vue',
title: 'Vue',
code: `<script setup>
import { Icon } from 'lucide-vue-next';
2024-06-28 10:25:27 +02:00
import { $CamelCase } from '@lucide/lab';
2023-06-04 16:59:38 +02:00
</script>
<template>
<Icon :iconNode="$CamelCase" />
2023-06-04 16:59:38 +02:00
</template>
`,
},
{
language: 'svelte',
2023-06-04 16:59:38 +02:00
title: 'Svelte',
code: `<script>
import { Icon } from 'lucide-svelte';
2024-06-28 10:25:27 +02:00
import { $CamelCase } from '@lucide/lab';
2023-06-04 16:59:38 +02:00
</script>
<Icon iconNode={$CamelCase} />
2023-06-04 16:59:38 +02:00
`,
},
{
language: 'tsx',
2023-06-04 16:59:38 +02:00
title: 'Preact',
code: `import { Icon } from 'lucide-preact';
2024-06-28 10:25:27 +02:00
import { $CamelCase } from '@lucide/lab';
2023-06-04 16:59:38 +02:00
const App = () => {
return (
2024-06-28 10:25:27 +02:00
<Icon iconNode={$CamelCase} />
2023-06-04 16:59:38 +02:00
);
};
export default App;
`,
},
{
language: 'tsx',
2023-06-04 16:59:38 +02:00
title: 'Solid',
code: `import { Icon } from 'lucide-solid';
2024-06-28 10:25:27 +02:00
import { $CamelCase } from '@lucide/lab';
2023-06-04 16:59:38 +02:00
const App = () => {
return (
2024-06-28 10:25:27 +02:00
<Icon iconNode={$CamelCase} />
2023-06-04 16:59:38 +02:00
);
};
export default App;
`,
},
{
language: 'tsx',
2023-06-04 16:59:38 +02:00
title: 'Angular',
code: `// app.module.ts
2024-06-28 10:25:27 +02:00
import { LucideAngularModule } from 'lucide-angular';
import { $CamelCase } from '@lucide/lab';
2023-06-04 16:59:38 +02:00
@NgModule({
imports: [
2024-06-28 10:25:27 +02:00
LucideAngularModule.pick({ $CamelCase })
2023-06-04 16:59:38 +02:00
],
})
// app.component.html
2024-06-28 10:25:27 +02:00
<lucide-icon name="$CamelCase"></lucide-icon>
2023-06-04 16:59:38 +02:00
`,
2024-02-01 22:38:21 +09:00
},
];
};
2023-06-04 16:59:38 +02:00
export type ThemeOptions =
| ThemeRegistration
2024-02-01 22:38:21 +09:00
| { light: ThemeRegistration; dark: ThemeRegistration };
2023-06-04 16:59:38 +02:00
const highLightCode = async (code: string, lang: string, active?: boolean) => {
const highlighter = await createHighlighter({
themes: ['github-light', 'github-dark'],
2024-02-01 22:38:21 +09:00
langs: Object.keys(bundledLanguages),
});
2023-06-04 16:59:38 +02:00
2024-02-01 22:38:21 +09:00
const highlightedCode = highlighter
.codeToHtml(code, {
lang,
themes: {
light: 'github-light',
dark: 'github-dark',
},
defaultColor: false,
})
.replace('shiki-themes', 'shiki-themes vp-code');
2023-06-04 16:59:38 +02:00
return `<div class="language-${lang} ${active ? 'active' : ''}">
<button title="Copy Code" class="copy"></button>
<span class="lang">${lang}</span>
${highlightedCode}
2024-02-01 22:38:21 +09:00
</div>`;
};
2023-06-04 16:59:38 +02:00
export default async function createCodeExamples() {
const codes = getIconCodes();
const codeExamplePromises = codes.map(async ({ title, language, code }, index) => {
2023-06-04 16:59:38 +02:00
const isFirst = index === 0;
const codeString = await highLightCode(code, language, isFirst);
2023-06-04 16:59:38 +02:00
return {
title,
language: language,
code: codeString,
2023-06-04 16:59:38 +02:00
};
2024-02-01 22:38:21 +09:00
});
2023-06-04 16:59:38 +02:00
return Promise.all(codeExamplePromises);
}