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

EthicalAd: specific placement for Antora and mdBook #475

Merged
merged 14 commits into from
Jan 14, 2025
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";
22 changes: 22 additions & 0 deletions src/ethicalads.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ export class EthicalAdsAddon extends AddonBase {
placement.classList.add("ethical-alabaster");
placement.classList.add("ethical-docsify");

placement.setAttribute("data-ea-type", "readthedocs-sidebar");
placement.setAttribute("data-ea-style", "image");
knownPlacementFound = true;
}
} else if (docTool.isAntora()) {
selector = "aside nav.nav-menu";
element = document.querySelector(selector);

if (this.elementAboveTheFold(element)) {
placement.classList.add("ethical-alabaster");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename this something more generic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We should use something like ethical-light-theme; considering that we are using it for that use case. However, it's not a 1:1 match. We will need to keep -alabaster as well since there are some thing very specific to it, I think. I opened #477


placement.setAttribute("data-ea-type", "readthedocs-sidebar");
placement.setAttribute("data-ea-style", "image");
knownPlacementFound = true;
}
} else if (docTool.isMdBook()) {
selector = "nav#sidebar mdbook-sidebar-scrollbox";
element = document.querySelector(selector);

if (this.elementAboveTheFold(element)) {
placement.classList.add("ethical-alabaster");

placement.setAttribute("data-ea-type", "readthedocs-sidebar");
placement.setAttribute("data-ea-style", "image");
knownPlacementFound = true;
Expand Down
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 */
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 = {};
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}"
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