mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 11:46:42 +00:00
Fix rendering of alerts in markdown when not breaking (#21856)
This commit is contained in:
parent
d8013a4db9
commit
5613df1d01
3
gallery/src/pages/misc/ha-markdown.markdown
Normal file
3
gallery/src/pages/misc/ha-markdown.markdown
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
title: Markdown
|
||||||
|
---
|
93
gallery/src/pages/misc/ha-markdown.ts
Normal file
93
gallery/src/pages/misc/ha-markdown.ts
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
import { css, html, LitElement } from "lit";
|
||||||
|
import "../../../../src/components/ha-card";
|
||||||
|
import "../../../../src/components/ha-markdown";
|
||||||
|
|
||||||
|
import { customElement } from "lit/decorators";
|
||||||
|
|
||||||
|
interface MarkdownContent {
|
||||||
|
content: string;
|
||||||
|
breaks: boolean;
|
||||||
|
allowSvg: boolean;
|
||||||
|
lazyImages: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mdContentwithDefaults = (md: Partial<MarkdownContent>) =>
|
||||||
|
({
|
||||||
|
breaks: false,
|
||||||
|
allowSvg: false,
|
||||||
|
lazyImages: false,
|
||||||
|
...md,
|
||||||
|
}) as MarkdownContent;
|
||||||
|
|
||||||
|
const generateContent = (md) => `
|
||||||
|
\`\`\`json
|
||||||
|
${JSON.stringify({ ...md, content: undefined })}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
${md.content}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const markdownContents: MarkdownContent[] = [
|
||||||
|
mdContentwithDefaults({
|
||||||
|
content: "_Hello_ **there** 👋, ~~nice~~ of you ||to|| show up.",
|
||||||
|
}),
|
||||||
|
...[true, false].map((breaks) =>
|
||||||
|
mdContentwithDefaults({
|
||||||
|
breaks,
|
||||||
|
content: `
|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
> [!TIP]
|
||||||
|
> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer dictum quis ante eu eleifend. Integer sed [consectetur est, nec elementum magna](#). Fusce lobortis lectus ac rutrum tincidunt. Quisque suscipit gravida ante, in convallis risus vulputate non.
|
||||||
|
|
||||||
|
key | description
|
||||||
|
-- | --
|
||||||
|
lorem | ipsum
|
||||||
|
|
||||||
|
- list item 1
|
||||||
|
- list item 2
|
||||||
|
|
||||||
|
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
@customElement("demo-misc-ha-markdown")
|
||||||
|
export class DemoMiscMarkdown extends LitElement {
|
||||||
|
protected render() {
|
||||||
|
return html`
|
||||||
|
<div class="container">
|
||||||
|
${markdownContents.map(
|
||||||
|
(md) =>
|
||||||
|
html`<ha-card>
|
||||||
|
<ha-markdown
|
||||||
|
.content=${generateContent(md)}
|
||||||
|
.breaks=${md.breaks}
|
||||||
|
.allowSvg=${md.allowSvg}
|
||||||
|
.lazyImages=${md.lazyImages}
|
||||||
|
></ha-markdown>
|
||||||
|
</ha-card>`
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
static get styles() {
|
||||||
|
return css`
|
||||||
|
ha-card {
|
||||||
|
margin: 12px;
|
||||||
|
padding: 12px;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"demo-misc-ha-markdown": DemoMiscMarkdown;
|
||||||
|
}
|
||||||
|
}
|
@ -96,7 +96,25 @@ class HaMarkdownElement extends ReactiveElement {
|
|||||||
|
|
||||||
haAlertNode.append(
|
haAlertNode.append(
|
||||||
...Array.from(node.childNodes)
|
...Array.from(node.childNodes)
|
||||||
.map((child) => Array.from(child.childNodes))
|
.map((child) => {
|
||||||
|
const arr = Array.from(child.childNodes);
|
||||||
|
if (!this.breaks && arr.length) {
|
||||||
|
// When we are not breaking, the first line of the blockquote is not considered,
|
||||||
|
// so we need to adjust the first child text content
|
||||||
|
const firstChild = arr[0];
|
||||||
|
if (
|
||||||
|
firstChild.nodeType === Node.TEXT_NODE &&
|
||||||
|
firstChild.textContent === gitHubAlertMatch.input &&
|
||||||
|
firstChild.textContent?.includes("\n")
|
||||||
|
) {
|
||||||
|
firstChild.textContent = firstChild.textContent
|
||||||
|
.split("\n")
|
||||||
|
.slice(1)
|
||||||
|
.join("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
})
|
||||||
.reduce((acc, val) => acc.concat(val), [])
|
.reduce((acc, val) => acc.concat(val), [])
|
||||||
.filter(
|
.filter(
|
||||||
(childNode) =>
|
(childNode) =>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user