Skip to content

Commit 8c2ca7e

Browse files
committed
Ensure that we place Quarto dependencies ahread of header-includes
1 parent d67fb27 commit 8c2ca7e

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { join } from "path/mod.ts";
99

1010
import * as ld from "../../core/lodash.ts";
1111

12-
import { Document, Element } from "../../core/deno-dom.ts";
12+
import { Document, Element, NodeType } from "../../core/deno-dom.ts";
1313

1414
import { pathWithForwardSlashes } from "../../core/path.ts";
1515

@@ -65,6 +65,8 @@ export function readAndInjectDependencies(
6565
if (htmlDependencies.length > 0) {
6666
const injector = domDependencyInjector(doc);
6767
processHtmlDependencies(htmlDependencies, inputDir, libDir, injector);
68+
// Finalize the injection
69+
injector.finalizeInjection();
6870
}
6971

7072
return Promise.resolve({
@@ -93,6 +95,8 @@ export function resolveDependencies(
9395
libDir,
9496
injector,
9597
);
98+
// Finalize the injection
99+
injector.finalizeInjection();
96100

97101
delete extras.html?.[kDependencies];
98102

@@ -144,6 +148,8 @@ interface HtmlInjector {
144148
injectHtml(html: string): void;
145149

146150
injectMeta(meta: Record<string, string>): void;
151+
152+
finalizeInjection(): void;
147153
}
148154

149155
function processHtmlDependencies(
@@ -225,9 +231,30 @@ function processHtmlDependencies(
225231
}
226232
}
227233

234+
const kDependencyTarget = "htmldependencies:E3FAD763";
235+
228236
function domDependencyInjector(
229237
doc: Document,
230238
): HtmlInjector {
239+
// Locates the placeholder target for inserting content
240+
const findTargetComment = () => {
241+
for (const node of doc.head.childNodes) {
242+
if (node.nodeType === NodeType.COMMENT_NODE) {
243+
if (
244+
node.textContent &&
245+
node.textContent.trim() === kDependencyTarget
246+
) {
247+
return node;
248+
}
249+
}
250+
}
251+
252+
// We couldn't find a placeholder comment, just insert
253+
// the nodes at the front of the head
254+
return doc.head.firstChild;
255+
};
256+
const targetComment = findTargetComment();
257+
231258
const injectEl = (
232259
el: Element,
233260
attribs?: Record<string, string>,
@@ -239,11 +266,11 @@ function domDependencyInjector(
239266
}
240267
}
241268
if (!afterBody) {
242-
doc.head.appendChild(el);
243-
doc.head.appendChild(doc.createTextNode("\n"));
269+
doc.head.insertBefore(doc.createTextNode("\n"), targetComment);
270+
doc.head.insertBefore(el, targetComment);
244271
} else {
245272
doc.body.appendChild(el);
246-
doc.head.appendChild(doc.createTextNode("\n"));
273+
doc.body.appendChild(doc.createTextNode("\n"));
247274
}
248275
};
249276

@@ -299,12 +326,18 @@ function domDependencyInjector(
299326
});
300327
};
301328

329+
const finalizeInjection = () => {
330+
// Remove the target comment
331+
targetComment.remove();
332+
};
333+
302334
return {
303335
injectScript,
304336
injectStyle,
305337
injectLink,
306338
injectMeta,
307339
injectHtml,
340+
finalizeInjection,
308341
};
309342
}
310343

@@ -393,11 +426,15 @@ function lineDependencyInjector(
393426
});
394427
};
395428

429+
const finalizeInjection = () => {
430+
};
431+
396432
return {
397433
injectScript,
398434
injectStyle,
399435
injectLink,
400436
injectMeta,
401437
injectHtml,
438+
finalizeInjection,
402439
};
403440
}

src/resources/formats/html/pandoc/template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
$styles.html()$
1010
</style>
1111

12+
<!-- htmldependencies:E3FAD763 -->
1213
$for(header-includes)$
1314
$header-includes$
1415
$endfor$

0 commit comments

Comments
 (0)