A platform for community discussion. Free, open, simple.
import Component from "@glimmer/component";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { trustHTML } from "@ember/template";
import DTooltip from "discourse/float-kit/components/d-tooltip";
import { apiInitializer } from "discourse/lib/api";
class InlineFootnote extends Component {
@action
preventDefault(event) {
event.preventDefault();
}
<template>
<DTooltip
@closeOnClickOutside={{true}}
@closeOnScroll={{false}}
@identifier="inline-footnote"
@interactive={{true}}
>
<:trigger>
{{! eslint-disable ember/template-no-invalid-link-text }}
<a
class="expand-footnote"
data-footnote-content={{@data.footnoteContent}}
data-footnote-id={{@data.footnoteId}}
href
role="button"
{{on "click" this.preventDefault}}
></a>
</:trigger>
<:content>
{{trustHTML @data.footnoteContent}}
</:content>
</DTooltip>
</template>
}
export default apiInitializer((api) => {
api.decorateCookedElement((elem, helper) => {
if (
!api.container.lookup("service:site-settings").display_footnotes_inline
) {
return;
}
const footnoteRefs = elem.querySelectorAll("sup.footnote-ref");
footnoteRefs.forEach((footnoteRef) => {
const refLink = footnoteRef.querySelector("a");
if (!refLink) {
return;
}
const footnoteId = refLink.getAttribute("href");
const footnoteElement = elem.querySelector(footnoteId)?.cloneNode(true);
footnoteElement
?.querySelectorAll("sup.footnote-ref, .footnote-backref")
.forEach((element) => element.remove());
const footnoteContent = footnoteElement?.innerHTML;
const expandableFootnote = document.createElement("span");
expandableFootnote.className = "inline-footnote";
footnoteRef.replaceWith(expandableFootnote);
helper.renderGlimmer(expandableFootnote, InlineFootnote, {
footnoteId,
footnoteContent,
});
});
if (footnoteRefs.length) {
elem.classList.add("inline-footnotes");
}
});
});