-
Notifications
You must be signed in to change notification settings - Fork 349
/
Copy pathformat-typst.ts
125 lines (116 loc) · 3.42 KB
/
format-typst.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* format-typst.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { join } from "../../deno_ral/path.ts";
import { RenderServices } from "../../command/render/types.ts";
import {
kCiteproc,
kColumns,
kDefaultImageExtension,
kFigFormat,
kFigHeight,
kFigWidth,
kNumberSections,
kSectionNumbering,
kShiftHeadingLevelBy,
kVariant,
kWrap,
} from "../../config/constants.ts";
import {
Format,
FormatExtras,
FormatPandoc,
Metadata,
PandocFlags,
} from "../../config/types.ts";
import { formatResourcePath } from "../../core/resources.ts";
import { createFormat } from "../formats-shared.ts";
export function typstFormat(): Format {
return createFormat("Typst", "pdf", {
execute: {
[kFigWidth]: 5.5,
[kFigHeight]: 3.5,
[kFigFormat]: "svg",
},
pandoc: {
standalone: true,
[kDefaultImageExtension]: "svg",
[kWrap]: "none",
[kCiteproc]: false,
},
resolveFormat: typstResolveFormat,
formatExtras: (
_input: string,
markdown: string,
flags: PandocFlags,
format: Format,
_libDir: string,
_services: RenderServices,
): FormatExtras => {
const pandoc: FormatPandoc = {};
const metadata: Metadata = {};
// provide default section numbering if required
if (
(flags?.[kNumberSections] === true ||
format.pandoc[kNumberSections] === true)
) {
// number-sections imples section-numbering
if (!format.metadata?.[kSectionNumbering]) {
metadata[kSectionNumbering] = "1.1.a";
}
}
// unless otherwise specified, pdfs with only level 2 or greater headings get their
// heading level shifted by -1.
const hasLevelOneHeadings = !!markdown.match(/\n^#\s.*$/gm);
if (
!hasLevelOneHeadings &&
flags?.[kShiftHeadingLevelBy] === undefined &&
format.pandoc?.[kShiftHeadingLevelBy] === undefined
) {
pandoc[kShiftHeadingLevelBy] = -1;
}
// force columns to wrap and move any 'columns' setting to metadata
const columns = format.pandoc[kColumns];
if (columns) {
pandoc[kColumns] = undefined;
metadata[kColumns] = columns;
}
// Provide a template and partials
const templateDir = formatResourcePath("typst", join("pandoc", "quarto"));
const templateContext = {
template: join(templateDir, "template.typ"),
partials: [
"definitions.typ",
"typst-template.typ",
"typst-show.typ",
"typst-title.typ",
"notes.typ",
"biblio.typ",
].map((partial) => join(templateDir, partial)),
};
return {
pandoc,
metadata,
templateContext,
};
},
});
}
function typstResolveFormat(format: Format) {
// Pandoc citeproc with typst output requires adjustment
// https://github.com/jgm/pandoc/commit/e89a3edf24a025d5bb0fe8c4c7a8e6e0208fa846
if (
format.pandoc?.[kCiteproc] === true &&
!format.pandoc.to?.includes("-citations") &&
!format.render[kVariant]?.includes("-citations")
) {
// citeproc: false is the default, so user setting it to true means they want to use
// Pandoc's citeproc which requires `citations` extensions to be disabled (e.g typst-citations)
// This adds the variants for them if not set already
format.render[kVariant] = [format.render?.[kVariant], "-citations"].join(
"",
);
}
}