Skip to content

Commit

Permalink
Add MSTeams CodeBlock element
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelUrman committed Aug 8, 2024
1 parent adbaed0 commit ba58486
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions adaptivecard/adaptivecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ const (
TypeElementTextRun string = "TextRun" // Introduced in version 1.2
)

// Known exension types for an Adaptive Card element.
//
// - 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
const (
TypeElementMSTeamsCodeBlock string = "CodeBlock"
)

// Sentinel errors for this package.
var (
// ErrInvalidType indicates that an invalid type was specified.
Expand Down Expand Up @@ -601,6 +608,15 @@ type Element struct {
// Separator, when true, indicates that a separating line shown should be
// drawn at the top of the element.
Separator bool `json:"separator,omitempty"`

// CodeSnippet provides the content for a CodeBlock element, specific to MSTeams.
CodeSnippet string `json:"codeSnippet,omitempty"`

// Language specifies the langauge of a CodeBlock element, specific to MSTeams.
Language string `json:"language,omitempty"`

// StartLineNumber specifies the initial line number of CodeBlock element, specific to MSTeams.
StartLineNumber int `json:"startLineNumber,omitempty"`
}

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

v.SelfValidate(TableColumnDefinitions(e.Columns))

case e.Type == TypeElementMSTeamsCodeBlock:
v.NotEmptyValue(e.CodeSnippet, "CodeSnippet", e.Type, ErrMissingValue)
v.NotEmptyValue(e.Language, "Language", e.Type, ErrMissingValue)
}

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

// NewCodeBlock creates a new CodeBlock element with snippet, language, and
// optional firstLine. This is an MSTeams extension element.
func NewCodeBlock(snippet string, language string, firstLine int) Element {
codeBlock := Element{
Type: TypeElementMSTeamsCodeBlock,
CodeSnippet: snippet,
Language: language,
StartLineNumber: firstLine,
}
return codeBlock
}

// cardBodyHasMention indicates whether an Adaptive Card body contains all
// specified Mention values. For every user mention, we require at least one
// match in an applicable Element in the Card Body.
Expand Down
1 change: 1 addition & 0 deletions adaptivecard/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func supportedElementTypes() []string {
TypeElementTable, // Introduced in version 1.5
TypeElementTextBlock,
TypeElementTextRun,
TypeElementMSTeamsCodeBlock,
}
}

Expand Down

0 comments on commit ba58486

Please sign in to comment.