A library for detecting the programming language of a code snippet.
npm install flourite



A fork of ts95/lang-detector, rewritten in Typescript with more language support.
Detects a programming language from a given string.
- Built-in support for CommonJS and ESM format
- Built-in Typescript typings
- No external dependencies
- 200 test cases and growing!
| Languages | | | | |
| --------- | ---------- | ---------- | ------ | ---------- |
| C | Dockerfile | Javascript | Pascal | SQL |
| C++ | Elixir | Julia | PHP | YAML |
| C# | Go | Kotlin | Python | Typescript |
| Clojure | HTML | Lua | Ruby | |
| CSS | Java | Markdown | Rust | |
``bash`
$ npm install flourite
or via a CDN (unpkg or jsdelivr)
`html`
`js
import flourite from 'flourite';
const code = flourite('cout << "Hello world" << endl;');
// {
// language: 'C++',
// statistics: {
// C: 0,
// Clojure: 0,
// 'C++': 5,
// CSS: 0,
// 'C#': 0,
// Dockerfile: 0,
// Elixir: 0,
// Go: 0,
// HTML: 0,
// Java: 0,
// Javascript: 0,
// Julia: 2,
// Kotlin: 0,
// Lua: 2,
// Markdown: 0,
// Pascal: 0,
// PHP: 0,
// Python: 0,
// Ruby: 0,
// Rust: 0,
// SQL: 0,
// Unknown: 1,
// YAML: 0,
// },
// linesOfCode: 1
// }
`
Or if you want to integrate it with Shiki, you could pass:
`js`
flourite('Console.WriteLine("Hello world!");', { shiki: true }).language;
// => csharp
flourite('fn partition
// => rust
If you want to handle Unknown value, you could pass:
`js`
const code = flourite("SELECT 'Hello world!' text FROM dual;", { noUnknown: true });
`typescript
import flourite from 'flourite';
import type { Options } from 'flourite';
const flouriteOptions: Options = {
heuristic: true,
};
const code = flourite('print!({:?}, &v);', flouriteOptions);
`
| Key | Type | Default | Description |
| --------- | --------- | ------- | ------------------------------------------------------------------------------------------------ |
| heuristic | boolean | true | Checks for codes on the top of the given input. Only checks when the lines of code is above 500. |boolean
| shiki | | false | Straightforward compatibility with Shiki's language specification type |boolean
| noUnknown | | false | If true, will not output Unknown on detected and statistics result |
If you're new to open source, we really recommend reading a few articles about contributing to open source projects:
- Open Source Guide's How to Contribute to Open Source
- Hacktoberfest Contributor's Guide: How To Find and Contribute to Open-Source Projects
- Tips for high-quality Pull Request
Then you can start by reading our contribution part and guidelines.
Two things that you can do for sure: Create a new language support and improve the regular expression performance on current supported languages.
Have fun!
- Use the Node.js version as defined on the .nvmrc file.npm run test:tdd
- Run to initiate a test driven development environment.npm run lint
- Run and npm run format` before commit a change.
For more details and explanation on how things work, see CONTRIBUTING