Skip to content

Commit f91f9e3

Browse files
MichaelUrmanatc0005
authored andcommitted
Add MSTeams CodeBlock element
1 parent f047a24 commit f91f9e3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

adaptivecard/adaptivecard.go

+32
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ const (
323323
TypeElementTextRun string = "TextRun" // Introduced in version 1.2
324324
)
325325

326+
// Known exension types for an Adaptive Card element.
327+
//
328+
// - https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-format?tabs=adaptive-md%2Cdesktop%2Cconnector-html#codeblock-in-adaptive-cards
329+
const (
330+
TypeElementMSTeamsCodeBlock string = "CodeBlock"
331+
)
332+
326333
// Sentinel errors for this package.
327334
var (
328335
// ErrInvalidType indicates that an invalid type was specified.
@@ -601,6 +608,15 @@ type Element struct {
601608
// Separator, when true, indicates that a separating line shown should be
602609
// drawn at the top of the element.
603610
Separator bool `json:"separator,omitempty"`
611+
612+
// CodeSnippet provides the content for a CodeBlock element, specific to MSTeams.
613+
CodeSnippet string `json:"codeSnippet,omitempty"`
614+
615+
// Language specifies the langauge of a CodeBlock element, specific to MSTeams.
616+
Language string `json:"language,omitempty"`
617+
618+
// StartLineNumber specifies the initial line number of CodeBlock element, specific to MSTeams.
619+
StartLineNumber int `json:"startLineNumber,omitempty"`
604620
}
605621

606622
// Container is an Element type that allows grouping items together.
@@ -1374,6 +1390,10 @@ func (e Element) Validate() error {
13741390
v.SelfValidate(TableRows(e.Rows))
13751391

13761392
v.SelfValidate(TableColumnDefinitions(e.Columns))
1393+
1394+
case e.Type == TypeElementMSTeamsCodeBlock:
1395+
v.NotEmptyValue(e.CodeSnippet, "CodeSnippet", e.Type, ErrMissingValue)
1396+
v.NotEmptyValue(e.Language, "Language", e.Type, ErrMissingValue)
13771397
}
13781398

13791399
// Return the last recorded validation error, or nil if no validation
@@ -3145,6 +3165,18 @@ func (c *Card) AddContainer(prepend bool, container Container) error {
31453165
return nil
31463166
}
31473167

3168+
// NewCodeBlock creates a new CodeBlock element with snippet, language, and
3169+
// optional firstLine. This is an MSTeams extension element.
3170+
func NewCodeBlock(snippet string, language string, firstLine int) Element {
3171+
codeBlock := Element{
3172+
Type: TypeElementMSTeamsCodeBlock,
3173+
CodeSnippet: snippet,
3174+
Language: language,
3175+
StartLineNumber: firstLine,
3176+
}
3177+
return codeBlock
3178+
}
3179+
31483180
// cardBodyHasMention indicates whether an Adaptive Card body contains all
31493181
// specified Mention values. For every user mention, we require at least one
31503182
// match in an applicable Element in the Card Body.

adaptivecard/getters.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func supportedElementTypes() []string {
3333
TypeElementTable, // Introduced in version 1.5
3434
TypeElementTextBlock,
3535
TypeElementTextRun,
36+
TypeElementMSTeamsCodeBlock,
3637
}
3738
}
3839

0 commit comments

Comments
 (0)