2022-12-22 16:03:08 +00:00
|
|
|
import { docLink } from "@goauthentik/common/global";
|
2022-12-19 11:48:02 +00:00
|
|
|
import "@goauthentik/elements/Alert";
|
2023-01-13 15:22:03 +00:00
|
|
|
import { Level } from "@goauthentik/elements/Alert";
|
2022-09-14 22:05:21 +00:00
|
|
|
import { AKElement } from "@goauthentik/elements/Base";
|
|
|
|
|
|
|
|
import { CSSResult, TemplateResult, html } from "lit";
|
2021-12-16 11:10:46 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
|
|
|
import { unsafeHTML } from "lit/directives/unsafe-html.js";
|
|
|
|
|
2022-09-14 22:05:21 +00:00
|
|
|
import AKGlobal from "@goauthentik/common/styles/authentik.css";
|
2021-12-16 11:10:46 +00:00
|
|
|
import PFContent from "@patternfly/patternfly/components/Content/content.css";
|
|
|
|
import PFList from "@patternfly/patternfly/components/List/list.css";
|
|
|
|
|
2021-12-16 11:18:43 +00:00
|
|
|
export interface MarkdownDocument {
|
|
|
|
html: string;
|
|
|
|
metadata: { [key: string]: string };
|
|
|
|
filename: string;
|
2022-12-19 11:48:02 +00:00
|
|
|
path: string;
|
2021-12-16 11:18:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 11:48:02 +00:00
|
|
|
export type Replacer = (input: string, md: MarkdownDocument) => string;
|
|
|
|
|
2021-12-16 11:10:46 +00:00
|
|
|
@customElement("ak-markdown")
|
2022-09-14 22:05:21 +00:00
|
|
|
export class Markdown extends AKElement {
|
2021-12-16 11:18:43 +00:00
|
|
|
@property({ attribute: false })
|
|
|
|
md?: MarkdownDocument;
|
2021-12-16 11:10:46 +00:00
|
|
|
|
2022-12-19 11:48:02 +00:00
|
|
|
@property({ attribute: false })
|
|
|
|
replacers: Replacer[] = [];
|
|
|
|
|
|
|
|
defaultReplacers: Replacer[] = [
|
|
|
|
this.replaceAdmonitions,
|
|
|
|
this.replaceList,
|
|
|
|
this.replaceRelativeLinks,
|
|
|
|
];
|
|
|
|
|
2021-12-16 11:10:46 +00:00
|
|
|
static get styles(): CSSResult[] {
|
|
|
|
return [PFList, PFContent, AKGlobal];
|
|
|
|
}
|
|
|
|
|
2022-12-19 11:48:02 +00:00
|
|
|
replaceAdmonitions(input: string): string {
|
|
|
|
const admonitionStart = /:::(\w+)<br\s\/>/gm;
|
|
|
|
const admonitionEnd = /:::/gm;
|
2023-01-13 15:22:03 +00:00
|
|
|
return (
|
|
|
|
input
|
|
|
|
.replaceAll(admonitionStart, "<ak-alert level='pf-m-$1'>")
|
|
|
|
.replaceAll(admonitionEnd, "</ak-alert>")
|
|
|
|
// Workaround for admonitions using caution instead of warning
|
|
|
|
.replaceAll("pf-m-caution", Level.Warning)
|
|
|
|
);
|
2022-12-19 11:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceList(input: string): string {
|
|
|
|
return input.replace("<ul>", "<ul class='pf-c-list'>");
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceRelativeLinks(input: string, md: MarkdownDocument): string {
|
|
|
|
const relativeLink = /href=".(.*)"/gm;
|
|
|
|
const cwd = process.env.CWD as string;
|
|
|
|
// cwd will point to $root/web, but the docs are in $root/website/docs
|
|
|
|
let relPath = md.path.replace(cwd + "site", "");
|
|
|
|
if (md.filename === "index.md") {
|
|
|
|
relPath = relPath.replace("index.md", "");
|
|
|
|
}
|
2022-12-22 16:03:08 +00:00
|
|
|
const baseURL = docLink("");
|
2022-12-19 11:48:02 +00:00
|
|
|
const fullURL = `${baseURL}${relPath}.$1`;
|
|
|
|
return input.replace(relativeLink, `href="${fullURL}" target="_blank"`);
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:10:46 +00:00
|
|
|
render(): TemplateResult {
|
|
|
|
if (!this.md) {
|
|
|
|
return html``;
|
|
|
|
}
|
2022-12-19 11:48:02 +00:00
|
|
|
let finalHTML = this.md.html;
|
|
|
|
const replacers = [...this.defaultReplacers, ...this.replacers];
|
|
|
|
replacers.forEach((r) => {
|
|
|
|
if (!this.md) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
finalHTML = r(finalHTML, this.md);
|
|
|
|
});
|
2021-12-16 11:18:43 +00:00
|
|
|
return html`${this.md?.metadata.title ? html`<h2>${this.md.metadata.title}</h2>` : html``}
|
|
|
|
${unsafeHTML(finalHTML)}`;
|
2021-12-16 11:10:46 +00:00
|
|
|
}
|
|
|
|
}
|