mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-10 19:40:25 +00:00
Add support for new GitHub alerts (#19470)
This commit is contained in:
@@ -3,7 +3,14 @@ import { customElement, property } from "lit/decorators";
|
||||
import { fireEvent } from "../common/dom/fire_event";
|
||||
import { renderMarkdown } from "../resources/render-markdown";
|
||||
|
||||
const _blockQuoteToAlert = { Note: "info", Warning: "warning" };
|
||||
const _legacyGitHubBlockQuoteToAlert = { Note: "info", Warning: "warning" };
|
||||
const _gitHubBlockQuoteToAlert = {
|
||||
"[!NOTE]": "info",
|
||||
"[!TIP]": "success",
|
||||
"[!IMPORTANT]": "info",
|
||||
"[!WARNING]": "warning",
|
||||
"[!CAUTION]": "error",
|
||||
};
|
||||
|
||||
@customElement("ha-markdown-element")
|
||||
class HaMarkdownElement extends ReactiveElement {
|
||||
@@ -71,18 +78,25 @@ class HaMarkdownElement extends ReactiveElement {
|
||||
// Map GitHub blockquote elements to our ha-alert element
|
||||
const firstElementChild = node.firstElementChild;
|
||||
const quoteTitleElement = firstElementChild?.firstElementChild;
|
||||
const quoteType =
|
||||
const blockQuoteType =
|
||||
firstElementChild?.firstChild?.textContent &&
|
||||
_gitHubBlockQuoteToAlert[firstElementChild.firstChild.textContent];
|
||||
const legacyBlockQuoteType =
|
||||
!blockQuoteType &&
|
||||
quoteTitleElement?.textContent &&
|
||||
_blockQuoteToAlert[quoteTitleElement.textContent];
|
||||
_legacyGitHubBlockQuoteToAlert[quoteTitleElement.textContent];
|
||||
|
||||
// GitHub is strict on how these are defined, we need to make sure we know what we have before starting to replace it
|
||||
if (quoteTitleElement?.nodeName === "STRONG" && quoteType) {
|
||||
if (
|
||||
blockQuoteType ||
|
||||
(quoteTitleElement?.nodeName === "STRONG" && legacyBlockQuoteType)
|
||||
) {
|
||||
const alertNote = document.createElement("ha-alert");
|
||||
alertNote.alertType = quoteType;
|
||||
alertNote.title =
|
||||
(firstElementChild!.childNodes[1].nodeName === "#text" &&
|
||||
firstElementChild!.childNodes[1].textContent?.trimStart()) ||
|
||||
"";
|
||||
alertNote.alertType = blockQuoteType || legacyBlockQuoteType;
|
||||
alertNote.title = blockQuoteType
|
||||
? ""
|
||||
: (firstElementChild!.childNodes[1].nodeName === "#text" &&
|
||||
firstElementChild!.childNodes[1].textContent?.trimStart()) ||
|
||||
"";
|
||||
|
||||
const childNodes = Array.from(firstElementChild!.childNodes);
|
||||
for (const child of childNodes.slice(
|
||||
|
||||
Reference in New Issue
Block a user