Files

155 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2024-02-01 22:38:21 +09:00
import { bundledLanguages, type ThemeRegistration } from 'shikiji';
import { getHighlighter } from 'shikiji';
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: 'html',
2023-06-04 16:59:38 +02:00
title: 'HTML',
2024-02-01 22:38:21 +09:00
code: `<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 { PascalCase } from 'lucide-react';
2023-06-04 16:59:38 +02:00
const App = () => {
return (
<PascalCase />
);
};
export default App;
`,
},
{
language: 'vue',
title: 'Vue',
code: `<script setup>
2023-06-04 16:59:38 +02:00
import { PascalCase } from 'lucide-vue-next';
</script>
<template>
<PascalCase />
</template>
`,
},
{
language: 'svelte',
2023-06-04 16:59:38 +02:00
title: 'Svelte',
code: `<script>
2023-06-04 16:59:38 +02:00
import { PascalCase } from 'lucide-svelte';
</script>
<PascalCase />
`,
},
{
language: 'tsx',
2023-06-04 16:59:38 +02:00
title: 'Preact',
code: `import { PascalCase } from 'lucide-preact';
2023-06-04 16:59:38 +02:00
const App = () => {
return (
<PascalCase />
);
};
export default App;
`,
},
{
language: 'tsx',
2023-06-04 16:59:38 +02:00
title: 'Solid',
code: `import { PascalCase } from 'lucide-solid';
2023-06-04 16:59:38 +02:00
const App = () => {
return (
<PascalCase />
);
};
export default App;
`,
},
{
language: 'tsx',
2023-06-04 16:59:38 +02:00
title: 'Angular',
code: `// app.module.ts
2023-06-04 16:59:38 +02:00
import { LucideAngularModule, PascalCase } from 'lucide-angular';
@NgModule({
imports: [
LucideAngularModule.pick({ PascalCase })
],
})
// app.component.html
<lucide-icon name="Name"></lucide-icon>
`,
},
{
language: 'html',
2023-06-04 16:59:38 +02:00
title: 'Icon Font',
code: `<style>
2023-06-04 16:59:38 +02:00
@import ('~lucide-static/font/Lucide.css');
</style>
<div class="icon-Name"></div>
`,
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 getHighlighter({
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);
}