@willbooster/shared-lib 10.24.0 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./appEnv.cjs"),t=require("./array.cjs"),n=require("./assert.cjs"),r=require("./concurrency.cjs"),i=require("./crypto.cjs"),a=require("./githubTemplates.cjs"),o=require("./sleep.cjs"),s=require("./error.cjs"),c=require("./filePath.cjs"),l=require("./formatIsoDate.cjs"),u=require("./github.cjs"),d=require("./http.cjs"),f=require("./foreignCjk.cjs"),p=require("./humanize.cjs"),m=require("./mail.cjs"),h=require("./object.cjs"),g=require("./markdown.cjs"),_=require("./parseIsoDate.cjs"),v=require("./parseCommandLineArgs.cjs"),y=require("./number.cjs"),b=require("./text.cjs"),x=require("./prompt.cjs"),S=require("./promptFormatters.cjs"),C=require("./shuffle.cjs"),w=require("./recoverJson.cjs"),T=require("./sqlite.cjs"),E=require("./testingRules.cjs"),D=require("./throttledFetch.cjs"),O=require("./timeout.cjs"),k=require("./url.cjs"),A=require("./yaml.cjs"),j=require("./zenkaku.cjs");exports.BUG_ISSUE_SECTIONS=a.BUG_ISSUE_SECTIONS,exports.CHANGE_ISSUE_SECTIONS=a.CHANGE_ISSUE_SECTIONS,exports.ISSUE_TEMPLATE_RULES=a.ISSUE_TEMPLATE_RULES,exports.PULL_REQUEST_BODY_RULES=a.PULL_REQUEST_BODY_RULES,exports.PULL_REQUEST_REQUIREMENTS_RULES=a.PULL_REQUEST_REQUIREMENTS_RULES,exports.PULL_REQUEST_SECTIONS=a.PULL_REQUEST_SECTIONS,exports.TEST_WRITING_RULES=E.TEST_WRITING_RULES,exports.chunk=t.chunk,exports.clamp=y.clamp,exports.createThrottledFetch=D.createThrottledFetch,exports.detectForeignCjkInJapanese=f.detectForeignCjkInJapanese,exports.ensureTruthy=n.ensureTruthy,exports.errorify=s.errorify,exports.escapeHtml=b.escapeHtml,exports.escapeLikePattern=T.escapeLikePattern,exports.escapePromptTag=x.escapePromptTag,exports.escapeRegExp=b.escapeRegExp,exports.extractCodeBlocks=g.extractCodeBlocks,exports.extractIfSingleOutermostCodeBlock=g.extractIfSingleOutermostCodeBlock,exports.extractSections=g.extractSections,exports.extractTaggedCodeBlock=g.extractTaggedCodeBlock,exports.findForeignCjkCharactersInJapanese=f.findForeignCjkCharactersInJapanese,exports.forEachConcurrently=r.forEachConcurrently,exports.formatDuration=p.formatDuration,exports.formatElapsedTimeInJapanese=p.formatElapsedTimeInJapanese,exports.formatFilesForPrompt=S.formatFilesForPrompt,exports.formatIsoDateInTimeZone=l.formatIsoDateInTimeZone,exports.formatMessagesForPrompt=S.formatMessagesForPrompt,exports.formatPrompt=x.formatPrompt,exports.getAppEnv=e.getAppEnv,exports.getConnectionLevelSqlitePragmas=T.getConnectionLevelSqlitePragmas,exports.getErrorMessage=s.getErrorMessage,exports.getFileExtension=c.getFileExtension,exports.getFormString=d.getFormString,exports.getPersistentSqlitePragmas=T.getPersistentSqlitePragmas,exports.getSafeRedirectPath=k.getSafeRedirectPath,exports.getTildeFence=b.getTildeFence,exports.hasErrorCode=s.hasErrorCode,exports.humanizeNumber=p.humanizeNumber,exports.ignoreEnoent=s.ignoreEnoent,exports.ignoreEnoentAsync=s.ignoreEnoentAsync,exports.ignoreError=s.ignoreError,exports.ignoreErrorAsync=s.ignoreErrorAsync,exports.isRecord=h.isRecord,exports.mailTemplates=m.mailTemplates,exports.mapConcurrently=r.mapConcurrently,exports.omitUndefined=h.omitUndefined,exports.parseBearerToken=d.parseBearerToken,exports.parseCommandLineArgs=v.parseCommandLineArgs,exports.parseGitHubRepositoryFullName=u.parseGitHubRepositoryFullName,exports.parseIsoDate=_.parseIsoDate,exports.parseMarkdownSections=g.parseMarkdownSections,exports.parsePositiveInteger=y.parsePositiveInteger,exports.quoteForShell=b.quoteForShell,exports.raceWithTimeout=O.raceWithTimeout,exports.recoverJson=w.recoverJson,exports.renderSectionChecklist=a.renderSectionChecklist,exports.renderSectionTemplate=a.renderSectionTemplate,exports.serializeForPrompt=x.serializeForPrompt,exports.serializeForPromptInTag=x.serializeForPromptInTag,exports.sha256HexAsync=i.sha256HexAsync,exports.shuffle=C.shuffle,exports.shuffleWithSeed=C.shuffleWithSeed,exports.sleep=o.sleep,exports.timingSafeEqualStringAsync=i.timingSafeEqualStringAsync,exports.toCodeBlock=b.toCodeBlock,exports.toTildeCodeBlock=b.toTildeCodeBlock,exports.truncate=b.truncate,exports.truncateForPrompt=x.truncateForPrompt,exports.withRetry=s.withRetry,exports.yamlStringifyOptions=A.yamlStringifyOptions,exports.zenkakuAlphanumericalsToHankaku=j.zenkakuAlphanumericalsToHankaku;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./appEnv.cjs"),t=require("./array.cjs"),n=require("./assert.cjs"),r=require("./concurrency.cjs"),i=require("./crypto.cjs"),a=require("./githubTemplates.cjs"),o=require("./sleep.cjs"),s=require("./error.cjs"),c=require("./filePath.cjs"),l=require("./formatIsoDate.cjs"),u=require("./github.cjs"),d=require("./http.cjs"),f=require("./foreignCjk.cjs"),p=require("./humanize.cjs"),m=require("./mail.cjs"),h=require("./object.cjs"),g=require("./markdown.cjs"),_=require("./parseIsoDate.cjs"),v=require("./parseCommandLineArgs.cjs"),y=require("./number.cjs"),b=require("./text.cjs"),x=require("./prompt.cjs"),S=require("./promptFormatters.cjs"),C=require("./shuffle.cjs"),w=require("./recoverJson.cjs"),T=require("./sqlite.cjs"),E=require("./testingRules.cjs"),D=require("./throttledFetch.cjs"),O=require("./timeout.cjs"),k=require("./url.cjs"),A=require("./yaml.cjs"),j=require("./zenkaku.cjs");exports.BUG_ISSUE_SECTIONS=a.BUG_ISSUE_SECTIONS,exports.CHANGE_ISSUE_SECTIONS=a.CHANGE_ISSUE_SECTIONS,exports.ISSUE_TEMPLATE_RULES=a.ISSUE_TEMPLATE_RULES,exports.PULL_REQUEST_BODY_RULES=a.PULL_REQUEST_BODY_RULES,exports.PULL_REQUEST_REQUIREMENTS_RULES=a.PULL_REQUEST_REQUIREMENTS_RULES,exports.PULL_REQUEST_SECTIONS=a.PULL_REQUEST_SECTIONS,exports.TEST_WRITING_RULES=E.TEST_WRITING_RULES,exports.chunk=t.chunk,exports.clamp=y.clamp,exports.createThrottledFetch=D.createThrottledFetch,exports.detectForeignCjkInJapanese=f.detectForeignCjkInJapanese,exports.ensureTruthy=n.ensureTruthy,exports.errorify=s.errorify,exports.escapeHtml=b.escapeHtml,exports.escapeLikePattern=T.escapeLikePattern,exports.escapePromptTag=x.escapePromptTag,exports.escapeRegExp=b.escapeRegExp,exports.extractCodeBlocks=g.extractCodeBlocks,exports.extractIfSingleOutermostCodeBlock=g.extractIfSingleOutermostCodeBlock,exports.extractSections=g.extractSections,exports.extractTaggedCodeBlock=g.extractTaggedCodeBlock,exports.findForeignCjkCharactersInJapanese=f.findForeignCjkCharactersInJapanese,exports.forEachConcurrently=r.forEachConcurrently,exports.formatDuration=p.formatDuration,exports.formatElapsedTimeInJapanese=p.formatElapsedTimeInJapanese,exports.formatFilesForPrompt=S.formatFilesForPrompt,exports.formatIsoDateInTimeZone=l.formatIsoDateInTimeZone,exports.formatMessagesForPrompt=S.formatMessagesForPrompt,exports.formatPrompt=x.formatPrompt,exports.getAppEnv=e.getAppEnv,exports.getConnectionLevelSqlitePragmas=T.getConnectionLevelSqlitePragmas,exports.getErrorMessage=s.getErrorMessage,exports.getFileExtension=c.getFileExtension,exports.getFormString=d.getFormString,exports.getPersistentSqlitePragmas=T.getPersistentSqlitePragmas,exports.getSafeRedirectPath=k.getSafeRedirectPath,exports.getTildeFence=b.getTildeFence,exports.hasErrorCode=s.hasErrorCode,exports.humanizeNumber=p.humanizeNumber,exports.ignoreEnoent=s.ignoreEnoent,exports.ignoreEnoentAsync=s.ignoreEnoentAsync,exports.ignoreError=s.ignoreError,exports.ignoreErrorAsync=s.ignoreErrorAsync,exports.isRecord=h.isRecord,exports.mailTemplates=m.mailTemplates,exports.mapConcurrently=r.mapConcurrently,exports.omitUndefined=h.omitUndefined,exports.parseBearerToken=d.parseBearerToken,exports.parseCommandLineArgs=v.parseCommandLineArgs,exports.parseGitHubRepositoryFullName=u.parseGitHubRepositoryFullName,exports.parseIsoDate=_.parseIsoDate,exports.parseMarkdownSections=g.parseMarkdownSections,exports.parsePositiveInteger=y.parsePositiveInteger,exports.quoteForShell=b.quoteForShell,exports.raceWithTimeout=O.raceWithTimeout,exports.recoverJson=w.recoverJson,exports.renderSectionChecklist=a.renderSectionChecklist,exports.renderSectionTemplate=a.renderSectionTemplate,exports.serializeForPrompt=x.serializeForPrompt,exports.serializeForPromptInTag=x.serializeForPromptInTag,exports.sha256HexAsync=i.sha256HexAsync,exports.shuffle=C.shuffle,exports.shuffleWithSeed=C.shuffleWithSeed,exports.sleep=o.sleep,exports.timingSafeEqualStringAsync=i.timingSafeEqualStringAsync,exports.toCodeBlock=b.toCodeBlock,exports.toTildeCodeBlock=b.toTildeCodeBlock,exports.truncate=b.truncate,exports.truncateForPrompt=x.truncateForPrompt,exports.unwrapFencedReply=b.unwrapFencedReply,exports.withRetry=s.withRetry,exports.yamlStringifyOptions=A.yamlStringifyOptions,exports.zenkakuAlphanumericalsToHankaku=j.zenkakuAlphanumericalsToHankaku;
package/dist/index.d.ts CHANGED
@@ -25,7 +25,7 @@ export { recoverJson } from './recoverJson.js';
25
25
  export type { JsonRepair, RecoveredJson, JsonRecovery } from './recoverJson.js';
26
26
  export { sleep } from './sleep.js';
27
27
  export { escapeLikePattern, getConnectionLevelSqlitePragmas, getPersistentSqlitePragmas } from './sqlite.js';
28
- export { escapeHtml, escapeRegExp, getTildeFence, quoteForShell, toCodeBlock, toTildeCodeBlock, truncate, } from './text.js';
28
+ export { escapeHtml, escapeRegExp, getTildeFence, quoteForShell, toCodeBlock, toTildeCodeBlock, truncate, unwrapFencedReply, } from './text.js';
29
29
  export { TEST_WRITING_RULES } from './testingRules.js';
30
30
  export { createThrottledFetch } from './throttledFetch.js';
31
31
  export { raceWithTimeout } from './timeout.js';
@@ -35,7 +35,7 @@ export { zenkakuAlphanumericalsToHankaku } from './zenkaku.js';
35
35
  export type { AppEnv } from './appEnv.js';
36
36
  export type { RetryOptions } from './error.js';
37
37
  export type { ForeignCjkLanguage } from './foreignCjk.js';
38
- export type { CodeBlock, ExtractCodeBlockOptions, MarkdownSection } from './markdown.js';
38
+ export type { CodeBlock, MarkdownSection } from './markdown.js';
39
39
  export type { TemplateSection } from './githubTemplates.js';
40
40
  export type { ThrottledFetchOptions } from './throttledFetch.js';
41
41
  export type { RaceWithTimeoutResult } from './timeout.js';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{getAppEnv as e}from"./appEnv.js";import{chunk as t}from"./array.js";import{ensureTruthy as n}from"./assert.js";import{forEachConcurrently as r,mapConcurrently as i}from"./concurrency.js";import{sha256HexAsync as a,timingSafeEqualStringAsync as o}from"./crypto.js";import{BUG_ISSUE_SECTIONS as s,CHANGE_ISSUE_SECTIONS as c,ISSUE_TEMPLATE_RULES as l,PULL_REQUEST_BODY_RULES as u,PULL_REQUEST_REQUIREMENTS_RULES as d,PULL_REQUEST_SECTIONS as f,renderSectionChecklist as p,renderSectionTemplate as m}from"./githubTemplates.js";import{sleep as h}from"./sleep.js";import{errorify as g,getErrorMessage as _,hasErrorCode as v,ignoreEnoent as y,ignoreEnoentAsync as b,ignoreError as x,ignoreErrorAsync as S,withRetry as C}from"./error.js";import{getFileExtension as w}from"./filePath.js";import{formatIsoDateInTimeZone as T}from"./formatIsoDate.js";import{parseGitHubRepositoryFullName as E}from"./github.js";import{getFormString as D,parseBearerToken as O}from"./http.js";import{detectForeignCjkInJapanese as k,findForeignCjkCharactersInJapanese as A}from"./foreignCjk.js";import{formatDuration as j,formatElapsedTimeInJapanese as M,humanizeNumber as N}from"./humanize.js";import{mailTemplates as P}from"./mail.js";import{isRecord as F,omitUndefined as I}from"./object.js";import{extractCodeBlocks as L,extractIfSingleOutermostCodeBlock as R,extractSections as z,extractTaggedCodeBlock as B,parseMarkdownSections as V}from"./markdown.js";import{parseIsoDate as H}from"./parseIsoDate.js";import{parseCommandLineArgs as U}from"./parseCommandLineArgs.js";import{clamp as W,parsePositiveInteger as G}from"./number.js";import{escapeHtml as K,escapeRegExp as q,getTildeFence as J,quoteForShell as Y,toCodeBlock as X,toTildeCodeBlock as Z,truncate as Q}from"./text.js";import{escapePromptTag as $,formatPrompt as ee,serializeForPrompt as te,serializeForPromptInTag as ne,truncateForPrompt as re}from"./prompt.js";import{formatFilesForPrompt as ie,formatMessagesForPrompt as ae}from"./promptFormatters.js";import{shuffle as oe,shuffleWithSeed as se}from"./shuffle.js";import{recoverJson as ce}from"./recoverJson.js";import{escapeLikePattern as le,getConnectionLevelSqlitePragmas as ue,getPersistentSqlitePragmas as de}from"./sqlite.js";import{TEST_WRITING_RULES as fe}from"./testingRules.js";import{createThrottledFetch as pe}from"./throttledFetch.js";import{raceWithTimeout as me}from"./timeout.js";import{getSafeRedirectPath as he}from"./url.js";import{yamlStringifyOptions as ge}from"./yaml.js";import{zenkakuAlphanumericalsToHankaku as _e}from"./zenkaku.js";export{s as BUG_ISSUE_SECTIONS,c as CHANGE_ISSUE_SECTIONS,l as ISSUE_TEMPLATE_RULES,u as PULL_REQUEST_BODY_RULES,d as PULL_REQUEST_REQUIREMENTS_RULES,f as PULL_REQUEST_SECTIONS,fe as TEST_WRITING_RULES,t as chunk,W as clamp,pe as createThrottledFetch,k as detectForeignCjkInJapanese,n as ensureTruthy,g as errorify,K as escapeHtml,le as escapeLikePattern,$ as escapePromptTag,q as escapeRegExp,L as extractCodeBlocks,R as extractIfSingleOutermostCodeBlock,z as extractSections,B as extractTaggedCodeBlock,A as findForeignCjkCharactersInJapanese,r as forEachConcurrently,j as formatDuration,M as formatElapsedTimeInJapanese,ie as formatFilesForPrompt,T as formatIsoDateInTimeZone,ae as formatMessagesForPrompt,ee as formatPrompt,e as getAppEnv,ue as getConnectionLevelSqlitePragmas,_ as getErrorMessage,w as getFileExtension,D as getFormString,de as getPersistentSqlitePragmas,he as getSafeRedirectPath,J as getTildeFence,v as hasErrorCode,N as humanizeNumber,y as ignoreEnoent,b as ignoreEnoentAsync,x as ignoreError,S as ignoreErrorAsync,F as isRecord,P as mailTemplates,i as mapConcurrently,I as omitUndefined,O as parseBearerToken,U as parseCommandLineArgs,E as parseGitHubRepositoryFullName,H as parseIsoDate,V as parseMarkdownSections,G as parsePositiveInteger,Y as quoteForShell,me as raceWithTimeout,ce as recoverJson,p as renderSectionChecklist,m as renderSectionTemplate,te as serializeForPrompt,ne as serializeForPromptInTag,a as sha256HexAsync,oe as shuffle,se as shuffleWithSeed,h as sleep,o as timingSafeEqualStringAsync,X as toCodeBlock,Z as toTildeCodeBlock,Q as truncate,re as truncateForPrompt,C as withRetry,ge as yamlStringifyOptions,_e as zenkakuAlphanumericalsToHankaku};
