mlld
Version:
mlld: llm scripting language
35 lines (33 loc) • 1.75 kB
JavaScript
import { __name } from './chunk-NJQT543K.mjs';
// interpreter/output/normalizer.ts
function normalizeOutput(output) {
const withoutLeadingNewlines = output.replace(/^\n+/, "");
const frontmatterRegex = /^---\n[\s\S]*?\n---\n/;
const frontmatterMatch = withoutLeadingNewlines.match(frontmatterRegex);
const frontmatter = frontmatterMatch ? frontmatterMatch[0] : "";
const content = frontmatterMatch ? withoutLeadingNewlines.slice(frontmatter.length) : withoutLeadingNewlines;
const normalized = content.replace(/^\n+/, "").replace(/[ \t]+$/gm, "").replace(/\n(#{1,6}\s)/g, "\n\n$1").replace(/(#{1,6}\s[^\n]+)\n([^\n#])/g, "$1\n\n$2").replace(/([^{\[\n])\n([^\n#\s\-}\]])/g, "$1\n\n$2").replace(/\n{3,}/g, "\n\n").replace(/\n*$/, "\n");
const separator = frontmatter && normalized && !normalized.startsWith("\n") ? "\n" : "";
return frontmatter + separator + normalized;
}
__name(normalizeOutput, "normalizeOutput");
function normalizeOutputWithOptions(output, options = {}) {
const { stripTrailingWhitespace = true, maxConsecutiveNewlines = 2, ensureTrailingNewline = true } = options;
let result = output;
if (stripTrailingWhitespace) {
result = result.replace(/[ \t]+$/gm, "");
}
if (maxConsecutiveNewlines > 0) {
const pattern = new RegExp(`\\n{${maxConsecutiveNewlines + 1},}`, "g");
const replacement = "\n".repeat(maxConsecutiveNewlines);
result = result.replace(pattern, replacement);
}
if (ensureTrailingNewline) {
result = result.replace(/\n*$/, "\n");
}
return result;
}
__name(normalizeOutputWithOptions, "normalizeOutputWithOptions");
export { normalizeOutput, normalizeOutputWithOptions };
//# sourceMappingURL=chunk-W2ZIGLG7.mjs.map
//# sourceMappingURL=chunk-W2ZIGLG7.mjs.map