Skip to content

Commit b7f4afe

Browse files
quanticleyantar92
authored andcommitted
lisp/ox-md.el: Add top level header setting
* lisp/ox-md.el (defcustom org-md-toplevel-hlevel): Define the customizable variable that defines the top level heading to use when exporting to markdown. (org-export-define-derived-backend): Read the value of the new `org-md-toplevel-hlevel' variable into the `:options-alist' for this backend (org-md--build-toc): Use the newly defined top-level heading setting to control the heading level for the "Table of Contents" text. (org-md-headline): Use the newly defined top-level heading setting as an offset to the heading level calculated by `org-export-get-relative-level'. This patch adds a new setting: `org-md-toplevel-hlevel' that controls which Markdown heading level is used for top level headings. This change makes markdown export more like HTML export, which has a `org-html-toplevel-hlevel' variable which does the same thing for HTML export. TINYCHANGE
1 parent a35d163 commit b7f4afe

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

doc/org-manual.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16405,6 +16405,7 @@ Settings]]), however, override everything.
1640516405
| ~:md-footnote-format~ | ~org-md-footnote-format~ |
1640616406
| ~:md-footnotes-section~ | ~org-md-footnotes-section~ |
1640716407
| ~:md-headline-style~ | ~org-md-headline-style~ |
16408+
| ~:md-toplevel-hlevel~ | ~org-md-toplevel-hlevel~ |
1640816409

1640916410
**** ODT specific properties
1641016411
:PROPERTIES:

etc/ORG-NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ file name. For example:
282282

283283
If a file does not have a title, the table will show the file name
284284
instead.
285+
*** New =org-md-toplevel-hlevel= variable for Markdown export
286+
287+
The =org-md-toplevel-hlevel= customization variable sets the heading
288+
level used for top level headings, much like how
289+
=org-html-toplevel-hlevel= sets the heading level used for top level
290+
headings in HTML export.
285291

286292
** New options
287293
*** A new custom setting =org-hide-drawer-startup= to control initial folding state of drawers

lisp/ox-md.el

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ The %s will be replaced by the footnote reference itself."
7171
:version "26.1"
7272
:package-version '(Org . "9.0"))
7373

74+
(defcustom org-md-toplevel-hlevel 1
75+
"Heading level to use for level 1 Org headings in markdown export.
76+
77+
If this is 1, headline levels will be preserved on export. If this is
78+
2, top level Org headings will be exported to level 2 markdown
79+
headings, level 2 Org headings will be exported to level 3 markdown
80+
headings, and so on.
81+
82+
Incrementing this value may be helpful when creating markdown to be
83+
included into another document or application that reserves top-level
84+
headings for its own use."
85+
:group 'org-export-md
86+
:type 'string)
87+
88+
7489

7590
;;; Define Back-End
7691

@@ -120,7 +135,8 @@ The %s will be replaced by the footnote reference itself."
120135
:options-alist
121136
'((:md-footnote-format nil nil org-md-footnote-format)
122137
(:md-footnotes-section nil nil org-md-footnotes-section)
123-
(:md-headline-style nil nil org-md-headline-style)))
138+
(:md-headline-style nil nil org-md-headline-style)
139+
(:md-toplevel-hlevel nil nil org-md-toplevel-hlevel)))
124140

125141

126142
;;; Filters
@@ -229,9 +245,10 @@ When optional argument SCOPE is non-nil, build a table of
229245
contents according to the specified element."
230246
(concat
231247
(unless scope
232-
(let ((style (plist-get info :md-headline-style))
248+
(let ((level (plist-get info :md-toplevel-hlevel))
249+
(style (plist-get info :md-headline-style))
233250
(title (org-html--translate "Table of Contents" info)))
234-
(org-md--headline-title style 1 title nil)))
251+
(org-md--headline-title style level title nil)))
235252
(mapconcat
236253
(lambda (headline)
237254
(let* ((indentation
@@ -350,7 +367,8 @@ CONTENTS is nil. INFO is a plist holding contextual information."
350367
CONTENTS is the headline contents. INFO is a plist used as
351368
a communication channel."
352369
(unless (org-element-property :footnote-section-p headline)
353-
(let* ((level (org-export-get-relative-level headline info))
370+
(let* ((level (+ (org-export-get-relative-level headline info)
371+
(1- (plist-get info :md-toplevel-hlevel))))
354372
(title (org-export-data (org-element-property :title headline) info))
355373
(todo (and (plist-get info :with-todo-keywords)
356374
(let ((todo (org-element-property :todo-keyword

0 commit comments

Comments
 (0)