Skip to content

Commit ed9ea28

Browse files
committed
Improve Part / Chapter Handling
- We rely upon the `#` heading to mark chapters, so this must stay inline. only emit other metadata with template - track the chapter depth - only emit the startatroot when the depth is 0
1 parent e38fc48 commit ed9ea28

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

src/project/types/book/book-config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ export type BookRenderItemType = "index" | "chapter" | "appendix" | "part";
273273

274274
export interface BookRenderItem {
275275
type: BookRenderItemType;
276+
depth: number;
276277
text?: string;
277278
file?: string;
278279
number?: number;
@@ -293,6 +294,7 @@ export async function bookRenderItems(
293294
const findInputs = async (
294295
type: BookRenderItemType,
295296
items: SidebarItem[],
297+
depth = 0,
296298
) => {
297299
const throwInputNotFound = (input: string) => {
298300
throw new Error(`Book ${type} '${input}' not found`);
@@ -303,8 +305,9 @@ export async function bookRenderItems(
303305
type: kBookItemPart,
304306
file: item.href,
305307
text: item.text,
308+
depth,
306309
});
307-
await findInputs(type, item.contents);
310+
await findInputs(type, item.contents, depth + 1);
308311
} else if (item.href) {
309312
const itemPath = join(projectDir, item.href);
310313
if (safeExistsSync(itemPath)) {
@@ -325,6 +328,7 @@ export async function bookRenderItems(
325328
type,
326329
file: item.href,
327330
number,
331+
depth,
328332
});
329333
}
330334
} else {
@@ -369,6 +373,7 @@ export async function bookRenderItems(
369373
await findChapters(kBookAppendix, {
370374
type: kBookItemAppendix,
371375
text: language[kSectionTitleAppendices] + " {.unnumbered}",
376+
depth: 0,
372377
});
373378

374379
// validate that all of the chapters exist

src/project/types/book/book-render.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ async function mergeExecutedFiles(
351351
file.executeResult.markdown,
352352
);
353353

354+
const yaml = partitioned?.yaml
355+
? readYamlFromMarkdown(partitioned?.yaml)
356+
: undefined;
357+
const frontTitle = frontMatterTitle(yaml);
358+
const titleMarkdown = frontTitle ? `# ${frontTitle}\n\n` : "";
359+
354360
const titleBlockPath = resourcePath(
355361
"projects/book/pandoc/title-block.md",
356362
);
@@ -362,7 +368,8 @@ async function mergeExecutedFiles(
362368
"\n```````"
363369
: "";
364370

365-
itemMarkdown = bookItemMetadata(project, item, file) + titleBlockMd +
371+
itemMarkdown = bookItemMetadata(project, item, file) + titleMarkdown +
372+
titleBlockMd +
366373
(partitioned?.markdown || file.executeResult.markdown);
367374
} else {
368375
throw new Error(
@@ -542,6 +549,7 @@ function bookItemMetadata(
542549
bookItemType: item.type,
543550
bookItemNumber: item.number ? item.number : null,
544551
bookItemFile: item.file,
552+
bookItemDepth: item.depth,
545553
};
546554

547555
const inlineMetadataEncoded = base64Encode(JSON.stringify(inlineMetadata));

src/resources/filters/quarto-pre/book-numbering.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function bookNumbering()
77
local file = currentFileMetadataState().file
88
if file ~= nil then
99
local bookItemType = file.bookItemType
10+
local bookItemDepth = file.bookItemDepth
1011
if bookItemType ~= nil then
1112
-- if we are in an unnumbered chapter then add unnumbered class
1213
if bookItemType == "chapter" and file.bookItemNumber == nil then
@@ -29,13 +30,12 @@ function bookNumbering()
2930
tappend(appendixPara.content, el.content)
3031
appendixPara.content:insert(pandoc.RawInline('latex', '}'))
3132
return appendixPara
32-
elseif bookItemType == "chapter" then
33+
elseif bookItemType == "chapter" and bookItemDepth == 0 then
3334
preState.usingBookmark = true
34-
35-
local bookmarkReset = pandoc.Para({
36-
pandoc.RawInline('latex', '\\bookmarksetup{startatroot}'),
35+
local bookmarkReset = pandoc.Div({
36+
pandoc.RawInline('latex', '\\bookmarksetup{startatroot}\n'),
37+
el
3738
})
38-
tappend(bookmarkReset.content, el.content)
3939
return bookmarkReset
4040
end
4141
end

src/resources/projects/book/pandoc/title-block.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
$-- Be very careful about whitespace in this document - line breaks are very meaningful
22
$-- and it is very easy to break the layout with poorly considered line breaks
3-
# $title$
43

54
$if(subtitle)$$subtitle$$endif$
65

0 commit comments

Comments
 (0)