Repository: mondeja/mkdocs-include-markdown-plugin (python)

Issue:
Make heading-offset cumulative over nested inclusions
Use case is:

**File A.md**: 
```# A``` 

**File B.md**: 
```# B```

**File C.md**: 
```
#C

{%
include-markdown "./A.md"
heading-offset=1
%}

{%
include-markdown "./B.md"
heading-offset=1
%}
```
which is already rendered as following:

# C

## A

## B

But if you have
**File D.md**: 
```
# D

{%
include-markdown "./C.md"
heading-offset=1
%}
```

it is currently rendered as:

# D

## C

## # A

## # B

While I expected it to be rendered as follows:

# D

## C

### A

### B

Therefore, I suggest making **heading-offset** parameter cumulative over nested inclusions in order to achieve that expected result. Thank you for your feedback.

Pull request:
Make heading-offset cumulative over nested inclusions
Closes #52

Patch:
diff --git a/.bumpversion.cfg b/.bumpversion.cfg
index 4a3b4f0..8ad3c10 100644
--- a/.bumpversion.cfg
+++ b/.bumpversion.cfg
@@ -1,5 +1,5 @@
 [bumpversion]
-current_version = 3.1.0
+current_version = 3.1.1
 
 [bumpversion:file:mkdocs_include_markdown_plugin/__init__.py]
 
diff --git a/mkdocs_include_markdown_plugin/__init__.py b/mkdocs_include_markdown_plugin/__init__.py
index 2245b6c..5c5bed3 100644
--- a/mkdocs_include_markdown_plugin/__init__.py
+++ b/mkdocs_include_markdown_plugin/__init__.py
@@ -1,2 +1,2 @@
 __title__ = 'mkdocs_include_markdown_plugin'
-__version__ = '3.1.0'
+__version__ = '3.1.1'
diff --git a/mkdocs_include_markdown_plugin/event.py b/mkdocs_include_markdown_plugin/event.py
index 18ad096..6c555d9 100644
--- a/mkdocs_include_markdown_plugin/event.py
+++ b/mkdocs_include_markdown_plugin/event.py
@@ -55,7 +55,7 @@ ARGUMENT_REGEXES = {
 }
 
 
-def get_file_content(markdown, abs_src_path):
+def get_file_content(markdown, abs_src_path, cumulative_heading_offset=0):
     page_src_path = Path(abs_src_path)
 
     def found_include_tag(match):
@@ -213,20 +213,28 @@ def get_file_content(markdown, abs_src_path):
         else:
             text_to_include = _includer_indent + text_to_include
 
-        # nested includes
-        new_text_to_include = get_file_content(text_to_include, file_path_abs)
-        if new_text_to_include != text_to_include:
-            text_to_include = new_text_to_include
-
         # heading offset
+        offset = cumulative_heading_offset
         offset_match = re.search(
             ARGUMENT_REGEXES['heading-offset'],
             arguments_string,
         )
+        if offset_match:
+            offset += int(offset_match.group(1))
+
+        # nested includes
+        new_text_to_include = get_file_content(
+            text_to_include,
+            file_path_abs,
+            cumulative_heading_offset=offset,
+        )
+        if new_text_to_include != text_to_include:
+            text_to_include = new_text_to_include
+
         if offset_match:
             text_to_include = process.increase_headings_offset(
                 text_to_include,
-                offset=int(offset_match.group(1)),
+                offset=offset,
             )
 
         if not bool_options['comments']['value']:
diff --git a/setup.cfg b/setup.cfg
index 203372c..efff56a 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
 name = mkdocs_include_markdown_plugin
-version = 3.1.0
+version = 3.1.1
 description = Mkdocs Markdown includer plugin.
 long_description = file: README.md
 long_description_content_type = text/markdown


Test patch:
diff --git a/tests/test_nested_includes.py b/tests/test_nested_includes.py
index 18bdb54..b141cef 100644
--- a/tests/test_nested_includes.py
+++ b/tests/test_nested_includes.py
@@ -114,11 +114,44 @@ Some test from final included.
 ''',
             id='includer -> file -> markdown',
         ),
+
+        # cumulative_heading_offset
+        pytest.param(
+            '''# Header
+
+{%
+  include-markdown "{filepath}"
+  heading-offset=1
+  comments=false
+%}''',
+            '''# Header 2
+
+{%
+  include-markdown "{filepath}"
+  heading-offset=1
+  comments=false
+%}
+''',
+            '''# Header 3
+''',
+            '''# Header
+
+## Header 2
+
+#### Header 3
+
+''',
+            id='cumulative_heading_offset',
+        ),
     ),
 )
 def test_nested_include(
-    first_includer_content, second_includer_content,
-    included_content, expected_result, page, tmp_path,
+    first_includer_content,
+    second_includer_content,
+    included_content,
+    expected_result,
+    page,
+    tmp_path,
 ):
     first_includer_filepath = tmp_path / 'first-includer.txt'
     second_includer_filepath = tmp_path / 'second-includer.txt'