Skip to content

Commit e87991c

Browse files
author
Georgi2704
committed
Add markdown component
1 parent 19bb02a commit e87991c

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2019-2025 SURF.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
from enum import Enum
14+
from functools import partial
15+
from types import new_class
16+
from typing import Annotated, Any, ClassVar, TypedDict, Union
17+
18+
from pydantic import BaseModel, Field
19+
20+
21+
class MarkdownColor(str, Enum):
22+
PRIMARY = "primary"
23+
SUCCESS = "success"
24+
WARNING = "warning"
25+
DANGER = "danger"
26+
ACCENT = "accent"
27+
28+
29+
class MarkdownData(TypedDict, total=False):
30+
content: Union[str, None]
31+
color: Union[MarkdownColor, str, None]
32+
33+
34+
class _Markdown(BaseModel):
35+
data: ClassVar[Union[MarkdownData, None]] = None
36+
37+
38+
Markdown = Annotated[_Markdown, Field(frozen=True, default=None, validate_default=False)]
39+
40+
41+
def create_markdown_schema(data: MarkdownData, schema: dict[str, Any]) -> None:
42+
schema.update(
43+
{
44+
"format": "markdown",
45+
"type": "string",
46+
"default": data,
47+
}
48+
)
49+
50+
51+
def markdown(
52+
*,
53+
content: Union[str, None] = None,
54+
color: Union[MarkdownColor, str] = MarkdownColor.PRIMARY,
55+
) -> type[Markdown]:
56+
57+
data: MarkdownData = {
58+
"content": content,
59+
"color": color,
60+
}
61+
62+
namespace = {"data": data}
63+
klass: type[Markdown] = new_class("MarkdownValue", (_Markdown,), {}, lambda ns: ns.update(namespace))
64+
65+
json_schema_extra = partial(create_markdown_schema, data)
66+
67+
return Annotated[
68+
klass,
69+
Field(frozen=True, default=None, validate_default=False, json_schema_extra=json_schema_extra),
70+
] # type: ignore

0 commit comments

Comments
 (0)