finish highlight.js styling (for chatgpt)

This commit is contained in:
Daniel Avila 2023-02-23 23:56:55 -05:00
parent e95e22de15
commit c58a9bbe93
12 changed files with 851 additions and 150 deletions

20
src/utils/regexSplit.js Normal file
View file

@ -0,0 +1,20 @@
// const string = 'Some text before\n```python\nThis is some `Python` code with ```backticks``` inside the enclosure\n```\nSome text after\n```javascript\nThis is some JavaScript code\n```\n';
const regex = /```([^`\n]*?)\n([\s\S]*?)\n```/g;
export default function regexSplit(string) {
const matches = [...string.matchAll(regex)];
const output = [matches[0].input.slice(0, matches[0].index)];
for (let i = 0; i < matches.length; i++) {
const [fullMatch, language, code] = matches[i];
// const formattedCode = code.replace(/`+/g, '\\`');
const formattedCode = code;
output.push(`\`\`\`${language}\n${formattedCode}\n\`\`\``);
if (i < matches.length - 1) {
const nextText = string.slice(matches[i].index + fullMatch.length, matches[i + 1].index);
output.push(nextText.trim());
}
}
return output;
}