Skip to content

modals/code-block-builder

Contains showCodeBlockBuilder, a modal that builds a plugin’s code block from a declarative list of fields, and formatCodeBlock, the pure formatter behind it.

Every field renders as one settings row. A field whose value differs from its default writes one key: value line into the block (a required field always does), the block is shown live with an optional preview the plugin draws, and the user copies it or inserts it into the note being edited.

Adapted from the code block creator modal of altarok’s obsidian-dev-utils (https://github.com/altarok/obsidian-dev-utils, src/code-block-creator/), under the MIT license below. The port takes the app from the caller, moves the styles into the library stylesheet, reuses this library’s setting components, and picks a fence longer than any backtick run in the values.

Copyright (c) 2026 Altarok

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Interface Description
CodeBlockBuilderBooleanField A boolean field, rendered as a toggle.
CodeBlockBuilderColorField A color field, rendered as a color picker. The value is the color as #rrggbb.
CodeBlockBuilderConditionalDropdownField A pair of cascading dropdowns: the first picks a group, the second one of that group’s options. Only the second dropdown’s value is written into the code block.
CodeBlockBuilderDropdownField A single-choice dropdown field.
CodeBlockBuilderFieldBase The members every CodeBlockBuilderField has.
CodeBlockBuilderFolderField A folder field: a dropdown of every folder of the vault. The value is the folder path, / for the vault root.
CodeBlockBuilderGroup A collapsible group of fields. One group is expanded at a time: expanding a group collapses the others.
CodeBlockBuilderMultipleDropdownField A multiple-choice dropdown field. Its line holds the selected options joined by CodeBlockBuilderMultipleDropdownField.separator.
CodeBlockBuilderOptionGroup A named group of options of a CodeBlockBuilderConditionalDropdownField.
CodeBlockBuilderPreviewParams The parameters of ShowCodeBlockBuilderParams.onUpdatePreview.
CodeBlockBuilderSliderField A slider field. A CodeBlockBuilderFieldBase.defaultValue outside the bounds widens them.
CodeBlockBuilderTextField A text field.
FormatCodeBlockParams The parameters of formatCodeBlock.
ShowCodeBlockBuilderParams The parameters of showCodeBlockBuilder.
Function Description
formatCodeBlock Formats a code block from its fields and their values. Each field holding a value other than its default, or marked CodeBlockBuilderFieldBase.isRequired, writes one line, in the order of the fields; an empty value writes nothing. The fence is one backtick longer than the longest backtick run in the lines, and never shorter than three.
showCodeBlockBuilder Opens the code block builder modal.
Type Description
CodeBlockBuilderField A field of the code block builder, other than a CodeBlockBuilderGroup.
CodeBlockBuilderInput An entry of ShowCodeBlockBuilderParams.fields: a field, or a group of fields.
CodeBlockBuilderOptions The options of a dropdown: a list of values shown as they are, or a map from each value to its display text.
CodeBlockBuilderValue The value of a CodeBlockBuilderField.