@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
44 lines (43 loc) • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitCodeSnippets = exports.extractTextBetweenBackticks = void 0;
/**
* Extract text between backticks
* @param input
* @returns
*/
function extractTextBetweenBackticks(input) {
var _a;
const regex = /```(?:\w+\n)?([\s\S]*?)```/s;
const match = input.match(regex);
return match ? (_a = match[1]) === null || _a === void 0 ? void 0 : _a.toString().trim() : input;
}
exports.extractTextBetweenBackticks = extractTextBetweenBackticks;
/**
* Separate code blocks from AI messages
*/
function splitCodeSnippets(input) {
// The regex pattern captures both code blocks and texts around them.
const codeBlockRegex = /(```[\s\S]*?```|```[a-z]+\n[\s\S]*?\n```)/g;
// Initialize an array to hold the parts of the string
const result = [];
// Split the input text by the regex, keeping the code blocks in the result
let lastIndex = 0;
let match;
while ((match = codeBlockRegex.exec(input)) !== null) {
// Get the text before the current code block
if (match.index > lastIndex) {
result.push(input.slice(lastIndex, match.index).trim());
}
// Add the code block
result.push(match[0].trim());
lastIndex = match.index + match[0].length;
}
// Add any remaining text after the last code block
if (lastIndex < input.length) {
result.push(input.slice(lastIndex).trim());
}
// Filter out any empty strings that might be present
return result.filter(Boolean);
}
exports.splitCodeSnippets = splitCodeSnippets;