Fixed markdown TOC generation

This commit is contained in:
Deniz Şafak
2025-09-18 03:08:30 +03:00
parent 1d8cb8eabd
commit 8c6e292f4f
2 changed files with 13 additions and 6 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
# 1.1.9 # 1.1.9 (pre-release)
- Fixed the issue where spaces were deleted before punctuation marks while generating subtitles. - Fixed the issue where spaces were deleted before punctuation marks while generating subtitles.
- Fixed markdown TOC generation breaks when "Replace single newlines" is enabled.
- Improvements in code and documentation.
# 1.1.8 # 1.1.8
- Added `.md` (Markdown) file extension support by @brianxiadong in PR #75 - Added `.md` (Markdown) file extension support by @brianxiadong in PR #75
+10 -5
View File
@@ -231,20 +231,24 @@ class HandlerDialog(QDialog):
if not self.markdown_text: if not self.markdown_text:
return return
text = clean_text(self.markdown_text) # Generate TOC from the original (dedented) markdown BEFORE cleaning,
text = textwrap.dedent(text) # so header ids/anchors are preserved for reliable position detection.
original_text = textwrap.dedent(self.markdown_text)
md = markdown.Markdown(extensions=['toc', 'fenced_code']) md = markdown.Markdown(extensions=['toc', 'fenced_code'])
html = md.convert(text) html = md.convert(original_text)
self.markdown_toc = md.toc_tokens self.markdown_toc = md.toc_tokens
# Use cleaned text for stored content/length calculations
cleaned_full_text = clean_text(original_text)
soup = BeautifulSoup(html, 'html.parser') soup = BeautifulSoup(html, 'html.parser')
self.content_texts = {} self.content_texts = {}
self.content_lengths = {} self.content_lengths = {}
if not self.markdown_toc: if not self.markdown_toc:
chapter_id = "markdown_content" chapter_id = "markdown_content"
self.content_texts[chapter_id] = text self.content_texts[chapter_id] = cleaned_full_text
self.content_lengths[chapter_id] = calculate_text_length(text) self.content_lengths[chapter_id] = calculate_text_length(cleaned_full_text)
return return
all_headers = [] all_headers = []
@@ -279,6 +283,7 @@ class HandlerDialog(QDialog):
header_tag = section_soup.find(attrs={'id': header_id}) header_tag = section_soup.find(attrs={'id': header_id})
if header_tag: if header_tag:
header_tag.decompose() header_tag.decompose()
# Clean section text for storage/lengths
section_text = clean_text(section_soup.get_text()).strip() section_text = clean_text(section_soup.get_text()).strip()
chapter_id = header_id chapter_id = header_id
if section_text: if section_text: