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

Add styles based on the documentation tool #473

Merged
merged 20 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 10 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
7 changes: 7 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export const ASCIIDOCTOR = "asciidoctor";
export const JEKYLL = "jekyll";
export const DOCSIFY = "docsify";
export const ANTORA = "antora";
export const MDBOOK = "mdbook";
export const FALLBACK_DOCTOOL = "fallback";

// Known documentation tools themes
export const SPHINX_ALABASTER = "alabaster";
export const SPHINX_FURO = "furo";
export const SPHINX_READTHEDOCS = "readthedocs";
export const SPHINX_IMMATERIAL = "immaterial";
25 changes: 25 additions & 0 deletions src/flyout.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
height: auto;
max-height: calc(100% - 100px);
overflow-y: auto;
line-height: 1rem;
}

.container.bottom-right {
Expand Down Expand Up @@ -155,3 +156,27 @@ small a {
text-decoration: none;
color: var(--readthedocs-flyout-link-color, rgb(42, 128, 185));
}

/* Specific styles based on documentation tools and themes */
div[tool="mkdocs-material"] {
--addons-flyout-font-size: 0.65rem;
line-height: 0.8rem;
}

div[tool="antora"] {
--addons-flyout-font-size: 0.7rem;
line-height: 0.9rem;
}

div[tool="mdbook"] {
--addons-flyout-font-size: 1.3rem;
}

div[tool="sphinx"][tool-theme="furo"] {
--addons-flyout-font-size: 0.75rem;
}

div[tool="sphinx"][tool-theme="immaterial"] {
--addons-flyout-font-size: 0.65rem;
line-height: 0.8rem;
}
20 changes: 15 additions & 5 deletions src/flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { classMap } from "lit/directives/class-map.js";
import { default as objectPath } from "object-path";

import styleSheet from "./flyout.css";
import { AddonBase, addUtmParameters, getLinkWithFilename } from "./utils";
import {
AddonBase,
addUtmParameters,
getLinkWithFilename,
docTool,
} from "./utils";
import { SPHINX, MKDOCS_MATERIAL } from "./constants";
import {
EVENT_READTHEDOCS_SEARCH_SHOW,
EVENT_READTHEDOCS_FLYOUT_HIDE,
Expand All @@ -31,9 +37,12 @@ export class FlyoutElement extends LitElement {
super();

this.config = null;
this.classes = {};
humitos marked this conversation as resolved.
Show resolved Hide resolved
this.opened = false;
this.floating = true;
this.position = "bottom-right";
this.classes = { floating: this.floating, container: true };
this.classes[this.position] = true;
this.readthedocsLogo = READTHEDOCS_LOGO;
}

Expand Down Expand Up @@ -310,11 +319,12 @@ export class FlyoutElement extends LitElement {
return nothing;
}

const classes = { floating: this.floating, container: true };
classes[this.position] = true;

return html`
<div class=${classMap(classes)}>
<div
tool="${docTool.documentationTool}"
tool-theme="${docTool.documentationTheme}"
humitos marked this conversation as resolved.
Show resolved Hide resolved
class=${classMap(this.classes)}
>
${this.renderHeader()}
<main class=${classMap({ closed: !this.opened })}>
${this.renderLanguages()} ${this.renderVersions()}
Expand Down
74 changes: 73 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { ajv } from "./data-validation";
import { default as objectPath } from "object-path";
import {
SPHINX,
SPHINX_FURO,
SPHINX_ALABASTER,
SPHINX_READTHEDOCS,
SPHINX_IMMATERIAL,
MDBOOK,
MKDOCS,
MKDOCS_MATERIAL,
DOCUSAURUS,
Expand Down Expand Up @@ -297,6 +302,7 @@ export class DocumentationTool {

constructor() {
this.documentationTool = this.getDocumentationTool();
this.documentationTheme = this.getDocumentationTheme();
console.debug(`Documentation tool detected: ${this.documentationTool}`);
}

Expand Down Expand Up @@ -411,10 +417,63 @@ export class DocumentationTool {
return DOCSIFY;
}

if (this.isMdBook()) {
return MDBOOK;
}

if (this.isAntora()) {
return ANTORA;
}

console.debug("We were not able to detect the documentation tool.");
return null;
}

getDocumentationTheme() {
const documentationTool =
this.documentationTool || this.getDocumentationTool();

if (documentationTool === SPHINX) {
if (this.isSphinxAlabasterLikeTheme()) {
return SPHINX_ALABASTER;
} else if (this.isSphinxReadTheDocsLikeTheme()) {
return SPHINX_READTHEDOCS;
} else if (this.isSphinxFuroLikeTheme()) {
return SPHINX_FURO;
} else if (this.isSphinxImmaterialLikeTheme()) {
return SPHINX_IMMATERIAL;
}
}

// TODO: add the other known themes
return null;
}

isAntora() {
if (
document.querySelectorAll('meta[name="generator"][content^="Antora"]')
.length
) {
return true;
}
return false;
}

isMdBook() {
// <head>
// <!-- Book generated using mdBook -->
// <meta charset="UTF-8">
// ...
if (
document.head.firstChild.nextSibling.textContent.includes(
"Book generated using mdBook",
)
) {
return true;
}
return false;
}

isDocsify() {
if (document.querySelectorAll("head > link[href*=docsify]").length) {
return true;
Expand All @@ -427,7 +486,8 @@ export class DocumentationTool {
this.isSphinxAlabasterLikeTheme() ||
this.isSphinxReadTheDocsLikeTheme() ||
this.isSphinxFuroLikeTheme() ||
this.isSphinxBookThemeLikeTheme()
this.isSphinxBookThemeLikeTheme() ||
this.isSphinxImmaterialLikeTheme()
);
}

Expand Down Expand Up @@ -531,6 +591,18 @@ export class DocumentationTool {
return false;
}

isSphinxImmaterialLikeTheme() {
if (
document.querySelectorAll(
'link[href^="_static/sphinx_immaterial_theme"]',
'a[href="https://github.com/jbms/sphinx-immaterial/"][rel="noopener"]',
).length
) {
return true;
}
return false;
}

isMaterialMkDocsTheme() {
if (
document.querySelectorAll(
Expand Down
12 changes: 10 additions & 2 deletions tests/__snapshots__/flyout.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
export const snapshots = {};

snapshots["Flyout addon snapshot flyout completely"] =
`<div class="bottom-right container floating">
`<div
class="bottom-right container floating"
tool=""
tool-theme=""
>
<header>
<img
alt="Read the Docs"
Expand Down Expand Up @@ -119,7 +123,11 @@ snapshots["Flyout addon snapshot flyout completely"] =
/* end snapshot Flyout addon snapshot flyout completely */

snapshots["Flyout addon snapshot flyout with search disabled"] =
`<div class="bottom-right container floating">
`<div
class="bottom-right container floating"
tool=""
tool-theme=""
>
<header>
<img
alt="Read the Docs"
Expand Down