How to get chapter title in book project #8999
Unanswered
jandermoreira
asked this question in
Q&A
Replies: 2 comments
-
|
Note that you could use the "native" format to help you develop your filter, see https://quarto.org/docs/extensions/lua.html#native-format. Why are you using level 1 header rather than the function Meta(meta)
if meta.title then
quarto.log.output("Title: " .. pandoc.utils.stringify(meta.title))
end
end
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I was able to extract the number from the pandoc title string using this filter and function: local function extract_number(str)
quarto.log.debug("[DEBUG] Input value:", str)
if type(str) ~= "string" then
quarto.log.debug("[DEBUG] Invalid type:", type(str))
return 0
end
local num = str:match("(%d+)")
quarto.log.debug("[DEBUG] Matched substring:", num)
if num then
local value = tonumber(num)
quarto.log.debug("[DEBUG] Converted number:", value)
return value
else
quarto.log.debug("[DEBUG] No number found at start of string.")
return 0
end
end
function Meta(meta)
if meta.title then
quarto.log.output("Title: " .. pandoc.utils.stringify(meta.title))
title = pandoc.utils.stringify(meta.title)
chapternumber = extract_number(title)
quarto.log.debug("Chapter number: " .. chapternumber )
end
endLots of debug statements because I don't really know what I'm doing. @mcanouil, as of Quarto 1.8, the native format doesn't output for book types. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
-
Description
I'm working on a filter to get several information from the document to generate an automatic preamble for each chapter in a book
However, I'm stuck in getting the chapter name.
What I'm trying to do
This is a MWE for the filter:
If I add this filter to a single document and run
quarto preview single_document.qmd, I get the debug output in terminal, so the filter finds the title (I'm assuming a single level 1 header in each qmd file). The following document was used for this test and Title I want is this one is part of the output in the terminal.This is the result:
The problem
However, when I add this filter to a book project, it prints out the title only for
index.qmdand ignores all other chapter files.To reproduce this issue:
Create a new book project
Just run
quarto create project bookand choose any name.Add the filter
Copy the above filter to a file
get_header.luaand add it to the project root.Also, add the following lines to
_quarto.yml:filters: - get_header.luaThen run quarto:
All headers are ignored, except for
index.qmd.Next steps?
I can't imagine other way to the get chapter name using
pandocorquartofunctions in the filter.If nothing else works, I intend to read the source file and extract the title using regular expressions. This seems to be a not very good looking workaround...
Beta Was this translation helpful? Give feedback.
All reactions