From 185c43c2d7c15cd6df44c448d60bc88ea2a47402 Mon Sep 17 00:00:00 2001
From: Rohit Patnaik <quanticle@quanticle.net>
Date: Sun, 21 Aug 2022 00:20:03 -0500
Subject: [PATCH] Added top level header setting for md export

This commit adds a customization, org-md-toplevel-hlevel, which sets the
heading level of top-level headings in the exported markdown. This makes
markdown export behave more like HTML export, which reads
org-html-toplevel-hlevel to decide what the top level heading for HTML
export should be.
---
 lisp/ox-md.el | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/lisp/ox-md.el b/lisp/ox-md.el
index 50db53910..7b9110890 100644
--- a/lisp/ox-md.el
+++ b/lisp/ox-md.el
@@ -71,6 +71,18 @@ The %s will be replaced by the footnote reference itself."
   :version "26.1"
   :package-version '(Org . "9.0"))
 
+(defcustom org-md-toplevel-hlevel 1
+  "The markdown heading level to use for level 1 headings in markdown export.
+If this is 1, the top level headings will be level 1 markdown headings. If this
+is 2, then top level headings will be level 2 markdown headings. By default 
+this is 1, so that org-mode headings correspond to the same level markdown
+headings. Incrementing this value may be helpful creating markdown for 
+inclusion in another document or application that reserves top-level headings
+for its own use."
+  :group 'org-export-md
+  :type 'string)
+
+
 
 ;;; Define Back-End
 
@@ -120,7 +132,8 @@ The %s will be replaced by the footnote reference itself."
   :options-alist
   '((:md-footnote-format nil nil org-md-footnote-format)
     (:md-footnotes-section nil nil org-md-footnotes-section)
-    (:md-headline-style nil nil org-md-headline-style)))
+    (:md-headline-style nil nil org-md-headline-style)
+    (:md-toplevel-hlevel nil nil org-md-toplevel-hlevel)))
 
 
 ;;; Filters
@@ -229,9 +242,10 @@ When optional argument SCOPE is non-nil, build a table of
 contents according to the specified element."
   (concat
    (unless scope
-     (let ((style (plist-get info :md-headline-style))
+     (let ((level (plist-get info :md-toplevel-hlevel))
+           (style (plist-get info :md-headline-style))
 	   (title (org-html--translate "Table of Contents" info)))
-       (org-md--headline-title style 1 title nil)))
+       (org-md--headline-title style level title nil)))
    (mapconcat
     (lambda (headline)
       (let* ((indentation
@@ -350,7 +364,8 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 CONTENTS is the headline contents.  INFO is a plist used as
 a communication channel."
   (unless (org-element-property :footnote-section-p headline)
-    (let* ((level (org-export-get-relative-level headline info))
+    (let* ((level (+ (org-export-get-relative-level headline info)
+                     (1- (plist-get info :md-toplevel-hlevel))))
 	   (title (org-export-data (org-element-property :title headline) info))
 	   (todo (and (plist-get info :with-todo-keywords)
 		      (let ((todo (org-element-property :todo-keyword
-- 
2.37.2.windows.2

