Add language auto-detection, add YAML, more shell languages

This commit is contained in:
Niek van der Maas
2023-03-21 15:52:28 +01:00
parent b6d2e4d1af
commit 7924dd8bca
3 changed files with 26 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
<script lang="ts">
import { Highlight } from 'svelte-highlight'
import flourite from 'flourite'
// Import both dark and light styles
import { github, githubDark } from 'svelte-highlight/styles'
@@ -22,6 +23,7 @@
shell,
php,
plaintext,
yaml,
type LanguageType
} from 'svelte-highlight/languages'
@@ -33,6 +35,12 @@
// Map lang string to LanguageType
let language: LanguageType<string>
// If no language is set, try to detect it using flourite
if (!lang) {
lang = flourite(text, { shiki: true }).language
}
switch (lang) {
case 'js':
case 'javascript':
@@ -63,11 +71,18 @@
case 'sh':
case 'shell':
case 'bash':
case 'console':
case 'shellscript':
case 'zsh':
language = shell
break
case 'php':
language = php
break
case 'yaml':
case 'yml':
language = yaml
break
default:
language = plaintext
}