cogent
cogent copied to clipboard
Automatically identify whether the AI prompt content is a string or a code
Describe the feature
In addition, we need to use MD rendering and highlight syntax when the input prompt content is a block of code, and we need a scanning algorithm to automatically identify which programming language it is in order to set the highlight color
Relevant code
No response
Yes, I will implement good MD code highlighting and rendering soon.
package main
import (
"fmt"
"regexp"
)
func extractCodeBlocks(text string) {
patterns := map[string]string{
"Go": `\bfunc\b|\bpackage\b|\bimport\b`,
"C++": `\bclass\b|\bint\b|\bvoid\b`,
"Python": `\bdef\b|\bimport\b|\bfrom\b`,
"Rust": `\bfn\b|\buse\b|\bmod\b`,
}
for lang, pattern := range patterns {
re := regexp.MustCompile(pattern)
matches := re.FindAllStringIndex(text, -1)
if len(matches) > 0 {
fmt.Printf("%s code blocks found:\n", lang)
for _, match := range matches {
fmt.Printf("Start line: %d, End line: %d\n", getLineNumber(text, match[0]), getLineNumber(text, match[1]))
}
}
}
}
func getLineNumber(text string, index int) int {
lines := 1
for i := 0; i < index; i++ {
if text[i] == '\n' {
lines++
}
}
return lines
}
func main() {
text := `
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, world!")
}
// C++ code block
class MyClass {
int val;
};
# Python code block
def hello():
print("Hello, world!")
// Rust code block
pub fn hello() {
println!("Hello, world!");
}
`
extractCodeBlocks(text)
}
Just to clarify, the issue is not that we need to magically detect the language of the code blocks. It tells us the language of the code blocks, and I just need to connect our markdown renderer with our syntax highlighter to achieve the appropriate highlighting.