Skip to content
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

bug/giscus-dark-toggle #5020

Merged
merged 21 commits into from
May 3, 2023
Merged
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
3 changes: 3 additions & 0 deletions news/changelog-1.4.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Giscus Dark Mode Themes
- Add support for setting the Giscus light/dark themes. ([#4820](https://github.com/quarto-dev/quarto-cli/issues/4820))

## Languages

- Add Slovak translation (thanks @tom67)
Expand Down
43 changes: 43 additions & 0 deletions src/core/giscus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,46 @@ export async function getGithubDiscussionsMetadata(repo: string) {
const jsonObj = await response.json();
return jsonObj as GithubDiscussionMetadata;
}

export type GiscusThemeToggleRecord = {
baseTheme: string;
altTheme: string;
}

export type GiscusTheme = {
light?: string;
dark?: string;
} | string;

enum GiscusThemeDefault {
light = "light",
dark = "dark",
}

export const buildGiscusThemeKeys = (
darkModeDefault: boolean,
theme: GiscusTheme
): GiscusThemeToggleRecord => {
if (typeof theme === "string") {
if (theme.length > 0) {
return { baseTheme: theme, altTheme: theme };
} else {
theme = { light: GiscusThemeDefault.light, dark: GiscusThemeDefault.dark };
}
}

const themeRecord: { light: string; dark: string } = theme as {
light: string;
dark: string;
};
const result = {
baseTheme: themeRecord.light ?? GiscusThemeDefault.light,
altTheme: themeRecord.dark ?? GiscusThemeDefault.dark,
};

if (darkModeDefault) {
[result.baseTheme, result.altTheme] = [result.altTheme, result.baseTheme];
}

return result;
};
16 changes: 14 additions & 2 deletions src/format/html/format-html.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// noinspection TypeScriptUnresolvedReference

/*
* format-html.ts
*
Expand Down Expand Up @@ -88,8 +90,9 @@ import {
RenderServices,
} from "../../command/render/types.ts";
import {
buildGiscusThemeKeys,
getDiscussionCategoryId,
getGithubDiscussionsMetadata,
getGithubDiscussionsMetadata, GiscusTheme, GiscusThemeToggleRecord,
} from "../../core/giscus.ts";
import { metadataPostProcessor } from "./format-html-meta.ts";
import { kHtmlEmptyPostProcessResult } from "../../command/render/constants.ts";
Expand Down Expand Up @@ -471,8 +474,17 @@ export async function htmlFormatExtras(
// giscus
if (options.giscus) {
const giscus = options.giscus as Record<string, unknown>;

giscus.category = giscus.category || "General";
giscus.theme = giscus.theme || "light";
giscus.theme = giscus.theme || "";

const themeToggleRecord:GiscusThemeToggleRecord =
buildGiscusThemeKeys(Boolean(options.darkModeDefault), giscus.theme as GiscusTheme)

giscus.baseTheme = themeToggleRecord.baseTheme;
giscus.altTheme = themeToggleRecord.altTheme;
giscus.theme = giscus.baseTheme;

giscus.mapping = giscus.mapping || "title";
giscus["reactions-enabled"] = giscus["reactions-enabled"] !== undefined
? giscus["reactions-enabled"]
Expand Down
23 changes: 20 additions & 3 deletions src/resources/editor/tools/vs-code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8515,6 +8515,23 @@ var require_yaml_intelligence_resources = __commonJS({
"transparent_dark",
"preferred_color_scheme"
]
},
{
object: {
closed: true,
properties: {
light: {
string: {
description: "The light theme name."
}
},
dark: {
string: {
description: "The dark theme name."
}
}
}
}
}
],
description: "The giscus theme to use when displaying comments."
Expand Down Expand Up @@ -21482,12 +21499,12 @@ var require_yaml_intelligence_resources = __commonJS({
mermaid: "%%"
},
"handlers/mermaid/schema.yml": {
_internalId: 158429,
_internalId: 158441,
type: "object",
description: "be an object",
properties: {
"mermaid-format": {
_internalId: 158421,
_internalId: 158433,
type: "enum",
enum: [
"png",
Expand All @@ -21503,7 +21520,7 @@ var require_yaml_intelligence_resources = __commonJS({
exhaustiveCompletions: true
},
theme: {
_internalId: 158428,
_internalId: 158440,
type: "anyOf",
anyOf: [
{
Expand Down
23 changes: 20 additions & 3 deletions src/resources/editor/tools/yaml/web-worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions src/resources/editor/tools/yaml/yaml-intelligence-resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,23 @@
"transparent_dark",
"preferred_color_scheme"
]
},
{
"object": {
"closed": true,
"properties": {
"light": {
"string": {
"description": "The light theme name."
}
},
"dark": {
"string": {
"description": "The dark theme name."
}
}
}
}
}
],
"description": "The giscus theme to use when displaying comments."
Expand Down Expand Up @@ -14458,12 +14475,12 @@
"mermaid": "%%"
},
"handlers/mermaid/schema.yml": {
"_internalId": 158429,
"_internalId": 158441,
"type": "object",
"description": "be an object",
"properties": {
"mermaid-format": {
"_internalId": 158421,
"_internalId": 158433,
"type": "enum",
"enum": [
"png",
Expand All @@ -14479,7 +14496,7 @@
"exhaustiveCompletions": true
},
"theme": {
"_internalId": 158428,
"_internalId": 158440,
"type": "anyOf",
"anyOf": [
{
Expand Down
4 changes: 3 additions & 1 deletion src/resources/formats/html/giscus/giscus.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
crossorigin="anonymous"
<%- giscus.loading ? `data-loading=${giscus.loading}` : '' %>
async>
</script>
</script>
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
41 changes: 39 additions & 2 deletions src/resources/formats/html/templates/quarto-html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,41 @@ window.document.addEventListener("DOMContentLoaded", function (event) {
}
}


const toggleGiscusIfUsed = (isAlternate, darkModeDefault) => {
const baseTheme = document.querySelector('#giscus-base-theme')?.value ?? 'light';
const alternateTheme = document.querySelector('#giscus-alt-theme')?.value ?? 'dark';

let newTheme = '';

if(darkModeDefault) {
newTheme = isAlternate ? baseTheme : alternateTheme;
} else {
newTheme = isAlternate ? alternateTheme : baseTheme;
}

const changeGiscusTheme = () => {

// From: https://github.com/giscus/giscus/issues/336
const sendMessage = (message) => {
const iframe = document.querySelector('iframe.giscus-frame');
if (!iframe) return;
iframe.contentWindow.postMessage({ giscus: message }, 'https://giscus.app');
}

sendMessage({
setConfig: {
theme: newTheme
}
});
}

const isGiscussLoaded = window.document.querySelector('iframe.giscus-frame') !== null;
if (isGiscussLoaded) {
changeGiscusTheme();
}
}

const toggleColorMode = (alternate) => {
// Switch the stylesheets
const alternateStylesheets = window.document.querySelectorAll('link.quarto-color-scheme.quarto-color-alternate');
Expand Down Expand Up @@ -125,14 +160,16 @@ window.document.addEventListener("DOMContentLoaded", function (event) {
return localAlternateSentinel;
}
}
let localAlternateSentinel = <%= darkModeDefault ? "'alternate'" : "'default'" %>;

const darkModeDefault = <%= darkModeDefault %>;
let localAlternateSentinel = darkModeDefault ? 'alternate' : 'default';

// Dark / light mode switch
window.quartoToggleColorScheme = () => {
// Read the current dark / light value
let toAlternate = !hasAlternateSentinel();
toggleColorMode(toAlternate);
setStyleSentinel(toAlternate);
toggleGiscusIfUsed(toAlternate, darkModeDefault);
};

// Ensure there is a toggle, if there isn't float one in the top right
Expand Down
10 changes: 10 additions & 0 deletions src/resources/schema/definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@
transparent_dark,
preferred_color_scheme,
]
- object:
closed: true
properties:
light:
string:
description: The light theme name.
dark:
string:
description: The dark theme name.

description: The giscus theme to use when displaying comments.
language:
string:
Expand Down
6 changes: 5 additions & 1 deletion src/resources/types/schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ the discussions feature must be enabled. */;
| "dark_dimmed"
| "transparent_dark"
| "preferred_color_scheme"
); /* The giscus theme to use when displaying comments. */
)
| {
dark?: string /* The dark theme name. */;
light?: string; /* The light theme name. */
}; /* The giscus theme to use when displaying comments. */
};
hypothesis?: boolean | {
assetRoot?: string /* The root URL from which assets are loaded. */;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.quarto/
_site
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
project:
type: website

website:
comments:
giscus:
repo: allenmanning/giscus-examples

format:
html:
theme:
dark: darkly
light: lightly
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam dapibus mattis malesuada. Sed fringilla posuere ultricies. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.quarto/
_site
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
project:
type: website

website:
comments:
giscus:
repo: allenmanning/giscus-examples
theme:
light: light
dark: cobalt

format:
html:
theme:
dark: darkly
light: lightly
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam dapibus mattis malesuada. Sed fringilla posuere ultricies. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.quarto/
_site
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
project:
type: website

website:
comments:
giscus:
repo: allenmanning/giscus-examples
theme:
light: light
dark: dark

format:
html:
theme:
dark: darkly
light: lightly
Loading