You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

code-block-language-detector

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

code-block-language-detector

Detects the programming language of code snippets

1.0.6
latest
npmnpm
Version published
Maintainers
0
Created
Source

Code Block Language Detector

A lightweight Node.js package to detect the programming language of a given code snippet. Built on top of highlight.js, it supports a wide range of programming languages.

Features

  • Detects programming language from code snippets.
  • Supports popular languages like JavaScript, Python, C++, Java, and more.
  • Option to restrict detection to specific languages for better accuracy.
  • Lightweight and easy to integrate.

Installation

Install the package via npm:

npm install code-block-language-detector

Usage

Basic Usage

Detect the programming language of a given code snippet:

const detectLanguage = require("code-block-language-detector");

const code = `const x = 42; console.log(x);`;
const language = detectLanguage(code);

console.log(language); // Output: 'javascript'

Restricting to Specific Languages

const detectLanguage = require("code-block-language-detector");

const code = `SELECT * FROM users WHERE age > 21;`;
const language = detectLanguage(code, ["sql", "javascript", "python"]);

console.log(language); // Output: 'sql'

Error Handling

If the provided code snippet is invalid or detection fails, the function returns "unknown":

const code = `+++---===`;
const language = detectLanguage(code);

console.log(language); // Output: 'unknown'

Supported Languages

The following languages are currently supported:

  • JavaScript
  • Python
  • Java
  • C
  • C++
  • Ruby
  • PHP
  • TypeScript
  • Go
  • Swift
  • Kotlin
  • Rust
  • C#
  • HTML
  • CSS
  • JSON
  • YAML
  • Bash
  • SQL
  • Perl
  • R
  • Dart
  • Scala
  • Shell

Keywords

language detector

FAQs

Package last updated on 15 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts