Format multiline strings in Elm, inspired by C# Raw String Literals and Java Text Blocks.
This library simplifies working with multiline strings in Elm, removing unwanted indentation and providing flexible formatting options. It’s ideal for generating clean SQL queries, email templates, HTML, Markdown, or other structured text.
Elm developers who need to format multiline strings without the extra indentation that Elm’s triple-quoted strings (""") preserve by default.
\) at the end of a line to suppress newlines, merging lines in the output.|) marker, perfect for templates requiring precise spacing.{{key}}) for dynamic templates, ideal for email or HTML generation.\n) for compatibility with different platforms or formats.Add the package to your Elm project using:
elm install TSBSoftware/textblockUse the textBlock function to process a multiline string with default settings:
import TextBlock
myText =
textBlock
"""
<div>
<p>Hello</p>
</div>
"""
-- Output: "<div>\n <p>Hello</p>\n</div>"Without textBlock, Elm preserves leading indentation:
"""
<div>
<p>Hello</p>
</div>
"""
-- Output: "\n <div>\n <p>Hello</p>\n </div>\n"Apply additional indentation with a custom character:
import TextBlock
someHtml =
TextBlock.textBlockWith { defaultOptions | indent = 4, indentChar = '.' }
"""
<div>
<p>Hello</p>
</div>
"""
-- Output: "....<div>\n.... <p>Hello</p>\n....</div>"Replace placeholders in a template:
import TextBlock
greeting =
TextBlock.textBlockWithFormat defaultOptions
[ ( "name", "World" ) ]
"""
Hello {{name}}!
"""
-- Output: "Hello World!"Merge lines using a backslash:
import TextBlock
text =
textBlock
"""
Hello \
World!
"""
-- Output: "Hello World!"Keep trailing spaces with a pipe (|):
import TextBlock
blocked =
textBlock
"""
Blocked |
Text |
"""
-- Output: "Blocked \nText " (trailing spaces preserved)textBlock is designed for single application. Applying it multiple times (e.g., textBlock (textBlock value)) may alter indentation unexpectedly and is not recommended.\n. Customize it via TextBlockOptions if needed (e.g., \r\n for Windows compatibility).