Skip to content

Commit 80f9f30

Browse files
here is the right way to do light and dark favicon
using the media attribute of link tag unfortunately it only works on Chrome :-( but it's a very nice way TODO: also allow project.favicon to choose icon resource or have {light,dark}
1 parent 2822eba commit 80f9f30

File tree

16 files changed

+127
-17
lines changed

16 files changed

+127
-17
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ interface HtmlInjector {
209209
href: string,
210210
rel: string,
211211
type?: string,
212+
media?: string,
212213
): void;
213214

214215
injectHtml(html: string): void;
@@ -350,7 +351,7 @@ function processHtmlDependencies(
350351
// Link tags
351352
if (dependency.links) {
352353
dependency.links.forEach((link) => {
353-
injector.injectLink(link.href, link.rel, link.type);
354+
injector.injectLink(link.href, link.rel, link.type, link.media);
354355
});
355356
}
356357

@@ -515,13 +516,17 @@ function domDependencyInjector(
515516
href: string,
516517
rel: string,
517518
type?: string,
519+
media?: string,
518520
) => {
519521
const linkEl = doc.createElement("link");
520522
linkEl.setAttribute("href", pathWithForwardSlashes(href));
521523
linkEl.setAttribute("rel", rel);
522524
if (type) {
523525
linkEl.setAttribute("type", type);
524526
}
527+
if (media) {
528+
linkEl.setAttribute("media", media);
529+
}
525530
injectEl(linkEl);
526531
};
527532

@@ -575,7 +580,7 @@ function lineDependencyInjector(
575580
`<link <%= attribs %> href="<%- href %>" rel="stylesheet" />`,
576581
);
577582
const rawLinkTemplate = ld.template(
578-
`<link href="<%- href %>" rel="<%- rel %>"<% if (type) { %> type="<%- type %>"<% } %> />`,
583+
`<link href="<%- href %>" rel="<%- rel %>"<% if (type) { %> type="<%- type %>"<% } %><% if (media) { %> media="<%- media %>"<% } %> />`,
579584
);
580585

581586
const inject = (content: string, afterBody?: boolean) => {
@@ -625,6 +630,7 @@ function lineDependencyInjector(
625630
href: string,
626631
rel: string,
627632
type?: string,
633+
media?: string,
628634
) => {
629635
if (!type) {
630636
type = "";

src/config/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export interface FormatDependency {
269269
version?: string;
270270
external?: boolean;
271271
meta?: Record<string, string>;
272-
links?: { rel: string; href: string; type?: string }[];
272+
links?: { rel: string; href: string; type?: string; media?: string }[];
273273
scripts?: DependencyHtmlFile[];
274274
stylesheets?: DependencyHtmlFile[];
275275
serviceworkers?: DependencyServiceWorker[];

src/project/types/website/website.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { ProjectCreate, ProjectOutputFile, ProjectType } from "../types.ts";
2020
import {
2121
Format,
22+
FormatDependency,
2223
FormatExtras,
2324
kDependencies,
2425
kHtmlFinalizers,
@@ -179,25 +180,42 @@ export const websiteProjectType: ProjectType = {
179180
}
180181

181182
// dependency for favicon if we have one
182-
let favicon = websiteConfigString(kSiteFavicon, project.config);
183-
if (!favicon) {
183+
let faviconLight = websiteConfigString(kSiteFavicon, project.config);
184+
let faviconDark = undefined; // until schema upgrade
185+
if (!faviconLight) {
184186
const brand = await project.resolveBrand();
185187
if (brand?.light) {
186-
favicon = getFavicon(brand.light);
188+
faviconLight = getFavicon(brand.light);
189+
}
190+
if (brand?.dark) {
191+
faviconDark = getFavicon(brand.dark);
187192
}
188193
}
189-
if (favicon) {
194+
if (faviconLight || faviconDark) {
190195
const offset = projectOffset(project, source);
191196
extras.html = extras.html || {};
192197
extras.html.dependencies = extras.html.dependencies || [];
193-
extras.html.dependencies.push({
198+
const faviconDep: FormatDependency = {
194199
name: kSiteFavicon,
195-
links: [{
200+
links: [],
201+
};
202+
if (faviconDark) {
203+
faviconDep.links!.push({
196204
rel: "icon",
197-
href: offset + "/" + favicon,
198-
type: contentType(favicon),
199-
}],
200-
});
205+
href: offset + "/" + faviconDark,
206+
type: contentType(faviconDark),
207+
media: "(prefers-color-scheme:dark)",
208+
});
209+
}
210+
if (faviconLight) {
211+
faviconDep.links!.push({
212+
rel: "icon",
213+
href: offset + "/" + faviconLight,
214+
type: contentType(faviconLight),
215+
media: "(prefers-color-scheme:light)",
216+
});
217+
}
218+
extras.html.dependencies.push(faviconDep);
201219
}
202220

203221
// pagetitle for home page if it has no title

tests/docs/smoke-all/brand/logo/brand-icon-small-favicon-book/index.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
_quarto:
33
tests:
44
html:
5-
ensureFileRegexMatches:
6-
- ['<link href="./logos/small.png" rel="icon" type="image/png">']
5+
ensureHtmlElements:
6+
- ['link[href="./logos/small.png"][rel="icon"][type="image/png"]']
77
- []
88
---
99

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
color:
2+
background:
3+
light: "#fff"
4+
dark: "#111"
5+
foreground:
6+
light: "#111"
7+
dark: "#fff"
8+
logo:
9+
small:
10+
light: sun-face.png
11+
dark: moon-face.png
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project:
2+
type: website
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
brand:
3+
color:
4+
background:
5+
light: "#fff"
6+
dark: "#111"
7+
foreground:
8+
light: "#111"
9+
dark: "#fff"
10+
logo:
11+
small:
12+
light: sun-face.png
13+
dark: moon-face.png
14+
_quarto:
15+
tests:
16+
html:
17+
ensureHtmlElements:
18+
-
19+
- 'link[rel="icon"][href="./sun-face.png"][media="(prefers-color-scheme:light)"]'
20+
- 'link[rel="icon"][href="./moon-face.png"][media="(prefers-color-scheme:dark)"]'
21+
- []
22+
---
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
color:
2+
background:
3+
light: "#fff"
4+
dark: "#111"
5+
foreground:
6+
light: "#111"
7+
dark: "#fff"
8+
logo:
9+
small:
10+
light: sun-face.png
11+
dark: moon-face.png
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
project:
2+
type: website
3+
theme:
4+
light: brand
5+
dark: [brand, dark-fixups.scss]
6+
website:
7+
navbar:
8+
style: "docked"
9+
search: true
10+
contents:
11+
- index.qmd
12+
- conclusion.qmd

0 commit comments

Comments
 (0)