Package com.atlassian.adf.model.node
Class CodeBlock
- java.lang.Object
-
- com.atlassian.adf.model.node.AbstractNode<C>
-
- com.atlassian.adf.model.node.AbstractContentNode<C,N>
-
- com.atlassian.adf.model.node.AbstractMarkedContentNode<CodeBlock,Text,CodeBlockMark>
-
- com.atlassian.adf.model.node.CodeBlock
-
- All Implemented Interfaces:
Element,Node,ContentNode<CodeBlock,Text>,DocContent,LayoutColumnContent,ListItemContent,Marked<CodeBlock,CodeBlockMark>,NonNestableBlockContent,TableCellContent
@Documentation(state=REVIEWED, date="2023-07-26") public class CodeBlock extends AbstractMarkedContentNode<CodeBlock,Text,CodeBlockMark> implements DocContent, LayoutColumnContent, ListItemContent, NonNestableBlockContent, TableCellContent
A container for lines of code. This is roughly equivalent to the combination of the<pre>and<code>HTML element types, although there may be more complicated styling, including language-aware syntax highlighting.If the
languageis unspecified or its value is not one of the supported languages, then thecodeBlockrenders as plain, monospace text.Although
codeBlockacceptstextnodes as content, they are not permitted to use any marks.Example
Java
codeBlock("var foo = {};\nvar bar = [];") .language("javascript");ADF
{ "type": "codeBlock", "attrs": { "language": "javascript" }, "content": [ { "type": "text", "text": "var foo = {};\nvar bar = [];" } ] }Result
var foo = {}; var bar = [];
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.atlassian.adf.model.node.AbstractNode
AbstractNode.ToStringHelper
-
-
Field Summary
-
Fields inherited from class com.atlassian.adf.model.node.AbstractMarkedContentNode
marks
-
Fields inherited from class com.atlassian.adf.model.node.AbstractContentNode
content
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidappendMarkedContentNodeFields(AbstractNode.ToStringHelper buf)Optional<Breakout>breakout()CodeBlockbreakout(Breakout breakout)static CodeBlockcodeBlock()static CodeBlockcodeBlock(Text content)static CodeBlockcodeBlock(Text... content)static CodeBlockcodeBlock(Iterable<? extends Text> content)static CodeBlockcodeBlock(String content)static CodeBlockcodeBlock(String... content)static CodeBlockcodeBlock(Stream<? extends Text> content)CodeBlockcontent(String content)CodeBlockcontent(String... content)Class<Text>contentClass()ReturnsClass<N>, the class representing the type of items held by this node.CodeBlockcopy()Returns a deep copy of this element, including copies of any nodes or marks that it contains.CodeBlockdefaultWidth()StringelementType()Thetypevalue that identifies this element, such as"paragraph"or"strong".CodeBlockfullWidth()Optional<String>language()Returns the language setting for this code block, if set.CodeBlocklanguage(String language)Sets the language that should be used to interpret the code block's contents.Class<CodeBlockMark>markClass()Returns the type of marks that may be applied to this node.protected booleanmarkedContentNodeEquals(CodeBlock other)protected intmarkedContentNodeHashCode()static CodeBlockpre()static CodeBlockpre(Text content)static CodeBlockpre(Text... content)static CodeBlockpre(Iterable<? extends Text> content)static CodeBlockpre(String content)static CodeBlockpre(String... content)static CodeBlockpre(Stream<? extends Text> content)Map<String,?>toMap()Transforms this element to a map ofStringvalues to various basic object types suitable for direct rendering as JSON content.protected voidvalidateContentNodeForAppend(Text node)Called on each content node before it is added to validate that the node can be accepted.CodeBlockwide()-
Methods inherited from class com.atlassian.adf.model.node.AbstractMarkedContentNode
appendContentNodeFields, contentNodeEquals, contentNodeHashCode, contentNodeValidate, createMarkHolder, mark, mark, markedContentNodeValidate, marks, marks, markTypes, parseMarks, unsupportedMarkFactory
-
Methods inherited from class com.atlassian.adf.model.node.AbstractContentNode
addContent, addContentIfPresent, allNodes, appendNodeFields, appendPlainText, appendPlainTextContentJoinedWith, appendPlainTextInlineContent, clear, content, content, content, content, content, contentFieldMaps, isEmpty, nodeEquals, nodeHashCode, parseContentItem, parseContentItems, parseOptionalContent, parseRequiredContent, parseRequiredContentAllowEmpty, removeIf, replaceContent, requireNotEmpty, transformContent, transformDescendants, trim, validate, validateContentItems
-
Methods inherited from class com.atlassian.adf.model.node.AbstractNode
doubleEq, doubleHash, equals, hashCode, isSupported, mapWithType, numberEq, numberHash, self, toPlainText, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.atlassian.adf.model.node.type.ContentNode
allNodes, allNodesOfType, allNodesOfTypeAsList, clear, content, content, content, content, content, isEmpty, removeIf, replaceContent, transformContent, transformDescendants
-
Methods inherited from interface com.atlassian.adf.model.Element
isSupported, validate
-
Methods inherited from interface com.atlassian.adf.model.node.Node
appendPlainText, toPlainText
-
-
-
-
Method Detail
-
codeBlock
public static CodeBlock codeBlock()
- Returns:
- a new, empty code block. At least one text node must be added for it to be valid.
-
codeBlock
public static CodeBlock codeBlock(String content)
- Parameters:
content- the text to include in the code block. As a convenience, it is implicitly wrapped in atextnode.- Returns:
- a new code block with the given content
-
codeBlock
public static CodeBlock codeBlock(String... content)
- Parameters:
content- the text to include in the code block. As a convenience, it is implicitly wrapped astextnodes.- Returns:
- a new code block with the given content
-
codeBlock
public static CodeBlock codeBlock(Text content)
- Returns:
- a new code block with the given content
-
codeBlock
public static CodeBlock codeBlock(Text... content)
- Returns:
- a new code block with the given content
-
codeBlock
public static CodeBlock codeBlock(Iterable<? extends Text> content)
- Returns:
- a new code block with the given content
-
codeBlock
public static CodeBlock codeBlock(Stream<? extends Text> content)
- Returns:
- a new code block with the given content
-
pre
public static CodeBlock pre()
- See Also:
codeBlock()
-
pre
public static CodeBlock pre(String content)
- See Also:
codeBlock(String)
-
pre
public static CodeBlock pre(String... content)
- See Also:
codeBlock(String[])
-
pre
public static CodeBlock pre(Text content)
- See Also:
codeBlock(Text)
-
pre
public static CodeBlock pre(Text... content)
- See Also:
codeBlock(Text[])
-
pre
public static CodeBlock pre(Iterable<? extends Text> content)
- See Also:
codeBlock(Iterable)
-
pre
public static CodeBlock pre(Stream<? extends Text> content)
- See Also:
codeBlock(Stream)
-
copy
public CodeBlock copy()
Description copied from interface:ElementReturns a deep copy of this element, including copies of any nodes or marks that it contains. The copy will not necessarily be in exactly the same state as the original in some cases. For example, atextnode that is used inside acodeBlockwill have the ability to use marks on it disabled, but a copy made of the text node using this method will not similarly disallow marks unless it is also added to a content node with those same restrictions.Implementations notes:
- Implementations should narrow the return type.
- Implementations should
return thisif the element is immutable. The@Immutableannotation should be used on the class to offer additional confirmation of this intent. - Implementations should
return parse(toMap())if they have state. - While there may be cases where it is worthwhile to do something more efficient than
the conversion to a map and back, this is discouraged because it would add yet another
fragile piece of code that breaks when new data is added to the node. The
parseandtoMapmethods already have to be updated in these circumstances, so it makes sense to take advantage of that.
-
content
public CodeBlock content(String content)
- Parameters:
content- the text to include in the code block. As a convenience, it is implicitly wrapped in atextnode.- Returns:
this
-
content
public CodeBlock content(String... content)
- Parameters:
content- the text to include in the code block. As a convenience, it is implicitly wrapped astextnodes.- Returns:
this
-
language
public Optional<String> language()
Returns the language setting for this code block, if set.- Returns:
- the language setting for this code block, or
empty()if not set.
-
language
public CodeBlock language(@Nullable String language)
Sets the language that should be used to interpret the code block's contents.- Parameters:
language- the language code for the table language. Unsupported values result in plain, monospace text.- Returns:
this- See Also:
- Available languages
-
wide
public CodeBlock wide()
-
fullWidth
public CodeBlock fullWidth()
-
defaultWidth
public CodeBlock defaultWidth()
-
validateContentNodeForAppend
protected void validateContentNodeForAppend(Text node)
Description copied from class:AbstractContentNodeCalled on each content node before it is added to validate that the node can be accepted.- Overrides:
validateContentNodeForAppendin classAbstractContentNode<CodeBlock,Text>- Parameters:
node- the node that is about to be added
-
contentClass
public Class<Text> contentClass()
Description copied from interface:ContentNodeReturnsClass<N>, the class representing the type of items held by this node.- Specified by:
contentClassin interfaceContentNode<CodeBlock,Text>
-
elementType
public String elementType()
Description copied from interface:ElementThetypevalue that identifies this element, such as"paragraph"or"strong".- Specified by:
elementTypein interfaceElement
-
toMap
public Map<String,?> toMap()
Description copied from interface:ElementTransforms this element to a map ofStringvalues to various basic object types suitable for direct rendering as JSON content.
-
markClass
public Class<CodeBlockMark> markClass()
Description copied from interface:MarkedReturns the type of marks that may be applied to this node.- Specified by:
markClassin interfaceMarked<CodeBlock,CodeBlockMark>- Specified by:
markClassin classAbstractMarkedContentNode<CodeBlock,Text,CodeBlockMark>- Returns:
- the type of marks that may be applied to this node, which is the concrete type for the generic
type
<M>on this interface.
-
markedContentNodeEquals
protected boolean markedContentNodeEquals(CodeBlock other)
- Overrides:
markedContentNodeEqualsin classAbstractMarkedContentNode<CodeBlock,Text,CodeBlockMark>
-
markedContentNodeHashCode
protected int markedContentNodeHashCode()
- Overrides:
markedContentNodeHashCodein classAbstractMarkedContentNode<CodeBlock,Text,CodeBlockMark>
-
appendMarkedContentNodeFields
protected void appendMarkedContentNodeFields(AbstractNode.ToStringHelper buf)
- Overrides:
appendMarkedContentNodeFieldsin classAbstractMarkedContentNode<CodeBlock,Text,CodeBlockMark>
-
-