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 - npm Package Compare versions

Comparing version

to
1.0.1

16

package.json
{
"name": "code-block-language-detector",
"version": "1.0.0",
"main": "index.js",
"version": "1.0.1",
"main": "src/index.js",
"scripts": {
"test": "mocha test"
},
"keywords": ["language detector","code language detector" ,"detect code language" ],
"keywords": [
"language detector",
"code language detector",
"detect code language"
],
"author": "Kiron",
"license": "ISC",
"description": "This package is a language detector for code blocks",
"license": "MIT",
"description": "Detects the programming language of code snippets",
"dependencies": {

@@ -18,3 +22,3 @@ "franc": "^6.2.0",

"detect-lang": "./src/cli.js"
},
},
"devDependencies": {

@@ -21,0 +25,0 @@ "mocha": "^10.8.2"

const hljs = require('highlight.js');
/**
* Detects the programming language of a given code snippet.
* @param {string} code - The code snippet to analyze.
* @param {string[]} [allowedLanguages] - List of languages to restrict detection to (optional).
* @returns {string} - The detected programming language or "unknown".
*/
function detectLanguage(code) {
function detectLanguage(code, allowedLanguages = [
'javascript', 'python', 'java', 'c', 'cpp', 'ruby', 'php',
'typescript', 'go', 'swift', 'kotlin', 'rust', 'csharp',
'html', 'css', 'json', 'yaml', 'bash', 'sql', 'perl',
'r', 'dart', 'scala', 'shell'
]) {
if (!code || typeof code !== 'string') {

@@ -13,3 +18,3 @@ throw new Error("Input must be a valid code string.");

const result = hljs.highlightAuto(code);
const result = hljs.highlightAuto(code, allowedLanguages);

@@ -16,0 +21,0 @@ // If no match, return "unknown"