diff --git a/openspace/skill_engine/skill_utils.py b/openspace/skill_engine/skill_utils.py index 044628a..be6f6a5 100644 --- a/openspace/skill_engine/skill_utils.py +++ b/openspace/skill_engine/skill_utils.py @@ -66,23 +66,14 @@ def _yaml_quote(value: str) -> str: def _format_frontmatter_value(value: Any) -> str: """Serialize a frontmatter value for re-emit after PyYAML parse.""" - if isinstance(value, bool): - return "true" if value else "false" - if value is None: - return "null" - if isinstance(value, (int, float)): - return str(value) if isinstance(value, str): return _yaml_quote(value) - try: - import yaml + import yaml - dumped = yaml.safe_dump( - value, default_flow_style=True, allow_unicode=True - ).strip() - return dumped - except Exception: - return _yaml_quote(str(value)) + # First line only: scalar dumps include a trailing '...' document end. + return yaml.safe_dump( + value, default_flow_style=True, allow_unicode=True + ).splitlines()[0] def _yaml_unquote(value: str) -> str: @@ -185,9 +176,9 @@ def normalize_frontmatter(content: str) -> str: """Re-serialize frontmatter with proper YAML quoting. Parses the existing frontmatter, then re-writes each value through - :func:`_yaml_quote` so that colons, hashes, and other special - characters are safely double-quoted. The body after ``---`` is - preserved verbatim. + :func:`_format_frontmatter_value` so scalars (bool/int/date) and + strings with special characters re-emit safely. The body after + ``---`` is preserved verbatim. Returns *content* unchanged if no frontmatter is found. """ diff --git a/tests/skill_engine/test_normalize_frontmatter.py b/tests/skill_engine/test_normalize_frontmatter.py index 128d86c..eb73f4e 100644 --- a/tests/skill_engine/test_normalize_frontmatter.py +++ b/tests/skill_engine/test_normalize_frontmatter.py @@ -11,6 +11,15 @@ def test_normalize_frontmatter_bool_and_int_scalars() -> None: assert "enabled: true" in out assert "version: 2" in out assert "# body" in out + assert "..." not in out + + +def test_normalize_frontmatter_date_scalar_no_document_end() -> None: + raw = "---\nname: demo\ncreated: 2024-01-01\nenabled: true\n---\n" + out = normalize_frontmatter(raw) + assert "created: 2024-01-01" in out + assert "..." not in out + assert "enabled: true" in out def test_normalize_frontmatter_string_still_quoted_when_needed() -> None: