Skip to content

Commit 4303f2c

Browse files
committed
Merge branch 'main' of github.com:quarto-dev/quarto-cli into main
2 parents 3f4f116 + 80d70b8 commit 4303f2c

File tree

5 files changed

+83
-56
lines changed

5 files changed

+83
-56
lines changed

src/command/render/filters.ts

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ import { mergeConfigs } from "../../core/config.ts";
5353
import { projectType } from "../../project/types/project-types.ts";
5454
import { readCodePage } from "../../core/windows.ts";
5555
import { authorsFilter, authorsFilterActive } from "./authors.ts";
56-
import {
57-
Extension,
58-
extensionIdString,
59-
} from "../../extension/extension-shared.ts";
6056

6157
const kQuartoParams = "quarto-params";
6258

@@ -565,48 +561,23 @@ function citeMethod(options: PandocOptions): CiteMethod | null {
565561

566562
function resolveFilterExtension(options: PandocOptions, filters: string[]) {
567563
// Resolve any filters that are provided by an extension
568-
const extensions = filterExtensions(options);
569-
return filters.flatMap((filter) => {
564+
const results = filters.flatMap((filter) => {
570565
if (filter !== kQuartoFilterMarker && !existsSync(filter)) {
571-
// Try to resolve this path to an extension
572-
const exactMatch = extensions.find((ext) => {
573-
const idStr = extensionIdString(ext.id);
574-
if (filter === idStr) {
575-
return true;
576-
}
577-
});
578-
if (exactMatch) {
579-
return exactMatch.contributes.filters || [];
566+
const extension = options.extension?.find(
567+
filter,
568+
options.source,
569+
"filters",
570+
options.project,
571+
);
572+
const filters = extension?.contributes.filters;
573+
if (filters) {
574+
return filters;
580575
} else {
581-
const nameMatch = extensions.find((ext) => {
582-
if (filter === ext.id.name) {
583-
return true;
584-
}
585-
});
586-
if (nameMatch) {
587-
return nameMatch.contributes.filters || [];
588-
} else {
589-
return filter;
590-
}
576+
return filter;
591577
}
592578
} else {
593579
return filter;
594580
}
595581
});
596-
}
597-
598-
function filterExtensions(options: PandocOptions) {
599-
const filterExts: Extension[] = [];
600-
if (options.extension) {
601-
const allExtensions = options.extension?.extensions(
602-
options.source,
603-
options.project,
604-
);
605-
Object.values(allExtensions).forEach((extension) => {
606-
if (extension.contributes.filters) {
607-
filterExts.push(extension);
608-
}
609-
});
610-
}
611-
return filterExts;
582+
return results;
612583
}

src/extension/extension-shared.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import SemVer from "semver/mod.ts";
88
import { Metadata } from "../config/types.ts";
99
import { ProjectContext } from "../project/types.ts";
1010

11-
export const kContributes = "contributes";
1211
export const kCommon = "common";
1312
export const kExtensionDir = "_extensions";
1413

1514
export const kTitle = "title";
1615
export const kAuthor = "author";
1716
export const kVersion = "version";
1817

18+
// TODO: rename format => formats
1919
export interface Extension extends Record<string, unknown> {
2020
id: ExtensionId;
2121
title: string;
2222
author: string;
2323
version?: SemVer;
2424
path: string;
25-
[kContributes]: {
25+
contributes: {
2626
shortcodes?: string[];
2727
filters?: string[];
2828
format?: Record<string, unknown>;
@@ -39,6 +39,12 @@ export interface ExtensionContext {
3939
input: string,
4040
project?: ProjectContext,
4141
): Extension | undefined;
42+
find(
43+
name: string,
44+
input: string,
45+
contributes: "shortcodes" | "filters" | "format",
46+
project?: ProjectContext,
47+
): Extension | undefined;
4248
}
4349

4450
export interface ExtensionId {

src/extension/extension.ts

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
extensionIdString,
2727
kAuthor,
2828
kCommon,
29-
kContributes,
3029
kExtensionDir,
3130
kTitle,
3231
kVersion,
@@ -61,9 +60,56 @@ export function createExtensionContext(): ExtensionContext {
6160
return resolveExtensionPaths(unresolved, input, project);
6261
};
6362

63+
const find = (
64+
name: string,
65+
input: string,
66+
contributes: "shortcodes" | "filters" | "format",
67+
project?: ProjectContext,
68+
): Extension | undefined => {
69+
// Filter the extension based upon what they contribute
70+
const exts = extensions(input, project).filter((ext) => {
71+
if (contributes === "shortcodes" && ext.contributes.shortcodes) {
72+
return true;
73+
} else if (contributes === "filters" && ext.contributes.filters) {
74+
return true;
75+
} else if (contributes === "format" && ext.contributes.format) {
76+
return true;
77+
} else {
78+
return false;
79+
}
80+
});
81+
82+
// Try to resolve using an exact match of the extension
83+
const exactMatch = exts.find((ext) => {
84+
const idStr = extensionIdString(ext.id);
85+
if (name === idStr) {
86+
return true;
87+
} else {
88+
return undefined;
89+
}
90+
});
91+
92+
// If there wasn't an exact match, try just using the name
93+
if (exactMatch) {
94+
return exactMatch;
95+
} else {
96+
const nameMatch = exts.find((ext) => {
97+
if (name === ext.id.name) {
98+
return true;
99+
}
100+
});
101+
if (nameMatch) {
102+
return nameMatch;
103+
} else {
104+
return undefined;
105+
}
106+
}
107+
};
108+
64109
return {
65110
extension,
66111
extensions,
112+
find,
67113
};
68114
}
69115

@@ -270,9 +316,9 @@ export function discoverExtensionPath(
270316
function validateExtension(extension: Extension) {
271317
let contribCount = 0;
272318
const contribs = [
273-
extension[kContributes].filters,
274-
extension[kContributes].shortcodes,
275-
extension[kContributes].format,
319+
extension.contributes.filters,
320+
extension.contributes.shortcodes,
321+
extension.contributes.format,
276322
];
277323
contribs.forEach((contrib) => {
278324
if (contrib) {
@@ -299,7 +345,7 @@ function readExtension(
299345
extensionFile: string,
300346
): Extension {
301347
const yaml = readYaml(extensionFile) as Metadata;
302-
const contributes = yaml[kContributes] as Metadata | undefined;
348+
const contributes = yaml.contributes as Metadata | undefined;
303349

304350
const title = yaml[kTitle] as string;
305351
const author = yaml[kAuthor] as string;
@@ -335,7 +381,7 @@ function readExtension(
335381
version,
336382
id: extensionId,
337383
path: extensionDir,
338-
[kContributes]: {
384+
contributes: {
339385
shortcodes: shortcodes.map((code) => join(extensionDir, code)),
340386
filters,
341387
format,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ export async function bookRenderItems(
357357
kBookItemChapter,
358358
bookChaptersToSidebarItems(bookInputs)
359359
.map((item) => normalizeSidebarItem(projectDir, item, context)),
360+
key === "chapters" ? 0 : 1, // next chapters under appendices, so start depth at level 1 (under appendix)
360361
);
361362
}
362363
};

src/project/types/website/listing/website-listing-read.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,16 @@ async function readContents(
541541
);
542542
}
543543
} else {
544-
const item = await listItemFromFile(file, project);
545-
if (item) {
546-
validateItem(listing, item, (field: string) => {
547-
return `The file ${file} is missing the required field '${field}'.`;
548-
});
549-
listingItemSources.add(item.source);
550-
listingItems.push(item.item);
544+
const isFile = Deno.statSync(file).isFile;
545+
if (isFile) {
546+
const item = await listItemFromFile(file, project);
547+
if (item) {
548+
validateItem(listing, item, (field: string) => {
549+
return `The file ${file} is missing the required field '${field}'.`;
550+
});
551+
listingItemSources.add(item.source);
552+
listingItems.push(item.item);
553+
}
551554
}
552555
}
553556
}

0 commit comments

Comments
 (0)