1
+ import{getAppEnv as e}from"./appEnv.js";import{chunk as t}from"./array.js";import{ensureTruthy as n}from"./assert.js";import{forEachConcurrently as r,mapConcurrently as i}from"./concurrency.js";import{sha256HexAsync as a,timingSafeEqualStringAsync as o}from"./crypto.js";import{BUG_ISSUE_SECTIONS as s,CHANGE_ISSUE_SECTIONS as c,ISSUE_TEMPLATE_RULES as l,PULL_REQUEST_BODY_RULES as u,PULL_REQUEST_REQUIREMENTS_RULES as d,PULL_REQUEST_SECTIONS as f,renderSectionChecklist as p,renderSectionTemplate as m}from"./githubTemplates.js";import{sleep as h}from"./sleep.js";import{errorify as g,getErrorMessage as _,hasErrorCode as v,ignoreEnoent as y,ignoreEnoentAsync as b,ignoreError as x,ignoreErrorAsync as S,withRetry as C}from"./error.js";import{getFileExtension as w}from"./filePath.js";import{formatIsoDateInTimeZone as T}from"./formatIsoDate.js";import{parseGitHubRepositoryFullName as E}from"./github.js";import{getFormString as D,parseBearerToken as O}from"./http.js";import{detectForeignCjkInJapanese as k,findForeignCjkCharactersInJapanese as A}from"./foreignCjk.js";import{formatDuration as j,formatElapsedTimeInJapanese as M,humanizeNumber as N}from"./humanize.js";import{mailTemplates as P}from"./mail.js";import{isRecord as F,omitUndefined as I}from"./object.js";import{extractCodeBlocks as L,extractIfSingleOutermostCodeBlock as R,extractSections as z,extractTaggedCodeBlock as B,parseMarkdownSections as V}from"./markdown.js";import{parseIsoDate as H}from"./parseIsoDate.js";import{parseCommandLineArgs as U}from"./parseCommandLineArgs.js";import{clamp as W,parsePositiveInteger as G}from"./number.js";import{escapeHtml as K,escapeRegExp as q,getTildeFence as J,quoteForShell as Y,toCodeBlock as X,toTildeCodeBlock as Z,truncate as Q,unwrapFencedReply as $}from"./text.js";import{escapePromptTag as ee,formatPrompt as te,serializeForPrompt as ne,serializeForPromptInTag as re,truncateForPrompt as ie}from"./prompt.js";import{formatFilesForPrompt as ae,formatMessagesForPrompt as oe}from"./promptFormatters.js";import{shuffle as se,shuffleWithSeed as ce}from"./shuffle.js";import{recoverJson as le}from"./recoverJson.js";import{escapeLikePattern as ue,getConnectionLevelSqlitePragmas as de,getPersistentSqlitePragmas as fe}from"./sqlite.js";import{TEST_WRITING_RULES as pe}from"./testingRules.js";import{createThrottledFetch as me}from"./throttledFetch.js";import{raceWithTimeout as he}from"./timeout.js";import{getSafeRedirectPath as ge}from"./url.js";import{yamlStringifyOptions as _e}from"./yaml.js";import{zenkakuAlphanumericalsToHankaku as ve}from"./zenkaku.js";export{s as BUG_ISSUE_SECTIONS,c as CHANGE_ISSUE_SECTIONS,l as ISSUE_TEMPLATE_RULES,u as PULL_REQUEST_BODY_RULES,d as PULL_REQUEST_REQUIREMENTS_RULES,f as PULL_REQUEST_SECTIONS,pe as TEST_WRITING_RULES,t as chunk,W as clamp,me as createThrottledFetch,k as detectForeignCjkInJapanese,n as ensureTruthy,g as errorify,K as escapeHtml,ue as escapeLikePattern,ee as escapePromptTag,q as escapeRegExp,L as extractCodeBlocks,R as extractIfSingleOutermostCodeBlock,z as extractSections,B as extractTaggedCodeBlock,A as findForeignCjkCharactersInJapanese,r as forEachConcurrently,j as formatDuration,M as formatElapsedTimeInJapanese,ae as formatFilesForPrompt,T as formatIsoDateInTimeZone,oe as formatMessagesForPrompt,te as formatPrompt,e as getAppEnv,de as getConnectionLevelSqlitePragmas,_ as getErrorMessage,w as getFileExtension,D as getFormString,fe as getPersistentSqlitePragmas,ge as getSafeRedirectPath,J as getTildeFence,v as hasErrorCode,N as humanizeNumber,y as ignoreEnoent,b as ignoreEnoentAsync,x as ignoreError,S as ignoreErrorAsync,F as isRecord,P as mailTemplates,i as mapConcurrently,I as omitUndefined,O as parseBearerToken,U as parseCommandLineArgs,E as parseGitHubRepositoryFullName,H as parseIsoDate,V as parseMarkdownSections,G as parsePositiveInteger,Y as quoteForShell,he as raceWithTimeout,le as recoverJson,p as renderSectionChecklist,m as renderSectionTemplate,ne as serializeForPrompt,re as serializeForPromptInTag,a as sha256HexAsync,se as shuffle,ce as shuffleWithSeed,h as sleep,o as timingSafeEqualStringAsync,X as toCodeBlock,Z as toTildeCodeBlock,Q as truncate,ie as truncateForPrompt,$ as unwrapFencedReply,C as withRetry,_e as yamlStringifyOptions,ve as zenkakuAlphanumericalsToHankaku};
package/dist/markdown.cjs CHANGED
@@ -1,7 +1,6 @@
1
- "use strict";function e(e){let t=y(e),n=[];for(let e=0;e<t.length;e++){let r=h(t[e]??``);if(!r)continue;let i=g(t,e+1,r);n.push({...r.language&&{language:r.language},code:t.slice(e+1,i).map(e=>v(e,r.indent)).join(`
2
- `),isClosed:i<t.length}),e=i}return n}function t(e,t){let{languages:i,fence:a}=n(t)?{languages:t}:t??{},o=e=>!i?.length||i.some(t=>t.toLowerCase()===e),s=a===void 0?void 0:r(e,a,o);if(s!==void 0)return s;let c=p(e);return!c||!o(c.language)?e:c.code}function n(e){return Array.isArray(e)}function r(e,t,n){let r=y(e),a=r.findLastIndex(e=>e.trimEnd()===t),o=i(r,a,t,n);if(o!==void 0)return r.slice(o+1,a).join(`
3
- `)}function i(e,t,n,r){for(let i=0;i<t;i++){let t=e[i]??``,a=t.trimEnd(),o=a.startsWith(n)&&a.charAt(n.length)!==n.charAt(0),s=a.slice(n.length).trim().split(/\s/u)[0]?.toLowerCase()??``;if(o&&(s===``||r(s)))return i;let c=h(t);c&&(i=g(e,i+1,c))}}const a=/* @__PURE__ */ new Set([``,`markdown`,`md`]);function o(e,t){let n=`<${t}>`,r=s(y(e),n);if(r!==void 0)return r;let i=p(e);if(i?.isWhole&&a.has(i.language))return s(y(i.code),n)}function s(e,t){for(let n=0;n<e.length;n++){let r=e[n]??``,i=h(r);if(i){n=g(e,n+1,i);continue}if(r.trim()!==t)continue;let a=n+1;for(;a<e.length&&!e[a]?.trim();)a++;let o=h(e[a]??``);if(!o)continue;let s=g(e,a+1,o);if(s<e.length)return e.slice(a+1,s).map(e=>v(e,o.indent)).join(`
4
- `).trim()}}function c(e,t){let n=l(e).map(e=>({...e,key:u(e.heading)})),r=[...new Set(t)].map(e=>({name:e,key:u(e)})),i=Object.create(null);for(let{name:e,key:t}of r){let a;for(let r of n)r.key===t&&(!a||r.depth<a.depth||r.depth===a.depth&&r.heading===e&&a.heading!==e)&&(a=r);a&&(a.heading!==e&&r.some(n=>n.name!==e&&n.key===t)||a.content&&(i[e]=a.content))}return i}function l(e){let t=p(e),n=t&&a.has(t.language)?t:void 0;if(n?.isWhole)return f(n.code);let r=f(e);return r.length===0&&n?f(n.code):r}function u(e){return d(d(e).replace(/^\d+(?:\.\d+)*(?:\.\s+|\)\s*|\s+)/u,``)).replaceAll(/\s+/gu,` `).toLowerCase()}function d(e){return e.replaceAll(/^[\s*_`~"'「」]+|[\s*_`~"'「」::]+$/gu,``)}function f(e){let t=y(e),n=m(t);return n.map((e,r)=>{let i=r+1;for(;i<n.length&&(n[i]?.depth??0)>e.depth;)i++;return{depth:e.depth,heading:e.text,content:t.slice(e.index+1,n[i]?.index).join(`
5
- `).trim()}})}function p(e){let t=y(e.replace(/^\s*\n/u,``).trimEnd()),n=h(t[0]??``);if(!n)return;let r=t.length-1,i=r>0&&_(t[r]??``,n)?r:g(t,1,n);return{language:n.language,code:t.slice(1,i).map(e=>v(e,n.indent)).join(`
6
- `),isWhole:i>=r}}function m(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n]??``,i=h(r);if(i){n=g(e,n+1,i);continue}let a=/^(#{1,6})(?:[ \t]+(.*))?$/u.exec(r);if(!a)continue;let o=(a[2]??``).replace(/(?:^|[ \t]+)#+[ \t]*$/u,``).trim().replaceAll(/(`+)(.+?)\1/gu,(e,t,n)=>n.startsWith(` `)&&n.endsWith(` `)?n.slice(1,-1):n);t.push({index:n,depth:a[1]?.length??1,text:o})}return t}function h(e){let t=/^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(e),n=t?.[2],r=t?.[3]??``;if(!(!n||n.startsWith("`")&&r.includes("`")))return{char:n.charAt(0),length:n.length,indent:t[1]?.length??0,language:r.trim().split(/\s/u)[0]?.toLowerCase()??``}}function g(e,t,n){let r=t;for(;r<e.length&&!_(e[r]??``,n);)r++;return r}function _(e,t){let n=/^ {0,3}(`{3,}|~{3,})[ \t]*$/u.exec(e)?.[1];return n?.charAt(0)===t.char&&n.length>=t.length}function v(e,t){let n=0;for(;n<t&&e[n]===` `;)n++;return e.slice(n)}function y(e){return e.split(/\r?\n/u)}exports.extractCodeBlocks=e,exports.extractIfSingleOutermostCodeBlock=t,exports.extractSections=c,exports.extractTaggedCodeBlock=o,exports.parseMarkdownSections=l;
1
+ "use strict";function e(e){let t=g(e),n=[];for(let e=0;e<t.length;e++){let r=f(t[e]??``);if(!r)continue;let i=p(t,e+1,r);n.push({...r.language&&{language:r.language},code:t.slice(e+1,i).map(e=>h(e,r.indent)).join(`
2
+ `),isClosed:i<t.length}),e=i}return n}function t(e,t){let n=u(e);return!n||t?.length&&!t.some(e=>e.toLowerCase()===n.language)?e:n.code}const n=/* @__PURE__ */ new Set([``,`markdown`,`md`]);function r(e,t){let r=`<${t}>`,a=i(g(e),r);if(a!==void 0)return a;let o=u(e);if(o?.isWhole&&n.has(o.language))return i(g(o.code),r)}function i(e,t){for(let n=0;n<e.length;n++){let r=e[n]??``,i=f(r);if(i){n=p(e,n+1,i);continue}if(r.trim()!==t)continue;let a=n+1;for(;a<e.length&&!e[a]?.trim();)a++;let o=f(e[a]??``);if(!o)continue;let s=p(e,a+1,o);if(s<e.length)return e.slice(a+1,s).map(e=>h(e,o.indent)).join(`
3
+ `).trim()}}function a(e,t){let n=o(e).map(e=>({...e,key:s(e.heading)})),r=[...new Set(t)].map(e=>({name:e,key:s(e)})),i=Object.create(null);for(let{name:e,key:t}of r){let a;for(let r of n)r.key===t&&(!a||r.depth<a.depth||r.depth===a.depth&&r.heading===e&&a.heading!==e)&&(a=r);a&&(a.heading!==e&&r.some(n=>n.name!==e&&n.key===t)||a.content&&(i[e]=a.content))}return i}function o(e){let t=u(e),r=t&&n.has(t.language)?t:void 0;if(r?.isWhole)return l(r.code);let i=l(e);return i.length===0&&r?l(r.code):i}function s(e){return c(c(e).replace(/^\d+(?:\.\d+)*(?:\.\s+|\)\s*|\s+)/u,``)).replaceAll(/\s+/gu,` `).toLowerCase()}function c(e){return e.replaceAll(/^[\s*_`~"'「」]+|[\s*_`~"'「」::]+$/gu,``)}function l(e){let t=g(e),n=d(t);return n.map((e,r)=>{let i=r+1;for(;i<n.length&&(n[i]?.depth??0)>e.depth;)i++;return{depth:e.depth,heading:e.text,content:t.slice(e.index+1,n[i]?.index).join(`
4
+ `).trim()}})}function u(e){let t=g(e.replace(/^\s*\n/u,``).trimEnd()),n=f(t[0]??``);if(!n)return;let r=t.length-1,i=r>0&&m(t[r]??``,n)?r:p(t,1,n);return{language:n.language,code:t.slice(1,i).map(e=>h(e,n.indent)).join(`
5
+ `),isWhole:i>=r}}function d(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n]??``,i=f(r);if(i){n=p(e,n+1,i);continue}let a=/^(#{1,6})(?:[ \t]+(.*))?$/u.exec(r);if(!a)continue;let o=(a[2]??``).replace(/(?:^|[ \t]+)#+[ \t]*$/u,``).trim().replaceAll(/(`+)(.+?)\1/gu,(e,t,n)=>n.startsWith(` `)&&n.endsWith(` `)?n.slice(1,-1):n);t.push({index:n,depth:a[1]?.length??1,text:o})}return t}function f(e){let t=/^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(e),n=t?.[2],r=t?.[3]??``;if(!(!n||n.startsWith("`")&&r.includes("`")))return{char:n.charAt(0),length:n.length,indent:t[1]?.length??0,language:r.trim().split(/\s/u)[0]?.toLowerCase()??``}}function p(e,t,n){let r=t;for(;r<e.length&&!m(e[r]??``,n);)r++;return r}function m(e,t){let n=/^ {0,3}(`{3,}|~{3,})[ \t]*$/u.exec(e)?.[1];return n?.charAt(0)===t.char&&n.length>=t.length}function h(e,t){let n=0;for(;n<t&&e[n]===` `;)n++;return e.slice(n)}function g(e){return e.split(/\r?\n/u)}exports.extractCodeBlocks=e,exports.extractIfSingleOutermostCodeBlock=t,exports.extractSections=a,exports.extractTaggedCodeBlock=r,exports.parseMarkdownSections=o;
7
6
  //# sourceMappingURL=markdown.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"markdown.cjs","names":["extractCodeBlocks","lines","splitLines","markdown","blocks","index","fence","parseOpeningFence","end","findClosingFence","stripIndent","line","extractIfSingleOutermostCodeBlock","isLanguageList","languagesOrOptions","isAllowed","languages","allowed","language","known","findBlockWithKnownFence","text","block","findOutermostCodeBlock","value","start","findSentOpeningFence","trimmed","isSentFence","opener","WRAPPED_MARKDOWN_LANGUAGES","extractTaggedCodeBlock","tag","tagName","found","findTaggedCodeBlock","wrapper","enclosingFence","fenceIndex","contentLine","extractSections","candidates","parseMarkdownSections","section","normalizeHeading","requested","names","name","sections","best","candidate","key","other","splitSections","stripHeadingEdges","heading","headings","findHeadings","nextIndex","lastIndex","isClosingFence","match","content","info","closer","indent"],"sources":["../src/markdown.ts"],"sourcesContent":["export interface CodeBlock {\n /** The first word of the info string, lowercased; `undefined` when the fence has none. */\n language?: string;\n code: string;\n /** `false` when the text ends before the closing fence, e.g. because the LLM output was cut off. */\n isClosed: boolean;\n}\n\ninterface Fence {\n char: string;\n length: number;\n indent: number;\n language: string;\n}\n\n/**\n * Extracts the fenced code blocks (backticks or tildes, any fence length) of Markdown text such as an LLM response.\n * A block runs to the first closing fence of the same character that is at least as long as its opening fence,\n * so a longer fence can hold shorter fences. Only fences indented by at most three spaces are recognized; fences\n * inside block quotes, deeply nested list items, and indented code blocks are not.\n */\nexport function extractCodeBlocks(markdown: string): CodeBlock[] {\n const lines = splitLines(markdown);\n const blocks: CodeBlock[] = [];\n for (let index = 0; index < lines.length; index++) {\n const fence = parseOpeningFence(lines[index] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, index + 1, fence);\n blocks.push({\n ...(fence.language && { language: fence.language }),\n code: lines\n .slice(index + 1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isClosed: end < lines.length,\n });\n index = end;\n }\n return blocks;\n}\n\nexport interface ExtractCodeBlockOptions {\n /** Allowed languages, compared case-insensitively; an omitted or empty list allows any language. */\n languages?: readonly string[];\n /** The exact fence the caller asked the LLM to wrap its response in, e.g. `getTildeFence(text)`. */\n fence?: string;\n}\n\n/**\n * Returns the contents of the code block that an LLM wrapped its whole response in, or `text` itself when the\n * response does not start with a fence or the fence's language is not allowed. A plain array is taken as `languages`.\n * When the response also ends with a matching closing fence, everything between its first and last lines is returned,\n * so fences the LLM nested without lengthening the outer fence stay in the contents. Otherwise the first block is\n * returned and text after it is dropped; an unclosed block runs to the end of the text.\n * With `fence`, a preamble and a postamble around the block are dropped first: everything between the first line\n * holding exactly `fence` and an allowed language or none, outside blocks of other languages, and the last later line\n * holding exactly `fence` is returned. Without such a pair, the rules above apply.\n */\nexport function extractIfSingleOutermostCodeBlock(\n text: string,\n languagesOrOptions?: readonly string[] | ExtractCodeBlockOptions\n): string {\n const { languages, fence }: ExtractCodeBlockOptions = isLanguageList(languagesOrOptions)\n ? { languages: languagesOrOptions }\n : (languagesOrOptions ?? {});\n const isAllowed = (language: string): boolean =>\n !languages?.length || languages.some((allowed) => allowed.toLowerCase() === language);\n const known = fence === undefined ? undefined : findBlockWithKnownFence(text, fence, isAllowed);\n if (known !== undefined) return known;\n const block = findOutermostCodeBlock(text);\n if (!block || !isAllowed(block.language)) return text;\n return block.code;\n}\n\nfunction isLanguageList(value: readonly string[] | ExtractCodeBlockOptions | undefined): value is readonly string[] {\n return Array.isArray(value);\n}\n\nfunction findBlockWithKnownFence(\n text: string,\n fence: string,\n isAllowed: (language: string) => boolean\n): string | undefined {\n const lines = splitLines(text);\n const end = lines.findLastIndex((line) => line.trimEnd() === fence);\n const start = findSentOpeningFence(lines, end, fence, isAllowed);\n if (start === undefined) return;\n return lines.slice(start + 1, end).join('\\n');\n}\n\n/** Returns the index of the first line before `end` opening `fence` with an allowed language or none, skipping other blocks. */\nfunction findSentOpeningFence(\n lines: readonly string[],\n end: number,\n fence: string,\n isAllowed: (language: string) => boolean\n): number | undefined {\n for (let index = 0; index < end; index++) {\n const line = lines[index] ?? '';\n const trimmed = line.trimEnd();\n const isSentFence = trimmed.startsWith(fence) && trimmed.charAt(fence.length) !== fence.charAt(0);\n const language = trimmed.slice(fence.length).trim().split(/\\s/u)[0]?.toLowerCase() ?? '';\n if (isSentFence && (language === '' || isAllowed(language))) return index;\n const opener = parseOpeningFence(line);\n if (opener) index = findClosingFence(lines, index + 1, opener);\n }\n}\n\nconst WRAPPED_MARKDOWN_LANGUAGES = new Set(['', 'markdown', 'md']);\n\n/**\n * Returns the trimmed contents of the first closed fenced code block (backticks or tildes) that follows a line holding\n * only `<tagName>` in text such as an LLM response, e.g. `<answer>\\n~~~\\n42\\n~~~`, or `undefined` when there is none.\n * Only blank lines may separate the tag line from the opening fence. Mentions of the tag within prose lines and tag\n * lines inside other fenced blocks (e.g. a format example) are skipped, but a whole-response `markdown`, `md`, or\n * unlabeled wrapper is read inside, as `parseMarkdownSections` does.\n */\nexport function extractTaggedCodeBlock(text: string, tagName: string): string | undefined {\n const tag = `<${tagName}>`;\n const found = findTaggedCodeBlock(splitLines(text), tag);\n if (found !== undefined) return found;\n // Only a real wrapper hides every tag line from the scan above; a leading example block paired with the tagged\n // block's closing fence merely looks like one, so its contents must not be read first.\n const wrapper = findOutermostCodeBlock(text);\n if (wrapper?.isWhole && WRAPPED_MARKDOWN_LANGUAGES.has(wrapper.language)) {\n return findTaggedCodeBlock(splitLines(wrapper.code), tag);\n }\n}\n\nfunction findTaggedCodeBlock(lines: readonly string[], tag: string): string | undefined {\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const enclosingFence = parseOpeningFence(line);\n if (enclosingFence) {\n index = findClosingFence(lines, index + 1, enclosingFence);\n continue;\n }\n if (line.trim() !== tag) continue;\n let fenceIndex = index + 1;\n while (fenceIndex < lines.length && !lines[fenceIndex]?.trim()) fenceIndex++;\n const fence = parseOpeningFence(lines[fenceIndex] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, fenceIndex + 1, fence);\n if (end < lines.length) {\n return lines\n .slice(fenceIndex + 1, end)\n .map((contentLine) => stripIndent(contentLine, fence.indent))\n .join('\\n')\n .trim();\n }\n }\n}\n\nexport interface MarkdownSection {\n depth: number;\n /** The heading text with the closing `#` sequence and inline-code backticks removed. */\n heading: string;\n /** The source text up to the next heading at the same or a shallower depth, trimmed; subsections are included. */\n content: string;\n}\n\n/**\n * Extracts the content of the section headed by each of `names` from Markdown text such as an LLM response, keyed by\n * the given names. A heading matches a name when both are equal after removing decorations at their edges (such as\n * `**bold**`, backticks, and quotes), a leading number like `1.` or `2.1`, and a trailing colon, ignoring case and spacing;\n * headings at any depth are candidates.\n * Among matching headings, the shallowest wins, then one equal to the name as written, then the first; each heading\n * serves at most one name. When distinct requested names normalize alike, only exact matches at the shallowest\n * matching depth are returned. Missing, empty, or ambiguous sections are absent from the prototype-free result.\n */\nexport function extractSections<const Name extends string>(\n markdown: string,\n names: readonly Name[]\n): Partial<Record<Name, string>> {\n const candidates = parseMarkdownSections(markdown).map((section) => ({\n ...section,\n key: normalizeHeading(section.heading),\n }));\n const requested = [...new Set(names)].map((name) => ({ name, key: normalizeHeading(name) }));\n const sections: Partial<Record<Name, string>> = Object.create(null);\n for (const { name, key } of requested) {\n let best: (typeof candidates)[number] | undefined;\n for (const candidate of candidates) {\n if (candidate.key !== key) continue;\n if (\n !best ||\n candidate.depth < best.depth ||\n (candidate.depth === best.depth && candidate.heading === name && best.heading !== name)\n ) {\n best = candidate;\n }\n }\n if (!best) continue;\n if (best.heading !== name && requested.some((other) => other.name !== name && other.key === key)) continue;\n if (best.content) sections[name] = best.content;\n }\n return sections;\n}\n\n/**\n * Returns every ATX heading of Markdown text in order, with the source text under it.\n * Only headings starting at the beginning of a line outside fenced code blocks count; setext headings (underlined\n * with `=` or `-`) are ignored. A whole-response `markdown`, `md`, or unlabeled wrapper is read inside as\n * `extractIfSingleOutermostCodeBlock` finds it, preserving nested fences. A leading wrapper followed by prose is\n * also read inside, but only when there are no headings outside fences.\n */\nexport function parseMarkdownSections(markdown: string): MarkdownSection[] {\n const block = findOutermostCodeBlock(markdown);\n const wrapper = block && WRAPPED_MARKDOWN_LANGUAGES.has(block.language) ? block : undefined;\n if (wrapper?.isWhole) return splitSections(wrapper.code);\n const sections = splitSections(markdown);\n return sections.length === 0 && wrapper ? splitSections(wrapper.code) : sections;\n}\n\nfunction normalizeHeading(heading: string): string {\n return stripHeadingEdges(stripHeadingEdges(heading).replace(/^\\d+(?:\\.\\d+)*(?:\\.\\s+|\\)\\s*|\\s+)/u, ''))\n .replaceAll(/\\s+/gu, ' ')\n .toLowerCase();\n}\n\nfunction stripHeadingEdges(text: string): string {\n // Only the edges: `_` and `*` inside a heading belong to names such as `my_file.py`.\n return text.replaceAll(/^[\\s*_`~\"'「」]+|[\\s*_`~\"'「」::]+$/gu, '');\n}\n\nfunction splitSections(markdown: string): MarkdownSection[] {\n const lines = splitLines(markdown);\n const headings = findHeadings(lines);\n return headings.map((heading, index) => {\n let nextIndex = index + 1;\n while (nextIndex < headings.length && (headings[nextIndex]?.depth ?? 0) > heading.depth) nextIndex++;\n return {\n depth: heading.depth,\n heading: heading.text,\n content: lines\n .slice(heading.index + 1, headings[nextIndex]?.index)\n .join('\\n')\n .trim(),\n };\n });\n}\n\nfunction findOutermostCodeBlock(text: string): { language: string; code: string; isWhole: boolean } | undefined {\n // Keep the opening fence's indentation, which is stripped from the contents.\n const lines = splitLines(text.replace(/^\\s*\\n/u, '').trimEnd());\n const fence = parseOpeningFence(lines[0] ?? '');\n if (!fence) return;\n const lastIndex = lines.length - 1;\n const end =\n lastIndex > 0 && isClosingFence(lines[lastIndex] ?? '', fence) ? lastIndex : findClosingFence(lines, 1, fence);\n return {\n language: fence.language,\n code: lines\n .slice(1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isWhole: end >= lastIndex,\n };\n}\n\nfunction findHeadings(lines: readonly string[]): { index: number; depth: number; text: string }[] {\n const headings: { index: number; depth: number; text: string }[] = [];\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const fence = parseOpeningFence(line);\n if (fence) {\n index = findClosingFence(lines, index + 1, fence);\n continue;\n }\n const match = /^(#{1,6})(?:[ \\t]+(.*))?$/u.exec(line);\n if (!match) continue;\n const text = (match[2] ?? '')\n // The closing sequence of an ATX heading needs a space before it, so `# C#` keeps its `#`.\n .replace(/(?:^|[ \\t]+)#+[ \\t]*$/u, '')\n .trim()\n .replaceAll(/(`+)(.+?)\\1/gu, (_match, _delimiter: string, content: string) =>\n content.startsWith(' ') && content.endsWith(' ') ? content.slice(1, -1) : content\n );\n headings.push({ index, depth: match[1]?.length ?? 1, text });\n }\n return headings;\n}\n\nfunction parseOpeningFence(line: string): Fence | undefined {\n const match = /^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(line);\n const fence = match?.[2];\n const info = match?.[3] ?? '';\n // A backtick in the info string makes the line inline code instead of a fence.\n if (!fence || (fence.startsWith('`') && info.includes('`'))) return;\n return {\n char: fence.charAt(0),\n length: fence.length,\n indent: match[1]?.length ?? 0,\n language: info.trim().split(/\\s/u)[0]?.toLowerCase() ?? '',\n };\n}\n\n/** Returns the index of the line closing `fence`, or the number of lines when it is never closed. */\nfunction findClosingFence(lines: readonly string[], start: number, fence: Fence): number {\n let index = start;\n while (index < lines.length && !isClosingFence(lines[index] ?? '', fence)) index++;\n return index;\n}\n\nfunction isClosingFence(line: string, fence: Fence): boolean {\n const closer = /^ {0,3}(`{3,}|~{3,})[ \\t]*$/u.exec(line)?.[1];\n return closer?.charAt(0) === fence.char && closer.length >= fence.length;\n}\n\nfunction stripIndent(line: string, indent: number): string {\n let index = 0;\n while (index < indent && line[index] === ' ') index++;\n return line.slice(index);\n}\n\nfunction splitLines(text: string): string[] {\n return text.split(/\\r?\\n/u);\n}\n"],"mappings":"A,aAqBA,SAAgBA,EAAkB,EAA+B,CAC/D,IAAMC,EAAQC,EAAWC,CAAQ,EAC3BC,EAAsB,CAAC,EAC7B,IAAK,IAAIC,EAAQ,EAAGA,EAAQJ,EAAM,OAAQ,IAAS,CACjD,IAAMK,EAAQC,EAAkBN,EAAMI,IAAU,EAAE,EAClD,GAAI,CAACC,EAAO,SACZ,IAAME,EAAMC,EAAiBR,EAAOI,EAAQ,EAAGC,CAAK,EACpDF,EAAO,KAAK,CACV,GAAIE,EAAM,UAAY,CAAE,SAAUA,EAAM,QAAS,EACjD,KAAML,EACH,MAAMI,EAAQ,EAAGG,CAAG,CAAC,CACrB,IAAK,GAASE,EAAYC,EAAML,EAAM,MAAM,CAAC,CAAC,CAC9C,KAAK;CAAI,EACZ,SAAUE,EAAMP,EAAM,MACxB,CAAC,EACD,EAAQO,CACV,CACA,OAAOJ,CACT,CAmBA,SAAgBQ,EACd,EACA,EACQ,CACR,GAAM,CAAE,YAAW,SAAmCC,EAAeC,CAAkB,EACnF,CAAE,UAAWA,CAAmB,EAC/BA,GAAsB,CAAC,EACtBC,EAAa,GACjB,CAACC,GAAW,QAAUA,EAA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EAAG,EAAE,EAAIA,CAC5E,EACFN,EAAS,KAAK,CAAE,QAAO,MAAOK,EAAM,EAAE,EAAE,QAAU,EAAG,MAAK,CAAC,CAC7D,CACA,OAAOL,CACT,CAEA,SAASjD,EAAkB,EAAiC,CAC1D,IAAMsD,EAAQ,+BAA+B,KAAKlD,CAAI,EAChDL,EAAQuD,IAAQ,GAChBE,EAAOF,IAAQ,IAAM,GAE3B,GAAI,GAACvD,GAAUA,EAAM,WAAW,GAAG,GAAKyD,EAAK,SAAS,GAAG,GACzD,MAAO,CACL,KAAMzD,EAAM,OAAO,CAAC,EACpB,OAAQA,EAAM,OACd,OAAQuD,EAAM,EAAE,EAAE,QAAU,EAC5B,SAAUE,EAAK,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,EAAE,EAAE,YAAY,GAAK,EAC1D,CACF,CAGA,SAAStD,EAAiB,EAA0B,EAAe,EAAsB,CACvF,IAAIJ,EAAQoB,EACZ,KAAOpB,EAAQJ,EAAM,QAAU,CAAC2D,EAAe3D,EAAMI,IAAU,GAAIC,CAAK,GAAG,IAC3E,OAAOD,CACT,CAEA,SAASuD,EAAe,EAAc,EAAuB,CAC3D,IAAMI,EAAS,+BAA+B,KAAKrD,CAAI,CAAC,GAAG,GAC3D,OAAOqD,GAAQ,OAAO,CAAC,IAAM1D,EAAM,MAAQ0D,EAAO,QAAU1D,EAAM,MACpE,CAEA,SAASI,EAAY,EAAc,EAAwB,CACzD,IAAIL,EAAQ,EACZ,KAAOA,EAAQ4D,GAAUtD,EAAKN,KAAW,KAAK,IAC9C,OAAOM,EAAK,MAAMN,CAAK,CACzB,CAEA,SAASH,EAAW,EAAwB,CAC1C,OAAOmB,EAAK,MAAM,QAAQ,CAC5B,C"}
1
+ {"version":3,"file":"markdown.cjs","names":["extractCodeBlocks","lines","splitLines","markdown","blocks","index","fence","parseOpeningFence","end","findClosingFence","stripIndent","line","extractIfSingleOutermostCodeBlock","block","findOutermostCodeBlock","text","languages","language","WRAPPED_MARKDOWN_LANGUAGES","extractTaggedCodeBlock","tag","tagName","found","findTaggedCodeBlock","wrapper","enclosingFence","fenceIndex","contentLine","extractSections","candidates","parseMarkdownSections","section","normalizeHeading","requested","names","name","sections","best","candidate","key","other","splitSections","stripHeadingEdges","heading","headings","findHeadings","nextIndex","lastIndex","isClosingFence","match","content","info","start","closer","indent"],"sources":["../src/markdown.ts"],"sourcesContent":["export interface CodeBlock {\n /** The first word of the info string, lowercased; `undefined` when the fence has none. */\n language?: string;\n code: string;\n /** `false` when the text ends before the closing fence, e.g. because the LLM output was cut off. */\n isClosed: boolean;\n}\n\ninterface Fence {\n char: string;\n length: number;\n indent: number;\n language: string;\n}\n\n/**\n * Extracts the fenced code blocks (backticks or tildes, any fence length) of Markdown text such as an LLM response.\n * A block runs to the first closing fence of the same character that is at least as long as its opening fence,\n * so a longer fence can hold shorter fences. Only fences indented by at most three spaces are recognized; fences\n * inside block quotes, deeply nested list items, and indented code blocks are not.\n */\nexport function extractCodeBlocks(markdown: string): CodeBlock[] {\n const lines = splitLines(markdown);\n const blocks: CodeBlock[] = [];\n for (let index = 0; index < lines.length; index++) {\n const fence = parseOpeningFence(lines[index] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, index + 1, fence);\n blocks.push({\n ...(fence.language && { language: fence.language }),\n code: lines\n .slice(index + 1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isClosed: end < lines.length,\n });\n index = end;\n }\n return blocks;\n}\n\n/**\n * Returns the contents of the code block that an LLM wrapped its whole response in, or `text` itself when the\n * response does not start with a fence or the fence's language is not one of a nonempty `languages` list\n * (compared case-insensitively). An omitted or empty list allows any language.\n * When the response also ends with a matching closing fence, everything between its first and last lines is returned,\n * so fences the LLM nested without lengthening the outer fence stay in the contents. Otherwise the first block is\n * returned and text after it is dropped; an unclosed block runs to the end of the text.\n */\nexport function extractIfSingleOutermostCodeBlock(text: string, languages?: readonly string[]): string {\n const block = findOutermostCodeBlock(text);\n if (!block) return text;\n if (languages?.length && !languages.some((language) => language.toLowerCase() === block.language)) return text;\n return block.code;\n}\n\nconst WRAPPED_MARKDOWN_LANGUAGES = new Set(['', 'markdown', 'md']);\n\n/**\n * Returns the trimmed contents of the first closed fenced code block (backticks or tildes) that follows a line holding\n * only `<tagName>` in text such as an LLM response, e.g. `<answer>\\n~~~\\n42\\n~~~`, or `undefined` when there is none.\n * Only blank lines may separate the tag line from the opening fence. Mentions of the tag within prose lines and tag\n * lines inside other fenced blocks (e.g. a format example) are skipped, but a whole-response `markdown`, `md`, or\n * unlabeled wrapper is read inside, as `parseMarkdownSections` does.\n */\nexport function extractTaggedCodeBlock(text: string, tagName: string): string | undefined {\n const tag = `<${tagName}>`;\n const found = findTaggedCodeBlock(splitLines(text), tag);\n if (found !== undefined) return found;\n // Only a real wrapper hides every tag line from the scan above; a leading example block paired with the tagged\n // block's closing fence merely looks like one, so its contents must not be read first.\n const wrapper = findOutermostCodeBlock(text);\n if (wrapper?.isWhole && WRAPPED_MARKDOWN_LANGUAGES.has(wrapper.language)) {\n return findTaggedCodeBlock(splitLines(wrapper.code), tag);\n }\n}\n\nfunction findTaggedCodeBlock(lines: readonly string[], tag: string): string | undefined {\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const enclosingFence = parseOpeningFence(line);\n if (enclosingFence) {\n index = findClosingFence(lines, index + 1, enclosingFence);\n continue;\n }\n if (line.trim() !== tag) continue;\n let fenceIndex = index + 1;\n while (fenceIndex < lines.length && !lines[fenceIndex]?.trim()) fenceIndex++;\n const fence = parseOpeningFence(lines[fenceIndex] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, fenceIndex + 1, fence);\n if (end < lines.length) {\n return lines\n .slice(fenceIndex + 1, end)\n .map((contentLine) => stripIndent(contentLine, fence.indent))\n .join('\\n')\n .trim();\n }\n }\n}\n\nexport interface MarkdownSection {\n depth: number;\n /** The heading text with the closing `#` sequence and inline-code backticks removed. */\n heading: string;\n /** The source text up to the next heading at the same or a shallower depth, trimmed; subsections are included. */\n content: string;\n}\n\n/**\n * Extracts the content of the section headed by each of `names` from Markdown text such as an LLM response, keyed by\n * the given names. A heading matches a name when both are equal after removing decorations at their edges (such as\n * `**bold**`, backticks, and quotes), a leading number like `1.` or `2.1`, and a trailing colon, ignoring case and spacing;\n * headings at any depth are candidates.\n * Among matching headings, the shallowest wins, then one equal to the name as written, then the first; each heading\n * serves at most one name. When distinct requested names normalize alike, only exact matches at the shallowest\n * matching depth are returned. Missing, empty, or ambiguous sections are absent from the prototype-free result.\n */\nexport function extractSections<const Name extends string>(\n markdown: string,\n names: readonly Name[]\n): Partial<Record<Name, string>> {\n const candidates = parseMarkdownSections(markdown).map((section) => ({\n ...section,\n key: normalizeHeading(section.heading),\n }));\n const requested = [...new Set(names)].map((name) => ({ name, key: normalizeHeading(name) }));\n const sections: Partial<Record<Name, string>> = Object.create(null);\n for (const { name, key } of requested) {\n let best: (typeof candidates)[number] | undefined;\n for (const candidate of candidates) {\n if (candidate.key !== key) continue;\n if (\n !best ||\n candidate.depth < best.depth ||\n (candidate.depth === best.depth && candidate.heading === name && best.heading !== name)\n ) {\n best = candidate;\n }\n }\n if (!best) continue;\n if (best.heading !== name && requested.some((other) => other.name !== name && other.key === key)) continue;\n if (best.content) sections[name] = best.content;\n }\n return sections;\n}\n\n/**\n * Returns every ATX heading of Markdown text in order, with the source text under it.\n * Only headings starting at the beginning of a line outside fenced code blocks count; setext headings (underlined\n * with `=` or `-`) are ignored. A whole-response `markdown`, `md`, or unlabeled wrapper is read inside as\n * `extractIfSingleOutermostCodeBlock` finds it, preserving nested fences. A leading wrapper followed by prose is\n * also read inside, but only when there are no headings outside fences.\n */\nexport function parseMarkdownSections(markdown: string): MarkdownSection[] {\n const block = findOutermostCodeBlock(markdown);\n const wrapper = block && WRAPPED_MARKDOWN_LANGUAGES.has(block.language) ? block : undefined;\n if (wrapper?.isWhole) return splitSections(wrapper.code);\n const sections = splitSections(markdown);\n return sections.length === 0 && wrapper ? splitSections(wrapper.code) : sections;\n}\n\nfunction normalizeHeading(heading: string): string {\n return stripHeadingEdges(stripHeadingEdges(heading).replace(/^\\d+(?:\\.\\d+)*(?:\\.\\s+|\\)\\s*|\\s+)/u, ''))\n .replaceAll(/\\s+/gu, ' ')\n .toLowerCase();\n}\n\nfunction stripHeadingEdges(text: string): string {\n // Only the edges: `_` and `*` inside a heading belong to names such as `my_file.py`.\n return text.replaceAll(/^[\\s*_`~\"'「」]+|[\\s*_`~\"'「」::]+$/gu, '');\n}\n\nfunction splitSections(markdown: string): MarkdownSection[] {\n const lines = splitLines(markdown);\n const headings = findHeadings(lines);\n return headings.map((heading, index) => {\n let nextIndex = index + 1;\n while (nextIndex < headings.length && (headings[nextIndex]?.depth ?? 0) > heading.depth) nextIndex++;\n return {\n depth: heading.depth,\n heading: heading.text,\n content: lines\n .slice(heading.index + 1, headings[nextIndex]?.index)\n .join('\\n')\n .trim(),\n };\n });\n}\n\nfunction findOutermostCodeBlock(text: string): { language: string; code: string; isWhole: boolean } | undefined {\n // Keep the opening fence's indentation, which is stripped from the contents.\n const lines = splitLines(text.replace(/^\\s*\\n/u, '').trimEnd());\n const fence = parseOpeningFence(lines[0] ?? '');\n if (!fence) return;\n const lastIndex = lines.length - 1;\n const end =\n lastIndex > 0 && isClosingFence(lines[lastIndex] ?? '', fence) ? lastIndex : findClosingFence(lines, 1, fence);\n return {\n language: fence.language,\n code: lines\n .slice(1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isWhole: end >= lastIndex,\n };\n}\n\nfunction findHeadings(lines: readonly string[]): { index: number; depth: number; text: string }[] {\n const headings: { index: number; depth: number; text: string }[] = [];\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const fence = parseOpeningFence(line);\n if (fence) {\n index = findClosingFence(lines, index + 1, fence);\n continue;\n }\n const match = /^(#{1,6})(?:[ \\t]+(.*))?$/u.exec(line);\n if (!match) continue;\n const text = (match[2] ?? '')\n // The closing sequence of an ATX heading needs a space before it, so `# C#` keeps its `#`.\n .replace(/(?:^|[ \\t]+)#+[ \\t]*$/u, '')\n .trim()\n .replaceAll(/(`+)(.+?)\\1/gu, (_match, _delimiter: string, content: string) =>\n content.startsWith(' ') && content.endsWith(' ') ? content.slice(1, -1) : content\n );\n headings.push({ index, depth: match[1]?.length ?? 1, text });\n }\n return headings;\n}\n\nfunction parseOpeningFence(line: string): Fence | undefined {\n const match = /^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(line);\n const fence = match?.[2];\n const info = match?.[3] ?? '';\n // A backtick in the info string makes the line inline code instead of a fence.\n if (!fence || (fence.startsWith('`') && info.includes('`'))) return;\n return {\n char: fence.charAt(0),\n length: fence.length,\n indent: match[1]?.length ?? 0,\n language: info.trim().split(/\\s/u)[0]?.toLowerCase() ?? '',\n };\n}\n\n/** Returns the index of the line closing `fence`, or the number of lines when it is never closed. */\nfunction findClosingFence(lines: readonly string[], start: number, fence: Fence): number {\n let index = start;\n while (index < lines.length && !isClosingFence(lines[index] ?? '', fence)) index++;\n return index;\n}\n\nfunction isClosingFence(line: string, fence: Fence): boolean {\n const closer = /^ {0,3}(`{3,}|~{3,})[ \\t]*$/u.exec(line)?.[1];\n return closer?.charAt(0) === fence.char && closer.length >= fence.length;\n}\n\nfunction stripIndent(line: string, indent: number): string {\n let index = 0;\n while (index < indent && line[index] === ' ') index++;\n return line.slice(index);\n}\n\nfunction splitLines(text: string): string[] {\n return text.split(/\\r?\\n/u);\n}\n"],"mappings":"A,aAqBA,SAAgBA,EAAkB,EAA+B,CAC/D,IAAMC,EAAQC,EAAWC,CAAQ,EAC3BC,EAAsB,CAAC,EAC7B,IAAK,IAAIC,EAAQ,EAAGA,EAAQJ,EAAM,OAAQ,IAAS,CACjD,IAAMK,EAAQC,EAAkBN,EAAMI,IAAU,EAAE,EAClD,GAAI,CAACC,EAAO,SACZ,IAAME,EAAMC,EAAiBR,EAAOI,EAAQ,EAAGC,CAAK,EACpDF,EAAO,KAAK,CACV,G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EAAS,+BAA+B,KAAK1C,CAAI,CAAC,GAAG,GAC3D,OAAO0C,GAAQ,OAAO,CAAC,IAAM/C,EAAM,MAAQ+C,EAAO,QAAU/C,EAAM,MACpE,CAEA,SAASI,EAAY,EAAc,EAAwB,CACzD,IAAIL,EAAQ,EACZ,KAAOA,EAAQiD,GAAU3C,EAAKN,KAAW,KAAK,IAC9C,OAAOM,EAAK,MAAMN,CAAK,CACzB,CAEA,SAASH,EAAW,EAAwB,CAC1C,OAAOa,EAAK,MAAM,QAAQ,CAC5B,C"}
@@ -12,23 +12,15 @@ export interface CodeBlock {
12
12
  * inside block quotes, deeply nested list items, and indented code blocks are not.
13
13
  */
14
14
  export declare function extractCodeBlocks(markdown: string): CodeBlock[];
15
- export interface ExtractCodeBlockOptions {
16
- /** Allowed languages, compared case-insensitively; an omitted or empty list allows any language. */
17
- languages?: readonly string[];
18
- /** The exact fence the caller asked the LLM to wrap its response in, e.g. `getTildeFence(text)`. */
19
- fence?: string;
20
- }
21
15
  /**
22
16
  * Returns the contents of the code block that an LLM wrapped its whole response in, or `text` itself when the
23
- * response does not start with a fence or the fence's language is not allowed. A plain array is taken as `languages`.
17
+ * response does not start with a fence or the fence's language is not one of a nonempty `languages` list
18
+ * (compared case-insensitively). An omitted or empty list allows any language.
24
19
  * When the response also ends with a matching closing fence, everything between its first and last lines is returned,
25
20
  * so fences the LLM nested without lengthening the outer fence stay in the contents. Otherwise the first block is
26
21
  * returned and text after it is dropped; an unclosed block runs to the end of the text.
27
- * With `fence`, a preamble and a postamble around the block are dropped first: everything between the first line
28
- * holding exactly `fence` and an allowed language or none, outside blocks of other languages, and the last later line
29
- * holding exactly `fence` is returned. Without such a pair, the rules above apply.
30
22
  */
31
- export declare function extractIfSingleOutermostCodeBlock(text: string, languagesOrOptions?: readonly string[] | ExtractCodeBlockOptions): string;
23
+ export declare function extractIfSingleOutermostCodeBlock(text: string, languages?: readonly string[]): string;
32
24
  /**
33
25
  * Returns the trimmed contents of the first closed fenced code block (backticks or tildes) that follows a line holding
34
26
  * only `<tagName>` in text such as an LLM response, e.g. `<answer>\n~~~\n42\n~~~`, or `undefined` when there is none.
package/dist/markdown.js CHANGED
@@ -1,7 +1,6 @@
1
- function e(e){let t=y(e),n=[];for(let e=0;e<t.length;e++){let r=h(t[e]??``);if(!r)continue;let i=g(t,e+1,r);n.push({...r.language&&{language:r.language},code:t.slice(e+1,i).map(e=>v(e,r.indent)).join(`
2
- `),isClosed:i<t.length}),e=i}return n}function t(e,t){let{languages:i,fence:a}=n(t)?{languages:t}:t??{},o=e=>!i?.length||i.some(t=>t.toLowerCase()===e),s=a===void 0?void 0:r(e,a,o);if(s!==void 0)return s;let c=p(e);return!c||!o(c.language)?e:c.code}function n(e){return Array.isArray(e)}function r(e,t,n){let r=y(e),a=r.findLastIndex(e=>e.trimEnd()===t),o=i(r,a,t,n);if(o!==void 0)return r.slice(o+1,a).join(`
3
- `)}function i(e,t,n,r){for(let i=0;i<t;i++){let t=e[i]??``,a=t.trimEnd(),o=a.startsWith(n)&&a.charAt(n.length)!==n.charAt(0),s=a.slice(n.length).trim().split(/\s/u)[0]?.toLowerCase()??``;if(o&&(s===``||r(s)))return i;let c=h(t);c&&(i=g(e,i+1,c))}}const a=/* @__PURE__ */ new Set([``,`markdown`,`md`]);function o(e,t){let n=`<${t}>`,r=s(y(e),n);if(r!==void 0)return r;let i=p(e);if(i?.isWhole&&a.has(i.language))return s(y(i.code),n)}function s(e,t){for(let n=0;n<e.length;n++){let r=e[n]??``,i=h(r);if(i){n=g(e,n+1,i);continue}if(r.trim()!==t)continue;let a=n+1;for(;a<e.length&&!e[a]?.trim();)a++;let o=h(e[a]??``);if(!o)continue;let s=g(e,a+1,o);if(s<e.length)return e.slice(a+1,s).map(e=>v(e,o.indent)).join(`
4
- `).trim()}}function c(e,t){let n=l(e).map(e=>({...e,key:u(e.heading)})),r=[...new Set(t)].map(e=>({name:e,key:u(e)})),i=Object.create(null);for(let{name:e,key:t}of r){let a;for(let r of n)r.key===t&&(!a||r.depth<a.depth||r.depth===a.depth&&r.heading===e&&a.heading!==e)&&(a=r);a&&(a.heading!==e&&r.some(n=>n.name!==e&&n.key===t)||a.content&&(i[e]=a.content))}return i}function l(e){let t=p(e),n=t&&a.has(t.language)?t:void 0;if(n?.isWhole)return f(n.code);let r=f(e);return r.length===0&&n?f(n.code):r}function u(e){return d(d(e).replace(/^\d+(?:\.\d+)*(?:\.\s+|\)\s*|\s+)/u,``)).replaceAll(/\s+/gu,` `).toLowerCase()}function d(e){return e.replaceAll(/^[\s*_`~"'「」]+|[\s*_`~"'「」::]+$/gu,``)}function f(e){let t=y(e),n=m(t);return n.map((e,r)=>{let i=r+1;for(;i<n.length&&(n[i]?.depth??0)>e.depth;)i++;return{depth:e.depth,heading:e.text,content:t.slice(e.index+1,n[i]?.index).join(`
5
- `).trim()}})}function p(e){let t=y(e.replace(/^\s*\n/u,``).trimEnd()),n=h(t[0]??``);if(!n)return;let r=t.length-1,i=r>0&&_(t[r]??``,n)?r:g(t,1,n);return{language:n.language,code:t.slice(1,i).map(e=>v(e,n.indent)).join(`
6
- `),isWhole:i>=r}}function m(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n]??``,i=h(r);if(i){n=g(e,n+1,i);continue}let a=/^(#{1,6})(?:[ \t]+(.*))?$/u.exec(r);if(!a)continue;let o=(a[2]??``).replace(/(?:^|[ \t]+)#+[ \t]*$/u,``).trim().replaceAll(/(`+)(.+?)\1/gu,(e,t,n)=>n.startsWith(` `)&&n.endsWith(` `)?n.slice(1,-1):n);t.push({index:n,depth:a[1]?.length??1,text:o})}return t}function h(e){let t=/^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(e),n=t?.[2],r=t?.[3]??``;if(!(!n||n.startsWith("`")&&r.includes("`")))return{char:n.charAt(0),length:n.length,indent:t[1]?.length??0,language:r.trim().split(/\s/u)[0]?.toLowerCase()??``}}function g(e,t,n){let r=t;for(;r<e.length&&!_(e[r]??``,n);)r++;return r}function _(e,t){let n=/^ {0,3}(`{3,}|~{3,})[ \t]*$/u.exec(e)?.[1];return n?.charAt(0)===t.char&&n.length>=t.length}function v(e,t){let n=0;for(;n<t&&e[n]===` `;)n++;return e.slice(n)}function y(e){return e.split(/\r?\n/u)}export{e as extractCodeBlocks,t as extractIfSingleOutermostCodeBlock,c as extractSections,o as extractTaggedCodeBlock,l as parseMarkdownSections};
1
+ function e(e){let t=g(e),n=[];for(let e=0;e<t.length;e++){let r=f(t[e]??``);if(!r)continue;let i=p(t,e+1,r);n.push({...r.language&&{language:r.language},code:t.slice(e+1,i).map(e=>h(e,r.indent)).join(`
2
+ `),isClosed:i<t.length}),e=i}return n}function t(e,t){let n=u(e);return!n||t?.length&&!t.some(e=>e.toLowerCase()===n.language)?e:n.code}const n=/* @__PURE__ */ new Set([``,`markdown`,`md`]);function r(e,t){let r=`<${t}>`,a=i(g(e),r);if(a!==void 0)return a;let o=u(e);if(o?.isWhole&&n.has(o.language))return i(g(o.code),r)}function i(e,t){for(let n=0;n<e.length;n++){let r=e[n]??``,i=f(r);if(i){n=p(e,n+1,i);continue}if(r.trim()!==t)continue;let a=n+1;for(;a<e.length&&!e[a]?.trim();)a++;let o=f(e[a]??``);if(!o)continue;let s=p(e,a+1,o);if(s<e.length)return e.slice(a+1,s).map(e=>h(e,o.indent)).join(`
3
+ `).trim()}}function a(e,t){let n=o(e).map(e=>({...e,key:s(e.heading)})),r=[...new Set(t)].map(e=>({name:e,key:s(e)})),i=Object.create(null);for(let{name:e,key:t}of r){let a;for(let r of n)r.key===t&&(!a||r.depth<a.depth||r.depth===a.depth&&r.heading===e&&a.heading!==e)&&(a=r);a&&(a.heading!==e&&r.some(n=>n.name!==e&&n.key===t)||a.content&&(i[e]=a.content))}return i}function o(e){let t=u(e),r=t&&n.has(t.language)?t:void 0;if(r?.isWhole)return l(r.code);let i=l(e);return i.length===0&&r?l(r.code):i}function s(e){return c(c(e).replace(/^\d+(?:\.\d+)*(?:\.\s+|\)\s*|\s+)/u,``)).replaceAll(/\s+/gu,` `).toLowerCase()}function c(e){return e.replaceAll(/^[\s*_`~"'「」]+|[\s*_`~"'「」::]+$/gu,``)}function l(e){let t=g(e),n=d(t);return n.map((e,r)=>{let i=r+1;for(;i<n.length&&(n[i]?.depth??0)>e.depth;)i++;return{depth:e.depth,heading:e.text,content:t.slice(e.index+1,n[i]?.index).join(`
4
+ `).trim()}})}function u(e){let t=g(e.replace(/^\s*\n/u,``).trimEnd()),n=f(t[0]??``);if(!n)return;let r=t.length-1,i=r>0&&m(t[r]??``,n)?r:p(t,1,n);return{language:n.language,code:t.slice(1,i).map(e=>h(e,n.indent)).join(`
5
+ `),isWhole:i>=r}}function d(e){let t=[];for(let n=0;n<e.length;n++){let r=e[n]??``,i=f(r);if(i){n=p(e,n+1,i);continue}let a=/^(#{1,6})(?:[ \t]+(.*))?$/u.exec(r);if(!a)continue;let o=(a[2]??``).replace(/(?:^|[ \t]+)#+[ \t]*$/u,``).trim().replaceAll(/(`+)(.+?)\1/gu,(e,t,n)=>n.startsWith(` `)&&n.endsWith(` `)?n.slice(1,-1):n);t.push({index:n,depth:a[1]?.length??1,text:o})}return t}function f(e){let t=/^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(e),n=t?.[2],r=t?.[3]??``;if(!(!n||n.startsWith("`")&&r.includes("`")))return{char:n.charAt(0),length:n.length,indent:t[1]?.length??0,language:r.trim().split(/\s/u)[0]?.toLowerCase()??``}}function p(e,t,n){let r=t;for(;r<e.length&&!m(e[r]??``,n);)r++;return r}function m(e,t){let n=/^ {0,3}(`{3,}|~{3,})[ \t]*$/u.exec(e)?.[1];return n?.charAt(0)===t.char&&n.length>=t.length}function h(e,t){let n=0;for(;n<t&&e[n]===` `;)n++;return e.slice(n)}function g(e){return e.split(/\r?\n/u)}export{e as extractCodeBlocks,t as extractIfSingleOutermostCodeBlock,a as extractSections,r as extractTaggedCodeBlock,o as parseMarkdownSections};
7
6
  //# sourceMappingURL=markdown.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"markdown.js","names":["extractCodeBlocks","lines","splitLines","markdown","blocks","index","fence","parseOpeningFence","end","findClosingFence","stripIndent","line","extractIfSingleOutermostCodeBlock","isLanguageList","languagesOrOptions","isAllowed","languages","allowed","language","known","findBlockWithKnownFence","text","block","findOutermostCodeBlock","value","start","findSentOpeningFence","trimmed","isSentFence","opener","WRAPPED_MARKDOWN_LANGUAGES","extractTaggedCodeBlock","tag","tagName","found","findTaggedCodeBlock","wrapper","enclosingFence","fenceIndex","contentLine","extractSections","candidates","parseMarkdownSections","section","normalizeHeading","requested","names","name","sections","best","candidate","key","other","splitSections","stripHeadingEdges","heading","headings","findHeadings","nextIndex","lastIndex","isClosingFence","match","content","info","closer","indent"],"sources":["../src/markdown.ts"],"sourcesContent":["export interface CodeBlock {\n /** The first word of the info string, lowercased; `undefined` when the fence has none. */\n language?: string;\n code: string;\n /** `false` when the text ends before the closing fence, e.g. because the LLM output was cut off. */\n isClosed: boolean;\n}\n\ninterface Fence {\n char: string;\n length: number;\n indent: number;\n language: string;\n}\n\n/**\n * Extracts the fenced code blocks (backticks or tildes, any fence length) of Markdown text such as an LLM response.\n * A block runs to the first closing fence of the same character that is at least as long as its opening fence,\n * so a longer fence can hold shorter fences. Only fences indented by at most three spaces are recognized; fences\n * inside block quotes, deeply nested list items, and indented code blocks are not.\n */\nexport function extractCodeBlocks(markdown: string): CodeBlock[] {\n const lines = splitLines(markdown);\n const blocks: CodeBlock[] = [];\n for (let index = 0; index < lines.length; index++) {\n const fence = parseOpeningFence(lines[index] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, index + 1, fence);\n blocks.push({\n ...(fence.language && { language: fence.language }),\n code: lines\n .slice(index + 1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isClosed: end < lines.length,\n });\n index = end;\n }\n return blocks;\n}\n\nexport interface ExtractCodeBlockOptions {\n /** Allowed languages, compared case-insensitively; an omitted or empty list allows any language. */\n languages?: readonly string[];\n /** The exact fence the caller asked the LLM to wrap its response in, e.g. `getTildeFence(text)`. */\n fence?: string;\n}\n\n/**\n * Returns the contents of the code block that an LLM wrapped its whole response in, or `text` itself when the\n * response does not start with a fence or the fence's language is not allowed. A plain array is taken as `languages`.\n * When the response also ends with a matching closing fence, everything between its first and last lines is returned,\n * so fences the LLM nested without lengthening the outer fence stay in the contents. Otherwise the first block is\n * returned and text after it is dropped; an unclosed block runs to the end of the text.\n * With `fence`, a preamble and a postamble around the block are dropped first: everything between the first line\n * holding exactly `fence` and an allowed language or none, outside blocks of other languages, and the last later line\n * holding exactly `fence` is returned. Without such a pair, the rules above apply.\n */\nexport function extractIfSingleOutermostCodeBlock(\n text: string,\n languagesOrOptions?: readonly string[] | ExtractCodeBlockOptions\n): string {\n const { languages, fence }: ExtractCodeBlockOptions = isLanguageList(languagesOrOptions)\n ? { languages: languagesOrOptions }\n : (languagesOrOptions ?? {});\n const isAllowed = (language: string): boolean =>\n !languages?.length || languages.some((allowed) => allowed.toLowerCase() === language);\n const known = fence === undefined ? undefined : findBlockWithKnownFence(text, fence, isAllowed);\n if (known !== undefined) return known;\n const block = findOutermostCodeBlock(text);\n if (!block || !isAllowed(block.language)) return text;\n return block.code;\n}\n\nfunction isLanguageList(value: readonly string[] | ExtractCodeBlockOptions | undefined): value is readonly string[] {\n return Array.isArray(value);\n}\n\nfunction findBlockWithKnownFence(\n text: string,\n fence: string,\n isAllowed: (language: string) => boolean\n): string | undefined {\n const lines = splitLines(text);\n const end = lines.findLastIndex((line) => line.trimEnd() === fence);\n const start = findSentOpeningFence(lines, end, fence, isAllowed);\n if (start === undefined) return;\n return lines.slice(start + 1, end).join('\\n');\n}\n\n/** Returns the index of the first line before `end` opening `fence` with an allowed language or none, skipping other blocks. */\nfunction findSentOpeningFence(\n lines: readonly string[],\n end: number,\n fence: string,\n isAllowed: (language: string) => boolean\n): number | undefined {\n for (let index = 0; index < end; index++) {\n const line = lines[index] ?? '';\n const trimmed = line.trimEnd();\n const isSentFence = trimmed.startsWith(fence) && trimmed.charAt(fence.length) !== fence.charAt(0);\n const language = trimmed.slice(fence.length).trim().split(/\\s/u)[0]?.toLowerCase() ?? '';\n if (isSentFence && (language === '' || isAllowed(language))) return index;\n const opener = parseOpeningFence(line);\n if (opener) index = findClosingFence(lines, index + 1, opener);\n }\n}\n\nconst WRAPPED_MARKDOWN_LANGUAGES = new Set(['', 'markdown', 'md']);\n\n/**\n * Returns the trimmed contents of the first closed fenced code block (backticks or tildes) that follows a line holding\n * only `<tagName>` in text such as an LLM response, e.g. `<answer>\\n~~~\\n42\\n~~~`, or `undefined` when there is none.\n * Only blank lines may separate the tag line from the opening fence. Mentions of the tag within prose lines and tag\n * lines inside other fenced blocks (e.g. a format example) are skipped, but a whole-response `markdown`, `md`, or\n * unlabeled wrapper is read inside, as `parseMarkdownSections` does.\n */\nexport function extractTaggedCodeBlock(text: string, tagName: string): string | undefined {\n const tag = `<${tagName}>`;\n const found = findTaggedCodeBlock(splitLines(text), tag);\n if (found !== undefined) return found;\n // Only a real wrapper hides every tag line from the scan above; a leading example block paired with the tagged\n // block's closing fence merely looks like one, so its contents must not be read first.\n const wrapper = findOutermostCodeBlock(text);\n if (wrapper?.isWhole && WRAPPED_MARKDOWN_LANGUAGES.has(wrapper.language)) {\n return findTaggedCodeBlock(splitLines(wrapper.code), tag);\n }\n}\n\nfunction findTaggedCodeBlock(lines: readonly string[], tag: string): string | undefined {\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const enclosingFence = parseOpeningFence(line);\n if (enclosingFence) {\n index = findClosingFence(lines, index + 1, enclosingFence);\n continue;\n }\n if (line.trim() !== tag) continue;\n let fenceIndex = index + 1;\n while (fenceIndex < lines.length && !lines[fenceIndex]?.trim()) fenceIndex++;\n const fence = parseOpeningFence(lines[fenceIndex] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, fenceIndex + 1, fence);\n if (end < lines.length) {\n return lines\n .slice(fenceIndex + 1, end)\n .map((contentLine) => stripIndent(contentLine, fence.indent))\n .join('\\n')\n .trim();\n }\n }\n}\n\nexport interface MarkdownSection {\n depth: number;\n /** The heading text with the closing `#` sequence and inline-code backticks removed. */\n heading: string;\n /** The source text up to the next heading at the same or a shallower depth, trimmed; subsections are included. */\n content: string;\n}\n\n/**\n * Extracts the content of the section headed by each of `names` from Markdown text such as an LLM response, keyed by\n * the given names. A heading matches a name when both are equal after removing decorations at their edges (such as\n * `**bold**`, backticks, and quotes), a leading number like `1.` or `2.1`, and a trailing colon, ignoring case and spacing;\n * headings at any depth are candidates.\n * Among matching headings, the shallowest wins, then one equal to the name as written, then the first; each heading\n * serves at most one name. When distinct requested names normalize alike, only exact matches at the shallowest\n * matching depth are returned. Missing, empty, or ambiguous sections are absent from the prototype-free result.\n */\nexport function extractSections<const Name extends string>(\n markdown: string,\n names: readonly Name[]\n): Partial<Record<Name, string>> {\n const candidates = parseMarkdownSections(markdown).map((section) => ({\n ...section,\n key: normalizeHeading(section.heading),\n }));\n const requested = [...new Set(names)].map((name) => ({ name, key: normalizeHeading(name) }));\n const sections: Partial<Record<Name, string>> = Object.create(null);\n for (const { name, key } of requested) {\n let best: (typeof candidates)[number] | undefined;\n for (const candidate of candidates) {\n if (candidate.key !== key) continue;\n if (\n !best ||\n candidate.depth < best.depth ||\n (candidate.depth === best.depth && candidate.heading === name && best.heading !== name)\n ) {\n best = candidate;\n }\n }\n if (!best) continue;\n if (best.heading !== name && requested.some((other) => other.name !== name && other.key === key)) continue;\n if (best.content) sections[name] = best.content;\n }\n return sections;\n}\n\n/**\n * Returns every ATX heading of Markdown text in order, with the source text under it.\n * Only headings starting at the beginning of a line outside fenced code blocks count; setext headings (underlined\n * with `=` or `-`) are ignored. A whole-response `markdown`, `md`, or unlabeled wrapper is read inside as\n * `extractIfSingleOutermostCodeBlock` finds it, preserving nested fences. A leading wrapper followed by prose is\n * also read inside, but only when there are no headings outside fences.\n */\nexport function parseMarkdownSections(markdown: string): MarkdownSection[] {\n const block = findOutermostCodeBlock(markdown);\n const wrapper = block && WRAPPED_MARKDOWN_LANGUAGES.has(block.language) ? block : undefined;\n if (wrapper?.isWhole) return splitSections(wrapper.code);\n const sections = splitSections(markdown);\n return sections.length === 0 && wrapper ? splitSections(wrapper.code) : sections;\n}\n\nfunction normalizeHeading(heading: string): string {\n return stripHeadingEdges(stripHeadingEdges(heading).replace(/^\\d+(?:\\.\\d+)*(?:\\.\\s+|\\)\\s*|\\s+)/u, ''))\n .replaceAll(/\\s+/gu, ' ')\n .toLowerCase();\n}\n\nfunction stripHeadingEdges(text: string): string {\n // Only the edges: `_` and `*` inside a heading belong to names such as `my_file.py`.\n return text.replaceAll(/^[\\s*_`~\"'「」]+|[\\s*_`~\"'「」::]+$/gu, '');\n}\n\nfunction splitSections(markdown: string): MarkdownSection[] {\n const lines = splitLines(markdown);\n const headings = findHeadings(lines);\n return headings.map((heading, index) => {\n let nextIndex = index + 1;\n while (nextIndex < headings.length && (headings[nextIndex]?.depth ?? 0) > heading.depth) nextIndex++;\n return {\n depth: heading.depth,\n heading: heading.text,\n content: lines\n .slice(heading.index + 1, headings[nextIndex]?.index)\n .join('\\n')\n .trim(),\n };\n });\n}\n\nfunction findOutermostCodeBlock(text: string): { language: string; code: string; isWhole: boolean } | undefined {\n // Keep the opening fence's indentation, which is stripped from the contents.\n const lines = splitLines(text.replace(/^\\s*\\n/u, '').trimEnd());\n const fence = parseOpeningFence(lines[0] ?? '');\n if (!fence) return;\n const lastIndex = lines.length - 1;\n const end =\n lastIndex > 0 && isClosingFence(lines[lastIndex] ?? '', fence) ? lastIndex : findClosingFence(lines, 1, fence);\n return {\n language: fence.language,\n code: lines\n .slice(1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isWhole: end >= lastIndex,\n };\n}\n\nfunction findHeadings(lines: readonly string[]): { index: number; depth: number; text: string }[] {\n const headings: { index: number; depth: number; text: string }[] = [];\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const fence = parseOpeningFence(line);\n if (fence) {\n index = findClosingFence(lines, index + 1, fence);\n continue;\n }\n const match = /^(#{1,6})(?:[ \\t]+(.*))?$/u.exec(line);\n if (!match) continue;\n const text = (match[2] ?? '')\n // The closing sequence of an ATX heading needs a space before it, so `# C#` keeps its `#`.\n .replace(/(?:^|[ \\t]+)#+[ \\t]*$/u, '')\n .trim()\n .replaceAll(/(`+)(.+?)\\1/gu, (_match, _delimiter: string, content: string) =>\n content.startsWith(' ') && content.endsWith(' ') ? content.slice(1, -1) : content\n );\n headings.push({ index, depth: match[1]?.length ?? 1, text });\n }\n return headings;\n}\n\nfunction parseOpeningFence(line: string): Fence | undefined {\n const match = /^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(line);\n const fence = match?.[2];\n const info = match?.[3] ?? '';\n // A backtick in the info string makes the line inline code instead of a fence.\n if (!fence || (fence.startsWith('`') && info.includes('`'))) return;\n return {\n char: fence.charAt(0),\n length: fence.length,\n indent: match[1]?.length ?? 0,\n language: info.trim().split(/\\s/u)[0]?.toLowerCase() ?? '',\n };\n}\n\n/** Returns the index of the line closing `fence`, or the number of lines when it is never closed. */\nfunction findClosingFence(lines: readonly string[], start: number, fence: Fence): number {\n let index = start;\n while (index < lines.length && !isClosingFence(lines[index] ?? '', fence)) index++;\n return index;\n}\n\nfunction isClosingFence(line: string, fence: Fence): boolean {\n const closer = /^ {0,3}(`{3,}|~{3,})[ \\t]*$/u.exec(line)?.[1];\n return closer?.charAt(0) === fence.char && closer.length >= fence.length;\n}\n\nfunction stripIndent(line: string, indent: number): string {\n let index = 0;\n while (index < indent && line[index] === ' ') index++;\n return line.slice(index);\n}\n\nfunction splitLines(text: string): string[] {\n return text.split(/\\r?\\n/u);\n}\n"],"mappings":"AAqBA,SAAgBA,EAAkB,EAA+B,CAC/D,IAAMC,EAAQC,EAAWC,CAAQ,EAC3BC,EAAsB,CAAC,EAC7B,IAAK,IAAIC,EAAQ,EAAGA,EAAQJ,EAAM,OAAQ,IAAS,CACjD,IAAMK,EAAQC,EAAkBN,EA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ASuD,EAAe,EAAc,EAAuB,CAC3D,IAAMI,EAAS,+BAA+B,KAAKrD,CAAI,CAAC,GAAG,GAC3D,OAAOqD,GAAQ,OAAO,CAAC,IAAM1D,EAAM,MAAQ0D,EAAO,QAAU1D,EAAM,MACpE,CAEA,SAASI,EAAY,EAAc,EAAwB,CACzD,IAAIL,EAAQ,EACZ,KAAOA,EAAQ4D,GAAUtD,EAAKN,KAAW,KAAK,IAC9C,OAAOM,EAAK,MAAMN,CAAK,CACzB,CAEA,SAASH,EAAW,EAAwB,CAC1C,OAAOmB,EAAK,MAAM,QAAQ,CAC5B,C"}
1
+ {"version":3,"file":"markdown.js","names":["extractCodeBlocks","lines","splitLines","markdown","blocks","index","fence","parseOpeningFence","end","findClosingFence","stripIndent","line","extractIfSingleOutermostCodeBlock","block","findOutermostCodeBlock","text","languages","language","WRAPPED_MARKDOWN_LANGUAGES","extractTaggedCodeBlock","tag","tagName","found","findTaggedCodeBlock","wrapper","enclosingFence","fenceIndex","contentLine","extractSections","candidates","parseMarkdownSections","section","normalizeHeading","requested","names","name","sections","best","candidate","key","other","splitSections","stripHeadingEdges","heading","headings","findHeadings","nextIndex","lastIndex","isClosingFence","match","content","info","start","closer","indent"],"sources":["../src/markdown.ts"],"sourcesContent":["export interface CodeBlock {\n /** The first word of the info string, lowercased; `undefined` when the fence has none. */\n language?: string;\n code: string;\n /** `false` when the text ends before the closing fence, e.g. because the LLM output was cut off. */\n isClosed: boolean;\n}\n\ninterface Fence {\n char: string;\n length: number;\n indent: number;\n language: string;\n}\n\n/**\n * Extracts the fenced code blocks (backticks or tildes, any fence length) of Markdown text such as an LLM response.\n * A block runs to the first closing fence of the same character that is at least as long as its opening fence,\n * so a longer fence can hold shorter fences. Only fences indented by at most three spaces are recognized; fences\n * inside block quotes, deeply nested list items, and indented code blocks are not.\n */\nexport function extractCodeBlocks(markdown: string): CodeBlock[] {\n const lines = splitLines(markdown);\n const blocks: CodeBlock[] = [];\n for (let index = 0; index < lines.length; index++) {\n const fence = parseOpeningFence(lines[index] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, index + 1, fence);\n blocks.push({\n ...(fence.language && { language: fence.language }),\n code: lines\n .slice(index + 1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isClosed: end < lines.length,\n });\n index = end;\n }\n return blocks;\n}\n\n/**\n * Returns the contents of the code block that an LLM wrapped its whole response in, or `text` itself when the\n * response does not start with a fence or the fence's language is not one of a nonempty `languages` list\n * (compared case-insensitively). An omitted or empty list allows any language.\n * When the response also ends with a matching closing fence, everything between its first and last lines is returned,\n * so fences the LLM nested without lengthening the outer fence stay in the contents. Otherwise the first block is\n * returned and text after it is dropped; an unclosed block runs to the end of the text.\n */\nexport function extractIfSingleOutermostCodeBlock(text: string, languages?: readonly string[]): string {\n const block = findOutermostCodeBlock(text);\n if (!block) return text;\n if (languages?.length && !languages.some((language) => language.toLowerCase() === block.language)) return text;\n return block.code;\n}\n\nconst WRAPPED_MARKDOWN_LANGUAGES = new Set(['', 'markdown', 'md']);\n\n/**\n * Returns the trimmed contents of the first closed fenced code block (backticks or tildes) that follows a line holding\n * only `<tagName>` in text such as an LLM response, e.g. `<answer>\\n~~~\\n42\\n~~~`, or `undefined` when there is none.\n * Only blank lines may separate the tag line from the opening fence. Mentions of the tag within prose lines and tag\n * lines inside other fenced blocks (e.g. a format example) are skipped, but a whole-response `markdown`, `md`, or\n * unlabeled wrapper is read inside, as `parseMarkdownSections` does.\n */\nexport function extractTaggedCodeBlock(text: string, tagName: string): string | undefined {\n const tag = `<${tagName}>`;\n const found = findTaggedCodeBlock(splitLines(text), tag);\n if (found !== undefined) return found;\n // Only a real wrapper hides every tag line from the scan above; a leading example block paired with the tagged\n // block's closing fence merely looks like one, so its contents must not be read first.\n const wrapper = findOutermostCodeBlock(text);\n if (wrapper?.isWhole && WRAPPED_MARKDOWN_LANGUAGES.has(wrapper.language)) {\n return findTaggedCodeBlock(splitLines(wrapper.code), tag);\n }\n}\n\nfunction findTaggedCodeBlock(lines: readonly string[], tag: string): string | undefined {\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const enclosingFence = parseOpeningFence(line);\n if (enclosingFence) {\n index = findClosingFence(lines, index + 1, enclosingFence);\n continue;\n }\n if (line.trim() !== tag) continue;\n let fenceIndex = index + 1;\n while (fenceIndex < lines.length && !lines[fenceIndex]?.trim()) fenceIndex++;\n const fence = parseOpeningFence(lines[fenceIndex] ?? '');\n if (!fence) continue;\n const end = findClosingFence(lines, fenceIndex + 1, fence);\n if (end < lines.length) {\n return lines\n .slice(fenceIndex + 1, end)\n .map((contentLine) => stripIndent(contentLine, fence.indent))\n .join('\\n')\n .trim();\n }\n }\n}\n\nexport interface MarkdownSection {\n depth: number;\n /** The heading text with the closing `#` sequence and inline-code backticks removed. */\n heading: string;\n /** The source text up to the next heading at the same or a shallower depth, trimmed; subsections are included. */\n content: string;\n}\n\n/**\n * Extracts the content of the section headed by each of `names` from Markdown text such as an LLM response, keyed by\n * the given names. A heading matches a name when both are equal after removing decorations at their edges (such as\n * `**bold**`, backticks, and quotes), a leading number like `1.` or `2.1`, and a trailing colon, ignoring case and spacing;\n * headings at any depth are candidates.\n * Among matching headings, the shallowest wins, then one equal to the name as written, then the first; each heading\n * serves at most one name. When distinct requested names normalize alike, only exact matches at the shallowest\n * matching depth are returned. Missing, empty, or ambiguous sections are absent from the prototype-free result.\n */\nexport function extractSections<const Name extends string>(\n markdown: string,\n names: readonly Name[]\n): Partial<Record<Name, string>> {\n const candidates = parseMarkdownSections(markdown).map((section) => ({\n ...section,\n key: normalizeHeading(section.heading),\n }));\n const requested = [...new Set(names)].map((name) => ({ name, key: normalizeHeading(name) }));\n const sections: Partial<Record<Name, string>> = Object.create(null);\n for (const { name, key } of requested) {\n let best: (typeof candidates)[number] | undefined;\n for (const candidate of candidates) {\n if (candidate.key !== key) continue;\n if (\n !best ||\n candidate.depth < best.depth ||\n (candidate.depth === best.depth && candidate.heading === name && best.heading !== name)\n ) {\n best = candidate;\n }\n }\n if (!best) continue;\n if (best.heading !== name && requested.some((other) => other.name !== name && other.key === key)) continue;\n if (best.content) sections[name] = best.content;\n }\n return sections;\n}\n\n/**\n * Returns every ATX heading of Markdown text in order, with the source text under it.\n * Only headings starting at the beginning of a line outside fenced code blocks count; setext headings (underlined\n * with `=` or `-`) are ignored. A whole-response `markdown`, `md`, or unlabeled wrapper is read inside as\n * `extractIfSingleOutermostCodeBlock` finds it, preserving nested fences. A leading wrapper followed by prose is\n * also read inside, but only when there are no headings outside fences.\n */\nexport function parseMarkdownSections(markdown: string): MarkdownSection[] {\n const block = findOutermostCodeBlock(markdown);\n const wrapper = block && WRAPPED_MARKDOWN_LANGUAGES.has(block.language) ? block : undefined;\n if (wrapper?.isWhole) return splitSections(wrapper.code);\n const sections = splitSections(markdown);\n return sections.length === 0 && wrapper ? splitSections(wrapper.code) : sections;\n}\n\nfunction normalizeHeading(heading: string): string {\n return stripHeadingEdges(stripHeadingEdges(heading).replace(/^\\d+(?:\\.\\d+)*(?:\\.\\s+|\\)\\s*|\\s+)/u, ''))\n .replaceAll(/\\s+/gu, ' ')\n .toLowerCase();\n}\n\nfunction stripHeadingEdges(text: string): string {\n // Only the edges: `_` and `*` inside a heading belong to names such as `my_file.py`.\n return text.replaceAll(/^[\\s*_`~\"'「」]+|[\\s*_`~\"'「」::]+$/gu, '');\n}\n\nfunction splitSections(markdown: string): MarkdownSection[] {\n const lines = splitLines(markdown);\n const headings = findHeadings(lines);\n return headings.map((heading, index) => {\n let nextIndex = index + 1;\n while (nextIndex < headings.length && (headings[nextIndex]?.depth ?? 0) > heading.depth) nextIndex++;\n return {\n depth: heading.depth,\n heading: heading.text,\n content: lines\n .slice(heading.index + 1, headings[nextIndex]?.index)\n .join('\\n')\n .trim(),\n };\n });\n}\n\nfunction findOutermostCodeBlock(text: string): { language: string; code: string; isWhole: boolean } | undefined {\n // Keep the opening fence's indentation, which is stripped from the contents.\n const lines = splitLines(text.replace(/^\\s*\\n/u, '').trimEnd());\n const fence = parseOpeningFence(lines[0] ?? '');\n if (!fence) return;\n const lastIndex = lines.length - 1;\n const end =\n lastIndex > 0 && isClosingFence(lines[lastIndex] ?? '', fence) ? lastIndex : findClosingFence(lines, 1, fence);\n return {\n language: fence.language,\n code: lines\n .slice(1, end)\n .map((line) => stripIndent(line, fence.indent))\n .join('\\n'),\n isWhole: end >= lastIndex,\n };\n}\n\nfunction findHeadings(lines: readonly string[]): { index: number; depth: number; text: string }[] {\n const headings: { index: number; depth: number; text: string }[] = [];\n for (let index = 0; index < lines.length; index++) {\n const line = lines[index] ?? '';\n const fence = parseOpeningFence(line);\n if (fence) {\n index = findClosingFence(lines, index + 1, fence);\n continue;\n }\n const match = /^(#{1,6})(?:[ \\t]+(.*))?$/u.exec(line);\n if (!match) continue;\n const text = (match[2] ?? '')\n // The closing sequence of an ATX heading needs a space before it, so `# C#` keeps its `#`.\n .replace(/(?:^|[ \\t]+)#+[ \\t]*$/u, '')\n .trim()\n .replaceAll(/(`+)(.+?)\\1/gu, (_match, _delimiter: string, content: string) =>\n content.startsWith(' ') && content.endsWith(' ') ? content.slice(1, -1) : content\n );\n headings.push({ index, depth: match[1]?.length ?? 1, text });\n }\n return headings;\n}\n\nfunction parseOpeningFence(line: string): Fence | undefined {\n const match = /^( {0,3})(`{3,}|~{3,})(.*)$/u.exec(line);\n const fence = match?.[2];\n const info = match?.[3] ?? '';\n // A backtick in the info string makes the line inline code instead of a fence.\n if (!fence || (fence.startsWith('`') && info.includes('`'))) return;\n return {\n char: fence.charAt(0),\n length: fence.length,\n indent: match[1]?.length ?? 0,\n language: info.trim().split(/\\s/u)[0]?.toLowerCase() ?? '',\n };\n}\n\n/** Returns the index of the line closing `fence`, or the number of lines when it is never closed. */\nfunction findClosingFence(lines: readonly string[], start: number, fence: Fence): number {\n let index = start;\n while (index < lines.length && !isClosingFence(lines[index] ?? '', fence)) index++;\n return index;\n}\n\nfunction isClosingFence(line: string, fence: Fence): boolean {\n const closer = /^ {0,3}(`{3,}|~{3,})[ \\t]*$/u.exec(line)?.[1];\n return closer?.charAt(0) === fence.char && closer.length >= fence.length;\n}\n\nfunction stripIndent(line: string, indent: number): string {\n let index = 0;\n while (index < indent && line[index] === ' ') index++;\n return line.slice(index);\n}\n\nfunction splitLines(text: string): string[] {\n return text.split(/\\r?\\n/u);\n}\n"],"mappings":"AAqBA,SAAgBA,EAAkB,EAA+B,CAC/D,IAAMC,EAAQC,EAAWC,CAAQ,EAC3BC,EAAsB,CAAC,EAC7B,IAAK,IAAIC,EAAQ,EAAGA,EAAQJ,EAAM,OAAQ,IAAS,CACjD,IAAMK,EAAQC,EAAkBN,EAAMI,IAAU,EAAE,EAClD,GAAI,CAACC,EAAO,SACZ,IAAME,EAAMC,EAAiBR,EAAOI,EAAQ,EAAGC,CAAK,EACpDF,EAAO,KAAK,CACV,GAAIE,EAAM,UAAY,CAAE,SAAUA,EAAM,QAAS,EACjD,KAAML,EACH,MAAMI,EAAQ,EAAGG,CAAG,CAAC,CACrB,IAAK,GAASE,EAAYC,EAAML,EAAM,MAAM,CAAC,CAAC,CAC9C,KAAK;CAAI,EACZ,SAAUE,EAAMP,EAAM,MACxB,CAAC,EACD,EAAQO,CACV,CACA,OAAOJ,CACT,CAUA,SAAgBQ,EAAkC,EAAc,EAAuC,CACrG,IAAMC,EAAQC,EAAuBC,CAAI,EAGzC,MAFI,CAACF,GACDG,GAAW,QAAU,CAACA,EAAU,KAAM,GAAaC,EAAS,YAAY,IAAMJ,EAAM,QAAQ,EAAUE,EACnGF,EAAM,IACf,CAEA,MAAMK,kBAA6B,IAAI,IAAI,CAAC,GAAI,WAAY,IAAI,CAAC,EASjE,SAAgBC,EAAuB,EAAc,EAAqC,CACxF,IAAMC,EAAM,IAAIC,EAAQ,GAClBC,EAAQC,EAAoBrB,EAAWa,CAAI,EAAGK,CAAG,EACvD,GAAIE,IAAU,IAAA,GAAW,OAAOA,EAGhC,IAAME,EAAUV,EAAuBC,CAAI,EAC3C,GAAIS,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,IAAQ,GAChBE,EAAOF,IAAQ,IAAM,GAE3B,GAAI,GAAC3C,GAAUA,EAAM,WAAW,GAAG,GAAK6C,EAAK,SAAS,GAAG,GACzD,MAAO,CACL,KAAM7C,EAAM,OAAO,CAAC,EACpB,OAAQA,EAAM,OACd,OAAQ2C,EAAM,EAAE,EAAE,QAAU,EAC5B,SAAUE,EAAK,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,EAAE,EAAE,YAAY,GAAK,EAC1D,CACF,CAGA,SAAS1C,EAAiB,EAA0B,EAAe,EAAsB,CACvF,IAAIJ,EAAQ+C,EACZ,KAAO/C,EAAQJ,EAAM,QAAU,CAAC+C,EAAe/C,EAAMI,IAAU,GAAIC,CAAK,GAAG,IAC3E,OAAOD,CACT,CAEA,SAAS2C,EAAe,EAAc,EAAuB,CAC3D,IAAMK,EAAS,+BAA+B,KAAK1C,CAAI,CAAC,GAAG,GAC3D,OAAO0C,GAAQ,OAAO,CAAC,IAAM/C,EAAM,MAAQ+C,EAAO,QAAU/C,EAAM,MACpE,CAEA,SAASI,EAAY,EAAc,EAAwB,CACzD,IAAIL,EAAQ,EACZ,KAAOA,EAAQiD,GAAU3C,EAAKN,KAAW,KAAK,IAC9C,OAAOM,EAAK,MAAMN,CAAK,CACzB,CAEA,SAASH,EAAW,EAAwB,CAC1C,OAAOa,EAAK,MAAM,QAAQ,CAC5B,C"}
package/dist/text.cjs CHANGED
@@ -1,4 +1,9 @@
1
1
  "use strict";function e(e){return e.replaceAll(/[$()*+.?[\\\]^{|}]/gu,String.raw`\$&`)}function t(e){return/^[\w./-]+$/u.test(e)?e:`'${e.replaceAll(`'`,String.raw`'\''`)}'`}function n(e,t=``){return a(`~`,e,t)}function r(e,t=``){return a("`",e,t)}function i(e){return o(`~`,e)}function a(e,t,n){let r=0;for(;n[r]===e;)r++;let i=o(e,t)+e.repeat(r);return`${i}${n.slice(r)}\n${t}${t.endsWith(`
2
2
  `)?``:`
3
- `}${i}`}function o(e,t){let n=e.repeat(3),r=2,i=t.indexOf(n);for(;i!==-1;){let a=i+3;for(;t[a]===e;)a++;r=Math.max(r,a-i),i=t.indexOf(n,a)}return e.repeat(r+1)}function s(e,t,n=`…`){if(e.length<=t)return e;if(t<n.length)throw RangeError(`maxLength (${t}) must be at least the ellipsis length (${n.length})`);return`${c(e,t-n.length)}${n}`}function c(e,t){let n=e.charCodeAt(t-1);return e.slice(0,n>=55296&&n<=56319?t-1:t)}const l={'"':`&quot;`,"&":`&amp;`,"'":`&#39;`,"<":`&lt;`,">":`&gt;`},u=/["&'<>]/gu;function d(e){return e.search(u)===-1?e:e.replaceAll(u,e=>l[e])}exports.escapeHtml=d,exports.escapeRegExp=e,exports.getTildeFence=i,exports.quoteForShell=t,exports.sliceWithoutSplittingSurrogates=c,exports.toCodeBlock=r,exports.toTildeCodeBlock=n,exports.truncate=s;
3
+ `}${i}`}function o(e,t){let n=e.repeat(3),r=2,i=t.indexOf(n);for(;i!==-1;){let a=i+3;for(;t[a]===e;)a++;r=Math.max(r,a-i),i=t.indexOf(n,a)}return e.repeat(r+1)}function s(e,t){if(t.length<3||f(t,`~`)!==t.length)throw RangeError(`fence must be a run of at least three tildes: ${t}`);let n=(e.startsWith(``)?e.slice(1):e).replaceAll(`\r
4
+ `,`
5
+ `).split(`
6
+ `);return c(n,t.length)??l(n)}function c(e,t){let n=e.findIndex(e=>f(e,`~`)>=t);if(n===-1)return;let r=f(e[n]??``,`~`),i=e.findLastIndex((e,t)=>t>n&&d(e,`~`,r));if(i!==-1)return e.slice(n+1,i).join(`
7
+ `)}function l(e){let t=e.findIndex(e=>e.trim()!==``),n=e.findLastIndex(e=>e.trim()!==``);if(t===n)return;let r=e[t]??``,i=f(r,"`");if(i<3||!u(r.slice(i))||!d(e[n]??``,"`",i))return;let a=e.slice(t+1,n);if(!a.some(e=>f(e,"`")>=i))return a.join(`
8
+ `)}function u(e){let t=e.trim(),n=0;for(;n<t.length&&t[n]!==` `&&t[n]!==` `;)n++;let r=t.slice(0,n).toLowerCase();return r===``||r===`md`||r===`markdown`}function d(e,t,n){let r=e.trimEnd();return r.length>=n&&f(r,t)===r.length}function f(e,t){let n=0;for(;e[n]===t;)n++;return n}function p(e,t,n=`…`){if(e.length<=t)return e;if(t<n.length)throw RangeError(`maxLength (${t}) must be at least the ellipsis length (${n.length})`);return`${m(e,t-n.length)}${n}`}function m(e,t){let n=e.charCodeAt(t-1);return e.slice(0,n>=55296&&n<=56319?t-1:t)}const h={'"':`&quot;`,"&":`&amp;`,"'":`&#39;`,"<":`&lt;`,">":`&gt;`},g=/["&'<>]/gu;function _(e){return e.search(g)===-1?e:e.replaceAll(g,e=>h[e])}exports.escapeHtml=_,exports.escapeRegExp=e,exports.getTildeFence=i,exports.quoteForShell=t,exports.sliceWithoutSplittingSurrogates=m,exports.toCodeBlock=r,exports.toTildeCodeBlock=n,exports.truncate=p,exports.unwrapFencedReply=s;
4
9
  //# sourceMappingURL=text.cjs.map
package/dist/text.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"text.cjs","names":["escapeRegExp","text","quoteForShell","value","toTildeCodeBlock","toFencedCodeBlock","language","toCodeBlock","getTildeFence","getFence","languageStart","fenceChar","fence","shortestFence","longestRun","start","end","truncate","maxLength","ellipsis","sliceWithoutSplittingSurrogates","lastKeptUnit","htmlEntities","htmlSpecialCharactersPattern","escapeHtml","character"],"sources":["../src/text.ts"],"sourcesContent":["/** Escapes every RegExp metacharacter so the text matches itself literally. */\nexport function escapeRegExp(text: string): string {\n return text.replaceAll(/[$()*+.?[\\\\\\]^{|}]/gu, String.raw`\\$&`);\n}\n\n/** A POSIX shell word: safe characters pass through, anything else is single-quoted. */\nexport function quoteForShell(value: string): string {\n return /^[\\w./-]+$/u.test(value) ? value : `'${value.replaceAll(\"'\", String.raw`'\\''`)}'`;\n}\n\n/**\n * Wraps text in a Markdown code block fenced with tildes, which are rarer than backticks in Markdown content.\n * The fence is longer than any run of tildes in the text so that the text cannot close it.\n */\nexport function toTildeCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('~', text, language);\n}\n\n/**\n * Wraps text in a Markdown code block fenced with backticks.\n * The fence is longer than any run of backticks in the text so that the text cannot close it.\n */\nexport function toCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('`', text, language);\n}\n\n/** Returns the tilde fence `toTildeCodeBlock` wraps `text` in: at least `~~~`, longer than any run of tildes in it. */\nexport function getTildeFence(text: string): string {\n return getFence('~', text);\n}\n\nfunction toFencedCodeBlock(fenceChar: string, text: string, language: string): string {\n let languageStart = 0;\n while (language[languageStart] === fenceChar) languageStart++;\n const fence = getFence(fenceChar, text) + fenceChar.repeat(languageStart);\n return `${fence}${language.slice(languageStart)}\\n${text}${text.endsWith('\\n') ? '' : '\\n'}${fence}`;\n}\n\nfunction getFence(fenceChar: string, text: string): string {\n const shortestFence = fenceChar.repeat(3);\n let longestRun = 2;\n let start = text.indexOf(shortestFence);\n while (start !== -1) {\n let end = start + 3;\n while (text[end] === fenceChar) end++;\n longestRun = Math.max(longestRun, end - start);\n start = text.indexOf(shortestFence, end);\n }\n return fenceChar.repeat(longestRun + 1);\n}\n\n/**\n * Shortens text to at most `maxLength` UTF-16 code units, including the `ellipsis` that marks the cut.\n * Throws a `RangeError` when `maxLength` cannot hold the ellipsis.\n */\nexport function truncate(text: string, maxLength: number, ellipsis = '…'): string {\n if (text.length <= maxLength) return text;\n if (maxLength < ellipsis.length) {\n throw new RangeError(`maxLength (${maxLength}) must be at least the ellipsis length (${ellipsis.length})`);\n }\n return `${sliceWithoutSplittingSurrogates(text, maxLength - ellipsis.length)}${ellipsis}`;\n}\n\n/**\n * Returns the first `end` code units of `text`, one fewer when the last kept unit is a high surrogate, whose low half\n * is either cut off or absent from `text`, because an unpaired surrogate is replaced with U+FFFD by encoders.\n */\nexport function sliceWithoutSplittingSurrogates(text: string, end: number): string {\n // oxlint-disable-next-line unicorn/prefer-code-point -- the check is on a single UTF-16 code unit\n const lastKeptUnit = text.charCodeAt(end - 1);\n return text.slice(0, lastKeptUnit >= 0xd8_00 && lastKeptUnit <= 0xdb_ff ? end - 1 : end);\n}\n\nconst htmlEntities: Readonly<Record<string, string>> = {\n '\"': '&quot;',\n '&': '&amp;',\n \"'\": '&#39;',\n '<': '&lt;',\n '>': '&gt;',\n};\n\nconst htmlSpecialCharactersPattern = /[\"&'<>]/gu;\n\n/** Escapes `&`, `<`, `>`, `\"`, and `'`, so the result is safe in HTML text and in quoted attribute values. */\nexport function escapeHtml(text: string): string {\n // Measured in V8 and JavaScriptCore: a native `search` first is faster than `replaceAll` alone for text without these\n // characters, and one native `replaceAll` beats a per-code-unit loop for text where they are rare.\n return text.search(htmlSpecialCharactersPattern) === -1\n ? text\n : text.replaceAll(htmlSpecialCharactersPattern, (character) => htmlEntities[character] as string);\n}\n"],"mappings":"A,aACA,SAAgBA,EAAa,EAAsB,CACjD,OAAOC,EAAK,WAAW,uBAAwB,OAAO,GAAG,KAAK,CAChE,CAGA,SAAgBC,EAAc,EAAuB,CACnD,MAAO,cAAc,KAAKC,CAAK,EAAIA,EAAQ,IAAIA,EAAM,WAAW,IAAK,OAAO,GAAG,MAAM,EAAE,EACzF,CAMA,SAAgBC,EAAiB,EAAc,EAAW,GAAY,CACpE,OAAOC,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAMA,SAAgBC,EAAY,EAAc,EAAW,GAAY,CAC/D,OAAOF,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAGA,SAAgBE,EAAc,EAAsB,CAClD,OAAOC,EAAS,IAAKR,CAAI,CAC3B,CAEA,SAASI,EAAkB,EAAmB,EAAc,EAA0B,CACpF,IAAIK,EAAgB,EACpB,KAAOJ,EAASI,KAAmBC,GAAW,IAC9C,IAAMC,EAAQH,EAASE,EAAWV,CAAI,EAAIU,EAAU,OAAOD,CAAa,EACxE,MAAO,GAAGE,IAAQN,EAAS,MAAMI,CAAa,EAAE,IAAIT,IAAOA,EAAK,SAAS;CAAI,EAAI,GAAK;IAAOW,GAC/F,CAEA,SAASH,EAAS,EAAmB,EAAsB,CACzD,IAAMI,EAAgBF,EAAU,OAAO,CAAC,EACpCG,EAAa,EACbC,EAAQd,EAAK,QAAQY,CAAa,EACtC,KAAOE,IAAU,IAAI,CACnB,IAAIC,EAAMD,EAAQ,EAClB,KAAOd,EAAKe,KAASL,GAAW,IAChC,EAAa,KAAK,IAAIG,EAAYE,EAAMD,CAAK,EAC7C,EAAQd,EAAK,QAAQY,EAAeG,CAAG,CACzC,CACA,OAAOL,EAAU,OAAOG,EAAa,CAAC,CACxC,CAMA,SAAgBG,EAAS,EAAc,EAAmB,EAAW,IAAa,CAChF,GAAIhB,EAAK,QAAUiB,EAAW,OAAOjB,EACrC,GAAIiB,EAAYC,EAAS,OACvB,MAAU,WAAW,cAAcD,EAAU,0CAA0CC,EAAS,OAAO,EAAE,EAE3G,MAAO,GAAGC,EAAgCnB,EAAMiB,EAAYC,EAAS,MAAM,IAAIA,GACjF,CAMA,SAAgBC,EAAgC,EAAc,EAAqB,CAEjF,IAAMC,EAAepB,EAAK,WAAWe,EAAM,CAAC,EAC5C,OAAOf,EAAK,MAAM,EAAGoB,GAAgB,OAAWA,GAAgB,MAAUL,EAAM,EAAIA,CAAG,CACzF,CAEA,MAAMM,EAAiD,CACrD,IAAK,SACL,IAAK,QACL,IAAK,QACL,IAAK,OACL,IAAK,MACP,EAEMC,EAA+B,YAGrC,SAAgBC,EAAW,EAAsB,CAG/C,OAAOvB,EAAK,OAAOsB,CAA4B,IAAM,GACjDtB,EACAA,EAAK,WAAWsB,EAA+B,GAAcD,EAAaG,EAAoB,CACpG,C"}
1
+ {"version":3,"file":"text.cjs","names":["escapeRegExp","text","quoteForShell","value","toTildeCodeBlock","toFencedCodeBlock","language","toCodeBlock","getTildeFence","getFence","languageStart","fenceChar","fence","shortestFence","longestRun","start","end","unwrapFencedReply","countLeading","lines","unwrapTildeWrapper","unwrapBacktickWrapper","line","minLength","openingLength","index","isFenceOnly","opening","length","isMarkdownInfo","inner","trimmed","info","wordEnd","word","count","char","truncate","maxLength","ellipsis","sliceWithoutSplittingSurrogates","lastKeptUnit","htmlEntities","htmlSpecialCharactersPattern","escapeHtml","character"],"sources":["../src/text.ts"],"sourcesContent":["/** Escapes every RegExp metacharacter so the text matches itself literally. */\nexport function escapeRegExp(text: string): string {\n return text.replaceAll(/[$()*+.?[\\\\\\]^{|}]/gu, String.raw`\\$&`);\n}\n\n/** A POSIX shell word: safe characters pass through, anything else is single-quoted. */\nexport function quoteForShell(value: string): string {\n return /^[\\w./-]+$/u.test(value) ? value : `'${value.replaceAll(\"'\", String.raw`'\\''`)}'`;\n}\n\n/**\n * Wraps text in a Markdown code block fenced with tildes, which are rarer than backticks in Markdown content.\n * The fence is longer than any run of tildes in the text so that the text cannot close it.\n */\nexport function toTildeCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('~', text, language);\n}\n\n/**\n * Wraps text in a Markdown code block fenced with backticks.\n * The fence is longer than any run of backticks in the text so that the text cannot close it.\n */\nexport function toCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('`', text, language);\n}\n\n/** Returns the tilde fence `toTildeCodeBlock` wraps `text` in: at least `~~~`, longer than any run of tildes in it. */\nexport function getTildeFence(text: string): string {\n return getFence('~', text);\n}\n\nfunction toFencedCodeBlock(fenceChar: string, text: string, language: string): string {\n let languageStart = 0;\n while (language[languageStart] === fenceChar) languageStart++;\n const fence = getFence(fenceChar, text) + fenceChar.repeat(languageStart);\n return `${fence}${language.slice(languageStart)}\\n${text}${text.endsWith('\\n') ? '' : '\\n'}${fence}`;\n}\n\nfunction getFence(fenceChar: string, text: string): string {\n const shortestFence = fenceChar.repeat(3);\n let longestRun = 2;\n let start = text.indexOf(shortestFence);\n while (start !== -1) {\n let end = start + 3;\n while (text[end] === fenceChar) end++;\n longestRun = Math.max(longestRun, end - start);\n start = text.indexOf(shortestFence, end);\n }\n return fenceChar.repeat(longestRun + 1);\n}\n\n/**\n * Returns the document an LLM replied with inside the wrapper fence it was asked to keep, or `undefined` when the reply\n * is not wrapped as required so that the caller can retry. `fence` is the tilde fence the document was sent in, e.g.\n * `getTildeFence(document)`; this relies on it being longer than any run of tildes in the document.\n * A leading BOM is removed, CRLF is read as LF, and trailing whitespace is ignored on fence lines. Fences start at the\n * beginning of a line. The result is the lines strictly between the wrapper's fence lines joined with LF, untrimmed,\n * so a final newline of the document is not kept.\n * - Tilde wrapper: the first line starting with at least `fence.length` tildes, followed by any info string, opens it,\n * and the last later line of only tildes, at least as many as the opening run, closes it. Text around it, backtick\n * fence lines included, is dropped.\n * - Otherwise, backtick wrapper: the first nonblank line is a run of at least three backticks with no info string or\n * one whose first word is `md` or `markdown` (case-insensitively), the last nonblank line is only backticks, at least\n * as many, and no line between them starts with that many backticks.\n */\nexport function unwrapFencedReply(text: string, fence: string): string | undefined {\n if (fence.length < 3 || countLeading(fence, '~') !== fence.length) {\n throw new RangeError(`fence must be a run of at least three tildes: ${fence}`);\n }\n const lines = (text.startsWith('\\uFEFF') ? text.slice(1) : text).replaceAll('\\r\\n', '\\n').split('\\n');\n return unwrapTildeWrapper(lines, fence.length) ?? unwrapBacktickWrapper(lines);\n}\n\nfunction unwrapTildeWrapper(lines: readonly string[], minLength: number): string | undefined {\n const start = lines.findIndex((line) => countLeading(line, '~') >= minLength);\n if (start === -1) return;\n const openingLength = countLeading(lines[start] ?? '', '~');\n const end = lines.findLastIndex((line, index) => index > start && isFenceOnly(line, '~', openingLength));\n if (end === -1) return;\n return lines.slice(start + 1, end).join('\\n');\n}\n\nfunction unwrapBacktickWrapper(lines: readonly string[]): string | undefined {\n const start = lines.findIndex((line) => line.trim() !== '');\n const end = lines.findLastIndex((line) => line.trim() !== '');\n if (start === end) return;\n const opening = lines[start] ?? '';\n const length = countLeading(opening, '`');\n if (length < 3 || !isMarkdownInfo(opening.slice(length)) || !isFenceOnly(lines[end] ?? '', '`', length)) return;\n const inner = lines.slice(start + 1, end);\n if (inner.some((line) => countLeading(line, '`') >= length)) return;\n return inner.join('\\n');\n}\n\nfunction isMarkdownInfo(info: string): boolean {\n const trimmed = info.trim();\n let wordEnd = 0;\n while (wordEnd < trimmed.length && trimmed[wordEnd] !== ' ' && trimmed[wordEnd] !== '\\t') wordEnd++;\n const word = trimmed.slice(0, wordEnd).toLowerCase();\n return word === '' || word === 'md' || word === 'markdown';\n}\n\nfunction isFenceOnly(line: string, fenceChar: string, minLength: number): boolean {\n const trimmed = line.trimEnd();\n return trimmed.length >= minLength && countLeading(trimmed, fenceChar) === trimmed.length;\n}\n\nfunction countLeading(line: string, char: string): number {\n let count = 0;\n while (line[count] === char) count++;\n return count;\n}\n\n/**\n * Shortens text to at most `maxLength` UTF-16 code units, including the `ellipsis` that marks the cut.\n * Throws a `RangeError` when `maxLength` cannot hold the ellipsis.\n */\nexport function truncate(text: string, maxLength: number, ellipsis = '…'): string {\n if (text.length <= maxLength) return text;\n if (maxLength < ellipsis.length) {\n throw new RangeError(`maxLength (${maxLength}) must be at least the ellipsis length (${ellipsis.length})`);\n }\n return `${sliceWithoutSplittingSurrogates(text, maxLength - ellipsis.length)}${ellipsis}`;\n}\n\n/**\n * Returns the first `end` code units of `text`, one fewer when the last kept unit is a high surrogate, whose low half\n * is either cut off or absent from `text`, because an unpaired surrogate is replaced with U+FFFD by encoders.\n */\nexport function sliceWithoutSplittingSurrogates(text: string, end: number): string {\n // oxlint-disable-next-line unicorn/prefer-code-point -- the check is on a single UTF-16 code unit\n const lastKeptUnit = text.charCodeAt(end - 1);\n return text.slice(0, lastKeptUnit >= 0xd8_00 && lastKeptUnit <= 0xdb_ff ? end - 1 : end);\n}\n\nconst htmlEntities: Readonly<Record<string, string>> = {\n '\"': '&quot;',\n '&': '&amp;',\n \"'\": '&#39;',\n '<': '&lt;',\n '>': '&gt;',\n};\n\nconst htmlSpecialCharactersPattern = /[\"&'<>]/gu;\n\n/** Escapes `&`, `<`, `>`, `\"`, and `'`, so the result is safe in HTML text and in quoted attribute values. */\nexport function escapeHtml(text: string): string {\n // Measured in V8 and JavaScriptCore: a native `search` first is faster than `replaceAll` alone for text without these\n // characters, and one native `replaceAll` beats a per-code-unit loop for text where they are rare.\n return text.search(htmlSpecialCharactersPattern) === -1\n ? text\n : text.replaceAll(htmlSpecialCharactersPattern, (character) => htmlEntities[character] as string);\n}\n"],"mappings":"A,aACA,SAAgBA,EAAa,EAAsB,CACjD,OAAOC,EAAK,WAAW,uBAAwB,OAAO,GAAG,KAAK,CAChE,CAGA,SAAgBC,EAAc,EAAuB,CACnD,MAAO,cAAc,KAAKC,CAAK,EAAIA,EAAQ,IAAIA,EAAM,WAAW,IAAK,OAAO,GAAG,MAAM,EAAE,EACzF,CAMA,SAAgBC,EAAiB,EAAc,EAAW,GAAY,CACpE,OAAOC,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAMA,SAAgBC,EAAY,EAAc,EAAW,GAAY,CAC/D,OAAOF,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAGA,SAAgBE,EAAc,EAAsB,CAClD,OAAOC,EAAS,IAAKR,CAAI,CAC3B,CAEA,SAASI,EAAkB,EAAmB,EAAc,EAA0B,CACpF,IAAIK,EAAgB,EACpB,KAAOJ,EAASI,KAAmBC,GAAW,IAC9C,IAAMC,EAAQH,EAASE,EAAWV,CAAI,EAAIU,EAAU,OAAOD,CAAa,EACxE,MAAO,GAAGE,IAAQN,EAAS,MAAMI,CAAa,EAAE,IAAIT,IAAOA,EAAK,SAAS;CAAI,EAAI,GAAK;IAAOW,GAC/F,CAEA,SAASH,EAAS,EAAmB,EAAsB,CACzD,IAAMI,EAAgBF,EAAU,OAAO,CAAC,EACpCG,EAAa,EACbC,EAAQd,EAAK,QAAQY,CAAa,EACtC,KAAOE,IAAU,IAAI,CACnB,IAAIC,EAAMD,EAAQ,EAClB,KAAOd,EAAKe,KAASL,GAAW,IAChC,EAAa,KAAK,IAAIG,EAAYE,EAAMD,CAAK,EAC7C,EAAQd,EAAK,QAAQY,EAAeG,CAAG,CACzC,CACA,OAAOL,E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 +14,21 @@ export declare function toTildeCodeBlock(text: string, language?: string): strin
14
14
  export declare function toCodeBlock(text: string, language?: string): string;
15
15
  /** Returns the tilde fence `toTildeCodeBlock` wraps `text` in: at least `~~~`, longer than any run of tildes in it. */
16
16
  export declare function getTildeFence(text: string): string;
17
+ /**
18
+ * Returns the document an LLM replied with inside the wrapper fence it was asked to keep, or `undefined` when the reply
19
+ * is not wrapped as required so that the caller can retry. `fence` is the tilde fence the document was sent in, e.g.
20
+ * `getTildeFence(document)`; this relies on it being longer than any run of tildes in the document.
21
+ * A leading BOM is removed, CRLF is read as LF, and trailing whitespace is ignored on fence lines. Fences start at the
22
+ * beginning of a line. The result is the lines strictly between the wrapper's fence lines joined with LF, untrimmed,
23
+ * so a final newline of the document is not kept.
24
+ * - Tilde wrapper: the first line starting with at least `fence.length` tildes, followed by any info string, opens it,
25
+ * and the last later line of only tildes, at least as many as the opening run, closes it. Text around it, backtick
26
+ * fence lines included, is dropped.
27
+ * - Otherwise, backtick wrapper: the first nonblank line is a run of at least three backticks with no info string or
28
+ * one whose first word is `md` or `markdown` (case-insensitively), the last nonblank line is only backticks, at least
29
+ * as many, and no line between them starts with that many backticks.
30
+ */
31
+ export declare function unwrapFencedReply(text: string, fence: string): string | undefined;
17
32
  /**
18
33
  * Shortens text to at most `maxLength` UTF-16 code units, including the `ellipsis` that marks the cut.
19
34
  * Throws a `RangeError` when `maxLength` cannot hold the ellipsis.
package/dist/text.js CHANGED
@@ -1,4 +1,9 @@
1
1
  function e(e){return e.replaceAll(/[$()*+.?[\\\]^{|}]/gu,String.raw`\$&`)}function t(e){return/^[\w./-]+$/u.test(e)?e:`'${e.replaceAll(`'`,String.raw`'\''`)}'`}function n(e,t=``){return a(`~`,e,t)}function r(e,t=``){return a("`",e,t)}function i(e){return o(`~`,e)}function a(e,t,n){let r=0;for(;n[r]===e;)r++;let i=o(e,t)+e.repeat(r);return`${i}${n.slice(r)}\n${t}${t.endsWith(`
2
2
  `)?``:`
3
- `}${i}`}function o(e,t){let n=e.repeat(3),r=2,i=t.indexOf(n);for(;i!==-1;){let a=i+3;for(;t[a]===e;)a++;r=Math.max(r,a-i),i=t.indexOf(n,a)}return e.repeat(r+1)}function s(e,t,n=`…`){if(e.length<=t)return e;if(t<n.length)throw RangeError(`maxLength (${t}) must be at least the ellipsis length (${n.length})`);return`${c(e,t-n.length)}${n}`}function c(e,t){let n=e.charCodeAt(t-1);return e.slice(0,n>=55296&&n<=56319?t-1:t)}const l={'"':`&quot;`,"&":`&amp;`,"'":`&#39;`,"<":`&lt;`,">":`&gt;`},u=/["&'<>]/gu;function d(e){return e.search(u)===-1?e:e.replaceAll(u,e=>l[e])}export{d as escapeHtml,e as escapeRegExp,i as getTildeFence,t as quoteForShell,c as sliceWithoutSplittingSurrogates,r as toCodeBlock,n as toTildeCodeBlock,s as truncate};
3
+ `}${i}`}function o(e,t){let n=e.repeat(3),r=2,i=t.indexOf(n);for(;i!==-1;){let a=i+3;for(;t[a]===e;)a++;r=Math.max(r,a-i),i=t.indexOf(n,a)}return e.repeat(r+1)}function s(e,t){if(t.length<3||f(t,`~`)!==t.length)throw RangeError(`fence must be a run of at least three tildes: ${t}`);let n=(e.startsWith(``)?e.slice(1):e).replaceAll(`\r
4
+ `,`
5
+ `).split(`
6
+ `);return c(n,t.length)??l(n)}function c(e,t){let n=e.findIndex(e=>f(e,`~`)>=t);if(n===-1)return;let r=f(e[n]??``,`~`),i=e.findLastIndex((e,t)=>t>n&&d(e,`~`,r));if(i!==-1)return e.slice(n+1,i).join(`
7
+ `)}function l(e){let t=e.findIndex(e=>e.trim()!==``),n=e.findLastIndex(e=>e.trim()!==``);if(t===n)return;let r=e[t]??``,i=f(r,"`");if(i<3||!u(r.slice(i))||!d(e[n]??``,"`",i))return;let a=e.slice(t+1,n);if(!a.some(e=>f(e,"`")>=i))return a.join(`
8
+ `)}function u(e){let t=e.trim(),n=0;for(;n<t.length&&t[n]!==` `&&t[n]!==` `;)n++;let r=t.slice(0,n).toLowerCase();return r===``||r===`md`||r===`markdown`}function d(e,t,n){let r=e.trimEnd();return r.length>=n&&f(r,t)===r.length}function f(e,t){let n=0;for(;e[n]===t;)n++;return n}function p(e,t,n=`…`){if(e.length<=t)return e;if(t<n.length)throw RangeError(`maxLength (${t}) must be at least the ellipsis length (${n.length})`);return`${m(e,t-n.length)}${n}`}function m(e,t){let n=e.charCodeAt(t-1);return e.slice(0,n>=55296&&n<=56319?t-1:t)}const h={'"':`&quot;`,"&":`&amp;`,"'":`&#39;`,"<":`&lt;`,">":`&gt;`},g=/["&'<>]/gu;function _(e){return e.search(g)===-1?e:e.replaceAll(g,e=>h[e])}export{_ as escapeHtml,e as escapeRegExp,i as getTildeFence,t as quoteForShell,m as sliceWithoutSplittingSurrogates,r as toCodeBlock,n as toTildeCodeBlock,p as truncate,s as unwrapFencedReply};
4
9
  //# sourceMappingURL=text.js.map
package/dist/text.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"text.js","names":["escapeRegExp","text","quoteForShell","value","toTildeCodeBlock","toFencedCodeBlock","language","toCodeBlock","getTildeFence","getFence","languageStart","fenceChar","fence","shortestFence","longestRun","start","end","truncate","maxLength","ellipsis","sliceWithoutSplittingSurrogates","lastKeptUnit","htmlEntities","htmlSpecialCharactersPattern","escapeHtml","character"],"sources":["../src/text.ts"],"sourcesContent":["/** Escapes every RegExp metacharacter so the text matches itself literally. */\nexport function escapeRegExp(text: string): string {\n return text.replaceAll(/[$()*+.?[\\\\\\]^{|}]/gu, String.raw`\\$&`);\n}\n\n/** A POSIX shell word: safe characters pass through, anything else is single-quoted. */\nexport function quoteForShell(value: string): string {\n return /^[\\w./-]+$/u.test(value) ? value : `'${value.replaceAll(\"'\", String.raw`'\\''`)}'`;\n}\n\n/**\n * Wraps text in a Markdown code block fenced with tildes, which are rarer than backticks in Markdown content.\n * The fence is longer than any run of tildes in the text so that the text cannot close it.\n */\nexport function toTildeCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('~', text, language);\n}\n\n/**\n * Wraps text in a Markdown code block fenced with backticks.\n * The fence is longer than any run of backticks in the text so that the text cannot close it.\n */\nexport function toCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('`', text, language);\n}\n\n/** Returns the tilde fence `toTildeCodeBlock` wraps `text` in: at least `~~~`, longer than any run of tildes in it. */\nexport function getTildeFence(text: string): string {\n return getFence('~', text);\n}\n\nfunction toFencedCodeBlock(fenceChar: string, text: string, language: string): string {\n let languageStart = 0;\n while (language[languageStart] === fenceChar) languageStart++;\n const fence = getFence(fenceChar, text) + fenceChar.repeat(languageStart);\n return `${fence}${language.slice(languageStart)}\\n${text}${text.endsWith('\\n') ? '' : '\\n'}${fence}`;\n}\n\nfunction getFence(fenceChar: string, text: string): string {\n const shortestFence = fenceChar.repeat(3);\n let longestRun = 2;\n let start = text.indexOf(shortestFence);\n while (start !== -1) {\n let end = start + 3;\n while (text[end] === fenceChar) end++;\n longestRun = Math.max(longestRun, end - start);\n start = text.indexOf(shortestFence, end);\n }\n return fenceChar.repeat(longestRun + 1);\n}\n\n/**\n * Shortens text to at most `maxLength` UTF-16 code units, including the `ellipsis` that marks the cut.\n * Throws a `RangeError` when `maxLength` cannot hold the ellipsis.\n */\nexport function truncate(text: string, maxLength: number, ellipsis = '…'): string {\n if (text.length <= maxLength) return text;\n if (maxLength < ellipsis.length) {\n throw new RangeError(`maxLength (${maxLength}) must be at least the ellipsis length (${ellipsis.length})`);\n }\n return `${sliceWithoutSplittingSurrogates(text, maxLength - ellipsis.length)}${ellipsis}`;\n}\n\n/**\n * Returns the first `end` code units of `text`, one fewer when the last kept unit is a high surrogate, whose low half\n * is either cut off or absent from `text`, because an unpaired surrogate is replaced with U+FFFD by encoders.\n */\nexport function sliceWithoutSplittingSurrogates(text: string, end: number): string {\n // oxlint-disable-next-line unicorn/prefer-code-point -- the check is on a single UTF-16 code unit\n const lastKeptUnit = text.charCodeAt(end - 1);\n return text.slice(0, lastKeptUnit >= 0xd8_00 && lastKeptUnit <= 0xdb_ff ? end - 1 : end);\n}\n\nconst htmlEntities: Readonly<Record<string, string>> = {\n '\"': '&quot;',\n '&': '&amp;',\n \"'\": '&#39;',\n '<': '&lt;',\n '>': '&gt;',\n};\n\nconst htmlSpecialCharactersPattern = /[\"&'<>]/gu;\n\n/** Escapes `&`, `<`, `>`, `\"`, and `'`, so the result is safe in HTML text and in quoted attribute values. */\nexport function escapeHtml(text: string): string {\n // Measured in V8 and JavaScriptCore: a native `search` first is faster than `replaceAll` alone for text without these\n // characters, and one native `replaceAll` beats a per-code-unit loop for text where they are rare.\n return text.search(htmlSpecialCharactersPattern) === -1\n ? text\n : text.replaceAll(htmlSpecialCharactersPattern, (character) => htmlEntities[character] as string);\n}\n"],"mappings":"AACA,SAAgBA,EAAa,EAAsB,CACjD,OAAOC,EAAK,WAAW,uBAAwB,OAAO,GAAG,KAAK,CAChE,CAGA,SAAgBC,EAAc,EAAuB,CACnD,MAAO,cAAc,KAAKC,CAAK,EAAIA,EAAQ,IAAIA,EAAM,WAAW,IAAK,OAAO,GAAG,MAAM,EAAE,EACzF,CAMA,SAAgBC,EAAiB,EAAc,EAAW,GAAY,CACpE,OAAOC,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAMA,SAAgBC,EAAY,EAAc,EAAW,GAAY,CAC/D,OAAOF,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAGA,SAAgBE,EAAc,EAAsB,CAClD,OAAOC,EAAS,IAAKR,CAAI,CAC3B,CAEA,SAASI,EAAkB,EAAmB,EAAc,EAA0B,CACpF,IAAIK,EAAgB,EACpB,KAAOJ,EAASI,KAAmBC,GAAW,IAC9C,IAAMC,EAAQH,EAASE,EAAWV,CAAI,EAAIU,EAAU,OAAOD,CAAa,EACxE,MAAO,GAAGE,IAAQN,EAAS,MAAMI,CAAa,EAAE,IAAIT,IAAOA,EAAK,SAAS;CAAI,EAAI,GAAK;IAAOW,GAC/F,CAEA,SAASH,EAAS,EAAmB,EAAsB,CACzD,IAAMI,EAAgBF,EAAU,OAAO,CAAC,EACpCG,EAAa,EACbC,EAAQd,EAAK,QAAQY,CAAa,EACtC,KAAOE,IAAU,IAAI,CACnB,IAAIC,EAAMD,EAAQ,EAClB,KAAOd,EAAKe,KAASL,GAAW,IAChC,EAAa,KAAK,IAAIG,EAAYE,EAAMD,CAAK,EAC7C,EAAQd,EAAK,QAAQY,EAAeG,CAAG,CACzC,CACA,OAAOL,EAAU,OAAOG,EAAa,CAAC,CACxC,CAMA,SAAgBG,EAAS,EAAc,EAAmB,EAAW,IAAa,CAChF,GAAIhB,EAAK,QAAUiB,EAAW,OAAOjB,EACrC,GAAIiB,EAAYC,EAAS,OACvB,MAAU,WAAW,cAAcD,EAAU,0CAA0CC,EAAS,OAAO,EAAE,EAE3G,MAAO,GAAGC,EAAgCnB,EAAMiB,EAAYC,EAAS,MAAM,IAAIA,GACjF,CAMA,SAAgBC,EAAgC,EAAc,EAAqB,CAEjF,IAAMC,EAAepB,EAAK,WAAWe,EAAM,CAAC,EAC5C,OAAOf,EAAK,MAAM,EAAGoB,GAAgB,OAAWA,GAAgB,MAAUL,EAAM,EAAIA,CAAG,CACzF,CAEA,MAAMM,EAAiD,CACrD,IAAK,SACL,IAAK,QACL,IAAK,QACL,IAAK,OACL,IAAK,MACP,EAEMC,EAA+B,YAGrC,SAAgBC,EAAW,EAAsB,CAG/C,OAAOvB,EAAK,OAAOsB,CAA4B,IAAM,GACjDtB,EACAA,EAAK,WAAWsB,EAA+B,GAAcD,EAAaG,EAAoB,CACpG,C"}
1
+ {"version":3,"file":"text.js","names":["escapeRegExp","text","quoteForShell","value","toTildeCodeBlock","toFencedCodeBlock","language","toCodeBlock","getTildeFence","getFence","languageStart","fenceChar","fence","shortestFence","longestRun","start","end","unwrapFencedReply","countLeading","lines","unwrapTildeWrapper","unwrapBacktickWrapper","line","minLength","openingLength","index","isFenceOnly","opening","length","isMarkdownInfo","inner","trimmed","info","wordEnd","word","count","char","truncate","maxLength","ellipsis","sliceWithoutSplittingSurrogates","lastKeptUnit","htmlEntities","htmlSpecialCharactersPattern","escapeHtml","character"],"sources":["../src/text.ts"],"sourcesContent":["/** Escapes every RegExp metacharacter so the text matches itself literally. */\nexport function escapeRegExp(text: string): string {\n return text.replaceAll(/[$()*+.?[\\\\\\]^{|}]/gu, String.raw`\\$&`);\n}\n\n/** A POSIX shell word: safe characters pass through, anything else is single-quoted. */\nexport function quoteForShell(value: string): string {\n return /^[\\w./-]+$/u.test(value) ? value : `'${value.replaceAll(\"'\", String.raw`'\\''`)}'`;\n}\n\n/**\n * Wraps text in a Markdown code block fenced with tildes, which are rarer than backticks in Markdown content.\n * The fence is longer than any run of tildes in the text so that the text cannot close it.\n */\nexport function toTildeCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('~', text, language);\n}\n\n/**\n * Wraps text in a Markdown code block fenced with backticks.\n * The fence is longer than any run of backticks in the text so that the text cannot close it.\n */\nexport function toCodeBlock(text: string, language = ''): string {\n return toFencedCodeBlock('`', text, language);\n}\n\n/** Returns the tilde fence `toTildeCodeBlock` wraps `text` in: at least `~~~`, longer than any run of tildes in it. */\nexport function getTildeFence(text: string): string {\n return getFence('~', text);\n}\n\nfunction toFencedCodeBlock(fenceChar: string, text: string, language: string): string {\n let languageStart = 0;\n while (language[languageStart] === fenceChar) languageStart++;\n const fence = getFence(fenceChar, text) + fenceChar.repeat(languageStart);\n return `${fence}${language.slice(languageStart)}\\n${text}${text.endsWith('\\n') ? '' : '\\n'}${fence}`;\n}\n\nfunction getFence(fenceChar: string, text: string): string {\n const shortestFence = fenceChar.repeat(3);\n let longestRun = 2;\n let start = text.indexOf(shortestFence);\n while (start !== -1) {\n let end = start + 3;\n while (text[end] === fenceChar) end++;\n longestRun = Math.max(longestRun, end - start);\n start = text.indexOf(shortestFence, end);\n }\n return fenceChar.repeat(longestRun + 1);\n}\n\n/**\n * Returns the document an LLM replied with inside the wrapper fence it was asked to keep, or `undefined` when the reply\n * is not wrapped as required so that the caller can retry. `fence` is the tilde fence the document was sent in, e.g.\n * `getTildeFence(document)`; this relies on it being longer than any run of tildes in the document.\n * A leading BOM is removed, CRLF is read as LF, and trailing whitespace is ignored on fence lines. Fences start at the\n * beginning of a line. The result is the lines strictly between the wrapper's fence lines joined with LF, untrimmed,\n * so a final newline of the document is not kept.\n * - Tilde wrapper: the first line starting with at least `fence.length` tildes, followed by any info string, opens it,\n * and the last later line of only tildes, at least as many as the opening run, closes it. Text around it, backtick\n * fence lines included, is dropped.\n * - Otherwise, backtick wrapper: the first nonblank line is a run of at least three backticks with no info string or\n * one whose first word is `md` or `markdown` (case-insensitively), the last nonblank line is only backticks, at least\n * as many, and no line between them starts with that many backticks.\n */\nexport function unwrapFencedReply(text: string, fence: string): string | undefined {\n if (fence.length < 3 || countLeading(fence, '~') !== fence.length) {\n throw new RangeError(`fence must be a run of at least three tildes: ${fence}`);\n }\n const lines = (text.startsWith('\\uFEFF') ? text.slice(1) : text).replaceAll('\\r\\n', '\\n').split('\\n');\n return unwrapTildeWrapper(lines, fence.length) ?? unwrapBacktickWrapper(lines);\n}\n\nfunction unwrapTildeWrapper(lines: readonly string[], minLength: number): string | undefined {\n const start = lines.findIndex((line) => countLeading(line, '~') >= minLength);\n if (start === -1) return;\n const openingLength = countLeading(lines[start] ?? '', '~');\n const end = lines.findLastIndex((line, index) => index > start && isFenceOnly(line, '~', openingLength));\n if (end === -1) return;\n return lines.slice(start + 1, end).join('\\n');\n}\n\nfunction unwrapBacktickWrapper(lines: readonly string[]): string | undefined {\n const start = lines.findIndex((line) => line.trim() !== '');\n const end = lines.findLastIndex((line) => line.trim() !== '');\n if (start === end) return;\n const opening = lines[start] ?? '';\n const length = countLeading(opening, '`');\n if (length < 3 || !isMarkdownInfo(opening.slice(length)) || !isFenceOnly(lines[end] ?? '', '`', length)) return;\n const inner = lines.slice(start + 1, end);\n if (inner.some((line) => countLeading(line, '`') >= length)) return;\n return inner.join('\\n');\n}\n\nfunction isMarkdownInfo(info: string): boolean {\n const trimmed = info.trim();\n let wordEnd = 0;\n while (wordEnd < trimmed.length && trimmed[wordEnd] !== ' ' && trimmed[wordEnd] !== '\\t') wordEnd++;\n const word = trimmed.slice(0, wordEnd).toLowerCase();\n return word === '' || word === 'md' || word === 'markdown';\n}\n\nfunction isFenceOnly(line: string, fenceChar: string, minLength: number): boolean {\n const trimmed = line.trimEnd();\n return trimmed.length >= minLength && countLeading(trimmed, fenceChar) === trimmed.length;\n}\n\nfunction countLeading(line: string, char: string): number {\n let count = 0;\n while (line[count] === char) count++;\n return count;\n}\n\n/**\n * Shortens text to at most `maxLength` UTF-16 code units, including the `ellipsis` that marks the cut.\n * Throws a `RangeError` when `maxLength` cannot hold the ellipsis.\n */\nexport function truncate(text: string, maxLength: number, ellipsis = '…'): string {\n if (text.length <= maxLength) return text;\n if (maxLength < ellipsis.length) {\n throw new RangeError(`maxLength (${maxLength}) must be at least the ellipsis length (${ellipsis.length})`);\n }\n return `${sliceWithoutSplittingSurrogates(text, maxLength - ellipsis.length)}${ellipsis}`;\n}\n\n/**\n * Returns the first `end` code units of `text`, one fewer when the last kept unit is a high surrogate, whose low half\n * is either cut off or absent from `text`, because an unpaired surrogate is replaced with U+FFFD by encoders.\n */\nexport function sliceWithoutSplittingSurrogates(text: string, end: number): string {\n // oxlint-disable-next-line unicorn/prefer-code-point -- the check is on a single UTF-16 code unit\n const lastKeptUnit = text.charCodeAt(end - 1);\n return text.slice(0, lastKeptUnit >= 0xd8_00 && lastKeptUnit <= 0xdb_ff ? end - 1 : end);\n}\n\nconst htmlEntities: Readonly<Record<string, string>> = {\n '\"': '&quot;',\n '&': '&amp;',\n \"'\": '&#39;',\n '<': '&lt;',\n '>': '&gt;',\n};\n\nconst htmlSpecialCharactersPattern = /[\"&'<>]/gu;\n\n/** Escapes `&`, `<`, `>`, `\"`, and `'`, so the result is safe in HTML text and in quoted attribute values. */\nexport function escapeHtml(text: string): string {\n // Measured in V8 and JavaScriptCore: a native `search` first is faster than `replaceAll` alone for text without these\n // characters, and one native `replaceAll` beats a per-code-unit loop for text where they are rare.\n return text.search(htmlSpecialCharactersPattern) === -1\n ? text\n : text.replaceAll(htmlSpecialCharactersPattern, (character) => htmlEntities[character] as string);\n}\n"],"mappings":"AACA,SAAgBA,EAAa,EAAsB,CACjD,OAAOC,EAAK,WAAW,uBAAwB,OAAO,GAAG,KAAK,CAChE,CAGA,SAAgBC,EAAc,EAAuB,CACnD,MAAO,cAAc,KAAKC,CAAK,EAAIA,EAAQ,IAAIA,EAAM,WAAW,IAAK,OAAO,GAAG,MAAM,EAAE,EACzF,CAMA,SAAgBC,EAAiB,EAAc,EAAW,GAAY,CACpE,OAAOC,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAMA,SAAgBC,EAAY,EAAc,EAAW,GAAY,CAC/D,OAAOF,EAAkB,IAAKJ,EAAMK,CAAQ,CAC9C,CAGA,SAAgBE,EAAc,EAAsB,CAClD,OAAOC,EAAS,IAAKR,CAAI,CAC3B,CAEA,SAASI,EAAkB,EAAmB,EAAc,EAA0B,CACpF,IAAIK,EAAgB,EACpB,KAAOJ,EAASI,KAAmBC,GAAW,IAC9C,IAAMC,EAAQH,EAASE,EAAWV,CAAI,EAAIU,EAAU,OAAOD,CAAa,EACxE,MAAO,GAAGE,IAAQN,EAAS,MAAMI,CAAa,EAAE,IAAIT,IAAOA,EAAK,SAAS;CAAI,EAAI,GAAK;IAAOW,GAC/F,CAEA,SAASH,EAAS,EAAmB,EAAsB,CACzD,IAAMI,EAAgBF,EAAU,OAAO,CAAC,EACpCG,EAAa,EACbC,EAAQd,EAAK,QAAQY,CAAa,EACtC,KAAOE,IAAU,IAAI,CACnB,IAAIC,EAAMD,EAAQ,EAClB,KAAOd,EAAKe,KAASL,GAAW,IAChC,EAAa,KAAK,IAAIG,EAAYE,EAAMD,CAAK,EAC7C,EAAQd,EAAK,QAAQY,EAAeG,CAAG,CACzC,CACA,OAAOL,EAAU,OAAOG,EAAa,CAAC,CACxC,CAgBA,SAAgBG,EAAkB,EAAc,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 "version": "11.0.0",
4
4
  "description": "Shared library for WillBooster projects",
5
5
  "license": "Apache-2.0",
6
6
  "author": "WillBooster Inc.",
@@ -53,7 +53,7 @@
53
53
  "@types/bun": "1.4.3",
54
54
  "@types/node": "26.6.5",
55
55
  "@willbooster/oxlint-config": "1.4.9",
56
- "@willbooster/wb": "22.32.2",
56
+ "@willbooster/wb": "22.32.3",
57
57
  "build-ts": "21.0.29",
58
58
  "oxlint": "1.88.0",
59
59
  "oxlint-tsgolint": "7.0.2004",