Skip to content

Commit 23dc008

Browse files
committed
Quartz sync: Jun 12, 2025, 10:28 AM
1 parent 9f225d1 commit 23dc008

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

quartz/components/scripts/slide.inline.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ function renderMermaidInSlide() {
118118
}
119119

120120

121+
function enableYouTubeJSAPI(html: string): string {
122+
return html.replace(/<iframe[^>]*?>/gi, (iframeTag) => {
123+
const srcMatch = iframeTag.match(/src=["']([^"']+)["']/i);
124+
if (!srcMatch) return iframeTag;
125+
126+
const src = srcMatch[1];
127+
128+
if (!src.includes("youtube.com/embed")) return iframeTag;
129+
if (src.includes("enablejsapi=1")) return iframeTag;
130+
131+
const newSrc = src.includes("?")
132+
? `${src}&enablejsapi=1`
133+
: `${src}?enablejsapi=1`;
134+
135+
const updatedTag = iframeTag.replace(src, newSrc);
136+
return updatedTag;
137+
});
138+
}
139+
121140
function anchorBlank(html: string): string {
122141
// return html.replace(/<a\b([^>]*?)>/g, '<a $1 target="_blank">')
123142
// return html.replace(/(?<!<sup>)<a\b([^>]*?)>/g, '<a $1 target="_blank">')
@@ -289,23 +308,18 @@ function appendRemark(option: SlideOptions) {
289308

290309
const body = document.querySelector(".center article")?.innerHTML
291310

292-
// sorry callback hell
293-
const data =
294-
anchorBlank(
295-
unwrapSlideNote(
296-
unwrapFootnotesSection(
297-
handleFootnote(
298-
handleIndex(
299-
injectSeparators(header + (option.tags ? tags : "") + body, separator),
300-
separator,
301-
option,
302-
makeIndex()
303-
),
304-
separator
305-
),
306-
)
307-
)
308-
)
311+
const pipe = <T>(value: T, ...fns: ((val: T) => T)[]): T =>
312+
fns.reduce((acc, fn) => fn(acc), value);
313+
314+
const data = pipe(
315+
injectSeparators(header + (option.tags ? tags : "") + body, separator),
316+
(d) => handleIndex(d, separator, option, makeIndex()),
317+
(d) => handleFootnote(d, separator),
318+
unwrapFootnotesSection,
319+
unwrapSlideNote,
320+
enableYouTubeJSAPI,
321+
anchorBlank
322+
);
309323

310324
document.body.innerHTML = ""
311325

@@ -332,8 +346,10 @@ function appendRemark(option: SlideOptions) {
332346
function paramOption(defaultOption: SlideOptions) {
333347
const params = new URLSearchParams(window.location.search)
334348

335-
const getBool = (key: string, fallback = false) =>
336-
params.has(key) ? params.get(key)?.toLowerCase() === "true" : fallback
349+
const getBool = (key: string, fallback = false) => {
350+
const value = params.get(key)?.toLowerCase();
351+
return value === "true" ? true : value === "false" ? false : fallback;
352+
};
337353

338354
type Ratio = SlideOptions["ratio"]
339355
const validRatios: Ratio[] = ["4:3", "16:9"];

quartz/static/scripts/slide.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27625,6 +27625,29 @@ function Navigation (events) {
2762527625
return currentSlideIndex;
2762627626
}
2762727627

27628+
function pauseAllMedia() {
27629+
// Pause all <video> and <audio>
27630+
document.querySelectorAll('video, audio').forEach((el) => {
27631+
if (!el.paused) el.pause();
27632+
});
27633+
27634+
// Attempt to pause YouTube iframes
27635+
document.querySelectorAll('iframe').forEach((iframe) => {
27636+
try {
27637+
iframe.contentWindow?.postMessage(
27638+
JSON.stringify({
27639+
event: 'command',
27640+
func: 'pauseVideo',
27641+
args: [],
27642+
}),
27643+
'*'
27644+
);
27645+
} catch (e) {
27646+
console.warn('Could not pause iframe:', e);
27647+
}
27648+
});
27649+
}
27650+
2762827651
function gotoSlideByIndex(slideIndex, noMessage) {
2762927652
var alreadyOnSlide = slideIndex === currentSlideIndex
2763027653
, slideOutOfRange = slideIndex < 0 || slideIndex > self.getSlideCount()-1
@@ -27636,6 +27659,8 @@ function Navigation (events) {
2763627659
return;
2763727660
}
2763827661

27662+
pauseAllMedia()
27663+
2763927664
if (currentSlideIndex !== -1) {
2764027665
events.emit('hideSlide', currentSlideIndex, false);
2764127666
}

0 commit comments

Comments
 (0)