Skip to content

this is the right way to do light and dark favicon #13097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/command/render/pandoc-dependencies-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ interface HtmlInjector {
href: string,
rel: string,
type?: string,
media?: string,
): void;

injectHtml(html: string): void;
Expand Down Expand Up @@ -350,7 +351,7 @@ function processHtmlDependencies(
// Link tags
if (dependency.links) {
dependency.links.forEach((link) => {
injector.injectLink(link.href, link.rel, link.type);
injector.injectLink(link.href, link.rel, link.type, link.media);
});
}

Expand Down Expand Up @@ -515,13 +516,17 @@ function domDependencyInjector(
href: string,
rel: string,
type?: string,
media?: string,
) => {
const linkEl = doc.createElement("link");
linkEl.setAttribute("href", pathWithForwardSlashes(href));
linkEl.setAttribute("rel", rel);
if (type) {
linkEl.setAttribute("type", type);
}
if (media) {
linkEl.setAttribute("media", media);
}
injectEl(linkEl);
};

Expand Down Expand Up @@ -575,7 +580,7 @@ function lineDependencyInjector(
`<link <%= attribs %> href="<%- href %>" rel="stylesheet" />`,
);
const rawLinkTemplate = ld.template(
`<link href="<%- href %>" rel="<%- rel %>"<% if (type) { %> type="<%- type %>"<% } %> />`,
`<link href="<%- href %>" rel="<%- rel %>"<% if (type) { %> type="<%- type %>"<% } %><% if (media) { %> media="<%- media %>"<% } %> />`,
);

const inject = (content: string, afterBody?: boolean) => {
Expand Down Expand Up @@ -625,6 +630,7 @@ function lineDependencyInjector(
href: string,
rel: string,
type?: string,
media?: string,
) => {
if (!type) {
type = "";
Expand Down
2 changes: 1 addition & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export interface FormatDependency {
version?: string;
external?: boolean;
meta?: Record<string, string>;
links?: { rel: string; href: string; type?: string }[];
links?: { rel: string; href: string; type?: string; media?: string }[];
scripts?: DependencyHtmlFile[];
stylesheets?: DependencyHtmlFile[];
serviceworkers?: DependencyServiceWorker[];
Expand Down
38 changes: 28 additions & 10 deletions src/project/types/website/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { ProjectCreate, ProjectOutputFile, ProjectType } from "../types.ts";
import {
Format,
FormatDependency,
FormatExtras,
kDependencies,
kHtmlFinalizers,
Expand Down Expand Up @@ -179,25 +180,42 @@ export const websiteProjectType: ProjectType = {
}

// dependency for favicon if we have one
let favicon = websiteConfigString(kSiteFavicon, project.config);
if (!favicon) {
let faviconLight = websiteConfigString(kSiteFavicon, project.config);
let faviconDark = undefined; // until schema upgrade
if (!faviconLight) {
const brand = await project.resolveBrand();
if (brand?.light) {
favicon = getFavicon(brand.light);
faviconLight = getFavicon(brand.light);
}
if (brand?.dark) {
faviconDark = getFavicon(brand.dark);
}
}
if (favicon) {
if (faviconLight || faviconDark) {
const offset = projectOffset(project, source);
extras.html = extras.html || {};
extras.html.dependencies = extras.html.dependencies || [];
extras.html.dependencies.push({
const faviconDep: FormatDependency = {
name: kSiteFavicon,
links: [{
links: [],
};
if (faviconDark) {
faviconDep.links!.push({
rel: "icon",
href: offset + "/" + favicon,
type: contentType(favicon),
}],
});
href: offset + "/" + faviconDark,
type: contentType(faviconDark),
media: "(prefers-color-scheme:dark)",
});
}
if (faviconLight) {
faviconDep.links!.push({
rel: "icon",
href: offset + "/" + faviconLight,
type: contentType(faviconLight),
media: "(prefers-color-scheme:light)",
});
}
extras.html.dependencies.push(faviconDep);
}

// pagetitle for home page if it has no title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
_quarto:
tests:
html:
ensureFileRegexMatches:
- ['<link href="./logos/small.png" rel="icon" type="image/png">']
ensureHtmlElements:
- ['link[href="./logos/small.png"][rel="icon"][type="image/png"]']
- []
---

Expand Down
1 change: 1 addition & 0 deletions tests/docs/smoke-all/brand/logo/favicon/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
11 changes: 11 additions & 0 deletions tests/docs/smoke-all/brand/logo/favicon/_brand.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
color:
background:
light: "#fff"
dark: "#111"
foreground:
light: "#111"
dark: "#fff"
logo:
small:
light: sun-face.png
dark: moon-face.png
2 changes: 2 additions & 0 deletions tests/docs/smoke-all/brand/logo/favicon/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
project:
type: website
22 changes: 22 additions & 0 deletions tests/docs/smoke-all/brand/logo/favicon/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
brand:
color:
background:
light: "#fff"
dark: "#111"
foreground:
light: "#111"
dark: "#fff"
logo:
small:
light: sun-face.png
dark: moon-face.png
_quarto:
tests:
html:
ensureHtmlElements:
-
- 'link[rel="icon"][href="./sun-face.png"][media="(prefers-color-scheme:light)"]'
- 'link[rel="icon"][href="./moon-face.png"][media="(prefers-color-scheme:dark)"]'
- []
---
11 changes: 11 additions & 0 deletions tests/docs/smoke-all/brand/logo/navbar/favicon/_brand.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
color:
background:
light: "#fff"
dark: "#111"
foreground:
light: "#111"
dark: "#fff"
logo:
small:
light: sun-face.png
dark: moon-face.png
12 changes: 12 additions & 0 deletions tests/docs/smoke-all/brand/logo/navbar/favicon/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
project:
type: website
theme:
light: brand
dark: [brand, dark-fixups.scss]
website:
navbar:
style: "docked"
search: true
contents:
- index.qmd
- conclusion.qmd
3 changes: 3 additions & 0 deletions tests/docs/smoke-all/brand/logo/navbar/favicon/conclusion.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: conclusion
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*-- scss:rules --*/

nav.sidebar.sidebar-navigation:not(.rollup) {
background-color: #282b30;
}

nav.navbar {
background-color: rgba(13, 108, 251, 25%);
}
15 changes: 15 additions & 0 deletions tests/docs/smoke-all/brand/logo/navbar/favicon/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: intro
_quarto:
tests:
html:
ensureHtmlElements:
-
# favicon
- 'link[rel="icon"][href="./sun-face.png"][media="(prefers-color-scheme:light)"]'
- 'link[rel="icon"][href="./moon-face.png"][media="(prefers-color-scheme:dark)"]'
# navbar
- 'img[class*="light-content"][src="./sun-face.png"][alt=""]'
- 'img[class*="dark-content"][src="./moon-face.png"][alt=""]'
- []
---
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/docs/smoke-all/brand/logo/website-favicon/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ title: "test-brand-favicon"
_quarto:
tests:
html:
ensureFileRegexMatches:
- ['<link href="./logos/small.png" rel="icon" type="image/png">']
ensureHtmlElements:
- ['link[href="./logos/small.png"][rel="icon"][type="image/png"]']
- []
---

Expand Down
Loading