UNPKG

mongodb-rag-core

Version:

Common elements used by MongoDB Chatbot Framework components.

22 lines 952 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasMarkdownCodeblock = exports.extractCodeFromMarkdown = void 0; /** Extracts code from markdown by removing all code blocks and returning the rest of the markdown. If there are no markdown code blocks, the original text is returned. */ function extractCodeFromMarkdown(maybeMarkdown) { if (hasMarkdownCodeblock(maybeMarkdown)) { const codeBlocks = maybeMarkdown.match(/```[\w]*\n([\s\S]*?)```/g) || []; return codeBlocks .map((block) => block.replaceAll(/```[\w]*\n([\s\S]*?)```/gs, "$1").trim()) .join("\n\n"); } return maybeMarkdown; } exports.extractCodeFromMarkdown = extractCodeFromMarkdown; function hasMarkdownCodeblock(maybeMarkdown) { return /```[\w]*\n([\s\S]*?)```/g.test(maybeMarkdown); } exports.hasMarkdownCodeblock = hasMarkdownCodeblock; //# sourceMappingURL=extractCodeFromMarkdown.js.map