-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extension to treat first heading level as title? #5615
Comments
The |
Use this form:
|
I'm not sure that I got my problem across. I realize that in AsciiDoc, I have a large number of Markdown files that use this approach and that I'd like to convert to AsciiDoc. So I need to convert |
Well, pandoc uses pandoc flavoured markdown... I was thinking you can could write a lua-filter to decrement all header levels by one, similar to this example: https://pandoc.org/lua-filters.html#modifying-pandocs-manual.txt-for-man-pages, but this causes the current pandoc version to crash on You could change this line and compile pandoc yourself... |
Specifically, the |
Or write a filter that decrements all heading levels by one, except for the heading 1, which should be set as the document meta data title. |
Relevant StackOverflow question and answer: Pandoc: set document title to first title |
@tarleb you're a lua wizard! I just grabbed another one of yours for numbered-chapter-reference |
Maybe we need a markdown extension that treats a unique level-one header as the metadata title, and promotes all other headers. Something like |
That would solve it for markdown.. but what about reading other formats like HTML? |
We could make the extension affect HTML as well. I don't know if there are other formats that use this convention for indicating titles. |
This issue is more vexing to me than expected... It seems to me now that what we need is an extension that treats a unique level-one header as the metadata title, and does nothing to the other headers. Let me explain... This markdown:
converted with
which is usually what you want: one h1, one h2. Now to the asciidoc and html readers:
Does that make sense? |
it makes sense, but that doesn't mean we should parse h2 -> Header 2 in this case. Converting HTML -> LaTeX, with this style of HTML, you'd generally want the h2's to convert to One could try to address this at the HTML writer level. When the special extension or option is set, Header 1 renders as h2. So, to summarize:
|
Ah, considering LaTeX output is indeed interesting. I had the impression now that the recommend md for html export is the following (otherwise you get two
But you're saying that for LaTeX export you'd usually want the following?
|
Yes, for LaTeX and most other formats. With the Relevant old issue: #686. |
I've been thinking about this some more and made peace with the fact that depending on what you're doing you'll always want to adjust your heading levels. Might depend on the output format you're converting to, or more importantly on where in any existing hierarchy your piece of text will fit into: say an existing website that uses So there will always be cases where you should simply make use of the
|
Interesting idea. But isn't it a bit odd if
unless also
This latter would definitely be a change to how EDIT: Even so I'm pretty positively disposed to this idea. I believe people have requested negative heading level shifts before. (See #4342) |
yes, I think that would be useful in some rare cases as well. I suppose |
🎉 |
I think it makes sense that shifting between metadata and level-1 heading occurs in both directions. I don't find a compelling counter-example. But if the effect applies to all inputs after concatenation, then can the user not provide a document title that doesn't get demoted to a mere header? Sometimes users split a book into files. Or a book may constitute a compilation of articles from sources written originally for standalone publication. For example, consider inputs:
The intention might be to represent:
Could the global metadata input be protected from demotion? Could shifting be selected at the granularity of individual inputs, and applied before concatenation? And would a title be handled differently if coming from the metadata given on the command line versus a YAML source? I wouldn't suggest a solution in which giving a title on the command line is the only way to protect it from demotion. |
@brainchild0 see |
Would this switch guard any single input file from transformations that would apply to others? I am not seeing any indication of such in the manual. |
I don't understand the question. (But I recommend you experiment to find out.) |
Following are the best experiments I can do currently: Looking at my first post in the issue, notice a sequence of three examples of file contents following the line beginning with "For example". I place them in Now I try:
The files The idea expressed in that post was that it might be useful if I could run a command using these three files, but is equivalent to With the current options, I think it is impossible. The closest approximation would be
Actually, the result currently is that the chapter titles are dropped, because only one title may be recognized for the document. With
What would be needed is a way to shift the contents of At first it may seem like an unusual case, but I think probably not so. It seems that currently any positive shift value prevents the input stream from giving any data that is used for the document title of the output. |
Correct, that can't currently be done.
I'd find it more intuitive (and useful) if the heading-shift transformation was done before the metadata was integrated, so the metadata from |
Actually on reflection, I'm not so sure about this. |
It's not obvious how to create a new issue that captures the immediate concerns without including the history. The sequence of processing would seem to be close to the following:
Which parts of this discussion, if any, would you want moved to a new issue, and which would you be less open to seriously considering at the current moment? |
Also, not sure about a compelling use case, but if a left shift squashes several header levels into one, then the original level of each affected header in principle can be preserved in ancillary data, like XHTML |
See #5957 for an unintended consequence of this change. |
I'm going to roll back:
This breaks some workflows that used to be supported with --base-heading-level (see #5957). Also: suppose you want to render a document with both latex and html. IT would be natural to use level-1 headings. But in the HTML version you might want level-2 headings, since the title will be rendered as level-1. So you'd want to shift heading levels, without depopulating the title in metadata. |
With positive heading shifts, starting in 2.8 this option caused metadata titles to be removed and changed to regular headings. This behavior is incompatible with the old behavior of `--base-header-level` and breaks old workflows, so with this commit we are rolling back this change. Now, there is an asymmetry in positive and negative heading level shifts: + With positive shifts, the metadata title stays the same and does not get changed to a heading in the body. + With negative shifts, a heading can be converted into the metadata title. I think this is a desirable combination of features, despite the asymmetry. One might, e.g., want to have a document with level-1 section headigs, but render it to HTML with level-2 headings, retaining the metadata title (which pandoc will render as a level-1 heading with the default template). Closes #5957. Revises #5615.
With positive heading shifts, starting in 2.8 this option caused metadata titles to be removed and changed to regular headings. This behavior is incompatible with the old behavior of `--base-header-level` and breaks old workflows, so with this commit we are rolling back this change. Now, there is an asymmetry in positive and negative heading level shifts: + With positive shifts, the metadata title stays the same and does not get changed to a heading in the body. + With negative shifts, a heading can be converted into the metadata title. I think this is a desirable combination of features, despite the asymmetry. One might, e.g., want to have a document with level-1 section headigs, but render it to HTML with level-2 headings, retaining the metadata title (which pandoc will render as a level-1 heading with the default template). Closes jgm#5957. Revises jgm#5615.
I use # Title of document
## First section
## Second section I love that the title of the document is now specified via The title is displayed via |
Given the recommendation only to use one h1 element per page, it would make a certain amount of sense to map a pandoc |
@rauschma's use case (which happens to be mine) can be solved by using this lua filter (adapted with a single changed line from one that @tarleb posted in 2019). This filter will make all local title
-- Set title from level 1 header, or
-- discard level 1 header if title is already set.
function make_header1_title (header)
if header.level >= 2 then
return header
end
if not title then
title = header.content
return {}
end
local msg = '[WARNING] title already set; discarding header "%s"\n'
io.stderr:write(msg:format(pandoc.utils.stringify(header)))
return {}
end
return {
{Meta = function (meta) title = meta.title end}, -- init title
{Header = make_header1_title},
{Meta = function (meta) meta.title = title; return meta end}, -- set title
} |
I'm using Pandoc 2.7.3 to convert Markdown to AsciiDoc. In the output file, the level of every heading is 1 deeper than expected.
README.md:
Invocation:
Output (README.adoc):
Expected output:
The text was updated successfully, but these errors were encountered: