Skip to content

Commit 55dea38

Browse files
committed
Ensure that all contributors to dependencies append
HTML dependencies were overwriting format-resources which were contributed earlier in the pipeline. This change ensures we’re always appending and adds type safety + exposes typed ability for typescript to contribute all types of supported resources. Fixes #1538
1 parent 25e8247 commit 55dea38

File tree

5 files changed

+108
-26
lines changed

5 files changed

+108
-26
lines changed

news/changelog-1.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373

7474
- support `echo: true` and other per-document settings (#1485)
7575

76+
## Extensions
77+
78+
- Properly copy `format-resources` for HTML based formats
79+
7680
## Publishing
7781

7882
- Detect authentication error for quarto.pub and re-establish credentials

src/command/render/pandoc-dependencies-html.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ import { lines } from "../../core/lib/text.ts";
2525
import { copyResourceFile } from "../../project/project-resources.ts";
2626
import { copyFileIfNewer } from "../../core/copy.ts";
2727
import { ProjectContext } from "../../project/types.ts";
28+
import {
29+
appendDependencies,
30+
HtmlFormatDependency,
31+
} from "./pandoc-dependencies.ts";
2832

2933
export function writeDependencies(
3034
dependenciesFile: string,
3135
extras: FormatExtras,
3236
) {
3337
if (extras.html?.[kDependencies]) {
34-
const dependencyLines: string[] = [];
35-
for (const dependency of extras.html?.[kDependencies]!) {
36-
dependencyLines.push(
37-
JSON.stringify({ type: "html", content: dependency }),
38-
);
39-
}
38+
const dependencies: HtmlFormatDependency[] = extras.html[kDependencies]!
39+
.map((dep) => {
40+
return {
41+
type: "html",
42+
content: dep,
43+
};
44+
});
4045

41-
if (dependencyLines.length > 0) {
42-
Deno.writeTextFileSync(
43-
dependenciesFile,
44-
`${dependencyLines.join("\n")}\n`,
45-
);
46-
}
46+
appendDependencies(dependenciesFile, dependencies);
4747
}
4848
}
4949

src/command/render/pandoc-dependencies-resources.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { copyTo } from "../../core/copy.ts";
1010
import { lines } from "../../core/text.ts";
1111

1212
import { basename, join } from "path/mod.ts";
13+
import {
14+
appendDependencies,
15+
FormatResourceDependency,
16+
} from "./pandoc-dependencies.ts";
1317

1418
export interface FormatResource {
1519
file: string;
@@ -27,20 +31,13 @@ export function writeFormatResources(
2731
? formatResources
2832
: [formatResources];
2933

30-
const dependencyLines: string[] = [];
31-
for (const file of files) {
32-
const path = join(inputDir, file);
33-
// Ensure this is input relative
34-
dependencyLines.push(
35-
JSON.stringify({ type: kFormatResources, content: { file: path } }),
36-
);
37-
}
38-
if (dependencyLines.length > 0) {
39-
Deno.writeTextFileSync(
40-
dependenciesFile,
41-
`${dependencyLines.join("\n")}\n`,
42-
);
43-
}
34+
const dependencies: FormatResourceDependency[] = files.map((file) => {
35+
return {
36+
type: kFormatResources,
37+
content: { file: join(inputDir, file) },
38+
};
39+
});
40+
appendDependencies(dependenciesFile, dependencies);
4441
}
4542
}
4643

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* pandoc-dependencies.ts
3+
*
4+
* Copyright (C) 2020 by RStudio, PBC
5+
*
6+
*/
7+
8+
import { FormatDependency } from "../../config/types.ts";
9+
import { appendTextFile } from "../../core/file.ts";
10+
11+
export interface HtmlFormatDependency {
12+
type: "html";
13+
content: FormatDependency;
14+
}
15+
16+
export interface FormatResourceDependency {
17+
type: "format-resources";
18+
content: {
19+
file: string;
20+
};
21+
}
22+
23+
export interface TextDependency {
24+
type: "text";
25+
content: {
26+
text: string;
27+
location: "before-body" | "after-body" | "in-header";
28+
};
29+
}
30+
31+
export interface FileDependency {
32+
type: "file";
33+
content: {
34+
path: string;
35+
};
36+
}
37+
38+
export interface UsePackageDependency {
39+
type: "usepackage";
40+
content: {
41+
package: string;
42+
options: string;
43+
};
44+
}
45+
46+
export function appendDependencies(
47+
dependenciesFile: string,
48+
dependencies: Array<
49+
| HtmlFormatDependency
50+
| FormatResourceDependency
51+
| TextDependency
52+
| FileDependency
53+
| UsePackageDependency
54+
>,
55+
) {
56+
const dependencyLines: string[] = [];
57+
for (const dependency of dependencies) {
58+
dependencyLines.push(
59+
JSON.stringify(dependency),
60+
);
61+
}
62+
if (dependencyLines.length > 0) {
63+
appendTextFile(
64+
dependenciesFile,
65+
`${dependencyLines.join("\n")}\n`,
66+
);
67+
}
68+
}

src/core/file.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ export async function visitLines(
3737
}
3838
}
3939

40+
export async function appendTextFile(
41+
path: string,
42+
text: string,
43+
) {
44+
const file = await Deno.open(path, { append: true });
45+
try {
46+
const encoder = new TextEncoder();
47+
file.writeSync(encoder.encode(text));
48+
} finally {
49+
file.close();
50+
}
51+
}
52+
4053
export async function touch(path: string) {
4154
if (Deno.build.os === "windows") {
4255
// Touch the file be rewriting it

0 commit comments

Comments
 (0)