Below is a single, self-contained Python function that merges the best ideas from all four solutions. It carefully merges continuation lines, detects repeated separator patterns, preserves valid tables unchanged, normalizes column counts, respects leading/trailing pipe style, and performs only minimal fixes if needed: ```python import re def fix_invalid_markdown_tables(markdown_text): def merge_continuation_lines(lines): merged = [] cur = None for ln in lines: if '|' in ln: if cur is not None: merged.append(cur) cur = ln elif ln.strip(): if cur is not None: cur += " " + ln.strip() else: cur = ln else: if cur is not None: merged.append(cur) cur = None merged.append(ln) if cur is not None: merged.append(cur) return merged def parse_row(line): s = line.strip() has_lead = s.startswith('|') has_tail = s.endswith('|') if has_lead: s = s[1:] if has_tail: s = s[:-1] return [c.strip() for c in s.split('|')] def is_separator_cell(cell): return re.fullmatch(r':?-{3,}:?', cell.strip()) is not None def is_separator_row(cells): return all(is_separator_cell(c) for c in cells if c.strip()) def is_valid_table(lines): if len(lines) < 2: return False rows = [parse_row(ln) for ln in lines] if not is_separator_row(rows[1]): return False n = len(rows[0]) return all(len(r) == n for r in rows) def detect_repeated_separators(rows): # Returns True if more than half of the odd rows are separator lines if len(rows) < 3: return False odd_sep_count = sum(is_separator_row(rows[i]) for i in range(1, len(rows), 2)) return odd_sep_count >= (len(rows) - 1)//2 def rebuild_row(cells, style): s = " | ".join(cells) if style[0]: s = "| " + s if style[1]: s += " |" return s def fix_table_block(block): merged = merge_continuation_lines(block) if len(merged) < 2: return block if is_valid_table(merged): return block rows_parsed = [parse_row(ln) for ln in merged] style = (merged[0].strip().startswith('|'), merged[0].strip().endswith('|')) max_cols = max(len(r) for r in rows_parsed) sep_flags = [is_separator_row(r) for r in rows_parsed] repeated_sep = detect_repeated_separators(rows_parsed) def normalize(r): if len(r) < max_cols: return r + ['']*(max_cols - len(r)) elif len(r) > max_cols: return r[:max_cols-1] + [' '.join(r[max_cols-1:])] else: return r rows_parsed = [normalize(r) for r in rows_parsed] fixed = [rows_parsed[0]] fixed.append(["---"]*max_cols) if repeated_sep: i = 1 while i < len(rows_parsed): if sep_flags[i]: i += 1 continue fixed.append(rows_parsed[i]) fixed.append(["---"]*max_cols) i += 1 else: for i in range(2, len(rows_parsed)): if not sep_flags[i]: fixed.append(rows_parsed[i]) return [rebuild_row(r, style) for r in fixed] lines = markdown_text.splitlines() out = [] block = [] in_table = False for line in lines: if '|' in line: block.append(line) in_table = True else: if in_table: out.extend(fix_table_block(block)) block = [] in_table = False out.append(line) if block: out.extend(fix_table_block(block)) return "\n".join(out) ```