Skip to content

Commit 5b78d13

Browse files
committed
Improve filter type system
1 parent c77893d commit 5b78d13

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

src/command/render/defaults.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { extname } from "path/mod.ts";
99
import { stringify } from "encoding/yaml.ts";
1010

11-
import { FormatPandoc } from "../../config/types.ts";
11+
import { FormatPandoc, QuartoFilter } from "../../config/types.ts";
1212
import { isLatexOutput } from "../../config/format.ts";
1313

1414
import {
@@ -121,6 +121,16 @@ export function pandocDefaultsMessage(
121121
}
122122
});
123123

124+
const filtersContains = (filters: QuartoFilter[], filter: QuartoFilter) => {
125+
return filters.find((sysFilter) => {
126+
const sysPath = typeof (sysFilter) === "string"
127+
? sysFilter
128+
: sysFilter.path;
129+
const filterPath = typeof (filter) === "string" ? filter : filter.path;
130+
return sysPath === filterPath;
131+
});
132+
};
133+
124134
// simplify crossref filter
125135
if (defaults.filters?.length) {
126136
defaults.filters = defaults.filters
@@ -137,7 +147,7 @@ export function pandocDefaultsMessage(
137147
filter !== layoutFilter() &&
138148
filter !== authorsFilter() &&
139149
filter !== quartoFinalizeFilter() &&
140-
!sysFilters.includes(filter);
150+
filtersContains(sysFilters, filter);
141151
});
142152
if (defaults.filters?.length === 0) {
143153
delete defaults.filters;

src/command/render/filters.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ import {
3535
kTocTitleDocument,
3636
} from "../../config/constants.ts";
3737
import { PandocOptions } from "./types.ts";
38-
import { FormatLanguage, FormatPandoc } from "../../config/types.ts";
38+
import {
39+
FormatLanguage,
40+
FormatPandoc,
41+
QuartoFilter,
42+
} from "../../config/types.ts";
3943
import { Metadata } from "../../config/types.ts";
4044
import { kProjectType } from "../../project/types.ts";
4145
import { bibEngine } from "../../config/pdf.ts";
@@ -482,7 +486,10 @@ function initFilterParams(dependenciesFile: string) {
482486

483487
const kQuartoFilterMarker = "quarto";
484488

485-
export function resolveFilters(filters: string[], options: PandocOptions) {
489+
export function resolveFilters(
490+
filters: QuartoFilter[],
491+
options: PandocOptions,
492+
): QuartoFilter[] | undefined {
486493
// build list of quarto filters
487494
const quartoFilters: string[] = [];
488495
quartoFilters.push(quartoPreFilter());
@@ -559,10 +566,18 @@ function citeMethod(options: PandocOptions): CiteMethod | null {
559566
}
560567
}
561568

562-
function resolveFilterExtension(options: PandocOptions, filters: string[]) {
569+
function resolveFilterExtension(
570+
options: PandocOptions,
571+
filters: QuartoFilter[],
572+
): QuartoFilter[] {
563573
// Resolve any filters that are provided by an extension
564574
const results = filters.flatMap((filter) => {
565-
if (filter !== kQuartoFilterMarker && !existsSync(filter)) {
575+
// Look for extension names in the filter list and result them
576+
// into the filters provided by the extension
577+
if (
578+
filter !== kQuartoFilterMarker && typeof (filter) === "string" &&
579+
!existsSync(filter)
580+
) {
566581
const extension = options.extension?.find(
567582
filter,
568583
options.source,

src/command/render/pandoc.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,16 @@ export async function runPandoc(
562562
];
563563

564564
// make the filter paths windows safe
565-
allDefaults.filters = allDefaults.filters.map(pandocMetadataPath);
565+
allDefaults.filters = allDefaults.filters.map((filter) => {
566+
if (typeof (filter) === "string") {
567+
return pandocMetadataPath(filter);
568+
} else {
569+
return {
570+
type: filter.type,
571+
path: pandocMetadataPath(filter.path),
572+
};
573+
}
574+
});
566575

567576
// Capture any format filter params
568577
const filterParams = extras[kFilterParams];

src/config/types.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ export interface SassBundle extends SassBundleLayers {
245245
attribs?: Record<string, string>;
246246
}
247247

248+
export type PandocFilter = {
249+
type: "json" | "lua";
250+
path: string;
251+
};
252+
253+
export type QuartoFilter = string | PandocFilter;
254+
248255
export interface FormatExtras {
249256
args?: string[];
250257
pandoc?: FormatPandoc;
@@ -254,8 +261,8 @@ export interface FormatExtras {
254261
[kIncludeBeforeBody]?: string[];
255262
[kIncludeAfterBody]?: string[];
256263
[kFilters]?: {
257-
pre?: string[];
258-
post?: string[];
264+
pre?: QuartoFilter[];
265+
post?: QuartoFilter[];
259266
};
260267
[kFilterParams]?: Record<string, unknown>;
261268
postprocessors?: Array<(output: string) => Promise<void>>;
@@ -383,7 +390,7 @@ export interface FormatPandoc {
383390
[kReferenceLocation]?: string;
384391
[kCiteproc]?: boolean;
385392
[kCiteMethod]?: string;
386-
[kFilters]?: string[];
393+
[kFilters]?: QuartoFilter[];
387394
[kPdfEngine]?: string;
388395
[kPdfEngineOpts]?: string[];
389396
[kPdfEngineOpt]?: string;

0 commit comments

Comments
 (0)