When working with rich text components in Appian, I encountered issues with displaying special characters correctly, particularly backslashes (\) and other escape characters like tabs (\t) and newlines (\n). These characters can be misinterpreted or escaped incorrectly, leading to incorrect rendering in the interface.
\
\t
\n
Consider the following example, where you have a string containing file paths with backslashes that you want to display in an a!richTextItem component:
a!richTextItem
"File Path:\nC:\Users\tUserXyz\Documents\Project\file.txt\tAnotherPath:\nC:\Users\nUserXyz\Documents\Report\report.txt"
Actual Output: "File Path: C:\Users UserXyz\Documents\Project\file.txt AnotherPath: C:\Users UserXyz\Documentseporteport.txt"Expected Output: "File Path: C:\Users\tUserXyz\Documents\Project\file.txt AnotherPath: C:\Users\nUserXyz\Documentseporteport.txt"
The issue arises because backslashes and other escape characters are special characters used for escaping other characters. When not properly escaped, they can be misinterpreted, leading to incorrect rendering. For example:
To correctly display these special characters in rich text components, I attempted to use the substitute function to replace each escape character with its intended representation.
substitute
Here’s the approach I took:
Escape Special Characters in the Source String: Used the substitute function to replace each escape character with its intended representation.
Use the Correct Substitution Pattern: Ensure that the substitution pattern correctly handles \n, \t, and \.
Here’s an example implementation demonstrating how I tried to handle special characters, particularly backslashes, tabs, and newlines, in an a!richTextItem component:
a!localVariables( local!formattedText: substitute( substitute( substitute( substitute( ri!inputText, "\n", char(10) ), "\t", char(9) ), "\\", char(92) ), "\r", "" ), local!formattedText )
Despite implementing the above approach, I still face issues with displaying special characters in file paths fetched from an API. If anyone has encountered similar issues or has suggestions for handling these escape characters more effectively, please share your insights or solutions.
Discussion posts and replies are publicly visible
There is a "more simple" approach to substituting multiple items in a string.
a!localVariables( local!text: "File Path:\nC:\Users\tUserXyz\Documents\Project\file.txt\tAnotherPath:\nC:\Users\NUserXyz\Documents\Report\report.txt", reduce( substitute(_,_,_), local!text, merge( {"\n", "\N", "\t", "\T", "\\", "\r", "\R"}, {char(10), char(10), char(9), char(9), char(92), "", ""} ) ) )
This uses the two arrays inside merge() to substitute the first item in the first array with the first item in the second array and so forth. I suggest to read the documentation on the functions reduce() and merge(), and also read the section around partial evaluation
docs.appian.com/.../expression-advanced-evaluation.html
I am still getting the issue.Actual Output: "File Path: C:\Users UserXyz\Documents\Project\file.txt AnotherPath: C:\Users UserXyz\Documentseporteport.txt"Expected Output: "File Path: C:\Users\tUserXyz\Documents\Project\file.txt AnotherPath: C:\Users\nUserXyz\Documentseporteport.txt"
Switch to "Raw" in the test output.
Raw output is as follows