Skip to content

Commit 08d84ed

Browse files
committed
fix: fix quote behaviour if starts but doesn't end on \n, more robust merge
1 parent 89e2898 commit 08d84ed

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/text2markdown/text2markdown.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _annotate_each_line(
8585
span_text_lines = doc_text[a_start:a_end].splitlines(keepends=True)
8686
offset = a_start
8787
for i, line in enumerate(span_text_lines):
88-
# add newline at the end of annotation group if `force_blank == True``
88+
# add newline at the end of annotation group if `add_newlines == True``
8989
add_newline = (i == len(span_text_lines) - 1) and add_newlines
9090

9191
line_start = offset
@@ -186,7 +186,7 @@ def _merge_annotations(anns: list[_Annotation], kinds: set[_AnnotationKind]) ->
186186
skip_next = a2.kind in kinds and skipped_ann and (skipped_ann.start, skipped_ann.end) == (a2.start, a2.end)
187187
continue
188188

189-
skip_next = a1.kind in kinds and a2.kind in kinds and (a1.start, a2.start) == (a1.end, a2.end)
189+
skip_next = a1.kind in kinds and a2.kind in kinds and (a1.start, a1.end) == (a2.start, a2.end)
190190
skipped_ann = a2
191191
yield a1
192192

@@ -352,8 +352,10 @@ def text2markdown(
352352
anns.update(_annotate_each_line(_Annotation(ann.start, ann.end, kind=kind), text))
353353

354354
case "quote":
355-
if ann.span.start > 0 and text[ann.span.start - 1] != "\n":
356-
# Only annotate block quotes; must be preceded with '\n' char
355+
if (ann.span.start > 0 and text[ann.span.start - 1] != "\n") or (
356+
ann.span.end < len(text) and bool(re.match(r"[^\S\n]*\S", text[ann.span.end :]))
357+
):
358+
# Only annotate block quotes; must be preceded and succeeded with '\n' char
357359
continue
358360
anns.update(
359361
_annotate_each_line(
@@ -413,7 +415,6 @@ def event_sort_key(e: _Event):
413415
md: list[str] = [] # Output markdown
414416
curr_idx = 0
415417
for pos, t, ann in events:
416-
kind = ann.kind
417418
if curr_idx != pos:
418419
md.append(text[curr_idx:pos])
419420

0 commit comments

Comments
 (0)