Repository navigation
test+feat: close rich Markdown preview residuals #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
cb5b4a7
bce91d8
3a1fd98
cfa818b
daadf3d
6dc4047
0359a9a
da1605c
f17d7c8
2f4b73a
2544dbb
211e1a6
9c54a23
41c6c9d
bc49e0d
e2a5038
332cc79
2834955
d0d4f40
796c1e8
6e9af65
50ae822
3f05e80
7a02780
365517a
0bee808
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -585,21 +585,43 @@ class UnsupportedConstruct(StrEnum): | |
| r"th|td|p|ul|ol|li)\b[^>]*>", | ||
| re.IGNORECASE, | ||
| ) | ||
| _ACCEPTED_RICH_MARKDOWN_CONSTRUCT_PATTERNS: Final = { | ||
| UnsupportedConstruct.EMPHASIS: (_EMPHASIS_PATTERN,), | ||
| UnsupportedConstruct.INLINE_CODE: (_RICH_INLINE_CODE_PATTERN,), | ||
| UnsupportedConstruct.LINK_OR_IMAGE: ( | ||
| _RICH_WIKILINK_PATTERN, | ||
| _RICH_AUTOLINK_PATTERN, | ||
| _RICH_INLINE_LINK_PATTERN, | ||
| _RICH_REFERENCE_LINK_PATTERN, | ||
| ), | ||
| UnsupportedConstruct.STRIKETHROUGH: (_RICH_STRIKETHROUGH_PATTERN,), | ||
| } | ||
|
|
||
|
|
||
| def contains_accepted_rich_markdown_construct( | ||
| source: str, | ||
| construct: UnsupportedConstruct, | ||
| ) -> bool: | ||
| """Return whether exact text contains the named accepted rich syntax.""" | ||
|
|
||
| if type(source) is not str: | ||
| raise TypeError("rich Markdown construct detection requires exact text") | ||
| if type(construct) is not UnsupportedConstruct: | ||
| raise TypeError("rich Markdown construct detection requires a closed construct") | ||
| return any( | ||
| pattern.search(source) is not None | ||
| for pattern in _ACCEPTED_RICH_MARKDOWN_CONSTRUCT_PATTERNS.get(construct, ()) | ||
| ) | ||
|
|
||
|
|
||
| def contains_rich_markdown_link(source: str) -> bool: | ||
| """Return whether exact UTF-8 text contains syntax accepted as a rich link.""" | ||
|
|
||
| if type(source) is not str: | ||
| raise TypeError("rich Markdown link detection requires exact text") | ||
| return any( | ||
| pattern.search(source) is not None | ||
| for pattern in ( | ||
| _RICH_WIKILINK_PATTERN, | ||
| _RICH_AUTOLINK_PATTERN, | ||
| _RICH_INLINE_LINK_PATTERN, | ||
| _RICH_REFERENCE_LINK_PATTERN, | ||
| ) | ||
| return contains_accepted_rich_markdown_construct( | ||
| source, | ||
| UnsupportedConstruct.LINK_OR_IMAGE, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -678,6 +700,78 @@ def unsupported_rich_markdown_inline(line: str) -> UnsupportedConstruct | None: | |
| return None if construct is UnsupportedConstruct.LIST else construct | ||
|
|
||
|
|
||
| def contains_only_accepted_rich_markdown_inline( | ||
| source: str, | ||
| construct: UnsupportedConstruct, | ||
| ) -> bool: | ||
| """Return whether one accepted construct has no malformed inline peer.""" | ||
|
|
||
| if type(source) is not str: | ||
| raise TypeError("rich Markdown inline validation requires exact text") | ||
| if not contains_accepted_rich_markdown_construct(source, construct): | ||
| return False | ||
| fence: str | None = None | ||
| fence_body_has_content = False | ||
| for line in source.splitlines(): | ||
| marker = _RICH_FENCE_PATTERN.match(line) | ||
| if marker is not None: | ||
| candidate = marker.group("fence") | ||
| if fence is None: | ||
| fence = candidate | ||
| language = line.lstrip()[len(candidate) :].strip() | ||
| if len(language) > MARKDOWN_CODE_LANGUAGE_MAX_LENGTH or any( | ||
| character.isspace() for character in language | ||
| ): | ||
| return False | ||
| fence_body_has_content = False | ||
| elif candidate[0] == fence[0] and len(candidate) >= len(fence): | ||
| if not fence_body_has_content: | ||
| return False | ||
| fence = None | ||
| continue | ||
| if fence is not None: | ||
| fence_body_has_content = fence_body_has_content or bool(line.strip()) | ||
| continue | ||
|
Comment on lines
+766
to
+779
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
stometa, when an accepted rich construct is followed by a list continuation such as AGENTS.md reference: AGENTS.md:L22-L24 Useful? React with 👍 / 👎. |
||
| if ( | ||
| _THEMATIC_BREAK_PATTERN.fullmatch(line) is not None | ||
| or _RICH_SETEXT_PATTERN.fullmatch(line) is not None | ||
| ): | ||
| continue | ||
| stripped = line.strip() | ||
| if stripped.startswith("<"): | ||
| if _RICH_ANGLE_LITERAL_PATTERN.fullmatch(stripped) is not None: | ||
| continue | ||
| if not _has_closed_rich_html_block(line, source): | ||
| return False | ||
| continue | ||
| inspected = _rich_markdown_inline_payload(line) | ||
| if unsupported_rich_markdown_inline(inspected) is not None: | ||
| return False | ||
| return fence is None | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def _rich_markdown_inline_payload(line: str) -> str: | ||
| inspected = line | ||
| if (heading := _RICH_ATX_HEADING_PATTERN.fullmatch(line)) is not None: | ||
| inspected = heading.group(2).strip() | ||
| elif (item := _RICH_LIST_ITEM_PATTERN.fullmatch(line)) is not None: | ||
| inspected = item.group(2) | ||
| elif line.lstrip().startswith(">"): | ||
| inspected = line.lstrip()[1:].lstrip() | ||
| if inspected.startswith("[!"): | ||
| inspected = _RICH_FOOTNOTE_PATTERN.sub("x", inspected, count=1) | ||
| return inspected | ||
|
|
||
|
|
||
| def _has_closed_rich_html_block(line: str, source: str) -> bool: | ||
| html_open = _RICH_HTML_OPEN_PATTERN.match(line) | ||
| return html_open is not None and re.search( | ||
| rf"</{re.escape(html_open.group('tag'))}[ \t]*>", | ||
| source, | ||
| re.IGNORECASE, | ||
| ) is not None | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class CompilationFailure: | ||
| """Typed all-or-nothing failure; it never carries partial ParsedDocument data.""" | ||
|
|
@@ -1356,15 +1450,7 @@ def _validate_rich_closed_grammar(section: ParsedSection, source: str) -> None: | |
| return | ||
| if len(lines) >= 2 and lines[0] == "---" and lines[-1] == "---": | ||
| return | ||
| html_open = _RICH_HTML_OPEN_PATTERN.match(lines[0]) if lines else None | ||
| if ( | ||
| html_open is not None | ||
| and re.search( | ||
| rf"</{html_open.group('tag')}[ \t]*>", | ||
| source, | ||
| re.IGNORECASE, | ||
| ) | ||
| ): | ||
| if lines and _has_closed_rich_html_block(lines[0], source): | ||
| return | ||
| if ( | ||
| section.kind is SectionKind.PARAGRAPH | ||
|
|
@@ -1395,23 +1481,14 @@ def _validate_rich_closed_grammar(section: ParsedSection, source: str) -> None: | |
| raise ValueError("rich section source must match the closed grammar") | ||
| return | ||
| for line in lines: | ||
| inspected = line | ||
| if section.kind is SectionKind.HEADING: | ||
| match = _RICH_ATX_HEADING_PATTERN.fullmatch(line) | ||
| if match is not None: | ||
| inspected = match.group(2).strip() | ||
| elif _RICH_SETEXT_PATTERN.fullmatch(line) is not None: | ||
| continue | ||
| elif section.kind is SectionKind.LIST: | ||
| match = _RICH_LIST_ITEM_PATTERN.fullmatch(line) | ||
| if match is not None: | ||
| inspected = match.group(2) | ||
| elif line.startswith((" ", "\t")): | ||
| inspected = line.lstrip() | ||
| elif line.lstrip().startswith(">"): | ||
| inspected = line.lstrip()[1:].lstrip() | ||
| if inspected.startswith("[!"): | ||
| inspected = _RICH_FOOTNOTE_PATTERN.sub("x", inspected, count=1) | ||
| if ( | ||
| section.kind is SectionKind.HEADING | ||
| and _RICH_SETEXT_PATTERN.fullmatch(line) is not None | ||
| ): | ||
| continue | ||
| inspected = _rich_markdown_inline_payload(line) | ||
| if section.kind is SectionKind.LIST and line.startswith((" ", "\t")): | ||
| inspected = inspected.lstrip() | ||
| if unsupported_rich_markdown_inline(inspected) is not None: | ||
| raise ValueError("rich section source must match the closed grammar") | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an otherwise accepted document contains a fenced block whose would-be closer has a suffix, such as
```oops, this condition treats that line as the closing fence because it checks only the marker character and length. The v3 parser's_is_closing_fencerequires the marker to be followed only by whitespace, so the worker instead refuses the document asCODE_BLOCK; the preview therefore returns an actionable scan handoff for malformed syntax that ADR-0093 says must remain generically unavailable. Validate the complete closing-fence line using the same rule as the v3 parser.AGENTS.md reference: AGENTS.md:L22-L24
Useful? React with 👍 / 👎.