Files
lucide/docs/.vitepress/lib/createCodeExamples.ts
T

219 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-04-30 20:32:37 +02:00
import {
BUNDLED_LANGUAGES,
type IThemeRegistration
} from 'shiki'
import {
getHighlighter,
} from 'shiki-processor'
type CodeExampleType = {
title: string,
lang: string,
codes: {
language?: string,
code: string,
metastring?: string,
}[],
}[]
2023-05-03 22:11:49 +02:00
const getIconCodes = (): CodeExampleType => {
2023-04-30 20:32:37 +02:00
return [
{
lang: 'html',
title: 'HTML',
codes: [
{
language: 'html',
2023-05-03 22:11:49 +02:00
code: `<i data-lucide-name="Name"></i>
2023-04-30 20:32:37 +02:00
`,
},
],
},
{
lang: 'tsx',
title: 'React',
codes: [
{
language: 'tsx',
2023-05-03 22:11:49 +02:00
code: `import { PascalCase } from 'lucide-react';
2023-04-30 20:32:37 +02:00
const App = () => {
return (
2023-05-04 09:28:44 +02:00
<PascalCase />
2023-04-30 20:32:37 +02:00
);
};
export default App;
`,
2023-05-03 22:11:49 +02:00
2023-04-30 20:32:37 +02:00
},
],
},
{
lang: 'vue',
title: 'Vue 3',
codes: [
{
language: 'vue',
2023-05-03 22:11:49 +02:00
code: `<script setup>
import { PascalCase } from 'lucide-vue-next';
2023-04-30 20:32:37 +02:00
</script>
2023-05-03 22:11:49 +02:00
<template>
<PascalCase />
</template>
2023-04-30 20:32:37 +02:00
`,
2023-05-03 22:11:49 +02:00
2023-04-30 20:32:37 +02:00
},
],
},
{
lang: 'svelte',
title: 'Svelte',
codes: [
{
2023-05-03 22:11:49 +02:00
language: 'svelte',
2023-04-30 20:32:37 +02:00
code: `<script>
2023-05-03 22:11:49 +02:00
import { PascalCase } from 'lucide-svelte';
2023-04-30 20:32:37 +02:00
</script>
2023-05-03 22:11:49 +02:00
<PascalCase />
2023-04-30 20:32:37 +02:00
`,
2023-05-03 22:11:49 +02:00
2023-04-30 20:32:37 +02:00
},
],
},
{
lang: 'preact',
title: 'Preact',
codes: [
{
language: 'tsx',
2023-05-03 22:11:49 +02:00
code: `import { PascalCase } from 'lucide-preact';
2023-04-30 20:32:37 +02:00
const App = () => {
return (
2023-05-03 22:11:49 +02:00
<PascalCase />
2023-04-30 20:32:37 +02:00
);
};
export default App;
`,
2023-05-03 22:11:49 +02:00
2023-04-30 20:32:37 +02:00
},
],
},
{
lang: 'solid',
title: 'Solid',
codes: [
{
language: 'tsx',
2023-05-03 22:11:49 +02:00
code: `import { PascalCase } from 'lucide-solid';
2023-04-30 20:32:37 +02:00
const App = () => {
return (
2023-05-03 22:11:49 +02:00
<PascalCase />
2023-04-30 20:32:37 +02:00
);
};
export default App;
`,
2023-05-03 22:11:49 +02:00
2023-04-30 20:32:37 +02:00
},
],
},
{
lang: 'angular',
title: 'Angular',
codes: [
{
language: 'tsx',
code: `// app.module.ts
2023-05-03 22:11:49 +02:00
import { LucideAngularModule, PascalCase } from 'lucide-angular';
2023-04-30 20:32:37 +02:00
@NgModule({
imports: [
2023-05-03 22:11:49 +02:00
LucideAngularModule.pick({ PascalCase })
2023-04-30 20:32:37 +02:00
],
})
// app.component.html
2023-05-03 22:11:49 +02:00
<lucide-icon name="Name"></lucide-icon>
2023-04-30 20:32:37 +02:00
`,
},
],
},
{
lang: 'html',
title: 'Icon Font',
codes: [
{
language: 'html',
code: `<style>
2023-05-03 22:11:49 +02:00
@import ('~lucide-static/font/Lucide.css');
2023-04-30 20:32:37 +02:00
</style>
2023-05-03 22:11:49 +02:00
<div class="icon-Name"></div>
2023-04-30 20:32:37 +02:00
`,
},
],
},
{
lang: 'dart',
title: 'Flutter',
codes: [
{
language: 'dart',
2023-05-03 22:11:49 +02:00
code: `Icon(LucideIcons.Name);
2023-04-30 20:32:37 +02:00
`,
},
],
},
]
}
export type ThemeOptions =
| IThemeRegistration
| { light: IThemeRegistration; dark: IThemeRegistration }
const highLightCode = async (code: string, lang: string, active?: boolean) => {
const highlighter = await getHighlighter({
themes: ['material-theme-palenight'],
langs: [...BUNDLED_LANGUAGES],
processors: []
})
const highlightedCode = highlighter.codeToHtml(code, {
lang,
// lineOptions,
theme: 'material-theme-palenight'
}).replace('background-color: #292D3E', '')
return `<div class="language-${lang} ${active ? 'active' : ''}">
<button title="Copy Code" class="copy"></button>
<span class="lang">${lang}</span>
${highlightedCode}
</div>`
}
2023-05-03 22:11:49 +02:00
export default async function createCodeExamples() {
const codes = getIconCodes();
2023-04-30 20:32:37 +02:00
const codeExamplePromises = codes.map(async (codeTemplate, index) => {
const { title, lang, codes } = codeTemplate;
const isFirst = index === 0;
const code = await highLightCode(codes[0].code, codes[0].language || lang, isFirst);
return {
title,
language: codes[0].language || lang,
code,
};
})
return Promise.all(codeExamplePromises);
}