2323from __future__ import annotations
2424
2525import json
26+ import re
2627from collections import defaultdict
2728from datetime import UTC , datetime
29+ from html import escape as escape_html
2830from io import StringIO
2931
3032from skillspector import __version__ as _skillspector_version
33+ from skillspector .nodes .report import _clean_text
3134
3235
3336def sorted_results (results : list [dict [str , object ]]) -> list [dict [str , object ]]:
@@ -373,6 +376,29 @@ def _format_json(results: list[dict[str, object]]) -> str:
373376# ═══════════════════════════════════════════════════════════════════
374377
375378
379+ def _markdown_plain_text (value : object ) -> str :
380+ """Remove display controls and keep a Markdown field on one line."""
381+ text = _clean_text (str (value )) or ""
382+ text = "" .join (c for c in text if c .isprintable () or c .isspace ())
383+ return " " .join (text .splitlines ())
384+
385+
386+ def _markdown_text (value : object ) -> str :
387+ """Keep scan-derived prose within its own Markdown line."""
388+ text = escape_html (_markdown_plain_text (value ), quote = False )
389+ return re .sub (r"([\\`*_{}\[\]()#+.!|>~-])" , r"\\\1" , text )
390+
391+
392+ def _markdown_code (value : object , * , table_cell : bool = False ) -> str :
393+ """Quote literal text without allowing code-span or table-cell breakouts."""
394+ text = _markdown_plain_text (value )
395+ if table_cell :
396+ text = text .replace ("|" , "\\ |" )
397+ delimiter = "`" * (max ((len (run ) for run in re .findall (r"`+" , text )), default = 0 ) + 1 )
398+ padding = " " if text .startswith (("`" , " " )) or text .endswith (("`" , " " )) else ""
399+ return f"{ delimiter } { padding } { text } { padding } { delimiter } "
400+
401+
376402def _format_markdown (results : list [dict [str , object ]]) -> str :
377403 lines : list [str ] = []
378404 total = len (results )
@@ -438,9 +464,12 @@ def _format_markdown(results: list[dict[str, object]]) -> str:
438464 lang = skill .get ("language" , "en" )
439465
440466 if r .get ("error" ):
441- lines .append (f"| ` { name } ` | ERR | ERROR | — | { lang } |" )
467+ lines .append (f"| { _markdown_code ( name , table_cell = True ) } | ERR | ERROR | — | { _markdown_text ( lang ) } |" )
442468 else :
443- lines .append (f"| `{ name } ` | { score } /100 | { sev } | { issues } | { lang } |" )
469+ lines .append (
470+ f"| { _markdown_code (name , table_cell = True )} | { score } /100 | { _markdown_text (sev )} | "
471+ f"{ issues } | { _markdown_text (lang )} |"
472+ )
444473 lines .append ("" )
445474
446475 # ── Issue details for HIGH / CRITICAL ────────────────────────
@@ -458,8 +487,8 @@ def _format_markdown(results: list[dict[str, object]]) -> str:
458487 risk = r .get ("risk_assessment" , {})
459488 name = skill .get ("name" , "?" )
460489 lines .append (
461- f"### { name } — { risk .get ('score' , 0 )} /100 "
462- f"{ risk .get ('severity' , 'HIGH' )} \n "
490+ f"### { _markdown_text ( name ) } — { risk .get ('score' , 0 )} /100 "
491+ f"{ _markdown_text ( risk .get ('severity' , 'HIGH' ) )} \n "
463492 )
464493 for issue in r .get ("issues" , []):
465494 sev = str (issue .get ("severity" , "LOW" )).upper ()
@@ -469,26 +498,27 @@ def _format_markdown(results: list[dict[str, object]]) -> str:
469498 loc_file = loc .get ("file" , "" ) if isinstance (loc , dict ) else ""
470499 rule_id = issue .get ("id" , "?" )
471500 explanation = issue .get ("explanation" , issue .get ("message" , "" ))
472- lines .append (f"- **{ emoji } { rule_id } **: { explanation } " )
501+ lines .append (f"- **{ emoji } { _markdown_text ( rule_id ) } **: { _markdown_text ( explanation ) } " )
473502 if loc_file :
474- lines .append (f" - Location: ` { loc_file } :{ loc_start } ` " )
503+ lines .append (f" - Location: { _markdown_code ( f' { loc_file } :{ loc_start } ' ) } " )
475504 conf = issue .get ("confidence" , 0 )
476505 lines .append (f" - Confidence: { float (conf ):.0%} " )
477506 rem = issue .get ("remediation" )
478507 if rem :
479- lines .append (f" - Remediation: { rem } " )
508+ lines .append (f" - Remediation: { _markdown_text ( rem ) } " )
480509 lines .append ("" )
481510 lines .append ("" )
482511
483512 exception_groups = _exception_groups (results )
484513 if exception_groups :
485514 lines .append ("## Ledger Exceptions\n " )
486515 for skill_name , exceptions in exception_groups :
487- lines .append (f"### { skill_name } \n " )
516+ lines .append (f"### { _markdown_text ( skill_name ) } \n " )
488517 for exception in exceptions :
489518 lines .append (
490- f"- **{ exception .get ('reason_code' , 'unknown' )} ** "
491- f"`{ exception .get ('path' , '' )} `: { exception .get ('message' , '' )} "
519+ f"- **{ _markdown_text (exception .get ('reason_code' , 'unknown' ))} ** "
520+ f"{ _markdown_code (exception .get ('path' , '' ))} : "
521+ f"{ _markdown_text (exception .get ('message' , '' ))} "
492522 )
493523 lines .append ("" )
494524
0 commit comments