@@ -532,23 +532,83 @@ def command_filename(self, template_name: str) -> str:
532532 def _extract_description (content : str ) -> str :
533533 """Extract the ``description`` value from YAML frontmatter.
534534
535- Scans lines between the first pair of ``---`` delimiters for a
536- top-level ``description:`` key. Returns the value (with
537- surrounding quotes stripped) or an empty string if not found .
535+ Parses the YAML frontmatter so block scalar descriptions (``|``
536+ and ``>``) keep their YAML semantics instead of being treated as
537+ raw text .
538538 """
539- in_frontmatter = False
540- for line in content .splitlines ():
541- stripped = line .rstrip ("\n \r " )
542- if stripped == "---" :
543- if not in_frontmatter :
544- in_frontmatter = True
545- continue
546- break # second ---
547- if in_frontmatter and stripped .startswith ("description:" ):
548- _ , _ , value = stripped .partition (":" )
549- return value .strip ().strip ('"' ).strip ("'" )
539+ import yaml
540+
541+ frontmatter_text , _ = TomlIntegration ._split_frontmatter (content )
542+ if not frontmatter_text :
543+ return ""
544+ try :
545+ frontmatter = yaml .safe_load (frontmatter_text ) or {}
546+ except yaml .YAMLError :
547+ return ""
548+
549+ if not isinstance (frontmatter , dict ):
550+ return ""
551+
552+ description = frontmatter .get ("description" , "" )
553+ if isinstance (description , str ):
554+ return description
550555 return ""
551556
557+ @staticmethod
558+ def _split_frontmatter (content : str ) -> tuple [str , str ]:
559+ """Split YAML frontmatter from the remaining content.
560+
561+ Returns ``("", content)`` when no complete frontmatter block is
562+ present. The body is preserved exactly as written so prompt text
563+ keeps its intended formatting.
564+ """
565+ if not content .startswith ("---" ):
566+ return "" , content
567+
568+ lines = content .splitlines (keepends = True )
569+ if not lines or lines [0 ].rstrip ("\r \n " ) != "---" :
570+ return "" , content
571+
572+ frontmatter_end = - 1
573+ for i , line in enumerate (lines [1 :], start = 1 ):
574+ if line .rstrip ("\r \n " ) == "---" :
575+ frontmatter_end = i
576+ break
577+
578+ if frontmatter_end == - 1 :
579+ return "" , content
580+
581+ frontmatter = "" .join (lines [1 :frontmatter_end ])
582+ body = "" .join (lines [frontmatter_end + 1 :])
583+ return frontmatter , body
584+
585+ @staticmethod
586+ def _render_toml_string (value : str ) -> str :
587+ """Render *value* as a TOML string literal.
588+
589+ Uses a basic string for single-line values, multiline basic
590+ strings for values containing newlines, and falls back to a
591+ literal string or escaped basic string when delimiters appear in
592+ the content.
593+ """
594+ if "\n " not in value and "\r " not in value :
595+ escaped = value .replace ("\\ " , "\\ \\ " ).replace ('"' , '\\ "' )
596+ return f'"{ escaped } "'
597+
598+ escaped = value .replace ("\\ " , "\\ \\ " )
599+ if '"""' not in escaped :
600+ return '"""\n ' + escaped + '"""'
601+ if "'''" not in value :
602+ return "'''\n " + value + "'''"
603+
604+ return '"' + (
605+ value .replace ("\\ " , "\\ \\ " )
606+ .replace ('"' , '\\ "' )
607+ .replace ("\n " , "\\ n" )
608+ .replace ("\r " , "\\ r" )
609+ .replace ("\t " , "\\ t" )
610+ ) + '"'
611+
552612 @staticmethod
553613 def _render_toml (description : str , body : str ) -> str :
554614 """Render a TOML command file from description and body.
@@ -558,39 +618,19 @@ def _render_toml(description: str, body: str) -> str:
558618 to multiline literal strings (``'''``) if the body contains
559619 ``\" \" \" ``, then to an escaped basic string as a last resort.
560620
561- The body is rstrip'd so the closing delimiter appears on the line
562- immediately after the last content line — matching the release
563- script's ``echo "$body"; echo '\" \" \" '`` pattern.
621+ The body is ``rstrip("\\ n")``'d before rendering, so the TOML
622+ value preserves content without forcing a trailing newline. As a
623+ result, multiline delimiters appear on their own line only when
624+ the rendered value itself ends with a newline.
564625 """
565626 toml_lines : list [str ] = []
566627
567628 if description :
568- desc = description .replace ('"' , '\\ "' )
569- toml_lines .append (f'description = "{ desc } "' )
629+ toml_lines .append (f"description = { TomlIntegration ._render_toml_string (description )} " )
570630 toml_lines .append ("" )
571631
572632 body = body .rstrip ("\n " )
573-
574- # Escape backslashes for basic multiline strings.
575- escaped = body .replace ("\\ " , "\\ \\ " )
576-
577- if '"""' not in escaped :
578- toml_lines .append ('prompt = """' )
579- toml_lines .append (escaped )
580- toml_lines .append ('"""' )
581- elif "'''" not in body :
582- toml_lines .append ("prompt = '''" )
583- toml_lines .append (body )
584- toml_lines .append ("'''" )
585- else :
586- escaped_body = (
587- body .replace ("\\ " , "\\ \\ " )
588- .replace ('"' , '\\ "' )
589- .replace ("\n " , "\\ n" )
590- .replace ("\r " , "\\ r" )
591- .replace ("\t " , "\\ t" )
592- )
593- toml_lines .append (f'prompt = "{ escaped_body } "' )
633+ toml_lines .append (f"prompt = { TomlIntegration ._render_toml_string (body )} " )
594634
595635 return "\n " .join (toml_lines ) + "\n "
596636
@@ -630,7 +670,8 @@ def setup(
630670 raw = src_file .read_text (encoding = "utf-8" )
631671 description = self ._extract_description (raw )
632672 processed = self .process_template (raw , self .key , script_type , arg_placeholder )
633- toml_content = self ._render_toml (description , processed )
673+ _ , body = self ._split_frontmatter (processed )
674+ toml_content = self ._render_toml (description , body )
634675 dst_name = self .command_filename (src_file .stem )
635676 dst_file = self .write_file_and_record (
636677 toml_content , dest / dst_name , project_root , manifest
0 commit comments