Fix: Correctly guess lexer when no language is specified

The `guess_lexer` function was not correctly handling code snippets when no language was specified. It was only considering the second line of the code, which could lead to incorrect lexer selection. This commit joins the lines of code (excluding the first and last) back into a single string before passing it to `guess_lexer`, ensuring the entire code block is considered for lexer identification.
This commit is contained in:
Hayden Johnson 2025-04-10 20:33:18 -07:00
parent fe4f739eb3
commit 0dcc44870e

View file

@ -97,8 +97,7 @@ def highlight_code(language_name, code):
lexer = get_lexer_by_name('bash')
else:
# If no language is specified, guess the lexer
print("LEXER NAME " + lexer_name)
lexer = guess_lexer(code.split('\n')[1:-1])
lexer = guess_lexer('\n'.join(code.split('\n')[1:-1]))
if not lexer:
# If no lexer is guessed, default to bash
lexer = get_lexer_by_name('bash')