From 53875876ddde6fc637f926e6b66b6ff315507dea Mon Sep 17 00:00:00 2001
From: ankraft <an.kraft@gmail.com>
Date: Fri, 26 Apr 2024 10:56:02 +0200
Subject: [PATCH] Fixed problem with removed first line for non-header clauses
 (ie. the first text in a document or a document without any header)

---
 toMkdocs/toMkdocs.py | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/toMkdocs/toMkdocs.py b/toMkdocs/toMkdocs.py
index 92fdb4c..531b97e 100644
--- a/toMkdocs/toMkdocs.py
+++ b/toMkdocs/toMkdocs.py
@@ -181,6 +181,7 @@ def analyseMarkdown(filename:str) -> list[Clause]:
 	with open(filename, 'r', encoding = 'utf-8', errors = 'replace') as file:
 		inLines = file.readlines()
 	
+	# The list of clauses. The first clause contains the text before the first heading.
 	outClauses:list[Clause] = [Clause(0, '', '', [])]
 
 	# Go through the lines and detect headers and codefences
@@ -262,6 +263,10 @@ def splitMarkdownDocument(clauses:list[Clause],
 		# Add the lines to the output clause
 		outClauses[-1].extend(clause)
 	
+	# Remove the first clauses if they contain no lines AND the title is empty
+	while outClauses[0].linesCount == 0 and not len(outClauses[0].title):
+		outClauses.pop(0)
+	
 	# Remove the first clause if it has no title
 	if not includeUntilFirstHeading:
 		while len(outClauses[0].title) == 0:
@@ -281,12 +286,14 @@ def prepareForMkdocs(clauses:list[Clause]) -> list[Clause]:
 			The list of clauses.
 	"""
 
-	# Remove the heading from the lines. The heading is the first line
+	# Remove the heading from the lines. The heading is (usually) the first line
 	# in the clause. This is done because MkDocs repeats the heading when
 	# displaying the page.
 	for clause in clauses:
 		if clause.linesCount > 0:
-			clause.lines.pop(0)
+			# Remove the first line from the clause if it is a heading
+			if clause.lines[0].lineType == LineType.HEADING:
+				clause.lines.pop(0)
 			# Also, remove the first empty lines if they exist
 			while clause.linesCount > 0 and clause.lines[0].text.strip() == '':
 				clause.lines.pop(0)
@@ -484,7 +491,8 @@ def processDocument(args:argparse.Namespace) -> None:
 	clauses = analyseMarkdown(document)
 	clauses = splitMarkdownDocument(clauses, 
 								 	[ t.casefold() for t in args.ignore_clause ], 
-									args.split_level,args.include_title)
+									args.split_level,
+									args.include_title)
 	clauses = updateLinks(clauses)
 	clauses = updateNotes(clauses)
 	clauses = prepareForMkdocs(clauses)
-- 
GitLab