website: remove hacky component injection code

This commit is contained in:
Yangshun
2021-08-07 22:41:29 +08:00
parent e3411ef50d
commit 348ad0c907
6 changed files with 129 additions and 90 deletions

View File

@ -4,10 +4,7 @@ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import styles from './styles.module.css';
const AD_ELEMENT_ID = 'ad-element-id';
const CONTAINER_SELECTOR = '[class^="tableOfContents"]';
function SidebarAd() {
export default function SidebarAd() {
return (
<a
className={styles.container}
@ -24,45 +21,3 @@ function SidebarAd() {
</a>
);
}
function initAd() {
const $adEl = (() => {
const $el = document.getElementById(AD_ELEMENT_ID);
if ($el) {
return $el;
}
const $tocEl = document.querySelector(CONTAINER_SELECTOR);
if ($tocEl == null) {
return null;
}
const $newEl = document.createElement('div');
$newEl.id = AD_ELEMENT_ID;
$tocEl.prepend($newEl);
return $newEl;
})();
if ($adEl == null) {
return;
}
ReactDOM.render(<SidebarAd />, $adEl);
}
if (ExecutionEnvironment.canUseDOM) {
window.onload = initAd;
}
export default (function (context, options) {
return {
name: 'sidebar-ad',
onRouteUpdate() {
// Render only after the page renders.
setTimeout(() => {
initAd();
}, 0);
},
};
})();