code-block-language-detector
Advanced tools
Comparing version
{ | ||
"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" |
3428
39.24%61
10.91%23
2200%