UNPKG

mlld

Version:

mlld: llm scripting language

1 lines 5.37 kB
{"version":3,"sources":["../interpreter/output/normalizer.ts"],"names":["normalizeOutput","output","withoutLeadingNewlines","replace","frontmatterRegex","frontmatterMatch","match","frontmatter","content","slice","length","normalized","separator","startsWith","normalizeOutputWithOptions","options","stripTrailingWhitespace","maxConsecutiveNewlines","ensureTrailingNewline","result","pattern","RegExp","replacement","repeat"],"mappings":";;;AAwBO,SAASA,gBAAgBC,MAAc,EAAA;AAG5C,EAAA,MAAMC,sBAAyBD,GAAAA,MAAAA,CAAOE,OAAQ,CAAA,MAAA,EAAQ,EAAA,CAAA;AACtD,EAAA,MAAMC,gBAAmB,GAAA,uBAAA;AACzB,EAAMC,MAAAA,gBAAAA,GAAmBH,sBAAuBI,CAAAA,KAAAA,CAAMF,gBAAAA,CAAAA;AACtD,EAAA,MAAMG,WAAcF,GAAAA,gBAAAA,GAAmBA,gBAAiB,CAAA,CAAA,CAAK,GAAA,EAAA;AAC7D,EAAA,MAAMG,UAAUH,gBAAmBH,GAAAA,sBAAAA,CAAuBO,KAAMF,CAAAA,WAAAA,CAAYG,MAAM,CAAIR,GAAAA,sBAAAA;AAEtF,EAAA,MAAMS,UAAaH,GAAAA,OAAAA,CAChBL,OAAQ,CAAA,MAAA,EAAQ,EAAA,CAAA,CAChBA,OAAQ,CAAA,WAAA,EAAa,EAAA,CAAA,CACrBA,OAAQ,CAAA,eAAA,EAAiB,QAAA,CAAA,CACzBA,OAAQ,CAAA,6BAAA,EAA+B,UAAA,CAAA,CACvCA,OAAQ,CAAA,8BAAA,EAAgC,UAAA,CAAA,CACxCA,OAAQ,CAAA,SAAA,EAAW,MAAA,CAAA,CACnBA,OAAQ,CAAA,MAAA,EAAQ,IAAA,CAAA;AAGnB,EAAMS,MAAAA,SAAAA,GAAaL,eAAeI,UAAc,IAAA,CAACA,WAAWE,UAAW,CAAA,IAAA,IAAS,IAAO,GAAA,EAAA;AACvF,EAAA,OAAON,cAAcK,SAAYD,GAAAA,UAAAA;AACnC;AArBgBX,MAAAA,CAAAA,eAAAA,EAAAA,iBAAAA,CAAAA;AAwCT,SAASc,0BACdb,CAAAA,MAAAA,EACAc,OAAgC,GAAA,EAAE,EAAA;AAElC,EAAA,MAAM,EACJC,uBAA0B,GAAA,IAAA,EAC1BC,yBAAyB,CACzBC,EAAAA,qBAAAA,GAAwB,MACtBH,GAAAA,OAAAA;AAEJ,EAAA,IAAII,MAASlB,GAAAA,MAAAA;AAEb,EAAA,IAAIe,uBAAyB,EAAA;AAC3BG,IAASA,MAAAA,GAAAA,MAAAA,CAAOhB,OAAQ,CAAA,WAAA,EAAa,EAAA,CAAA;AACvC;AAEA,EAAA,IAAIc,yBAAyB,CAAG,EAAA;AAC9B,IAAA,MAAMG,UAAU,IAAIC,MAAAA,CAAO,OAAOJ,sBAAyB,GAAA,CAAA,MAAO,GAAA,CAAA;AAClE,IAAMK,MAAAA,WAAAA,GAAc,IAAKC,CAAAA,MAAAA,CAAON,sBAAAA,CAAAA;AAChCE,IAASA,MAAAA,GAAAA,MAAAA,CAAOhB,OAAQiB,CAAAA,OAAAA,EAASE,WAAAA,CAAAA;AACnC;AAEA,EAAA,IAAIJ,qBAAuB,EAAA;AACzBC,IAASA,MAAAA,GAAAA,MAAAA,CAAOhB,OAAQ,CAAA,MAAA,EAAQ,IAAA,CAAA;AAClC;AAEA,EAAOgB,OAAAA,MAAAA;AACT;AA3BgBL,MAAAA,CAAAA,0BAAAA,EAAAA,4BAAAA,CAAAA","file":"chunk-W2ZIGLG7.mjs","sourcesContent":["/**\n * Output Normalizer\n *\n * Simple line-based normalization that replaces Prettier.\n * Eliminates hanging bug, JSON protection hacks, and adds ~0ms overhead.\n */\n\n/**\n * Normalize output content\n *\n * Rules:\n * 1. Strip leading newlines\n * 2. Strip trailing whitespace per line\n * 3. Ensure blank line before headers\n * 4. Ensure blank line after headers\n * 5. Collapse 3+ newlines to max 2 (one blank line)\n * 6. Ensure single trailing newline\n *\n * This replaces Prettier markdown formatting with a simple,\n * predictable normalization that doesn't require workarounds.\n *\n * @param output Raw output string\n * @returns Normalized output\n */\nexport function normalizeOutput(output: string): string {\n // Protect frontmatter blocks from all formatting rules\n // Match frontmatter after stripping leading newlines\n const withoutLeadingNewlines = output.replace(/^\\n+/, '');\n const frontmatterRegex = /^---\\n[\\s\\S]*?\\n---\\n/;\n const frontmatterMatch = withoutLeadingNewlines.match(frontmatterRegex);\n const frontmatter = frontmatterMatch ? frontmatterMatch[0] : '';\n const content = frontmatterMatch ? withoutLeadingNewlines.slice(frontmatter.length) : withoutLeadingNewlines;\n\n const normalized = content\n .replace(/^\\n+/, '') // Strip leading newlines from content\n .replace(/[ \\t]+$/gm, '') // Strip trailing whitespace per line\n .replace(/\\n(#{1,6}\\s)/g, '\\n\\n$1') // Blank line before headers\n .replace(/(#{1,6}\\s[^\\n]+)\\n([^\\n#])/g, '$1\\n\\n$2') // Blank line after headers (if next line isn't header/blank)\n .replace(/([^{\\[\\n])\\n([^\\n#\\s\\-}\\]])/g, '$1\\n\\n$2') // Blank line between paragraphs (exclude JSON/arrays/headers/lists)\n .replace(/\\n{3,}/g, '\\n\\n') // Max one blank line (collapse extras from rules)\n .replace(/\\n*$/, '\\n'); // Single trailing newline\n\n // Add blank line after frontmatter if present and content follows\n const separator = (frontmatter && normalized && !normalized.startsWith('\\n')) ? '\\n' : '';\n return frontmatter + separator + normalized;\n}\n\n/**\n * Normalize output with custom options\n */\nexport interface NormalizationOptions {\n /** Strip trailing whitespace (default: true) */\n stripTrailingWhitespace?: boolean;\n\n /** Maximum consecutive newlines (default: 2) */\n maxConsecutiveNewlines?: number;\n\n /** Ensure trailing newline (default: true) */\n ensureTrailingNewline?: boolean;\n}\n\n/**\n * Normalize output with custom options\n */\nexport function normalizeOutputWithOptions(\n output: string,\n options: NormalizationOptions = {}\n): string {\n const {\n stripTrailingWhitespace = true,\n maxConsecutiveNewlines = 2,\n ensureTrailingNewline = true\n } = options;\n\n let result = output;\n\n if (stripTrailingWhitespace) {\n result = result.replace(/[ \\t]+$/gm, '');\n }\n\n if (maxConsecutiveNewlines > 0) {\n const pattern = new RegExp(`\\\\n{${maxConsecutiveNewlines + 1},}`, 'g');\n const replacement = '\\n'.repeat(maxConsecutiveNewlines);\n result = result.replace(pattern, replacement);\n }\n\n if (ensureTrailingNewline) {\n result = result.replace(/\\n*$/, '\\n');\n }\n\n return result;\n}\n"]}