Fix #84: AI markdownifyer plugin corrupts valid Markdown structures #123
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "typosaurus/ticket-84"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Resolves #84.
Plan
Implementation Plan: Protect Markdown Structure During AI Correction
Objective
Prevent the AI correction pipeline from corrupting valid Markdown structures (fenced code blocks, inline code, headings, lists, tables) by extracting code blocks before sending content to the LLM and reinserting them afterward.
Changes Required
1. Add Markdown structure preservation utility
File:
devplacepy/services/markdown_preserve.py(new)MarkdownPreserverclass with:extract_blocks(text: str) -> tuple[str, list[str]]: Scans text for fenced code blocks (triple backticks, possibly with language identifier) and inline code spans (single backticks). Replaces each with a unique placeholder token (e.g.,{%CODE_BLOCK_1%}). Returns modified text and list of original blocks.restore_blocks(text: str, blocks: list[str]) -> str: Replaces placeholders with original blocks.re.finditer(r'```(?:\w+)?\n.*?```', text, re.DOTALL)and for inline codere.finditer(r'[^\n]+')`. Ensure placeholders do not conflict with existing content.2. Modify correction pipeline to use preserver
File:
devplacepy/services/correction.pyMarkdownPreserverfrom new module.correct_text(line ~123-140), before constructing prompt and callinggateway_complete:extract_blocks(text)to get preserved blocks and sanitized text.contentwith sanitized text).restore_blocks(ai_response, blocks)to reinsert preserved blocks._run_correction(line ~187-216), theresultfromcorrect_textwill now contain restored blocks; no further changes needed.File:
devplacepy/services/ai_modifier.pymodify_text(line ~30-41) and_run_modification(line ~76-109), since it uses the same pattern.3. Add structural validation check
File:
devplacepy/services/correction.py(after restoration) and/ordevplacepy/services/ai_modifier.py```differs), log warning and possibly fall back to original? If the AI corrupted the structure despite extraction, the restoration will reinsert the original blocks, ensuring no corruption. The extraction already removes code blocks, so the AI cannot corrupt them. The validation is secondary.4. Update existing tests and add new tests
File:
tests/unit/services/correction.py(existing)test_correction_preserves_code_blocks:python\nprint('hello')\n\nmore text").gateway_completeto return a modified version that might corrupt code (e.g., "some text\nmodified\nmore text").File:
tests/unit/services/ai_modifier.py(existing)modify_textto ensure preservation works.File:
tests/unit/services/markdown_preserve.py(new)extract_blocksandrestore_blocks:5. Documentation update
File:
templates/docs/ai-modifier.html(line 35-36)File:
devplacepy/services/CLAUDE.md(line 21-27)6. Lint and code style
ruff checkor equivalent to fix any issues.Definition of Done
devplacepy/services/markdown_preserve.pyimplemented withextract_blocksandrestore_blocksmethods.correction.pyandai_modifier.pyupdated to extract code blocks before AI call and restore after response.tests/unit/services/correction.pyandtests/unit/services/ai_modifier.pyupdated to reflect the new prompt content and to test code block preservation.tests/unit/services/markdown_preserve.pycover extraction/restoration edge cases.ruff check .or whatever the project uses; no fixable issues introduced).templates/docs/ai-modifier.htmlanddevplacepy/services/CLAUDE.mdupdated to accurately describe the new protection mechanism.Opened automatically by Typosaurus.
Checkout
From your project repository, check out a new branch and test the changes.