1+ import type { Paragraph , PhrasingContent , Root , RootContent } from "mdast" ;
12import type { Processor } from "unified" ;
23import { isWindowsAbsolutePath } from "./path.ts" ;
34import type { PluggableList } from "unified" ;
@@ -167,6 +168,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
167168 remarkGithubAlerts ,
168169 remarkNormalizeListItemIndentation ,
169170 remarkCodexDirectives ,
171+ remarkBreakAroundImageLines ,
170172 remarkPreserveCodeMeta ,
171173 remarkNormalizeLinksAndTagInlineCode ,
172174] ;
@@ -177,6 +179,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [
177179 remarkGithubAlerts ,
178180 remarkNormalizeListItemIndentation ,
179181 remarkCodexDirectives ,
182+ remarkBreakAroundImageLines ,
180183 remarkBreaks ,
181184 remarkPreserveCodeMeta ,
182185 remarkNormalizeLinksAndTagInlineCode ,
@@ -189,6 +192,61 @@ export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
189192 [ rehypeSanitize , CHAT_MARKDOWN_SANITIZE_SCHEMA ] ,
190193] ;
191194
195+ const isImageNode = ( node : PhrasingContent | undefined ) : boolean =>
196+ node ?. type === "image" ||
197+ node ?. type === "imageReference" ||
198+ ( ( node ?. type === "link" || node ?. type === "linkReference" ) &&
199+ node . children . length === 1 &&
200+ isImageNode ( node . children [ 0 ] ) ) ;
201+ const endsLine = ( node : PhrasingContent | undefined ) =>
202+ node === undefined ||
203+ node . type === "break" ||
204+ ( node . type === "text" && / \n [ \t ] * $ / . test ( node . value ) ) ;
205+ const startsLine = ( node : PhrasingContent | undefined ) =>
206+ node === undefined ||
207+ node . type === "break" ||
208+ ( node . type === "text" && / ^ [ \t ] * \n / . test ( node . value ) ) ;
209+
210+ function breakLinesAroundImages ( paragraph : Paragraph ) {
211+ const children = paragraph . children ;
212+ const ownsLine = ( index : number ) =>
213+ isImageNode ( children [ index ] ) &&
214+ endsLine ( children [ index - 1 ] ) &&
215+ startsLine ( children [ index + 1 ] ) ;
216+ paragraph . children = children . flatMap ( ( child , index ) : PhrasingContent [ ] => {
217+ if ( child . type !== "text" ) return [ child ] ;
218+ const afterImage = ownsLine ( index - 1 ) ;
219+ const beforeImage = ownsLine ( index + 1 ) ;
220+ if ( ! afterImage && ! beforeImage ) return [ child ] ;
221+ const parts : PhrasingContent [ ] = [ ] ;
222+ let value = child . value ;
223+ if ( afterImage ) {
224+ value = value . replace ( / ^ [ \t ] * \n / , "" ) ;
225+ parts . push ( { type : "break" } ) ;
226+ }
227+ const breakAfter = beforeImage && / \n [ \t ] * $ / . test ( value ) ;
228+ if ( breakAfter ) value = value . replace ( / [ \t ] * \n [ \t ] * $ / , "" ) ;
229+ if ( value ) parts . push ( { type : "text" , value } ) ;
230+ if ( breakAfter ) parts . push ( { type : "break" } ) ;
231+ return parts ;
232+ } ) ;
233+ }
234+
235+ /**
236+ * Agents label screenshots with a line like `**Palette**` right above the image. CommonMark
237+ * joins those lines into one paragraph, which puts the image inline beside its label. An
238+ * image alone on its source line keeps that line; prose and inline badges still wrap.
239+ */
240+ function remarkBreakAroundImageLines ( ) {
241+ return ( tree : Root ) => {
242+ const visit = ( node : Root | RootContent ) => {
243+ if ( node . type === "paragraph" ) breakLinesAroundImages ( node ) ;
244+ else if ( "children" in node ) node . children . forEach ( visit ) ;
245+ } ;
246+ visit ( tree ) ;
247+ } ;
248+ }
249+
192250type MarkdownAstNode = {
193251 type ?: string ;
194252 meta ?: unknown ;
0 commit comments